1 package com.android.storagemanager.automatic;
2 
3 import android.content.Context;
4 import android.net.ConnectivityManager;
5 import android.net.Network;
6 import android.net.NetworkInfo;
7 import android.os.BatteryManager;
8 import android.os.PowerManager;
9 import android.provider.Settings;
10 
11 /**
12  * Utility class to check the status of some preconditions that are used by
13  * {@link DownloadsBackupJobService} and {@link AutomaticStorageManagementJobService}.
14  */
15 public class JobPreconditions {
16 
isNetworkMetered(Context context)17     public static boolean isNetworkMetered(Context context) {
18         ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(
19                 Context.CONNECTIVITY_SERVICE);
20         if (connectivityManager != null) {
21             return connectivityManager.isActiveNetworkMetered();
22         }
23         return true;
24     }
25 
isWifiConnected(Context context)26     public static boolean isWifiConnected(Context context) {
27         ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(
28                 Context.CONNECTIVITY_SERVICE);
29         if (connectivityManager != null) {
30             for (Network network : connectivityManager.getAllNetworks()) {
31                 NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
32                 if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI
33                         && networkInfo.isConnected()) {
34                     return true;
35                 }
36             }
37         }
38 
39         return false;
40     }
41 
isCharging(Context context)42     public static boolean isCharging(Context context) {
43         BatteryManager batteryManager = (BatteryManager) context.getSystemService(
44                 Context.BATTERY_SERVICE);
45         if (batteryManager != null) {
46             return batteryManager.isCharging();
47         }
48         return false;
49     }
50 
isIdle(Context context)51     public static boolean isIdle(Context context) {
52         PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
53         if (powerManager != null) {
54             return powerManager.isDeviceIdleMode();
55         }
56         return false;
57     }
58 
59 }
60