1package com.android.storagemanager.automatic;
2
3import android.content.Context;
4import android.net.ConnectivityManager;
5import android.net.Network;
6import android.net.NetworkInfo;
7import android.os.BatteryManager;
8import android.os.PowerManager;
9import 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 */
15public class JobPreconditions {
16
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
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
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
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