mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-29 05:43:55 +01:00
With more information while sending logs (#2222)
* With more information while sending logs * With java docs and unit tests * Changes based on code review
This commit is contained in:
parent
5317063689
commit
2ea6bd7f65
6 changed files with 305 additions and 14 deletions
|
|
@ -0,0 +1,75 @@
|
|||
package fr.free.nrw.commons.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import fr.free.nrw.commons.utils.model.ConnectionType;
|
||||
import fr.free.nrw.commons.utils.model.NetworkConnectionType;
|
||||
|
||||
import static fr.free.nrw.commons.utils.model.ConnectionType.CELLULAR;
|
||||
import static fr.free.nrw.commons.utils.model.ConnectionType.CELLULAR_3G;
|
||||
import static fr.free.nrw.commons.utils.model.ConnectionType.CELLULAR_4G;
|
||||
import static fr.free.nrw.commons.utils.model.ConnectionType.NO_INTERNET;
|
||||
import static fr.free.nrw.commons.utils.model.ConnectionType.WIFI_NETWORK;
|
||||
import static fr.free.nrw.commons.utils.model.NetworkConnectionType.FOUR_G;
|
||||
import static fr.free.nrw.commons.utils.model.NetworkConnectionType.THREE_G;
|
||||
import static fr.free.nrw.commons.utils.model.NetworkConnectionType.TWO_G;
|
||||
import static fr.free.nrw.commons.utils.model.NetworkConnectionType.UNKNOWN;
|
||||
import static fr.free.nrw.commons.utils.model.NetworkConnectionType.WIFI;
|
||||
|
||||
/**
|
||||
* Util class to get any information about the user's device
|
||||
* Ensure that any sensitive information like IMEI is not fetched/shared without user's consent
|
||||
*/
|
||||
public class DeviceInfoUtil {
|
||||
private static final Map<NetworkConnectionType, ConnectionType> TYPE_MAPPING = new HashMap<>();
|
||||
|
||||
static {
|
||||
TYPE_MAPPING.put(TWO_G, CELLULAR);
|
||||
TYPE_MAPPING.put(THREE_G, CELLULAR_3G);
|
||||
TYPE_MAPPING.put(FOUR_G, CELLULAR_4G);
|
||||
TYPE_MAPPING.put(WIFI, WIFI_NETWORK);
|
||||
TYPE_MAPPING.put(UNKNOWN, CELLULAR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get network connection type
|
||||
* @param context
|
||||
* @return wifi/cellular-4g/cellular-3g/cellular-2g/no-internet
|
||||
*/
|
||||
public static ConnectionType getConnectionType(Context context) {
|
||||
if (!NetworkUtils.isInternetConnectionEstablished(context)) {
|
||||
return NO_INTERNET;
|
||||
}
|
||||
NetworkConnectionType networkType = NetworkUtils.getNetworkType(context);
|
||||
ConnectionType deviceNetworkType = TYPE_MAPPING.get(networkType);
|
||||
return deviceNetworkType == null ? CELLULAR : deviceNetworkType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Device manufacturer
|
||||
* @return
|
||||
*/
|
||||
public static String getDeviceManufacturer() {
|
||||
return Build.MANUFACTURER;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Device model name
|
||||
* @return
|
||||
*/
|
||||
public static String getDeviceModel() {
|
||||
return Build.DEVICE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Android version. Eg. 4.4.2
|
||||
* @return
|
||||
*/
|
||||
public static String getAndroidVersion() {
|
||||
return Build.VERSION.RELEASE;
|
||||
}
|
||||
}
|
||||
|
|
@ -5,14 +5,18 @@ import android.annotation.SuppressLint;
|
|||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.telephony.TelephonyManager;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import fr.free.nrw.commons.utils.model.NetworkConnectionType;
|
||||
|
||||
public class NetworkUtils {
|
||||
|
||||
/**
|
||||
* https://developer.android.com/training/monitoring-device-state/connectivity-monitoring#java
|
||||
* Check if internet connection is established.
|
||||
*
|
||||
* @param context context passed to this method could be null.
|
||||
* @return Returns current internet connection status. Returns false if null context was passed.
|
||||
*/
|
||||
|
|
@ -22,13 +26,65 @@ public class NetworkUtils {
|
|||
return false;
|
||||
}
|
||||
|
||||
ConnectivityManager cm =
|
||||
(ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
|
||||
if (cm == null) {
|
||||
return false;
|
||||
}
|
||||
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
|
||||
NetworkInfo activeNetwork = getNetworkInfo(context);
|
||||
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect network connection type
|
||||
*/
|
||||
static NetworkConnectionType getNetworkType(Context context) {
|
||||
TelephonyManager telephonyManager = (TelephonyManager) context.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
|
||||
if (telephonyManager == null) {
|
||||
return NetworkConnectionType.UNKNOWN;
|
||||
}
|
||||
|
||||
NetworkInfo networkInfo = getNetworkInfo(context);
|
||||
if (networkInfo == null) {
|
||||
return NetworkConnectionType.UNKNOWN;
|
||||
}
|
||||
|
||||
int network = networkInfo.getType();
|
||||
if (network == ConnectivityManager.TYPE_WIFI) {
|
||||
return NetworkConnectionType.WIFI;
|
||||
}
|
||||
|
||||
int mobileNetwork = telephonyManager.getNetworkType();
|
||||
switch (mobileNetwork) {
|
||||
case TelephonyManager.NETWORK_TYPE_GPRS:
|
||||
case TelephonyManager.NETWORK_TYPE_EDGE:
|
||||
case TelephonyManager.NETWORK_TYPE_CDMA:
|
||||
case TelephonyManager.NETWORK_TYPE_1xRTT:
|
||||
return NetworkConnectionType.TWO_G;
|
||||
case TelephonyManager.NETWORK_TYPE_HSDPA:
|
||||
case TelephonyManager.NETWORK_TYPE_UMTS:
|
||||
case TelephonyManager.NETWORK_TYPE_HSUPA:
|
||||
case TelephonyManager.NETWORK_TYPE_HSPA:
|
||||
case TelephonyManager.NETWORK_TYPE_EHRPD:
|
||||
case TelephonyManager.NETWORK_TYPE_EVDO_0:
|
||||
case TelephonyManager.NETWORK_TYPE_EVDO_A:
|
||||
case TelephonyManager.NETWORK_TYPE_EVDO_B:
|
||||
return NetworkConnectionType.THREE_G;
|
||||
case TelephonyManager.NETWORK_TYPE_LTE:
|
||||
case TelephonyManager.NETWORK_TYPE_HSPAP:
|
||||
return NetworkConnectionType.FOUR_G;
|
||||
default:
|
||||
return NetworkConnectionType.UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracted private method to get nullable network info
|
||||
*/
|
||||
@Nullable
|
||||
private static NetworkInfo getNetworkInfo(Context context) {
|
||||
ConnectivityManager connectivityManager =
|
||||
(ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
|
||||
if (connectivityManager == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return connectivityManager.getActiveNetworkInfo();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
package fr.free.nrw.commons.utils.model;
|
||||
|
||||
public enum ConnectionType {
|
||||
WIFI_NETWORK("wifi"), CELLULAR_4G("cellular-4g"), CELLULAR_3G("cellular-3g"), CELLULAR("cellular"), NO_INTERNET("no-internet");
|
||||
|
||||
private final String text;
|
||||
|
||||
ConnectionType(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package fr.free.nrw.commons.utils.model;
|
||||
|
||||
public enum NetworkConnectionType {
|
||||
WIFI, TWO_G, THREE_G, FOUR_G, UNKNOWN
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue