| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 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 |
|
| 39 |
ChargingModifier(@NonNull InternalResourceService irs) { |
| 40 |
super(); |
| 41 |
mIrs = irs; |
| 42 |
mChargingTracker = new ChargingTracker(); |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
public void setup() { |
| 47 |
mChargingTracker.startTracking(mIrs.getContext()); |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public void tearDown() { |
| 52 |
mChargingTracker.stopTracking(mIrs.getContext()); |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
long getModifiedCostToProduce(long ctp) { |
| 57 |
return modifyValue(ctp); |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
long getModifiedPrice(long price) { |
| 62 |
return modifyValue(price); |
| 63 |
} |
| 64 |
|
| 65 |
private long modifyValue(long val) { |
| 66 |
if (mChargingTracker.mCharging) { |
| 67 |
return 0; |
| 68 |
} |
| 69 |
return val; |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 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 |
|
| 83 |
|
| 84 |
|
| 85 |
private volatile boolean mCharging; |
| 86 |
|
| 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 |
|
| 98 |
final BatteryManager batteryManager = context.getSystemService(BatteryManager.class); |
| 99 |
mCharging = batteryManager.isCharging(); |
| 100 |
|
| 101 |
mIsSetup = true; |
| 102 |
} |
| 103 |
|
| 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 |
| 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 |
} |