Last active
June 28, 2023 04:22
-
-
Save hendrawd/01f215fd332d84793e600e7f82fc154b to your computer and use it in GitHub Desktop.
Helper class for getting Device Information
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 your.app.util; | |
import android.annotation.SuppressLint; | |
import android.app.Activity; | |
import android.content.ContentResolver; | |
import android.content.Context; | |
import android.content.pm.PackageInfo; | |
import android.content.pm.PackageManager; | |
import android.content.pm.PackageManager.NameNotFoundException; | |
import android.content.res.Configuration; | |
import android.graphics.Point; | |
import android.location.LocationManager; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
import android.net.NetworkInfo.State; | |
import android.os.Build; | |
import android.os.Environment; | |
import android.os.Looper; | |
import android.os.PowerManager; | |
import android.provider.Settings; | |
import android.support.annotation.Nullable; | |
import android.text.TextUtils; | |
import android.view.Display; | |
import java.util.Enumeration; | |
import java.util.Properties; | |
import your.app.BuildConfig; | |
/** | |
* Helper class for getting Device Information | |
* Improved code from https://stackoverflow.com/a/3236201/3940133 | |
* This class needs <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> in AndroidManifest.xml | |
* | |
* @author hendrawd | |
* @see <a href="https://stackoverflow.com/a/3236201/3940133">Source</a> | |
*/ | |
public class DeviceInformation { | |
public static boolean isConnectedToMobileInternet(Context c) { | |
ConnectivityManager connectivityManager = getConnectivityManager(c); | |
if (connectivityManager == null) { | |
return false; | |
} | |
State mobile = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); | |
return mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING; | |
} | |
public static boolean isInternetAvailable(Context c) { | |
ConnectivityManager connectivityManager = getConnectivityManager(c); | |
if (connectivityManager == null) { | |
return false; | |
} | |
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); | |
return activeNetworkInfo != null; | |
} | |
@Nullable | |
private static ConnectivityManager getConnectivityManager(Context context) { | |
return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
} | |
public static String getInfosAboutDevice(Activity a) { | |
StringBuilder sb = new StringBuilder(); | |
// application info | |
sb.append("\n APP Package Name: ").append(BuildConfig.APPLICATION_ID) | |
.append("\n App Version Name: ").append(BuildConfig.VERSION_NAME) | |
.append("\n App Version Code: ").append(BuildConfig.VERSION_CODE) | |
.append("\n"); | |
sb.append("\n OS Version: ").append(System.getProperty("os.version")).append(" (").append(android.os.Build.VERSION.INCREMENTAL).append(")") | |
.append("\n OS API Level: ").append(android.os.Build.VERSION.SDK) | |
.append("\n Device: ").append(android.os.Build.DEVICE) | |
.append("\n Model (and Product): ").append(android.os.Build.MODEL).append(" (").append(android.os.Build.PRODUCT).append(")"); | |
// more from | |
// http://developer.android.com/reference/android/os/Build.html : | |
sb.append("\n Manufacturer: ").append(android.os.Build.MANUFACTURER) | |
.append("\n Other TAGS: ").append(android.os.Build.TAGS) | |
.append("\n screenWidth: ").append(a.getWindow().getWindowManager().getDefaultDisplay().getWidth()) | |
.append("\n screenHeight: ").append(a.getWindow().getWindowManager().getDefaultDisplay().getHeight()) | |
.append("\n Keyboard available: ").append(a.getResources().getConfiguration().keyboard != Configuration.KEYBOARD_NOKEYS) | |
.append("\n Trackball available: ").append(a.getResources().getConfiguration().navigation == Configuration.NAVIGATION_TRACKBALL) | |
.append("\n SD Card state: ").append(Environment.getExternalStorageState()); | |
Properties p = System.getProperties(); | |
Enumeration keys = p.keys(); | |
String key; | |
while (keys.hasMoreElements()) { | |
key = (String) keys.nextElement(); | |
sb.append("\n > ").append(key).append(" = ").append(p.get(key)); | |
} | |
return sb.toString(); | |
} | |
public static boolean isPositioningViaWifiEnabled(Context context) { | |
ContentResolver cr = context.getContentResolver(); | |
String enabledProviders = Settings.Secure.getString(cr, | |
Settings.Secure.LOCATION_PROVIDERS_ALLOWED); | |
if (!TextUtils.isEmpty(enabledProviders)) { | |
// not the fastest way to do that :) | |
String[] providersList = TextUtils.split(enabledProviders, ","); | |
for (String provider : providersList) { | |
if (LocationManager.NETWORK_PROVIDER.equals(provider)) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
public static boolean isScreenOn(Context context) { | |
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); | |
return powerManager != null && powerManager.isScreenOn(); | |
} | |
public static boolean isConnectedToWifi(Context c) { | |
ConnectivityManager connectivityManager = getConnectivityManager(c); | |
if (connectivityManager == null) { | |
return false; | |
} | |
State wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); | |
return wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING; | |
} | |
/** | |
* @return true if the current thread is the UI thread | |
*/ | |
public static boolean isUiThread() { | |
return Looper.getMainLooper().getThread() == Thread.currentThread(); | |
} | |
/** | |
* @param a Activity | |
* @return the size with size.x=width and size.y=height | |
*/ | |
public static Point getScreenSize(Activity a) { | |
return getScreenSize(a.getWindowManager().getDefaultDisplay()); | |
} | |
@SuppressLint({"NewApi", "ObsoleteSdkInt"}) | |
public static Point getScreenSize(Display d) { | |
Point size = new Point(); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { | |
d.getSize(size); | |
} else { | |
size.x = d.getWidth(); | |
size.y = d.getHeight(); | |
} | |
return size; | |
} | |
} |
Awesome. Kudos for this awesome code
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great helper class!