OSDN Git Service

Moving initializing of the listeners and CacheService to ACTION_MEDIA_MOUNTED for...
authorVenkat Krishnaraj <venkatkrishnaraj@venkat-krishnarajs-macbook-pro.local>
Tue, 26 Jan 2010 02:29:39 +0000 (18:29 -0800)
committerChih-Chung Chang <chihchung@google.com>
Tue, 26 Jan 2010 17:03:08 +0000 (09:03 -0800)
src/com/cooliris/cache/BootReceiver.java

index a976326..35fc3c3 100644 (file)
@@ -18,6 +18,7 @@ import android.util.Log;
 public class BootReceiver extends BroadcastReceiver {
     private static final String TAG = "BootReceiver";
     private final Handler mHandler = new Handler();
+    private boolean mListenersInitialized = false;
 
     @Override
     public void onReceive(final Context context, Intent intent) {
@@ -27,30 +28,30 @@ public class BootReceiver extends BroadcastReceiver {
             CacheService.markDirty(context);
             CacheService.startCache(context, true);
         } else if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
-            // Do nothing, wait for the mediascanner to be done after mounting.
-            ;
+            if (!mListenersInitialized) {
+                // We add special listeners for the MediaProvider
+                mListenersInitialized = true;
+                final Handler handler = mHandler;
+                final ContentObserver localObserver = new ContentObserver(handler) {
+                    public void onChange(boolean selfChange) {
+                        if (!LocalDataSource.sObserverActive) {
+                            CacheService.senseDirty(context, null);
+                        }
+                    }
+                };
+                // Start listening perpetually.
+                Uri uriImages = Images.Media.EXTERNAL_CONTENT_URI;
+                Uri uriVideos = Video.Media.EXTERNAL_CONTENT_URI;
+                ContentResolver cr = context.getContentResolver();
+                cr.registerContentObserver(uriImages, false, localObserver);
+                cr.registerContentObserver(uriVideos, false, localObserver);
+            }
         } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)) {
             final Uri fileUri = intent.getData();
             final long bucketId = SingleDataSource.parseBucketIdFromFileUri(fileUri.toString());
             if (!CacheService.isPresentInCache(bucketId)) {
                 CacheService.markDirty(context);
             }
-        } else if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
-            // We add special listeners for the MediaProvider
-            final Handler handler = mHandler;
-            final ContentObserver localObserver = new ContentObserver(handler) {
-                public void onChange(boolean selfChange) {
-                    if (!LocalDataSource.sObserverActive) {
-                        CacheService.senseDirty(context, null);
-                    }
-                }
-            };
-            // Start listening perpetually.
-            Uri uriImages = Images.Media.EXTERNAL_CONTENT_URI;
-            Uri uriVideos = Video.Media.EXTERNAL_CONTENT_URI;
-            ContentResolver cr = context.getContentResolver();
-            cr.registerContentObserver(uriImages, false, localObserver);
-            cr.registerContentObserver(uriVideos, false, localObserver);
         } else if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
             LocalDataSource.sThumbnailCache.close();
             LocalDataSource.sThumbnailCacheVideo.close();