Merge "Unhiding Forced App Standby"

This commit is contained in:
TreeHugger Robot
2017-09-09 07:47:23 +00:00
committed by Android (Google) Code Review
4 changed files with 6 additions and 73 deletions

View File

@@ -245,8 +245,6 @@ class AlarmManagerService extends SystemService {
private static final long DEFAULT_LISTENER_TIMEOUT = 5 * 1000;
private static final boolean DEFAULT_BACKGROUND_RESTRICTIONS_ENABLED = false;
// Minimum futurity of a new alarm
public long MIN_FUTURITY = DEFAULT_MIN_FUTURITY;
@@ -266,8 +264,6 @@ class AlarmManagerService extends SystemService {
// Direct alarm listener callback timeout
public long LISTENER_TIMEOUT = DEFAULT_LISTENER_TIMEOUT;
public boolean BACKGROUND_ALARMS_BLOCKED = DEFAULT_BACKGROUND_RESTRICTIONS_ENABLED;
private ContentResolver mResolver;
private final KeyValueListParser mParser = new KeyValueListParser(',');
private long mLastAllowWhileIdleWhitelistDuration = -1;
@@ -326,21 +322,6 @@ class AlarmManagerService extends SystemService {
DEFAULT_ALLOW_WHILE_IDLE_WHITELIST_DURATION);
LISTENER_TIMEOUT = mParser.getLong(KEY_LISTENER_TIMEOUT,
DEFAULT_LISTENER_TIMEOUT);
BACKGROUND_ALARMS_BLOCKED = mParser.getBoolean(KEY_BG_RESTRICTIONS_ENABLED,
DEFAULT_BACKGROUND_RESTRICTIONS_ENABLED);
if (!BACKGROUND_ALARMS_BLOCKED) {
// TODO: remove this code and constant when feature is turned on
// deliver all blocked alarms
final ArrayList<Alarm> allBlockedAlarms = new ArrayList<>();
for (int i = mPendingBackgroundAlarms.size() - 1; i >=0; i--) {
allBlockedAlarms.addAll(mPendingBackgroundAlarms.valueAt(i));
}
mPendingBackgroundAlarms = new SparseArray<>();
deliverPendingBackgroundAlarmsLocked(allBlockedAlarms,
SystemClock.elapsedRealtime());
} else if (DEBUG_BG_LIMIT) {
Slog.d(TAG, "Background limiting enabled");
}
updateAllowWhileIdleMinTimeLocked();
updateAllowWhileIdleWhitelistDurationLocked();
@@ -2392,9 +2373,6 @@ class AlarmManagerService extends SystemService {
}
private boolean isBackgroundRestricted(Alarm alarm) {
if (!mConstants.BACKGROUND_ALARMS_BLOCKED) {
return false;
}
if (alarm.alarmClock != null) {
// Don't block alarm clocks
return false;

View File

@@ -228,7 +228,6 @@ public final class JobSchedulerService extends com.android.server.SystemService
private static final String KEY_MAX_WORK_RESCHEDULE_COUNT = "max_work_reschedule_count";
private static final String KEY_MIN_LINEAR_BACKOFF_TIME = "min_linear_backoff_time";
private static final String KEY_MIN_EXP_BACKOFF_TIME = "min_exp_backoff_time";
private static final String KEY_BG_JOBS_RESTRICTED = "bg_jobs_restricted";
private static final int DEFAULT_MIN_IDLE_COUNT = 1;
private static final int DEFAULT_MIN_CHARGING_COUNT = 1;
@@ -244,7 +243,6 @@ public final class JobSchedulerService extends com.android.server.SystemService
private static final int DEFAULT_BG_MODERATE_JOB_COUNT = 4;
private static final int DEFAULT_BG_LOW_JOB_COUNT = 1;
private static final int DEFAULT_BG_CRITICAL_JOB_COUNT = 1;
private static final boolean DEFAULT_BG_JOBS_RESTRICTED = false;
private static final int DEFAULT_MAX_STANDARD_RESCHEDULE_COUNT = Integer.MAX_VALUE;
private static final int DEFAULT_MAX_WORK_RESCHEDULE_COUNT = Integer.MAX_VALUE;
private static final long DEFAULT_MIN_LINEAR_BACKOFF_TIME = JobInfo.MIN_BACKOFF_MILLIS;
@@ -338,11 +336,6 @@ public final class JobSchedulerService extends com.android.server.SystemService
*/
long MIN_EXP_BACKOFF_TIME = DEFAULT_MIN_EXP_BACKOFF_TIME;
/**
* Runtime switch for throttling background jobs
*/
boolean BACKGROUND_JOBS_RESTRICTED = DEFAULT_BG_JOBS_RESTRICTED;
private ContentResolver mResolver;
private final KeyValueListParser mParser = new KeyValueListParser(',');
@@ -421,12 +414,6 @@ public final class JobSchedulerService extends com.android.server.SystemService
DEFAULT_MIN_LINEAR_BACKOFF_TIME);
MIN_EXP_BACKOFF_TIME = mParser.getLong(KEY_MIN_EXP_BACKOFF_TIME,
DEFAULT_MIN_EXP_BACKOFF_TIME);
final boolean bgJobsRestricted = mParser.getBoolean(KEY_BG_JOBS_RESTRICTED,
DEFAULT_BG_JOBS_RESTRICTED);
if (bgJobsRestricted != BACKGROUND_JOBS_RESTRICTED) {
mBackgroundJobsController.enableRestrictionsLocked(
BACKGROUND_JOBS_RESTRICTED = bgJobsRestricted);
}
}
}
@@ -486,9 +473,6 @@ public final class JobSchedulerService extends com.android.server.SystemService
pw.print(" "); pw.print(KEY_MIN_EXP_BACKOFF_TIME); pw.print("=");
pw.print(MIN_EXP_BACKOFF_TIME); pw.println();
pw.print(" "); pw.print(KEY_BG_JOBS_RESTRICTED); pw.print("=");
pw.print(BACKGROUND_JOBS_RESTRICTED); pw.println();
}
}

View File

@@ -48,14 +48,12 @@ public final class BackgroundJobsController extends StateController {
private static final Object sCreationLock = new Object();
private static volatile BackgroundJobsController sController;
/* Runtime switch to keep feature under wraps */
private boolean mEnableSwitch;
private final JobSchedulerService mJobSchedulerService;
private final IAppOpsService mAppOpsService;
private final IDeviceIdleController mDeviceIdleController;
private final SparseBooleanArray mForegroundUids;
private int[] mPowerWhitelistedAppIds;
private int[] mPowerWhitelistedUserAppIds;
private int[] mTempWhitelistedAppIds;
/**
* Only tracks jobs for which source package app op RUN_ANY_IN_BACKGROUND is not ALLOWED.
@@ -81,7 +79,8 @@ public final class BackgroundJobsController extends StateController {
try {
switch (intent.getAction()) {
case PowerManager.ACTION_POWER_SAVE_WHITELIST_CHANGED:
mPowerWhitelistedAppIds = mDeviceIdleController.getAppIdWhitelist();
mPowerWhitelistedUserAppIds =
mDeviceIdleController.getAppIdUserWhitelist();
break;
case PowerManager.ACTION_POWER_SAVE_TEMP_WHITELIST_CHANGED:
mTempWhitelistedAppIds = mDeviceIdleController.getAppIdTempWhitelist();
@@ -110,7 +109,7 @@ public final class BackgroundJobsController extends StateController {
try {
mAppOpsService.startWatchingMode(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, null,
new AppOpsWatcher());
mPowerWhitelistedAppIds = mDeviceIdleController.getAppIdWhitelist();
mPowerWhitelistedUserAppIds = mDeviceIdleController.getAppIdUserWhitelist();
mTempWhitelistedAppIds = mDeviceIdleController.getAppIdTempWhitelist();
} catch (RemoteException rexc) {
// Shouldn't happen as they are in the same process.
@@ -121,8 +120,6 @@ public final class BackgroundJobsController extends StateController {
powerWhitelistFilter.addAction(PowerManager.ACTION_POWER_SAVE_TEMP_WHITELIST_CHANGED);
context.registerReceiverAsUser(mDozeWhitelistReceiver, UserHandle.ALL, powerWhitelistFilter,
null, null);
mEnableSwitch = false;
}
@Override
@@ -170,7 +167,6 @@ public final class BackgroundJobsController extends StateController {
@Override
public void dumpControllerStateLocked(final PrintWriter pw, final int filterUid) {
pw.println("Background restrictions: global switch = " + mEnableSwitch);
pw.print("Foreground uids: [");
for (int i = 0; i < mForegroundUids.size(); i++) {
if (mForegroundUids.valueAt(i)) pw.print(mForegroundUids.keyAt(i) + " ");
@@ -206,14 +202,6 @@ public final class BackgroundJobsController extends StateController {
});
}
public void enableRestrictionsLocked(boolean enable) {
mEnableSwitch = enable;
Slog.d(LOG_TAG, "Background jobs restrictions switch changed to " + mEnableSwitch);
if (checkAllTrackedJobsLocked()) {
mStateChangedListener.onControllerStateChanged();
}
}
void startTrackingJobLocked(JobStatus jobStatus) {
final int uid = jobStatus.getSourceUid();
ArraySet<JobStatus> jobsForUid = mTrackedJobs.get(uid);
@@ -255,11 +243,11 @@ public final class BackgroundJobsController extends StateController {
boolean isWhitelistedLocked(int uid) {
return ArrayUtils.contains(mTempWhitelistedAppIds, UserHandle.getAppId(uid))
|| ArrayUtils.contains(mPowerWhitelistedAppIds, UserHandle.getAppId(uid));
|| ArrayUtils.contains(mPowerWhitelistedUserAppIds, UserHandle.getAppId(uid));
}
boolean canRunJobLocked(int uid) {
return !mEnableSwitch || mForegroundUids.get(uid) || isWhitelistedLocked(uid);
return mForegroundUids.get(uid) || isWhitelistedLocked(uid);
}
private final class AppOpsWatcher extends IAppOpsCallback.Stub {

View File

@@ -116,7 +116,6 @@ public class BackgroundRestrictionsTest {
intentFilter.addAction(ACTION_JOB_STARTED);
intentFilter.addAction(ACTION_JOB_STOPPED);
mContext.registerReceiver(mJobStateChangeReceiver, intentFilter);
setGlobalSwitch(true);
setAppOpsModeAllowed(true);
setPowerWhiteListed(false);
}
@@ -131,16 +130,6 @@ public class BackgroundRestrictionsTest {
assertTrue("Job did not start after scheduling", awaitJobStart(DEFAULT_WAIT_TIMEOUT));
}
@Test
public void testGlobalSwitch() throws Exception {
setGlobalSwitch(false); // Job should not stop now.
scheduleAndAssertJobStarted();
setAppOpsModeAllowed(false);
mIActivityManager.makePackageIdle(TEST_APP_PACKAGE, UserHandle.USER_CURRENT);
assertFalse("Job stopped even when feature switch is off",
awaitJobStop(DEFAULT_WAIT_TIMEOUT));
}
@Test
public void testPowerWhiteList() throws Exception {
scheduleAndAssertJobStarted();
@@ -163,16 +152,10 @@ public class BackgroundRestrictionsTest {
cancelJobsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(cancelJobsIntent);
mContext.unregisterReceiver(mJobStateChangeReceiver);
setGlobalSwitch(false);
setAppOpsModeAllowed(true);
setPowerWhiteListed(false);
}
private void setGlobalSwitch(boolean enabled) {
Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.JOB_SCHEDULER_CONSTANTS, "bg_jobs_restricted=" + enabled);
}
private void setPowerWhiteListed(boolean whitelist) throws RemoteException {
if (whitelist) {
mDeviceIdleController.addPowerSaveWhitelistApp(TEST_APP_PACKAGE);