Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sample.myutilities.activities;
- /**
- * @author Devrath
- */
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.os.BatteryManager;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.telephony.TelephonyManager;
- import android.text.TextUtils;
- import android.util.Log;
- import android.widget.Toast;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.GooglePlayServicesUtil;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.location.LocationRequest;
- import com.google.android.gms.location.LocationServices;
- import org.json.JSONException;
- import org.json.JSONObject;
- import org.xml.sax.Locator;
- import java.util.Locale;
- public class DeviceInformation implements LocationListener{
- String finalResponseValue;
- //********************************* Variables for the network information**********************************//
- public static final String TAG_NOT_CONNECTED = "Not connected to network";
- public static final String TAG_WIFI_CONNECTED = "Connected to wifi";
- public static final String TAG_MOBILE_NOT_CONNECTED = "Not measurable mobile network";
- public static final String TAG_NOT_CONNECTED_TO_ANY_NETWORK = "Not connected to WIFI and MOBILE network";
- String networkType;
- //********************************* Variables for the network information**********************************//
- //********************************* Variables for the location information**********************************//
- boolean isLatitudeLongitudeAvailable=false;
- private Context context;
- // flag for GPS status
- boolean isGPSEnabled = false;
- // flag for network status
- boolean isNetworkEnabled = false;
- // flag for GPS status
- boolean canGetLocation = false;
- Location location; // location
- double latitude; // latitude
- double longitude; // longitude
- // The minimum distance to change Updates in meters
- private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
- // The minimum time between updates in milliseconds
- private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
- // Declaring a Location Manager
- protected LocationManager locationManager;
- //********************************* Variables for the location information**********************************//
- //********************************* Variables for the battery information **********************************//
- String batteryLevel;
- //********************************* Variables for the battery information **********************************//
- //********************************* Variables for the battery information **********************************//
- String mDeviceName;
- //********************************* Variables for the battery information **********************************//
- //********************************* Variables for the Device specs******************************************//
- int sdkIntValue;
- String versionCode;
- String versionName;
- //********************************* Variables for the Device specs******************************************//
- public String getFinalJsonResult() {
- return finalResponseValue=createJsonResponse();
- }
- //********************************* Constructor ************************************************************//
- public DeviceInformation(Context context) {
- super();
- this.context = context;
- this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
- getLocation();// -------------------------------------- GET LOCATION TYPE
- networkType=getNetworkClass(context);// --------------- GET NETWORK TYPE
- batteryLevel= batteryLevel() + "%";// ------------- GET BATTERY LEVEL
- sdkIntValue=Integer.valueOf(android.os.Build.VERSION.SDK_INT);
- getBuildVersionCodesAndHumanVersionName(sdkIntValue);// -------------------------------- GET SDKINTVALUE, VERSIONCODE, VERSIONNAME
- mDeviceName=android.os.Build.MODEL;// ----------------- GET DEVICE NAME
- stopUsingGPS();//--- Stopping GPS so that it won't consume battery
- }
- //********************************* Constructor ************************************************************//
- private String createJsonResponse() {
- JSONObject mainObj=new JSONObject();
- String finalResponse = null;
- try {
- mainObj.put("sdkIntValue",sdkIntValue);
- mainObj.put("versionCode",versionCode);
- mainObj.put("versionName",versionName);
- mainObj.put("mDeviceName",mDeviceName);
- mainObj.put("batteryLevel",batteryLevel);
- mainObj.put("networkType",networkType);
- mainObj.put("latitude",latitude);
- mainObj.put("longitude",longitude);
- mainObj.put("isLatitudeLongitudeAvailable",isLatitudeLongitudeAvailable);
- finalResponse=mainObj.toString();
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return finalResponse;
- }
- /**
- * *********************************************************************************************************
- * ************************* This method below is used to get the type of network*****************************
- * **********************************************************************************************************
- */
- public static String getNetworkClass(Context context) {
- ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo info = cm.getActiveNetworkInfo();
- if (info == null || !info.isConnected())
- return TAG_NOT_CONNECTED; //////////////////// not connected
- if (info.getType() == ConnectivityManager.TYPE_WIFI)
- return TAG_WIFI_CONNECTED; //////////////////// Wifi Connected
- if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
- int networkType = info.getSubtype();
- switch (networkType) {
- case TelephonyManager.NETWORK_TYPE_GPRS:
- return "100 kbps"; ///////////////////// ~ 100 kbps
- case TelephonyManager.NETWORK_TYPE_EDGE:
- return "50-100 kbps"; ///////////////////// ~ 50-100 kbps
- case TelephonyManager.NETWORK_TYPE_CDMA:
- return "14-64 kbps"; ///////////////////// ~ 14-64 kbps
- case TelephonyManager.NETWORK_TYPE_1xRTT:
- return "50-100 kbps"; ///////////////////// ~ 50-100 kbps
- case TelephonyManager.NETWORK_TYPE_IDEN:
- return "2G"; ///////////////////// ~ 2G
- case TelephonyManager.NETWORK_TYPE_UMTS:
- return "~ 400-7000 kbps"; ///////////////// ~ 400-7000 kbps
- case TelephonyManager.NETWORK_TYPE_EVDO_0:
- return "~ 400-1000 kbps"; ///////////////// ~ 400-1000 kbps
- case TelephonyManager.NETWORK_TYPE_EVDO_A:
- return "~ 600-1400 kbps"; ///////////////// ~ 600-1400 kbps
- case TelephonyManager.NETWORK_TYPE_HSDPA:
- return "~ 2-14 Mbps"; ///////////////////// ~ 2-14 Mbps
- case TelephonyManager.NETWORK_TYPE_HSUPA:
- return "~ 1-23 Mbps"; ///////////////////// ~ 1-23 Mbps
- case TelephonyManager.NETWORK_TYPE_HSPA:
- return "~ 700-1700 kbps";////////////////// ~ 700-1700 kbps
- case TelephonyManager.NETWORK_TYPE_EVDO_B:
- return "~ 5 Mbps"; ///////////////////// ~ 5 Mbps
- case TelephonyManager.NETWORK_TYPE_EHRPD:
- return "~ 1-2 Mbps"; ///////////////////// ~ 1-2 Mbps
- case TelephonyManager.NETWORK_TYPE_HSPAP:
- return "3G"; /////////////////////////// ~ 3G
- case TelephonyManager.NETWORK_TYPE_LTE:
- return "4G"; /////////////////////////// ~ 4G
- default:
- return TAG_MOBILE_NOT_CONNECTED; /////// Not connected to mobile network
- }
- }
- return "TAG_NOT_CONNECTED_TO_ANY_NETWORK"; /////// Not connected to any network
- }
- /************************************************************************************************************
- ************************** This method above is used to get the type of network*****************************
- ************************************************************************************************************/
- /**
- * *********************************************************************************************************
- * ************************* This methods below is used to get the location information**********************
- * **********************************************************************************************************
- */
- public Location getLocation() {
- try {
- locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
- // getting GPS status
- isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
- // getting network status
- isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
- if (!isGPSEnabled && !isNetworkEnabled) {
- // no network provider is enabled
- } else {
- this.canGetLocation = true;
- // First get location from Network Provider
- if (isNetworkEnabled) {
- locationManager.requestLocationUpdates(
- LocationManager.NETWORK_PROVIDER,
- MIN_TIME_BW_UPDATES,
- MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
- Log.d("Network", "Network");
- if (locationManager != null) {
- location = locationManager
- .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
- if (location != null) {
- isLatitudeLongitudeAvailable=true;
- latitude = location.getLatitude();
- longitude = location.getLongitude();
- }
- }
- }
- // if GPS Enabled get lat/long using GPS Services
- if (isGPSEnabled) {
- if (location == null) {
- locationManager.requestLocationUpdates(
- LocationManager.GPS_PROVIDER,
- MIN_TIME_BW_UPDATES,
- MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
- Log.d("GPS Enabled", "GPS Enabled");
- if (locationManager != null) {
- location = locationManager
- .getLastKnownLocation(LocationManager.GPS_PROVIDER);
- if (location != null) {
- isLatitudeLongitudeAvailable=true;
- latitude = location.getLatitude();
- longitude = location.getLongitude();
- }
- }
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return location;
- }
- /**
- * Stop using GPS listener
- * Calling this function will stop using GPS in your app
- * */
- public void stopUsingGPS(){
- if(locationManager != null){
- locationManager.removeUpdates(DeviceInformation.this);
- }
- }
- /**
- * Function to get latitude
- * */
- public double getLatitude(){
- if(location != null){
- latitude = location.getLatitude();
- }
- // return latitude
- return latitude;
- }
- /**
- * Function to get longitude
- * */
- public double getLongitude(){
- if(location != null){
- longitude = location.getLongitude();
- }
- // return longitude
- return longitude;
- }
- /**
- * Function to check GPS/wifi enabled
- * @return boolean
- * */
- public boolean canGetLocation() {
- return this.canGetLocation;
- }
- @Override
- public void onLocationChanged(Location location) {
- latitude = location.getLatitude();
- longitude = location.getLongitude();
- //A new Location has been found
- stopUsingGPS();
- }
- @Override
- public void onProviderDisabled(String provider) {
- }
- @Override
- public void onProviderEnabled(String provider) {
- }
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {
- }
- /**
- * **********************************************************************************************************
- * ************************* This methods above is used to get the location information**********************
- * **********************************************************************************************************
- */
- /**
- * **********************************************************************************************************
- * ************************* This method below is used to get the device battery information****************
- * **********************************************************************************************************
- */
- public float batteryLevel() {
- Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
- int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
- int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
- // Error checking that probably isn't needed but I added just in case.
- if(level == -1 || scale == -1) {
- return 50.0f;
- }
- return ((float)level / (float)scale) * 100.0f;
- }
- /**
- * **********************************************************************************************************
- * ************************* This method above is used to get the device battery information****************
- * **********************************************************************************************************
- */
- /**
- * ***************************************************************************************************************************************
- * ************************* This methods Below is used to Get Android API level of phone currently running my application****************
- * ***************************************************************************************************************************************
- */
- public void getBuildVersionCodesAndHumanVersionName(int sdkIntValue){
- switch (sdkIntValue){
- case 1:
- versionCode="BASE";
- versionName="Android 1.0 (no codename)";
- break;
- case 2:
- versionCode="BASE_1_1";
- versionName="Android 1.1 Petit Four";
- break;
- case 3:
- versionCode="CUPCAKE";
- versionName="Android 1.5 Cupcake";
- break;
- case 4:
- versionCode="DONUT";
- versionName="Android 1.6 Donut";
- break;
- case 5:
- versionCode="ECLAIR";
- versionName="Android 2.0 Eclair";
- break;
- case 6:
- versionCode="ECLAIR_0_1";
- versionName="Android 2.0.1 Eclair";
- break;
- case 7:
- versionCode="ECLAIR_MR1";
- versionName="Android 2.1 Eclair";
- break;
- case 8:
- versionCode="FROYO";
- versionName="Android 2.2 Froyo";
- break;
- case 9:
- versionCode="GINGERBREAD";
- versionName="Android 2.3 Gingerbread";
- break;
- case 10:
- versionCode="GINGERBREAD_MR1";
- versionName="Android 2.3.3 Gingerbread";
- break;
- case 11:
- versionCode="HONEYCOMB";
- versionName="Android 3.0 Honeycomb";
- break;
- case 12:
- versionCode="HONEYCOMB_MR1";
- versionName="Android 3.1 Honeycomb";
- break;
- case 13:
- versionCode="HONEYCOMB_MR2";
- versionName="Android 3.2 Honeycomb";
- break;
- case 14:
- versionCode="ICE_CREAM_SANDWICH";
- versionName="Android 4.0 Ice Cream Sandwich";
- break;
- case 15:
- versionCode="ICE_CREAM_SANDWICH_MR1";
- versionName="Android 4.0.3 Ice Cream Sandwich";
- break;
- case 16:
- versionCode="JELLY_BEAN";
- versionName="Android 4.1 Jellybean";
- break;
- case 17:
- versionCode="JELLY_BEAN_MR1";
- versionName="Android 4.2 Jellybean";
- break;
- case 18:
- versionCode="JELLY_BEAN_MR2";
- versionName=" Android 4.3 Jellybean";
- break;
- case 19:
- versionCode="KITKAT";
- versionName="Android 4.4 KitKat";
- break;
- case 20:
- versionCode="KITKAT_WATCH";
- versionName=" Android 4.4 KitKat Watch";
- break;
- case 21:
- versionCode="LOLLIPOP";
- versionName="Android 5.0 Lollipop";
- break;
- case 22:
- versionCode="LOLLIPOP_MR1";
- versionName="Android 5.1 Lollipop";
- break;
- case 10000:
- versionCode="CUR_DEVELOPMENT";
- versionName="Current Development Build";
- break;
- default :
- versionCode="UNKNOWN";
- versionName="UNKNOWN";
- }
- }
- /**
- * ***************************************************************************************************************************************
- * ************************* This methods above is used to Get Android API level of phone currently running my application****************
- * ***************************************************************************************************************************************
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement