From b24514189a3b5d0dbae967a5535243a8f01d67d7 Mon Sep 17 00:00:00 2001 From: Fan Zhang Date: Tue, 19 Mar 2019 15:45:11 -0700 Subject: [PATCH] Store the application context statically in FeatureFactory It stores the application context in a static class, and will be destoryed when application stops. Bug: 124701288 Test: robo Change-Id: I4678072f3f38ae682be6ba075c85e63c49f8febe --- .../android/settings/overlay/FeatureFactory.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/com/android/settings/overlay/FeatureFactory.java b/src/com/android/settings/overlay/FeatureFactory.java index 23136a423cd..d9af345d652 100644 --- a/src/com/android/settings/overlay/FeatureFactory.java +++ b/src/com/android/settings/overlay/FeatureFactory.java @@ -20,6 +20,8 @@ import android.content.Context; import android.text.TextUtils; import android.util.Log; +import androidx.annotation.Nullable; + import com.android.settings.R; import com.android.settings.accounts.AccountFeatureProvider; import com.android.settings.applications.ApplicationFeatureProvider; @@ -50,6 +52,7 @@ public abstract class FeatureFactory { private static final boolean DEBUG = false; protected static FeatureFactory sFactory; + protected static Context sAppContext; /** * Returns a factory for creating feature controllers. Creates the factory if it does not @@ -60,6 +63,9 @@ public abstract class FeatureFactory { if (sFactory != null) { return sFactory; } + if (sAppContext == null) { + sAppContext = context.getApplicationContext(); + } if (DEBUG) Log.d(LOG_TAG, "getFactory"); final String clsName = context.getString(R.string.config_featureFactory); @@ -76,6 +82,16 @@ public abstract class FeatureFactory { return sFactory; } + /** + * Returns an application {@link Context} used to create this {@link FeatureFactory}. If the + * factory has not been properly created yet (aka {@link #getFactory} has not been called), this + * will return null. + */ + @Nullable + public static Context getAppContext() { + return sAppContext; + } + public abstract AssistGestureFeatureProvider getAssistGestureFeatureProvider(); public abstract SuggestionFeatureProvider getSuggestionFeatureProvider(Context context);