Home | History | Annotate | Download | only in systemalarm
      1 /*
      2  * Copyright 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package androidx.work.impl.background.systemalarm;
     18 
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.util.Log;
     23 
     24 import androidx.work.impl.background.systemalarm.ConstraintProxy.BatteryChargingProxy;
     25 import androidx.work.impl.background.systemalarm.ConstraintProxy.BatteryNotLowProxy;
     26 import androidx.work.impl.background.systemalarm.ConstraintProxy.NetworkStateProxy;
     27 import androidx.work.impl.background.systemalarm.ConstraintProxy.StorageNotLowProxy;
     28 import androidx.work.impl.utils.PackageManagerHelper;
     29 
     30 
     31 /**
     32  * The {@link BroadcastReceiver} responsible for updating constraint proxies.
     33  */
     34 public class ConstraintProxyUpdateReceiver extends BroadcastReceiver {
     35     private static final String TAG = "ConstrntProxyUpdtRecvr";
     36 
     37     static final String ACTION = "androidx.work.impl.background.systemalarm.UpdateProxies";
     38 
     39     static final String KEY_BATTERY_NOT_LOW_PROXY_ENABLED = "KEY_BATTERY_NOT_LOW_PROXY_ENABLED";
     40     static final String KEY_BATTERY_CHARGING_PROXY_ENABLED = "KEY_BATTERY_CHARGING_PROXY_ENABLED";
     41     static final String KEY_STORAGE_NOT_LOW_PROXY_ENABLED = "KEY_STORAGE_NOT_LOW_PROXY_ENABLED";
     42     static final String KEY_NETWORK_STATE_PROXY_ENABLED = "KEY_NETWORK_STATE_PROXY_ENABLED";
     43 
     44     /**
     45      * @param batteryNotLowProxyEnabled   {@code true} if {@link BatteryNotLowProxy needs to be
     46      *                                    enabled.}
     47      * @param batteryChargingProxyEnabled {@code true} if {@link BatteryChargingProxy needs to be
     48      *                                    enabled.}
     49      * @param storageNotLowProxyEnabled   {@code true} if {@link StorageNotLowProxy needs to be
     50      *                                    enabled.}
     51      * @param networkStateProxyEnabled    {@code true} if {@link NetworkStateProxy needs to be
     52      *                                    enabled.}
     53      * @return an {@link Intent} with information about the constraint proxies which need to be
     54      * enabled.
     55      */
     56     public static Intent newConstraintProxyUpdateIntent(
     57             boolean batteryNotLowProxyEnabled,
     58             boolean batteryChargingProxyEnabled,
     59             boolean storageNotLowProxyEnabled,
     60             boolean networkStateProxyEnabled) {
     61 
     62         Intent intent = new Intent(ACTION);
     63         intent.putExtra(KEY_BATTERY_NOT_LOW_PROXY_ENABLED, batteryNotLowProxyEnabled)
     64                 .putExtra(KEY_BATTERY_CHARGING_PROXY_ENABLED, batteryChargingProxyEnabled)
     65                 .putExtra(KEY_STORAGE_NOT_LOW_PROXY_ENABLED, storageNotLowProxyEnabled)
     66                 .putExtra(KEY_NETWORK_STATE_PROXY_ENABLED, networkStateProxyEnabled);
     67 
     68         return intent;
     69     }
     70 
     71     @Override
     72     public void onReceive(Context context, Intent intent) {
     73         String action = intent != null ? intent.getAction() : null;
     74         if (!ACTION.equals(action)) {
     75             Log.d(TAG, String.format("Ignoring unknown action %s", action));
     76         } else {
     77             boolean batteryNotLowProxyEnabled = intent.getBooleanExtra(
     78                     KEY_BATTERY_NOT_LOW_PROXY_ENABLED, false);
     79             boolean batteryChargingProxyEnabled = intent.getBooleanExtra(
     80                     KEY_BATTERY_CHARGING_PROXY_ENABLED, false);
     81             boolean storageNotLowProxyEnabled = intent.getBooleanExtra(
     82                     KEY_STORAGE_NOT_LOW_PROXY_ENABLED, false);
     83             boolean networkStateProxyEnabled = intent.getBooleanExtra(
     84                     KEY_NETWORK_STATE_PROXY_ENABLED, false);
     85 
     86             Log.d(TAG, String.format("Updating proxies: BatteryNotLowProxy enabled (%s), "
     87                             + "BatteryChargingProxy enabled (%s), "
     88                             + "StorageNotLowProxy (%s), "
     89                             + "NetworkStateProxy enabled (%s)", batteryNotLowProxyEnabled,
     90                     batteryChargingProxyEnabled, storageNotLowProxyEnabled,
     91                     networkStateProxyEnabled));
     92 
     93             PackageManagerHelper.setComponentEnabled(context, BatteryNotLowProxy.class,
     94                     batteryNotLowProxyEnabled);
     95             PackageManagerHelper.setComponentEnabled(context, BatteryChargingProxy.class,
     96                     batteryChargingProxyEnabled);
     97             PackageManagerHelper.setComponentEnabled(context, StorageNotLowProxy.class,
     98                     storageNotLowProxyEnabled);
     99             PackageManagerHelper.setComponentEnabled(context, NetworkStateProxy.class,
    100                     networkStateProxyEnabled);
    101         }
    102     }
    103 }
    104