Loading src/com/android/settings/fuelgauge/batterytip/AnomalyConfigJobService.java +20 −13 Original line number Diff line number Diff line Loading @@ -71,7 +71,11 @@ public class AnomalyConfigJobService extends JobService { ThreadUtils.postOnBackgroundThread(() -> { final StatsManager statsManager = getSystemService(StatsManager.class); checkAnomalyConfig(statsManager); try { BatteryTipUtils.uploadAnomalyPendingIntent(this, statsManager); } catch (StatsManager.StatsUnavailableException e) { Log.w(TAG, "Failed to uploadAnomalyPendingIntent.", e); } jobFinished(params, false /* wantsReschedule */); }); Loading @@ -96,23 +100,26 @@ public class AnomalyConfigJobService extends JobService { Log.i(TAG, "CurrentVersion: " + currentVersion + " new version: " + newVersion); if (newVersion > currentVersion) { statsManager.removeConfiguration(StatsManagerConfig.ANOMALY_CONFIG_KEY); try { statsManager.removeConfig(StatsManagerConfig.ANOMALY_CONFIG_KEY); } catch (StatsManager.StatsUnavailableException e) { Log.i(TAG, "When updating anomaly config, failed to first remove the old config " + StatsManagerConfig.ANOMALY_CONFIG_KEY, e); } if (!TextUtils.isEmpty(rawConfig)) { try { final byte[] config = Base64.decode(rawConfig, Base64.DEFAULT); if (statsManager.addConfiguration(StatsManagerConfig.ANOMALY_CONFIG_KEY, config)) { statsManager.addConfig(StatsManagerConfig.ANOMALY_CONFIG_KEY, config); Log.i(TAG, "Upload the anomaly config. configKey: " + StatsManagerConfig.ANOMALY_CONFIG_KEY); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt(KEY_ANOMALY_CONFIG_VERSION, newVersion); editor.commit(); } else { Log.i(TAG, "Upload the anomaly config failed. configKey: " + StatsManagerConfig.ANOMALY_CONFIG_KEY); } } catch (IllegalArgumentException e) { Log.e(TAG, "Anomaly raw config is in wrong format", e); } catch (StatsManager.StatsUnavailableException e) { Log.i(TAG, "Upload of anomaly config failed for configKey " + StatsManagerConfig.ANOMALY_CONFIG_KEY, e); } } } Loading src/com/android/settings/fuelgauge/batterytip/AnomalyConfigReceiver.java +5 −1 Original line number Diff line number Diff line Loading @@ -41,7 +41,11 @@ public class AnomalyConfigReceiver extends BroadcastReceiver { // Check whether to update the config AnomalyConfigJobService.scheduleConfigUpdate(context); try { BatteryTipUtils.uploadAnomalyPendingIntent(context, statsManager); } catch (StatsManager.StatsUnavailableException e) { Log.w(TAG, "Failed to uploadAnomalyPendingIntent.", e); } if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { AnomalyCleanupJobService.scheduleCleanUp(context); Loading src/com/android/settings/fuelgauge/batterytip/BatteryTipUtils.java +5 −3 Original line number Diff line number Diff line Loading @@ -110,12 +110,14 @@ public class BatteryTipUtils { /** * Upload the {@link PendingIntent} to {@link StatsManager} for anomaly detection * @throws StatsManager.StatsUnavailableException if failed to communicate with stats service */ public static void uploadAnomalyPendingIntent(Context context, StatsManager statsManager) { public static void uploadAnomalyPendingIntent(Context context, StatsManager statsManager) throws StatsManager.StatsUnavailableException { final Intent extraIntent = new Intent(context, AnomalyDetectionReceiver.class); final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, extraIntent, PendingIntent.FLAG_UPDATE_CURRENT); statsManager.setBroadcastSubscriber(StatsManagerConfig.ANOMALY_CONFIG_KEY, StatsManagerConfig.SUBSCRIBER_ID, pendingIntent); statsManager.setBroadcastSubscriber(pendingIntent, StatsManagerConfig.ANOMALY_CONFIG_KEY, StatsManagerConfig.SUBSCRIBER_ID); } } tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyConfigJobServiceTest.java +6 −4 Original line number Diff line number Diff line Loading @@ -104,7 +104,8 @@ public class AnomalyConfigJobServiceTest { } @Test public void checkAnomalyConfig_newConfigExist_removeOldConfig() { public void checkAnomalyConfig_newConfigExist_removeOldConfig() throws StatsManager.StatsUnavailableException{ Settings.Global.putInt(application.getContentResolver(), Settings.Global.ANOMALY_CONFIG_VERSION, ANOMALY_CONFIG_VERSION); Settings.Global.putString(application.getContentResolver(), Settings.Global.ANOMALY_CONFIG, Loading @@ -112,11 +113,12 @@ public class AnomalyConfigJobServiceTest { mJobService.checkAnomalyConfig(mStatsManager); verify(mStatsManager).removeConfiguration(StatsManagerConfig.ANOMALY_CONFIG_KEY); verify(mStatsManager).removeConfig(StatsManagerConfig.ANOMALY_CONFIG_KEY); } @Test public void checkAnomalyConfig_newConfigExist_uploadNewConfig() { public void checkAnomalyConfig_newConfigExist_uploadNewConfig() throws StatsManager.StatsUnavailableException{ Settings.Global.putInt(application.getContentResolver(), Settings.Global.ANOMALY_CONFIG_VERSION, ANOMALY_CONFIG_VERSION); Settings.Global.putString(application.getContentResolver(), Settings.Global.ANOMALY_CONFIG, Loading @@ -124,7 +126,7 @@ public class AnomalyConfigJobServiceTest { mJobService.checkAnomalyConfig(mStatsManager); verify(mStatsManager).addConfiguration(eq(StatsManagerConfig.ANOMALY_CONFIG_KEY), any()); verify(mStatsManager).addConfig(eq(StatsManagerConfig.ANOMALY_CONFIG_KEY), any()); } } Loading
src/com/android/settings/fuelgauge/batterytip/AnomalyConfigJobService.java +20 −13 Original line number Diff line number Diff line Loading @@ -71,7 +71,11 @@ public class AnomalyConfigJobService extends JobService { ThreadUtils.postOnBackgroundThread(() -> { final StatsManager statsManager = getSystemService(StatsManager.class); checkAnomalyConfig(statsManager); try { BatteryTipUtils.uploadAnomalyPendingIntent(this, statsManager); } catch (StatsManager.StatsUnavailableException e) { Log.w(TAG, "Failed to uploadAnomalyPendingIntent.", e); } jobFinished(params, false /* wantsReschedule */); }); Loading @@ -96,23 +100,26 @@ public class AnomalyConfigJobService extends JobService { Log.i(TAG, "CurrentVersion: " + currentVersion + " new version: " + newVersion); if (newVersion > currentVersion) { statsManager.removeConfiguration(StatsManagerConfig.ANOMALY_CONFIG_KEY); try { statsManager.removeConfig(StatsManagerConfig.ANOMALY_CONFIG_KEY); } catch (StatsManager.StatsUnavailableException e) { Log.i(TAG, "When updating anomaly config, failed to first remove the old config " + StatsManagerConfig.ANOMALY_CONFIG_KEY, e); } if (!TextUtils.isEmpty(rawConfig)) { try { final byte[] config = Base64.decode(rawConfig, Base64.DEFAULT); if (statsManager.addConfiguration(StatsManagerConfig.ANOMALY_CONFIG_KEY, config)) { statsManager.addConfig(StatsManagerConfig.ANOMALY_CONFIG_KEY, config); Log.i(TAG, "Upload the anomaly config. configKey: " + StatsManagerConfig.ANOMALY_CONFIG_KEY); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt(KEY_ANOMALY_CONFIG_VERSION, newVersion); editor.commit(); } else { Log.i(TAG, "Upload the anomaly config failed. configKey: " + StatsManagerConfig.ANOMALY_CONFIG_KEY); } } catch (IllegalArgumentException e) { Log.e(TAG, "Anomaly raw config is in wrong format", e); } catch (StatsManager.StatsUnavailableException e) { Log.i(TAG, "Upload of anomaly config failed for configKey " + StatsManagerConfig.ANOMALY_CONFIG_KEY, e); } } } Loading
src/com/android/settings/fuelgauge/batterytip/AnomalyConfigReceiver.java +5 −1 Original line number Diff line number Diff line Loading @@ -41,7 +41,11 @@ public class AnomalyConfigReceiver extends BroadcastReceiver { // Check whether to update the config AnomalyConfigJobService.scheduleConfigUpdate(context); try { BatteryTipUtils.uploadAnomalyPendingIntent(context, statsManager); } catch (StatsManager.StatsUnavailableException e) { Log.w(TAG, "Failed to uploadAnomalyPendingIntent.", e); } if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { AnomalyCleanupJobService.scheduleCleanUp(context); Loading
src/com/android/settings/fuelgauge/batterytip/BatteryTipUtils.java +5 −3 Original line number Diff line number Diff line Loading @@ -110,12 +110,14 @@ public class BatteryTipUtils { /** * Upload the {@link PendingIntent} to {@link StatsManager} for anomaly detection * @throws StatsManager.StatsUnavailableException if failed to communicate with stats service */ public static void uploadAnomalyPendingIntent(Context context, StatsManager statsManager) { public static void uploadAnomalyPendingIntent(Context context, StatsManager statsManager) throws StatsManager.StatsUnavailableException { final Intent extraIntent = new Intent(context, AnomalyDetectionReceiver.class); final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, extraIntent, PendingIntent.FLAG_UPDATE_CURRENT); statsManager.setBroadcastSubscriber(StatsManagerConfig.ANOMALY_CONFIG_KEY, StatsManagerConfig.SUBSCRIBER_ID, pendingIntent); statsManager.setBroadcastSubscriber(pendingIntent, StatsManagerConfig.ANOMALY_CONFIG_KEY, StatsManagerConfig.SUBSCRIBER_ID); } }
tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyConfigJobServiceTest.java +6 −4 Original line number Diff line number Diff line Loading @@ -104,7 +104,8 @@ public class AnomalyConfigJobServiceTest { } @Test public void checkAnomalyConfig_newConfigExist_removeOldConfig() { public void checkAnomalyConfig_newConfigExist_removeOldConfig() throws StatsManager.StatsUnavailableException{ Settings.Global.putInt(application.getContentResolver(), Settings.Global.ANOMALY_CONFIG_VERSION, ANOMALY_CONFIG_VERSION); Settings.Global.putString(application.getContentResolver(), Settings.Global.ANOMALY_CONFIG, Loading @@ -112,11 +113,12 @@ public class AnomalyConfigJobServiceTest { mJobService.checkAnomalyConfig(mStatsManager); verify(mStatsManager).removeConfiguration(StatsManagerConfig.ANOMALY_CONFIG_KEY); verify(mStatsManager).removeConfig(StatsManagerConfig.ANOMALY_CONFIG_KEY); } @Test public void checkAnomalyConfig_newConfigExist_uploadNewConfig() { public void checkAnomalyConfig_newConfigExist_uploadNewConfig() throws StatsManager.StatsUnavailableException{ Settings.Global.putInt(application.getContentResolver(), Settings.Global.ANOMALY_CONFIG_VERSION, ANOMALY_CONFIG_VERSION); Settings.Global.putString(application.getContentResolver(), Settings.Global.ANOMALY_CONFIG, Loading @@ -124,7 +126,7 @@ public class AnomalyConfigJobServiceTest { mJobService.checkAnomalyConfig(mStatsManager); verify(mStatsManager).addConfiguration(eq(StatsManagerConfig.ANOMALY_CONFIG_KEY), any()); verify(mStatsManager).addConfig(eq(StatsManagerConfig.ANOMALY_CONFIG_KEY), any()); } }