From 123c4a38514c003aac50caed7ad3cb6ab8f2515a Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Wed, 26 Jan 2011 23:13:18 +0900 Subject: [PATCH] Fix BroadcastReceiver leakage in SubtypeSwitcher Bug: 3345931 Change-Id: If31a6e803ea7e1a857085ced354722c484fda075 --- .../android/inputmethod/latin/LatinIME.java | 16 ++++++++++++---- .../inputmethod/latin/SubtypeSwitcher.java | 18 ++++-------------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 78674b4e4..2bc5184fc 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -37,6 +37,7 @@ import android.content.res.Configuration; import android.content.res.Resources; import android.inputmethodservice.InputMethodService; import android.media.AudioManager; +import android.net.ConnectivityManager; import android.os.Debug; import android.os.Handler; import android.os.Message; @@ -388,8 +389,10 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mOrientation = res.getConfiguration().orientation; initSuggestPuncList(); - // register to receive ringer mode changes for silent mode - IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION); + // register to receive ringer mode change and network state change. + final IntentFilter filter = new IntentFilter(); + filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION); + filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(mReceiver, filter); mVoiceConnector = VoiceIMEConnector.init(this, prefs, mHandler); prefs.registerOnSharedPreferenceChangeListener(this); @@ -1986,11 +1989,16 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } - // receive ringer mode changes to detect silent mode + // receive ringer mode change and network state change. private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - updateRingerMode(); + final String action = intent.getAction(); + if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) { + updateRingerMode(); + } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { + mSubtypeSwitcher.onNetworkStateChanged(intent); + } } }; diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java index 67ca9aaad..f4262cc99 100644 --- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java +++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java @@ -23,10 +23,8 @@ import com.android.inputmethod.voice.SettingsUtil; import com.android.inputmethod.voice.VoiceIMEConnector; import com.android.inputmethod.voice.VoiceInput; -import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.res.Configuration; @@ -91,8 +89,7 @@ public class SubtypeSwitcher { } public static void init(LatinIME service, SharedPreferences prefs) { - sInstance.mPrefs = prefs; - sInstance.resetParams(service); + sInstance.initialize(service, prefs); sInstance.updateAllParameters(); SubtypeLocale.init(service); @@ -102,8 +99,9 @@ public class SubtypeSwitcher { // Intentional empty constructor for singleton. } - private void resetParams(LatinIME service) { + private void initialize(LatinIME service, SharedPreferences prefs) { mService = service; + mPrefs = prefs; mResources = service.getResources(); mImm = (InputMethodManager) service.getSystemService(Context.INPUT_METHOD_SERVICE); mConnectivityManager = (ConnectivityManager) service.getSystemService( @@ -125,14 +123,6 @@ public class SubtypeSwitcher { final NetworkInfo info = mConnectivityManager.getActiveNetworkInfo(); mIsNetworkConnected = (info != null && info.isConnected()); - final BroadcastReceiver receiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - onNetworkStateChanged(intent); - } - }; - service.registerReceiver(receiver, - new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); } // Update all parameters stored in SubtypeSwitcher. @@ -376,7 +366,7 @@ public class SubtypeSwitcher { return true; } - private void onNetworkStateChanged(Intent intent) { + public void onNetworkStateChanged(Intent intent) { final boolean noConnection = intent.getBooleanExtra( ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); mIsNetworkConnected = !noConnection;