1 /* 2 * Copyright 2017 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 package androidx.work.impl.background.systemalarm; 17 18 import static androidx.work.NetworkType.NOT_REQUIRED; 19 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.util.Log; 24 25 import androidx.work.Constraints; 26 import androidx.work.impl.model.WorkSpec; 27 28 import java.util.List; 29 30 abstract class ConstraintProxy extends BroadcastReceiver { 31 private static final String TAG = "ConstraintProxy"; 32 33 @Override onReceive(Context context, Intent intent)34 public void onReceive(Context context, Intent intent) { 35 Log.d(TAG, String.format("onReceive : %s", intent)); 36 Intent constraintChangedIntent = CommandHandler.createConstraintsChangedIntent(context); 37 context.startService(constraintChangedIntent); 38 } 39 40 /** 41 * Proxy for Battery Not Low constraint 42 */ 43 public static class BatteryNotLowProxy extends ConstraintProxy { 44 } 45 46 /** 47 * Proxy for Battery Charging constraint 48 */ 49 public static class BatteryChargingProxy extends ConstraintProxy { 50 } 51 52 /** 53 * Proxy for Storage Not Low constraint 54 */ 55 public static class StorageNotLowProxy extends ConstraintProxy { 56 } 57 58 /** 59 * Proxy for Network State constraints 60 */ 61 public static class NetworkStateProxy extends ConstraintProxy { 62 } 63 64 /** 65 * Enables/Disables proxies based on constraints in {@link WorkSpec}s 66 * 67 * @param context {@link Context} 68 * @param workSpecs list of {@link WorkSpec}s to update proxies against 69 */ updateAll(Context context, List<WorkSpec> workSpecs)70 static void updateAll(Context context, List<WorkSpec> workSpecs) { 71 boolean batteryNotLowProxyEnabled = false; 72 boolean batteryChargingProxyEnabled = false; 73 boolean storageNotLowProxyEnabled = false; 74 boolean networkStateProxyEnabled = false; 75 76 for (WorkSpec workSpec : workSpecs) { 77 Constraints constraints = workSpec.constraints; 78 batteryNotLowProxyEnabled |= constraints.requiresBatteryNotLow(); 79 batteryChargingProxyEnabled |= constraints.requiresCharging(); 80 storageNotLowProxyEnabled |= constraints.requiresStorageNotLow(); 81 networkStateProxyEnabled |= 82 constraints.getRequiredNetworkType() != NOT_REQUIRED; 83 84 if (batteryNotLowProxyEnabled && batteryChargingProxyEnabled 85 && storageNotLowProxyEnabled && networkStateProxyEnabled) { 86 break; 87 } 88 } 89 90 Intent updateProxyIntent = 91 ConstraintProxyUpdateReceiver.newConstraintProxyUpdateIntent( 92 batteryNotLowProxyEnabled, 93 batteryChargingProxyEnabled, 94 storageNotLowProxyEnabled, 95 networkStateProxyEnabled); 96 97 // ConstraintProxies are being updated via a separate broadcast receiver. 98 // For more information on why we do this look at b/73549299 99 context.sendBroadcast(updateProxyIntent); 100 } 101 } 102