| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.server.job.restrictions; |
| 18 |
|
| 19 |
import android.app.job.JobInfo; |
| 20 |
import android.app.job.JobParameters; |
| 21 |
import android.os.PowerManager; |
| 22 |
import android.os.PowerManager.OnThermalStatusChangedListener; |
| 23 |
import android.util.IndentingPrintWriter; |
| 24 |
|
| 25 |
import com.android.internal.annotations.VisibleForTesting; |
| 26 |
import com.android.server.job.JobSchedulerService; |
| 27 |
import com.android.server.job.controllers.JobStatus; |
| 28 |
|
| 29 |
public class ThermalStatusRestriction extends JobRestriction { |
| 30 |
private static final String TAG = "ThermalStatusRestriction"; |
| 31 |
|
| 32 |
|
| 33 |
private static final int LOW_PRIORITY_THRESHOLD = PowerManager.THERMAL_STATUS_LIGHT; |
| 34 |
|
| 35 |
private static final int HIGHER_PRIORITY_THRESHOLD = PowerManager.THERMAL_STATUS_MODERATE; |
| 36 |
|
| 37 |
private static final int LOWER_THRESHOLD = LOW_PRIORITY_THRESHOLD; |
| 38 |
|
| 39 |
private static final int UPPER_THRESHOLD = PowerManager.THERMAL_STATUS_SEVERE; |
| 40 |
|
| 41 |
private volatile int mThermalStatus = PowerManager.THERMAL_STATUS_NONE; |
| 42 |
|
| 43 |
public ThermalStatusRestriction(JobSchedulerService service) { |
| 44 |
super(service, JobParameters.STOP_REASON_DEVICE_STATE, |
| 45 |
JobParameters.INTERNAL_STOP_REASON_DEVICE_THERMAL); |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public void onSystemServicesReady() { |
| 50 |
final PowerManager powerManager = |
| 51 |
mService.getTestableContext().getSystemService(PowerManager.class); |
| 52 |
|
| 53 |
powerManager.addThermalStatusListener(new OnThermalStatusChangedListener() { |
| 54 |
@Override |
| 55 |
public void onThermalStatusChanged(int status) { |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
final boolean significantChange = |
| 66 |
|
| 67 |
|
| 68 |
(status >= LOWER_THRESHOLD && status <= UPPER_THRESHOLD) |
| 69 |
|
| 70 |
|
| 71 |
|| (mThermalStatus >= LOWER_THRESHOLD && status < LOWER_THRESHOLD) |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|| (mThermalStatus < UPPER_THRESHOLD && status > UPPER_THRESHOLD); |
| 76 |
mThermalStatus = status; |
| 77 |
if (significantChange) { |
| 78 |
mService.onControllerStateChanged(null); |
| 79 |
} |
| 80 |
} |
| 81 |
}); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public boolean isJobRestricted(JobStatus job) { |
| 86 |
if (mThermalStatus >= UPPER_THRESHOLD) { |
| 87 |
return true; |
| 88 |
} |
| 89 |
final int priority = job.getEffectivePriority(); |
| 90 |
if (mThermalStatus >= HIGHER_PRIORITY_THRESHOLD) { |
| 91 |
|
| 92 |
|
| 93 |
return !job.shouldTreatAsExpeditedJob() |
| 94 |
&& !(priority == JobInfo.PRIORITY_HIGH |
| 95 |
&& mService.isCurrentlyRunningLocked(job)); |
| 96 |
} |
| 97 |
if (mThermalStatus >= LOW_PRIORITY_THRESHOLD) { |
| 98 |
|
| 99 |
|
| 100 |
return (priority == JobInfo.PRIORITY_LOW && !mService.isCurrentlyRunningLocked(job)) |
| 101 |
|| priority == JobInfo.PRIORITY_MIN; |
| 102 |
} |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
@VisibleForTesting |
| 107 |
int getThermalStatus() { |
| 108 |
return mThermalStatus; |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public void dumpConstants(IndentingPrintWriter pw) { |
| 113 |
pw.print("Thermal status: "); |
| 114 |
pw.println(mThermalStatus); |
| 115 |
} |
| 116 |
} |