Advertisement
Guest User

Untitled

a guest
May 29th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.05 KB | None | 0 0
  1. package com.sample.myutilities.activities;
  2.  
  3. /**
  4. * @author Devrath
  5. */
  6.  
  7. import android.content.BroadcastReceiver;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.IntentFilter;
  11. import android.location.Location;
  12. import android.location.LocationListener;
  13. import android.location.LocationManager;
  14. import android.net.ConnectivityManager;
  15. import android.net.NetworkInfo;
  16. import android.os.BatteryManager;
  17. import android.os.Build;
  18. import android.os.Bundle;
  19. import android.os.IBinder;
  20. import android.telephony.TelephonyManager;
  21. import android.text.TextUtils;
  22. import android.util.Log;
  23. import android.widget.Toast;
  24.  
  25. import com.google.android.gms.common.ConnectionResult;
  26. import com.google.android.gms.common.GooglePlayServicesUtil;
  27. import com.google.android.gms.common.api.GoogleApiClient;
  28. import com.google.android.gms.location.LocationRequest;
  29. import com.google.android.gms.location.LocationServices;
  30.  
  31. import org.json.JSONException;
  32. import org.json.JSONObject;
  33. import org.xml.sax.Locator;
  34.  
  35. import java.util.Locale;
  36.  
  37. public class DeviceInformation implements LocationListener{
  38.  
  39.  
  40. String finalResponseValue;
  41.  
  42. //********************************* Variables for the network information**********************************//
  43. public static final String TAG_NOT_CONNECTED = "Not connected to network";
  44. public static final String TAG_WIFI_CONNECTED = "Connected to wifi";
  45. public static final String TAG_MOBILE_NOT_CONNECTED = "Not measurable mobile network";
  46. public static final String TAG_NOT_CONNECTED_TO_ANY_NETWORK = "Not connected to WIFI and MOBILE network";
  47. String networkType;
  48. //********************************* Variables for the network information**********************************//
  49.  
  50. //********************************* Variables for the location information**********************************//
  51.  
  52. boolean isLatitudeLongitudeAvailable=false;
  53. private Context context;
  54. // flag for GPS status
  55. boolean isGPSEnabled = false;
  56.  
  57. // flag for network status
  58. boolean isNetworkEnabled = false;
  59.  
  60. // flag for GPS status
  61. boolean canGetLocation = false;
  62.  
  63. Location location; // location
  64. double latitude; // latitude
  65. double longitude; // longitude
  66.  
  67. // The minimum distance to change Updates in meters
  68. private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
  69.  
  70. // The minimum time between updates in milliseconds
  71. private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
  72.  
  73. // Declaring a Location Manager
  74. protected LocationManager locationManager;
  75. //********************************* Variables for the location information**********************************//
  76.  
  77.  
  78. //********************************* Variables for the battery information **********************************//
  79. String batteryLevel;
  80. //********************************* Variables for the battery information **********************************//
  81.  
  82. //********************************* Variables for the battery information **********************************//
  83. String mDeviceName;
  84. //********************************* Variables for the battery information **********************************//
  85.  
  86. //********************************* Variables for the Device specs******************************************//
  87.  
  88. int sdkIntValue;
  89. String versionCode;
  90. String versionName;
  91. //********************************* Variables for the Device specs******************************************//
  92.  
  93. public String getFinalJsonResult() {
  94. return finalResponseValue=createJsonResponse();
  95. }
  96.  
  97. //********************************* Constructor ************************************************************//
  98. public DeviceInformation(Context context) {
  99. super();
  100. this.context = context;
  101. this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  102.  
  103. getLocation();// -------------------------------------- GET LOCATION TYPE
  104. networkType=getNetworkClass(context);// --------------- GET NETWORK TYPE
  105. batteryLevel= batteryLevel() + "%";// ------------- GET BATTERY LEVEL
  106. sdkIntValue=Integer.valueOf(android.os.Build.VERSION.SDK_INT);
  107. getBuildVersionCodesAndHumanVersionName(sdkIntValue);// -------------------------------- GET SDKINTVALUE, VERSIONCODE, VERSIONNAME
  108. mDeviceName=android.os.Build.MODEL;// ----------------- GET DEVICE NAME
  109.  
  110. stopUsingGPS();//--- Stopping GPS so that it won't consume battery
  111.  
  112. }
  113.  
  114.  
  115.  
  116.  
  117. //********************************* Constructor ************************************************************//
  118.  
  119.  
  120. private String createJsonResponse() {
  121. JSONObject mainObj=new JSONObject();
  122. String finalResponse = null;
  123. try {
  124. mainObj.put("sdkIntValue",sdkIntValue);
  125. mainObj.put("versionCode",versionCode);
  126. mainObj.put("versionName",versionName);
  127.  
  128. mainObj.put("mDeviceName",mDeviceName);
  129. mainObj.put("batteryLevel",batteryLevel);
  130. mainObj.put("networkType",networkType);
  131. mainObj.put("latitude",latitude);
  132. mainObj.put("longitude",longitude);
  133. mainObj.put("isLatitudeLongitudeAvailable",isLatitudeLongitudeAvailable);
  134. finalResponse=mainObj.toString();
  135.  
  136. } catch (JSONException e) {
  137. e.printStackTrace();
  138. }
  139. return finalResponse;
  140. }
  141.  
  142. /**
  143. * *********************************************************************************************************
  144. * ************************* This method below is used to get the type of network*****************************
  145. * **********************************************************************************************************
  146. */
  147. public static String getNetworkClass(Context context) {
  148. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  149. NetworkInfo info = cm.getActiveNetworkInfo();
  150. if (info == null || !info.isConnected())
  151. return TAG_NOT_CONNECTED; //////////////////// not connected
  152. if (info.getType() == ConnectivityManager.TYPE_WIFI)
  153. return TAG_WIFI_CONNECTED; //////////////////// Wifi Connected
  154. if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
  155. int networkType = info.getSubtype();
  156. switch (networkType) {
  157. case TelephonyManager.NETWORK_TYPE_GPRS:
  158. return "100 kbps"; ///////////////////// ~ 100 kbps
  159. case TelephonyManager.NETWORK_TYPE_EDGE:
  160. return "50-100 kbps"; ///////////////////// ~ 50-100 kbps
  161. case TelephonyManager.NETWORK_TYPE_CDMA:
  162. return "14-64 kbps"; ///////////////////// ~ 14-64 kbps
  163. case TelephonyManager.NETWORK_TYPE_1xRTT:
  164. return "50-100 kbps"; ///////////////////// ~ 50-100 kbps
  165. case TelephonyManager.NETWORK_TYPE_IDEN:
  166. return "2G"; ///////////////////// ~ 2G
  167. case TelephonyManager.NETWORK_TYPE_UMTS:
  168. return "~ 400-7000 kbps"; ///////////////// ~ 400-7000 kbps
  169. case TelephonyManager.NETWORK_TYPE_EVDO_0:
  170. return "~ 400-1000 kbps"; ///////////////// ~ 400-1000 kbps
  171. case TelephonyManager.NETWORK_TYPE_EVDO_A:
  172. return "~ 600-1400 kbps"; ///////////////// ~ 600-1400 kbps
  173. case TelephonyManager.NETWORK_TYPE_HSDPA:
  174. return "~ 2-14 Mbps"; ///////////////////// ~ 2-14 Mbps
  175. case TelephonyManager.NETWORK_TYPE_HSUPA:
  176. return "~ 1-23 Mbps"; ///////////////////// ~ 1-23 Mbps
  177. case TelephonyManager.NETWORK_TYPE_HSPA:
  178. return "~ 700-1700 kbps";////////////////// ~ 700-1700 kbps
  179. case TelephonyManager.NETWORK_TYPE_EVDO_B:
  180. return "~ 5 Mbps"; ///////////////////// ~ 5 Mbps
  181. case TelephonyManager.NETWORK_TYPE_EHRPD:
  182. return "~ 1-2 Mbps"; ///////////////////// ~ 1-2 Mbps
  183. case TelephonyManager.NETWORK_TYPE_HSPAP:
  184. return "3G"; /////////////////////////// ~ 3G
  185. case TelephonyManager.NETWORK_TYPE_LTE:
  186. return "4G"; /////////////////////////// ~ 4G
  187. default:
  188. return TAG_MOBILE_NOT_CONNECTED; /////// Not connected to mobile network
  189. }
  190. }
  191. return "TAG_NOT_CONNECTED_TO_ANY_NETWORK"; /////// Not connected to any network
  192. }
  193.  
  194. /************************************************************************************************************
  195. ************************** This method above is used to get the type of network*****************************
  196. ************************************************************************************************************/
  197.  
  198.  
  199. /**
  200. * *********************************************************************************************************
  201. * ************************* This methods below is used to get the location information**********************
  202. * **********************************************************************************************************
  203. */
  204.  
  205.  
  206.  
  207. public Location getLocation() {
  208. try {
  209. locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
  210.  
  211. // getting GPS status
  212. isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  213.  
  214. // getting network status
  215. isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  216.  
  217. if (!isGPSEnabled && !isNetworkEnabled) {
  218. // no network provider is enabled
  219. } else {
  220. this.canGetLocation = true;
  221. // First get location from Network Provider
  222. if (isNetworkEnabled) {
  223. locationManager.requestLocationUpdates(
  224. LocationManager.NETWORK_PROVIDER,
  225. MIN_TIME_BW_UPDATES,
  226. MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  227. Log.d("Network", "Network");
  228. if (locationManager != null) {
  229. location = locationManager
  230. .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  231. if (location != null) {
  232. isLatitudeLongitudeAvailable=true;
  233. latitude = location.getLatitude();
  234. longitude = location.getLongitude();
  235. }
  236. }
  237. }
  238. // if GPS Enabled get lat/long using GPS Services
  239. if (isGPSEnabled) {
  240. if (location == null) {
  241. locationManager.requestLocationUpdates(
  242. LocationManager.GPS_PROVIDER,
  243. MIN_TIME_BW_UPDATES,
  244. MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  245. Log.d("GPS Enabled", "GPS Enabled");
  246. if (locationManager != null) {
  247. location = locationManager
  248. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  249. if (location != null) {
  250. isLatitudeLongitudeAvailable=true;
  251. latitude = location.getLatitude();
  252. longitude = location.getLongitude();
  253. }
  254. }
  255. }
  256. }
  257. }
  258.  
  259. } catch (Exception e) {
  260. e.printStackTrace();
  261. }
  262.  
  263. return location;
  264. }
  265.  
  266. /**
  267. * Stop using GPS listener
  268. * Calling this function will stop using GPS in your app
  269. * */
  270. public void stopUsingGPS(){
  271. if(locationManager != null){
  272. locationManager.removeUpdates(DeviceInformation.this);
  273. }
  274. }
  275.  
  276. /**
  277. * Function to get latitude
  278. * */
  279. public double getLatitude(){
  280. if(location != null){
  281. latitude = location.getLatitude();
  282. }
  283.  
  284. // return latitude
  285. return latitude;
  286. }
  287.  
  288. /**
  289. * Function to get longitude
  290. * */
  291. public double getLongitude(){
  292. if(location != null){
  293. longitude = location.getLongitude();
  294. }
  295.  
  296. // return longitude
  297. return longitude;
  298. }
  299.  
  300. /**
  301. * Function to check GPS/wifi enabled
  302. * @return boolean
  303. * */
  304. public boolean canGetLocation() {
  305. return this.canGetLocation;
  306. }
  307.  
  308.  
  309. @Override
  310. public void onLocationChanged(Location location) {
  311. latitude = location.getLatitude();
  312. longitude = location.getLongitude();
  313. //A new Location has been found
  314. stopUsingGPS();
  315. }
  316.  
  317. @Override
  318. public void onProviderDisabled(String provider) {
  319. }
  320.  
  321. @Override
  322. public void onProviderEnabled(String provider) {
  323. }
  324.  
  325. @Override
  326. public void onStatusChanged(String provider, int status, Bundle extras) {
  327. }
  328.  
  329.  
  330.  
  331. /**
  332. * **********************************************************************************************************
  333. * ************************* This methods above is used to get the location information**********************
  334. * **********************************************************************************************************
  335. */
  336.  
  337.  
  338. /**
  339. * **********************************************************************************************************
  340. * ************************* This method below is used to get the device battery information****************
  341. * **********************************************************************************************************
  342. */
  343.  
  344. public float batteryLevel() {
  345. Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
  346. int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
  347. int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
  348.  
  349. // Error checking that probably isn't needed but I added just in case.
  350. if(level == -1 || scale == -1) {
  351. return 50.0f;
  352. }
  353.  
  354. return ((float)level / (float)scale) * 100.0f;
  355. }
  356.  
  357. /**
  358. * **********************************************************************************************************
  359. * ************************* This method above is used to get the device battery information****************
  360. * **********************************************************************************************************
  361. */
  362.  
  363. /**
  364. * ***************************************************************************************************************************************
  365. * ************************* This methods Below is used to Get Android API level of phone currently running my application****************
  366. * ***************************************************************************************************************************************
  367. */
  368.  
  369. public void getBuildVersionCodesAndHumanVersionName(int sdkIntValue){
  370. switch (sdkIntValue){
  371. case 1:
  372. versionCode="BASE";
  373. versionName="Android 1.0 (no codename)";
  374. break;
  375. case 2:
  376. versionCode="BASE_1_1";
  377. versionName="Android 1.1 Petit Four";
  378. break;
  379. case 3:
  380. versionCode="CUPCAKE";
  381. versionName="Android 1.5 Cupcake";
  382. break;
  383. case 4:
  384. versionCode="DONUT";
  385. versionName="Android 1.6 Donut";
  386. break;
  387. case 5:
  388. versionCode="ECLAIR";
  389. versionName="Android 2.0 Eclair";
  390. break;
  391. case 6:
  392. versionCode="ECLAIR_0_1";
  393. versionName="Android 2.0.1 Eclair";
  394. break;
  395. case 7:
  396. versionCode="ECLAIR_MR1";
  397. versionName="Android 2.1 Eclair";
  398. break;
  399. case 8:
  400. versionCode="FROYO";
  401. versionName="Android 2.2 Froyo";
  402. break;
  403. case 9:
  404. versionCode="GINGERBREAD";
  405. versionName="Android 2.3 Gingerbread";
  406. break;
  407. case 10:
  408. versionCode="GINGERBREAD_MR1";
  409. versionName="Android 2.3.3 Gingerbread";
  410. break;
  411. case 11:
  412. versionCode="HONEYCOMB";
  413. versionName="Android 3.0 Honeycomb";
  414. break;
  415. case 12:
  416. versionCode="HONEYCOMB_MR1";
  417. versionName="Android 3.1 Honeycomb";
  418. break;
  419. case 13:
  420. versionCode="HONEYCOMB_MR2";
  421. versionName="Android 3.2 Honeycomb";
  422. break;
  423. case 14:
  424. versionCode="ICE_CREAM_SANDWICH";
  425. versionName="Android 4.0 Ice Cream Sandwich";
  426. break;
  427. case 15:
  428. versionCode="ICE_CREAM_SANDWICH_MR1";
  429. versionName="Android 4.0.3 Ice Cream Sandwich";
  430. break;
  431. case 16:
  432. versionCode="JELLY_BEAN";
  433. versionName="Android 4.1 Jellybean";
  434. break;
  435. case 17:
  436. versionCode="JELLY_BEAN_MR1";
  437. versionName="Android 4.2 Jellybean";
  438. break;
  439. case 18:
  440. versionCode="JELLY_BEAN_MR2";
  441. versionName=" Android 4.3 Jellybean";
  442. break;
  443. case 19:
  444. versionCode="KITKAT";
  445. versionName="Android 4.4 KitKat";
  446. break;
  447. case 20:
  448. versionCode="KITKAT_WATCH";
  449. versionName=" Android 4.4 KitKat Watch";
  450. break;
  451. case 21:
  452. versionCode="LOLLIPOP";
  453. versionName="Android 5.0 Lollipop";
  454. break;
  455. case 22:
  456. versionCode="LOLLIPOP_MR1";
  457. versionName="Android 5.1 Lollipop";
  458. break;
  459. case 10000:
  460. versionCode="CUR_DEVELOPMENT";
  461. versionName="Current Development Build";
  462. break;
  463. default :
  464. versionCode="UNKNOWN";
  465. versionName="UNKNOWN";
  466. }
  467.  
  468. }
  469.  
  470. /**
  471. * ***************************************************************************************************************************************
  472. * ************************* This methods above is used to Get Android API level of phone currently running my application****************
  473. * ***************************************************************************************************************************************
  474. */
  475.  
  476.  
  477. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement