1 /*
2  * Copyright (C) 2021 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 com.android.server.tare;
18 
19 import android.annotation.NonNull;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.os.BatteryManager;
25 import android.os.SystemClock;
26 import android.util.IndentingPrintWriter;
27 import android.util.Log;
28 import android.util.Slog;
29 
30 /** Modifier that makes things free when the device is charging. */
31 class ChargingModifier extends Modifier {
32     private static final String TAG = "TARE-" + ChargingModifier.class.getSimpleName();
33     private static final boolean DEBUG = InternalResourceService.DEBUG
34             || Log.isLoggable(TAG, Log.DEBUG);
35 
36     private final InternalResourceService mIrs;
37     private final ChargingTracker mChargingTracker;
38 
ChargingModifier(@onNull InternalResourceService irs)39     ChargingModifier(@NonNull InternalResourceService irs) {
40         super();
41         mIrs = irs;
42         mChargingTracker = new ChargingTracker();
43     }
44 
45     @Override
setup()46     public void setup() {
47         mChargingTracker.startTracking(mIrs.getContext());
48     }
49 
50     @Override
tearDown()51     public void tearDown() {
52         mChargingTracker.stopTracking(mIrs.getContext());
53     }
54 
55     @Override
getModifiedCostToProduce(long ctp)56     long getModifiedCostToProduce(long ctp) {
57         return modifyValue(ctp);
58     }
59 
60     @Override
getModifiedPrice(long price)61     long getModifiedPrice(long price) {
62         return modifyValue(price);
63     }
64 
modifyValue(long val)65     private long modifyValue(long val) {
66         if (mChargingTracker.mCharging) {
67             return 0;
68         }
69         return val;
70     }
71 
72     @Override
dump(IndentingPrintWriter pw)73     void dump(IndentingPrintWriter pw) {
74         pw.print("charging=");
75         pw.println(mChargingTracker.mCharging);
76     }
77 
78     private final class ChargingTracker extends BroadcastReceiver {
79         private boolean mIsSetup = false;
80 
81         /**
82          * Track whether we're "charging", where charging means that we're ready to commit to
83          * doing work.
84          */
85         private volatile boolean mCharging;
86 
startTracking(@onNull Context context)87         public void startTracking(@NonNull Context context) {
88             if (mIsSetup) {
89                 return;
90             }
91 
92             final IntentFilter filter = new IntentFilter();
93             filter.addAction(BatteryManager.ACTION_CHARGING);
94             filter.addAction(BatteryManager.ACTION_DISCHARGING);
95             context.registerReceiver(this, filter);
96 
97             // Initialise tracker state.
98             final BatteryManager batteryManager = context.getSystemService(BatteryManager.class);
99             mCharging = batteryManager.isCharging();
100 
101             mIsSetup = true;
102         }
103 
stopTracking(@onNull Context context)104         public void stopTracking(@NonNull Context context) {
105             if (!mIsSetup) {
106                 return;
107             }
108 
109             context.unregisterReceiver(this);
110             mIsSetup = false;
111         }
112 
113         @Override
onReceive(Context context, Intent intent)114         public void onReceive(Context context, Intent intent) {
115             final String action = intent.getAction();
116             if (BatteryManager.ACTION_CHARGING.equals(action)) {
117                 if (DEBUG) {
118                     Slog.d(TAG, "Received charging intent, fired @ "
119                             + SystemClock.elapsedRealtime());
120                 }
121                 if (!mCharging) {
122                     mCharging = true;
123                     mIrs.onDeviceStateChanged();
124                 }
125             } else if (BatteryManager.ACTION_DISCHARGING.equals(action)) {
126                 if (DEBUG) {
127                     Slog.d(TAG, "Disconnected from power.");
128                 }
129                 if (mCharging) {
130                     mCharging = false;
131                     mIrs.onDeviceStateChanged();
132                 }
133             }
134         }
135     }
136 }
137