diff options
author | Khalid Zubair <kzubair@cyngn.com> | 2016-06-24 14:37:35 -0700 |
---|---|---|
committer | Scott Mertz <scott@cyngn.com> | 2016-06-24 17:08:50 -0700 |
commit | a1649aa270ee08c4079b006ae99842ee0e50f441 (patch) | |
tree | 19c16f116b0c79e7b3142c1ff9075c19f8e60d36 | |
parent | 4da63b534eecadab856bf4e39d74aa208798dca1 (diff) | |
download | android_packages_apps_FMRadio-a1649aa270ee08c4079b006ae99842ee0e50f441.tar.gz android_packages_apps_FMRadio-a1649aa270ee08c4079b006ae99842ee0e50f441.tar.bz2 android_packages_apps_FMRadio-a1649aa270ee08c4079b006ae99842ee0e50f441.zip |
Fix exponential content observer registration
On each call to ContentObserver.onChange(), updateUi() re-registers a
ContentObserver. On the first onChange, a second observer is
registered and for the next change, both observers call updateUi()
registering two more, and so on. After n onChanges there are 2^n
observers.
Remove the duplicate registrations and register a ContentObserver only
if the station changes.
FEIJ-1373
Change-Id: I07d0bd968165dfda5601f72fd20641bb9d62a5f1
-rw-r--r-- | src/com/android/fmradio/FmRecordActivity.java | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/com/android/fmradio/FmRecordActivity.java b/src/com/android/fmradio/FmRecordActivity.java index a625250..09cbb75 100644 --- a/src/com/android/fmradio/FmRecordActivity.java +++ b/src/com/android/fmradio/FmRecordActivity.java @@ -142,9 +142,16 @@ public class FmRecordActivity extends Activity implements mStationName.setText(stationName); mRadioText.setText(radioText); int id = cursor.getInt(cursor.getColumnIndex(Station._ID)); - resolver.registerContentObserver( - ContentUris.withAppendedId(Station.CONTENT_URI, id), false, - mContentObserver); + + if (mWatchedId != id) { + if (mWatchedId != -1) { + resolver.unregisterContentObserver(mContentObserver); + } + resolver.registerContentObserver( + ContentUris.withAppendedId(Station.CONTENT_URI, id), + false, mContentObserver); + mWatchedId = id; + } // If no station name and no radio text, hide the view if ((!TextUtils.isEmpty(stationName)) || (!TextUtils.isEmpty(radioText))) { @@ -282,7 +289,9 @@ public class FmRecordActivity extends Activity implements mService.unregisterFmRadioListener(mFmListener); } unbindService(mServiceConnection); - mContext.getContentResolver().unregisterContentObserver(mContentObserver); + if (mWatchedId != -1) { + mContext.getContentResolver().unregisterContentObserver(mContentObserver); + } super.onDestroy(); } @@ -471,6 +480,7 @@ public class FmRecordActivity extends Activity implements setResult(RESULT_OK, intent); } + private int mWatchedId = -1; private final ContentObserver mContentObserver = new ContentObserver(new Handler()) { public void onChange(boolean selfChange) { updateUi(); |