From b112f26ccef2ddcca67f14fd53b9ad297e452a4a Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Thu, 24 May 2018 13:41:50 +0100 Subject: Use TCM context if application context is unavailble. In TextClassificationManager, we use the application context to reduce the likelihood of leaking the underlying Activity. Unfortunately, some places (e.g. auto, system_server) don't actually provide us an application context, so we need to fall back to whatever context was passed in to TCM in the first place. Bug: 80188317 Test: none Change-Id: Ib4b2e6ed543d3ed56cefd8f56717855158cdc0fe --- .../view/textclassifier/TextClassificationManager.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/core/java/android/view/textclassifier/TextClassificationManager.java b/core/java/android/view/textclassifier/TextClassificationManager.java index aee0aa719bc2..dc1194bbe07c 100644 --- a/core/java/android/view/textclassifier/TextClassificationManager.java +++ b/core/java/android/view/textclassifier/TextClassificationManager.java @@ -120,7 +120,7 @@ public final class TextClassificationManager { synchronized (mLock) { if (mSettings == null) { mSettings = TextClassificationConstants.loadFromString(Settings.Global.getString( - mContext.getApplicationContext().getContentResolver(), + getApplicationContext().getContentResolver(), Settings.Global.TEXT_CLASSIFIER_CONSTANTS)); } return mSettings; @@ -186,8 +186,8 @@ public final class TextClassificationManager { protected void finalize() throws Throwable { try { // Note that fields could be null if the constructor threw. - if (mContext != null && mSettingsObserver != null) { - mContext.getApplicationContext().getContentResolver() + if (mSettingsObserver != null) { + getApplicationContext().getContentResolver() .unregisterContentObserver(mSettingsObserver); } } finally { @@ -240,6 +240,12 @@ public final class TextClassificationManager { } } + Context getApplicationContext() { + return mContext.getApplicationContext() != null + ? mContext.getApplicationContext() + : mContext; + } + /** @hide */ public static TextClassificationConstants getSettings(Context context) { Preconditions.checkNotNull(context); @@ -261,7 +267,7 @@ public final class TextClassificationManager { SettingsObserver(TextClassificationManager tcm) { super(null); mTcm = new WeakReference<>(tcm); - tcm.mContext.getApplicationContext().getContentResolver().registerContentObserver( + tcm.getApplicationContext().getContentResolver().registerContentObserver( Settings.Global.getUriFor(Settings.Global.TEXT_CLASSIFIER_CONSTANTS), false /* notifyForDescendants */, this); -- cgit v1.2.3-59-g8ed1b