Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9cb9ecc1 authored by Khalid Zubair's avatar Khalid Zubair
Browse files

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
(cherry picked from commit a1649aa2)
parent b8c08ed9
Loading
Loading
Loading
Loading
+14 −4
Original line number Original line Diff line number Diff line
@@ -142,9 +142,16 @@ public class FmRecordActivity extends Activity implements
                mStationName.setText(stationName);
                mStationName.setText(stationName);
                mRadioText.setText(radioText);
                mRadioText.setText(radioText);
                int id = cursor.getInt(cursor.getColumnIndex(Station._ID));
                int id = cursor.getInt(cursor.getColumnIndex(Station._ID));

                if (mWatchedId != id) {
                    if (mWatchedId != -1) {
                        resolver.unregisterContentObserver(mContentObserver);
                    }
                    resolver.registerContentObserver(
                    resolver.registerContentObserver(
                        ContentUris.withAppendedId(Station.CONTENT_URI, id), false,
                            ContentUris.withAppendedId(Station.CONTENT_URI, id),
                        mContentObserver);
                            false, mContentObserver);
                    mWatchedId = id;
                }
                // If no station name and no radio text, hide the view
                // If no station name and no radio text, hide the view
                if ((!TextUtils.isEmpty(stationName))
                if ((!TextUtils.isEmpty(stationName))
                        || (!TextUtils.isEmpty(radioText))) {
                        || (!TextUtils.isEmpty(radioText))) {
@@ -282,7 +289,9 @@ public class FmRecordActivity extends Activity implements
            mService.unregisterFmRadioListener(mFmListener);
            mService.unregisterFmRadioListener(mFmListener);
        }
        }
        unbindService(mServiceConnection);
        unbindService(mServiceConnection);
        if (mWatchedId != -1) {
            mContext.getContentResolver().unregisterContentObserver(mContentObserver);
            mContext.getContentResolver().unregisterContentObserver(mContentObserver);
        }
        super.onDestroy();
        super.onDestroy();
    }
    }


@@ -471,6 +480,7 @@ public class FmRecordActivity extends Activity implements
        setResult(RESULT_OK, intent);
        setResult(RESULT_OK, intent);
    }
    }


    private int mWatchedId = -1;
    private final ContentObserver mContentObserver = new ContentObserver(new Handler()) {
    private final ContentObserver mContentObserver = new ContentObserver(new Handler()) {
        public void onChange(boolean selfChange) {
        public void onChange(boolean selfChange) {
            updateUi();
            updateUi();