Last active
February 8, 2020 07:46
-
-
Save esabook/ec9372bbe8840177be57e6f2e6fe174a to your computer and use it in GitHub Desktop.
device info reader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ***.utils; | |
import android.Manifest; | |
import android.annotation.SuppressLint; | |
import android.content.Context; | |
import android.content.pm.PackageManager; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
import android.net.wifi.WifiInfo; | |
import android.net.wifi.WifiManager; | |
import android.os.Build; | |
import android.support.annotation.RequiresPermission; | |
import android.telephony.TelephonyManager; | |
import android.text.TextUtils; | |
import android.util.Log; | |
import ***.BuildConfig; | |
import java.net.NetworkInterface; | |
import java.net.SocketException; | |
import java.util.Enumeration; | |
import java.util.Locale; | |
public class __DeviceInfo { | |
private final Context context; | |
private final TelephonyManager tm; | |
public __DeviceInfo(Context ctx) { | |
this.context = ctx; | |
this.tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); | |
} | |
/** | |
* Check valid data string. | |
* | |
* @param data the data | |
* @return the string | |
*/ | |
static String checkValidData(String data) { | |
String tempData = data; | |
if ((tempData == null) || tempData.isEmpty()) { | |
tempData = "-"; | |
} | |
return tempData; | |
} | |
/** | |
* Check valid data string [ ]. | |
* | |
* @param data the data | |
* @return the string [ ] | |
*/ | |
static String[] checkValidData(String[] data) { | |
String[] tempData = data; | |
if ((tempData == null) || (tempData.length == 0)) { | |
tempData = new String[]{"-"}; | |
} | |
return tempData; | |
} | |
/** | |
* Handle illegal character in result string. | |
* | |
* @param result the result | |
* @return the string | |
*/ | |
static String handleIllegalCharacterInResult(String result) { | |
String tempResult = result; | |
if ((tempResult != null) && tempResult.contains(" ")) { | |
tempResult = tempResult.replaceAll(" ", "_"); | |
} | |
return tempResult; | |
} | |
/** | |
* Has permission method. | |
* | |
* @param context the context | |
* @param permission the permission | |
* @return the boolean | |
*/ | |
static boolean hasPermission(Context context, String permission) { | |
final boolean permGranted = | |
context.checkCallingOrSelfPermission(permission) == PackageManager.PERMISSION_GRANTED; | |
if (BuildConfig.DEBUG && !permGranted) { | |
Log.e("Permission", ">\t" + permission); | |
Log.w("Permission", | |
"\nPermission not granted/missing!\nMake sure you have declared the permission in your manifest file (and granted it on API 23+).\n"); | |
} | |
return permGranted; | |
} | |
@RequiresPermission(allOf = { | |
Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.INTERNET | |
}) | |
public final String getNetworkType() { | |
if (hasPermission(this.context, Manifest.permission.ACCESS_NETWORK_STATE)) { | |
final ConnectivityManager cm = | |
(ConnectivityManager) this.context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
if (cm != null) { | |
final NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); | |
if (activeNetwork == null) return "Unknown"; | |
return activeNetwork.getTypeName(); | |
} | |
} | |
return "-"; | |
} | |
/** | |
* Gets BSSID of Connected WiFi | |
* <p> | |
* You need to declare the below permission in the manifest file to use this properly | |
* <p> | |
* <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> | |
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> | |
* | |
* @return Return the basic service set identifier (BSSID) of the current access point. | |
*/ | |
@RequiresPermission(allOf = { | |
Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.ACCESS_NETWORK_STATE | |
}) | |
public final String getWifiBSSID() { | |
String result = null; | |
if (hasPermission(this.context, Manifest.permission.ACCESS_WIFI_STATE)) { | |
final ConnectivityManager cm = | |
(ConnectivityManager) this.context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
if (cm != null) { | |
final NetworkInfo networkInfo = cm.getActiveNetworkInfo(); | |
if (networkInfo == null) { | |
result = null; | |
} | |
if ((networkInfo != null) && networkInfo.isConnected()) { | |
WifiManager wifiManager = | |
(WifiManager) this.context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); | |
if (wifiManager != null) { | |
WifiInfo connectionInfo = wifiManager.getConnectionInfo(); | |
if ((connectionInfo != null) && !TextUtils.isEmpty(connectionInfo.getSSID())) { | |
result = connectionInfo.getBSSID(); | |
} | |
} | |
} | |
} | |
} | |
return checkValidData(result); | |
} | |
/** | |
* Gets Link Speed of Connected WiFi | |
* <p> | |
* You need to declare the below permission in the manifest file to use this properly | |
* <p> | |
* <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> | |
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> | |
* | |
* @return link speed | |
*/ | |
@RequiresPermission(allOf = { | |
Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.ACCESS_NETWORK_STATE | |
}) | |
public final String getWifiLinkSpeed() { | |
String result = null; | |
if (hasPermission(this.context, Manifest.permission.ACCESS_WIFI_STATE)) { | |
final ConnectivityManager cm = | |
(ConnectivityManager) this.context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
if (cm != null) { | |
final NetworkInfo networkInfo = cm.getActiveNetworkInfo(); | |
if (networkInfo == null) { | |
result = null; | |
} | |
if ((networkInfo != null) && networkInfo.isConnected()) { | |
WifiManager wifiManager = | |
(WifiManager) this.context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); | |
if (wifiManager != null) { | |
WifiInfo connectionInfo = wifiManager.getConnectionInfo(); | |
if ((connectionInfo != null) && !TextUtils.isEmpty(connectionInfo.getSSID())) { | |
result = connectionInfo.getLinkSpeed() + " Mbps"; | |
} | |
} | |
} | |
} | |
} | |
return checkValidData(result); | |
} | |
/** | |
* Gets WiFi MAC Address | |
* <p> | |
* You need to declare the below permission in the manifest file to use this properly | |
* <p> | |
* <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> | |
* | |
* @return the wifi mac | |
*/ | |
@SuppressLint("HardwareIds") | |
@RequiresPermission(Manifest.permission.ACCESS_WIFI_STATE) | |
public final String getWifiMAC() { | |
String result = "02:00:00:00:00:00"; | |
if (hasPermission(this.context, Manifest.permission.ACCESS_WIFI_STATE)) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
// Hardware ID are restricted in Android 6+ | |
// https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id | |
Enumeration<NetworkInterface> interfaces = null; | |
try { | |
interfaces = NetworkInterface.getNetworkInterfaces(); | |
} catch (final SocketException e) { | |
Log.e("Permission", e.getMessage(), e); | |
} | |
while ((interfaces != null) && interfaces.hasMoreElements()) { | |
final NetworkInterface networkInterface = interfaces.nextElement(); | |
byte[] addr = new byte[0]; | |
try { | |
addr = networkInterface.getHardwareAddress(); | |
} catch (final SocketException e) { | |
Log.e("Permission", e.getMessage(), e); | |
} | |
if ((addr == null) || (addr.length == 0)) { | |
continue; | |
} | |
final StringBuilder buf = new StringBuilder(); | |
for (final byte b : addr) { | |
buf.append(String.format("%02X:", Byte.valueOf(b))); | |
} | |
if (buf.length() > 0) { | |
buf.deleteCharAt(buf.length() - 1); | |
} | |
final String mac = buf.toString(); | |
final String wifiInterfaceName = "wlan0"; | |
result = wifiInterfaceName.equals(networkInterface.getName()) ? mac : result; | |
} | |
} else { | |
final WifiManager wm = | |
(WifiManager) this.context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); | |
if (wm != null) { | |
result = wm.getConnectionInfo().getMacAddress(); | |
} | |
} | |
} | |
return checkValidData(result); | |
} | |
/** | |
* Gets SSID of Connected WiFi | |
* <p> | |
* You need to declare the below permission in the manifest file to use this properly | |
* <p> | |
* <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> | |
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> | |
* | |
* @return Returns the service set identifier (SSID) of the current 802.11 network | |
*/ | |
@RequiresPermission(allOf = { | |
Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.ACCESS_NETWORK_STATE | |
}) | |
public final String getWifiSSID() { | |
String result = null; | |
if (hasPermission(this.context, Manifest.permission.ACCESS_WIFI_STATE)) { | |
final ConnectivityManager cm = | |
(ConnectivityManager) this.context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
if (cm != null) { | |
final NetworkInfo networkInfo = cm.getActiveNetworkInfo(); | |
if (networkInfo == null) { | |
result = null; | |
} | |
if ((networkInfo != null) && networkInfo.isConnected()) { | |
WifiManager wifiManager = | |
(WifiManager) this.context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); | |
if (wifiManager != null) { | |
WifiInfo connectionInfo = wifiManager.getConnectionInfo(); | |
if ((connectionInfo != null) && !TextUtils.isEmpty(connectionInfo.getSSID())) { | |
result = connectionInfo.getSSID(); | |
} | |
} | |
} | |
} | |
} | |
return checkValidData(result); | |
} | |
/** | |
* Gets carrier. | |
* | |
* @return the carrier | |
*/ | |
public final String getCarrier() { | |
String result = null; | |
if ((tm != null) && (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA)) { | |
result = this.tm.getNetworkOperatorName().toLowerCase(Locale.getDefault()); | |
} | |
return checkValidData( | |
handleIllegalCharacterInResult(result)); | |
} | |
/** | |
* Gets country. | |
* | |
* @return the country | |
*/ | |
public final String getCountry() { | |
final String result; | |
if ((tm != null) && (tm.getSimState() == TelephonyManager.SIM_STATE_READY)) { | |
result = this.tm.getSimCountryIso().toLowerCase(Locale.getDefault()); | |
} else { | |
final Locale locale = Locale.getDefault(); | |
result = locale.getCountry().toLowerCase(locale); | |
} | |
return checkValidData( | |
handleIllegalCharacterInResult(result)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment