Fix memory leaks in Settings
Bug: 80507279
Test: inspected hprof before and after fix
Change-Id: I6ea2925695deb6261263649e858484e1667ec522
Merged-In: I6ea2925695deb6261263649e858484e1667ec522
diff --git a/src/com/android/settings/fuelgauge/BatteryUtils.java b/src/com/android/settings/fuelgauge/BatteryUtils.java
index c62af8f..2dc3ed2 100644
--- a/src/com/android/settings/fuelgauge/BatteryUtils.java
+++ b/src/com/android/settings/fuelgauge/BatteryUtils.java
@@ -24,8 +24,8 @@
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.BatteryStats;
-import android.os.Bundle;
import android.os.Build;
+import android.os.Bundle;
import android.os.Process;
import android.os.SystemClock;
import android.os.UserHandle;
@@ -35,7 +35,6 @@
import android.support.annotation.StringRes;
import android.support.annotation.VisibleForTesting;
import android.support.annotation.WorkerThread;
-import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;
import android.util.SparseLongArray;
@@ -48,7 +47,6 @@
import com.android.settings.fuelgauge.batterytip.AnomalyInfo;
import com.android.settings.fuelgauge.batterytip.StatsManagerConfig;
import com.android.settings.overlay.FeatureFactory;
-
import com.android.settingslib.fuelgauge.PowerWhitelistBackend;
import com.android.settingslib.utils.PowerUtil;
@@ -93,14 +91,14 @@
public static BatteryUtils getInstance(Context context) {
if (sInstance == null || sInstance.isDataCorrupted()) {
- sInstance = new BatteryUtils(context);
+ sInstance = new BatteryUtils(context.getApplicationContext());
}
return sInstance;
}
@VisibleForTesting
BatteryUtils(Context context) {
- mContext = context.getApplicationContext();
+ mContext = context;
mPackageManager = context.getPackageManager();
mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
mPowerUsageFeatureProvider = FeatureFactory.getFactory(
diff --git a/src/com/android/settings/overlay/FeatureFactoryImpl.java b/src/com/android/settings/overlay/FeatureFactoryImpl.java
index d058b3b..0f7d58c 100644
--- a/src/com/android/settings/overlay/FeatureFactoryImpl.java
+++ b/src/com/android/settings/overlay/FeatureFactoryImpl.java
@@ -94,7 +94,8 @@
@Override
public PowerUsageFeatureProvider getPowerUsageFeatureProvider(Context context) {
if (mPowerUsageFeatureProvider == null) {
- mPowerUsageFeatureProvider = new PowerUsageFeatureProviderImpl(context);
+ mPowerUsageFeatureProvider = new PowerUsageFeatureProviderImpl(
+ context.getApplicationContext());
}
return mPowerUsageFeatureProvider;
}
@@ -102,7 +103,8 @@
@Override
public DashboardFeatureProvider getDashboardFeatureProvider(Context context) {
if (mDashboardFeatureProvider == null) {
- mDashboardFeatureProvider = new DashboardFeatureProviderImpl(context);
+ mDashboardFeatureProvider = new DashboardFeatureProviderImpl(
+ context.getApplicationContext());
}
return mDashboardFeatureProvider;
}
@@ -118,10 +120,11 @@
@Override
public ApplicationFeatureProvider getApplicationFeatureProvider(Context context) {
if (mApplicationFeatureProvider == null) {
- mApplicationFeatureProvider = new ApplicationFeatureProviderImpl(context,
- new PackageManagerWrapper(context.getPackageManager()),
+ final Context appContext = context.getApplicationContext();
+ mApplicationFeatureProvider = new ApplicationFeatureProviderImpl(appContext,
+ new PackageManagerWrapper(appContext.getPackageManager()),
AppGlobals.getPackageManager(),
- (DevicePolicyManager) context
+ (DevicePolicyManager) appContext
.getSystemService(Context.DEVICE_POLICY_SERVICE));
}
return mApplicationFeatureProvider;
@@ -138,12 +141,14 @@
@Override
public EnterprisePrivacyFeatureProvider getEnterprisePrivacyFeatureProvider(Context context) {
if (mEnterprisePrivacyFeatureProvider == null) {
- mEnterprisePrivacyFeatureProvider = new EnterprisePrivacyFeatureProviderImpl(context,
- (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE),
- new PackageManagerWrapper(context.getPackageManager()),
- UserManager.get(context),
- (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE),
- context.getResources());
+ final Context appContext = context.getApplicationContext();
+ mEnterprisePrivacyFeatureProvider = new EnterprisePrivacyFeatureProviderImpl(appContext,
+ (DevicePolicyManager) appContext
+ .getSystemService(Context.DEVICE_POLICY_SERVICE),
+ new PackageManagerWrapper(appContext.getPackageManager()),
+ UserManager.get(appContext),
+ (ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE),
+ appContext.getResources());
}
return mEnterprisePrivacyFeatureProvider;
}
@@ -172,7 +177,8 @@
@Override
public SuggestionFeatureProvider getSuggestionFeatureProvider(Context context) {
if (mSuggestionFeatureProvider == null) {
- mSuggestionFeatureProvider = new SuggestionFeatureProviderImpl(context);
+ mSuggestionFeatureProvider = new SuggestionFeatureProviderImpl(
+ context.getApplicationContext());
}
return mSuggestionFeatureProvider;
}
@@ -180,7 +186,7 @@
@Override
public UserFeatureProvider getUserFeatureProvider(Context context) {
if (mUserFeatureProvider == null) {
- mUserFeatureProvider = new UserFeatureProviderImpl(context);
+ mUserFeatureProvider = new UserFeatureProviderImpl(context.getApplicationContext());
}
return mUserFeatureProvider;
}
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/BackgroundActivityPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/BackgroundActivityPreferenceControllerTest.java
index 389b714..8bbb334 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/BackgroundActivityPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/BackgroundActivityPreferenceControllerTest.java
@@ -17,6 +17,7 @@
package com.android.settings.fuelgauge;
import static com.google.common.truth.Truth.assertThat;
+
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doNothing;
@@ -89,6 +90,7 @@
mShadowContext = RuntimeEnvironment.application;
FakeFeatureFactory.setupForTest();
+ when(mContext.getApplicationContext()).thenReturn(mContext);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
when(mContext.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(mAppOpsManager);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
@@ -118,7 +120,7 @@
@Test
public void testHandlePreferenceTreeClick_restrictApp_showDialog() {
doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager)
- .checkOpNoThrow(anyInt(), anyInt(), anyString());
+ .checkOpNoThrow(anyInt(), anyInt(), anyString());
mController.handlePreferenceTreeClick(mPreference);
@@ -128,7 +130,7 @@
@Test
public void testHandlePreferenceTreeClick_unRestrictApp_showDialog() {
doReturn(AppOpsManager.MODE_IGNORED).when(mAppOpsManager)
- .checkOpNoThrow(anyInt(), anyInt(), anyString());
+ .checkOpNoThrow(anyInt(), anyInt(), anyString());
mController.handlePreferenceTreeClick(mPreference);