1/* 2 * Copyright (C) 2015 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 17package com.android.server.content; 18 19import android.app.usage.UsageStatsManagerInternal; 20import android.app.usage.UsageStatsManagerInternal.AppIdleStateChangeListener; 21import android.os.UserHandle; 22 23import com.android.server.LocalServices; 24 25/** 26 * Helper to listen for app idle and charging status changes and restart backed off 27 * sync operations. 28 */ 29class AppIdleMonitor extends AppIdleStateChangeListener { 30 31 private final SyncManager mSyncManager; 32 private final UsageStatsManagerInternal mUsageStats; 33 private boolean mAppIdleParoleOn; 34 35 AppIdleMonitor(SyncManager syncManager) { 36 mSyncManager = syncManager; 37 mUsageStats = LocalServices.getService(UsageStatsManagerInternal.class); 38 mAppIdleParoleOn = mUsageStats.isAppIdleParoleOn(); 39 40 mUsageStats.addAppIdleStateChangeListener(this); 41 } 42 43 void setAppIdleParoleOn(boolean appIdleParoleOn) { 44 if (mAppIdleParoleOn == appIdleParoleOn) { 45 return; 46 } 47 mAppIdleParoleOn = appIdleParoleOn; 48 if (mAppIdleParoleOn) { 49 mSyncManager.onAppNotIdle(null, UserHandle.USER_ALL); 50 } 51 } 52 53 boolean isAppIdle(String packageName, int uidForAppId, int userId) { 54 return !mAppIdleParoleOn && mUsageStats.isAppIdle(packageName, uidForAppId, userId); 55 } 56 57 @Override 58 public void onAppIdleStateChanged(String packageName, int userId, boolean idle) { 59 // Don't care if the app is becoming idle 60 if (idle) return; 61 mSyncManager.onAppNotIdle(packageName, userId); 62 } 63 64 @Override 65 public void onParoleStateChanged(boolean isParoleOn) { 66 setAppIdleParoleOn(isParoleOn); 67 } 68} 69