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
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 @@
                 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 @@
             mService.unregisterFmRadioListener(mFmListener);
         }
         unbindService(mServiceConnection);
-        mContext.getContentResolver().unregisterContentObserver(mContentObserver);
+        if (mWatchedId != -1) {
+            mContext.getContentResolver().unregisterContentObserver(mContentObserver);
+        }
         super.onDestroy();
     }
 
@@ -471,6 +480,7 @@
         setResult(RESULT_OK, intent);
     }
 
+    private int mWatchedId = -1;
     private final ContentObserver mContentObserver = new ContentObserver(new Handler()) {
         public void onChange(boolean selfChange) {
             updateUi();