OSDN Git Service

Simplify the LocalData API.
authorSascha Haeberling <haeberling@google.com>
Fri, 20 Dec 2013 19:59:35 +0000 (11:59 -0800)
committerSascha Haeberling <haeberling@google.com>
Fri, 20 Dec 2013 21:45:53 +0000 (13:45 -0800)
By removing the Context from most method calls, it makes the
API much more concise and focus on what's important.

Change-Id: Ib79344365bd4d6a33f8ef59d1d28b5e9780102f5

src/com/android/camera/CameraActivity.java
src/com/android/camera/data/AbstractLocalDataAdapterWrapper.java
src/com/android/camera/data/CameraDataAdapter.java
src/com/android/camera/data/FixedFirstDataAdapter.java
src/com/android/camera/data/FixedLastDataAdapter.java
src/com/android/camera/data/LocalDataAdapter.java
src/com/android/camera/data/LocalMediaData.java

index b2546fc..64b4c08 100644 (file)
@@ -533,7 +533,7 @@ public class CameraActivity extends Activity
                 }
             };
 
-    private LocalDataAdapter.LocalDataListener mLocalDataListener =
+    private final LocalDataAdapter.LocalDataListener mLocalDataListener =
             new LocalDataAdapter.LocalDataListener() {
                 @Override
                 public void onMetadataUpdated(List<Integer> updatedData) {
@@ -658,7 +658,7 @@ public class CameraActivity extends Activity
                         hideSessionProgress();
                         updateSessionProgress(0);
                     }
-                    mDataAdapter.refresh(CameraActivity.this, uri, /* isInProgress */ false);
+                    mDataAdapter.refresh(uri, /* isInProgress */ false);
                 }
 
                 @Override
@@ -679,7 +679,7 @@ public class CameraActivity extends Activity
 
                 @Override
                 public void onSessionUpdated(Uri uri) {
-                    mDataAdapter.refresh(CameraActivity.this, uri, /* isInProgress */ true);
+                    mDataAdapter.refresh(uri, /* isInProgress */ true);
                 }
             };
 
@@ -807,12 +807,12 @@ public class CameraActivity extends Activity
         String mimeType = cr.getType(uri);
         if (mimeType.startsWith("video/")) {
             sendBroadcast(new Intent(CameraUtil.ACTION_NEW_VIDEO, uri));
-            mDataAdapter.addNewVideo(this, uri);
+            mDataAdapter.addNewVideo(uri);
         } else if (mimeType.startsWith("image/")) {
             CameraUtil.broadcastNewPicture(this, uri);
-            mDataAdapter.addNewPhoto(this, uri);
+            mDataAdapter.addNewPhoto(uri);
         } else if (mimeType.startsWith(PlaceholderManager.PLACEHOLDER_MIME_TYPE)) {
-            mDataAdapter.addNewPhoto(this, uri);
+            mDataAdapter.addNewPhoto(uri);
         } else {
             android.util.Log.w(TAG, "Unknown new media with MIME type:"
                     + mimeType + ", uri:" + uri);
@@ -840,7 +840,7 @@ public class CameraActivity extends Activity
     }
 
     private void removeData(int dataID) {
-        mDataAdapter.removeData(CameraActivity.this, dataID);
+        mDataAdapter.removeData(dataID);
         if (mDataAdapter.getTotalNumber() > 1) {
             showUndoDeletionBar();
         } else {
@@ -948,7 +948,7 @@ public class CameraActivity extends Activity
         mPanoramaViewHelper = new PanoramaViewHelper(this);
         mPanoramaViewHelper.onCreate();
         // Set up the camera preview first so the preview shows up ASAP.
-        mDataAdapter = new CameraDataAdapter(
+        mDataAdapter = new CameraDataAdapter(getApplicationContext(),
                 new ColorDrawable(getResources().getColor(R.color.photo_placeholder)));
         mDataAdapter.setLocalDataListener(mLocalDataListener);
 
@@ -1000,7 +1000,7 @@ public class CameraActivity extends Activity
         if (!mSecureCamera) {
             mFilmstripController.setDataAdapter(mDataAdapter);
             if (!isCaptureIntent()) {
-                mDataAdapter.requestLoad(this);
+                mDataAdapter.requestLoad();
             }
         } else {
             // Put a lock placeholder as the last image by setting its date to
@@ -1015,6 +1015,7 @@ public class CameraActivity extends Activity
                 }
             });
             mDataAdapter = new FixedLastDataAdapter(
+                    getApplicationContext(),
                     mDataAdapter,
                     new SimpleViewData(
                             v,
@@ -1141,7 +1142,7 @@ public class CameraActivity extends Activity
                 // If it's secure camera, requestLoad() should not be called
                 // as it will load all the data.
                 if (!mFilmstripVisible) {
-                    mDataAdapter.requestLoad(this);
+                    mDataAdapter.requestLoad();
                 }
             }
         }
@@ -1462,7 +1463,7 @@ public class CameraActivity extends Activity
             return;
         }
         hideUndoDeletionBar(false);
-        mDataAdapter.executeDeletion(CameraActivity.this);
+        mDataAdapter.executeDeletion();
     }
 
     public void showUndoDeletionBar() {
@@ -1669,7 +1670,7 @@ public class CameraActivity extends Activity
 
         updateBottomControlsByData(currentData);
         if (!mDataAdapter.isMetadataUpdated(dataId)) {
-            mDataAdapter.updateMetadata(this, dataId);
+            mDataAdapter.updateMetadata(dataId);
         }
     }
 
index c2e1666..4f47462 100644 (file)
@@ -16,7 +16,6 @@
 
 package com.android.camera.data;
 
-import android.content.ContentResolver;
 import android.content.Context;
 import android.net.Uri;
 
@@ -31,6 +30,7 @@ import android.net.Uri;
  */
 public abstract class AbstractLocalDataAdapterWrapper implements LocalDataAdapter {
 
+    protected final Context mContext;
     protected final LocalDataAdapter mAdapter;
     protected int mSuggestedWidth;
     protected int mSuggestedHeight;
@@ -38,12 +38,14 @@ public abstract class AbstractLocalDataAdapterWrapper implements LocalDataAdapte
     /**
      * Constructor.
      *
-     * @param wrappedAdapter  The {@link LocalDataAdapter} to be wrapped.
+     * @param context A valid Android context.
+     * @param wrappedAdapter The {@link LocalDataAdapter} to be wrapped.
      */
-    AbstractLocalDataAdapterWrapper(LocalDataAdapter wrappedAdapter) {
+    AbstractLocalDataAdapterWrapper(Context context, LocalDataAdapter wrappedAdapter) {
         if (wrappedAdapter == null) {
             throw new AssertionError("data adapter is null");
         }
+        mContext = context;
         mAdapter = wrappedAdapter;
     }
 
@@ -65,18 +67,18 @@ public abstract class AbstractLocalDataAdapterWrapper implements LocalDataAdapte
     }
 
     @Override
-    public void requestLoad(Context context) {
-        mAdapter.requestLoad(context);
+    public void requestLoad() {
+        mAdapter.requestLoad();
     }
 
     @Override
-    public void addNewVideo(Context context, Uri uri) {
-        mAdapter.addNewVideo(context, uri);
+    public void addNewVideo(Uri uri) {
+        mAdapter.addNewVideo(uri);
     }
 
     @Override
-    public void addNewPhoto(Context context, Uri uri) {
-        mAdapter.addNewPhoto(context, uri);
+    public void addNewPhoto(Uri uri) {
+        mAdapter.addNewPhoto(uri);
     }
 
     @Override
@@ -90,8 +92,8 @@ public abstract class AbstractLocalDataAdapterWrapper implements LocalDataAdapte
     }
 
     @Override
-    public boolean executeDeletion(Context context) {
-        return mAdapter.executeDeletion(context);
+    public boolean executeDeletion() {
+        return mAdapter.executeDeletion();
     }
 
     @Override
@@ -100,13 +102,13 @@ public abstract class AbstractLocalDataAdapterWrapper implements LocalDataAdapte
     }
 
     @Override
-    public void refresh(Context context, Uri uri, boolean isInProgressSession) {
-        mAdapter.refresh(context, uri, isInProgressSession);
+    public void refresh(Uri uri, boolean isInProgressSession) {
+        mAdapter.refresh(uri, isInProgressSession);
     }
 
     @Override
-    public void updateMetadata(Context context, int dataId) {
-        mAdapter.updateMetadata(context, dataId);
+    public void updateMetadata(int dataId) {
+        mAdapter.updateMetadata(dataId);
     }
 
     @Override
index f573412..c0cb83f 100644 (file)
@@ -22,7 +22,6 @@ import android.database.Cursor;
 import android.graphics.drawable.Drawable;
 import android.net.Uri;
 import android.os.AsyncTask;
-import android.os.Bundle;
 import android.provider.MediaStore;
 import android.util.Log;
 import android.view.View;
@@ -44,6 +43,8 @@ public class CameraDataAdapter implements LocalDataAdapter {
     private static final int DEFAULT_DECODE_SIZE = 1600;
     private static final String[] CAMERA_PATH = { Storage.DIRECTORY + "%" };
 
+    private final Context mContext;
+
     private LocalDataList mImages;
 
     private Listener mListener;
@@ -55,7 +56,8 @@ public class CameraDataAdapter implements LocalDataAdapter {
 
     private LocalData mLocalDataToDelete;
 
-    public CameraDataAdapter(Drawable placeHolder) {
+    public CameraDataAdapter(Context context, Drawable placeHolder) {
+        mContext = context;
         mImages = new LocalDataList();
         mPlaceHolder = placeHolder;
     }
@@ -66,15 +68,15 @@ public class CameraDataAdapter implements LocalDataAdapter {
     }
 
     @Override
-    public void requestLoad(Context context) {
-        QueryTask qtask = new QueryTask(context);
-        qtask.execute(context.getContentResolver());
+    public void requestLoad() {
+        QueryTask qtask = new QueryTask();
+        qtask.execute(mContext.getContentResolver());
     }
 
 
     @Override
-    public void updateMetadata(Context context, int dataId) {
-        new MetadataUpdateTask(context).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, dataId);
+    public void updateMetadata(int dataId) {
+        new MetadataUpdateTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, dataId);
     }
 
     @Override
@@ -142,29 +144,29 @@ public class CameraDataAdapter implements LocalDataAdapter {
     }
 
     @Override
-    public void removeData(Context c, int dataID) {
+    public void removeData(int dataID) {
         if (dataID >= mImages.size()) {
             return;
         }
         LocalData d = mImages.remove(dataID);
         // Delete previously removed data first.
-        executeDeletion(c);
+        executeDeletion();
         mLocalDataToDelete = d;
         mListener.onDataRemoved(dataID, d);
     }
 
     // TODO: put the database query on background thread
     @Override
-    public void addNewVideo(Context context, Uri uri) {
-        Cursor c = context.getContentResolver().query(uri,
+    public void addNewVideo(Uri uri) {
+        Cursor cursor = mContext.getContentResolver().query(uri,
                 LocalMediaData.VideoData.QUERY_PROJECTION,
                 MediaStore.Images.Media.DATA + " like ? ", CAMERA_PATH,
                 LocalMediaData.VideoData.QUERY_ORDER);
-        if (c == null || !c.moveToFirst()) {
+        if (cursor == null || !cursor.moveToFirst()) {
             return;
         }
         int pos = findDataByContentUri(uri);
-        LocalMediaData.VideoData newData = LocalMediaData.VideoData.buildFromCursor(c);
+        LocalMediaData.VideoData newData = LocalMediaData.VideoData.buildFromCursor(cursor);
         if (pos != -1) {
             // A duplicate one, just do a substitute.
             updateData(pos, newData);
@@ -176,22 +178,22 @@ public class CameraDataAdapter implements LocalDataAdapter {
 
     // TODO: put the database query on background thread
     @Override
-    public void addNewPhoto(Context context, Uri uri) {
-        Cursor c = context.getContentResolver().query(uri,
+    public void addNewPhoto(Uri uri) {
+        Cursor cursor = mContext.getContentResolver().query(uri,
                 LocalMediaData.PhotoData.QUERY_PROJECTION,
                 MediaStore.Images.Media.DATA + " like ? ", CAMERA_PATH,
                 LocalMediaData.PhotoData.QUERY_ORDER);
         LocalMediaData.PhotoData newData = null;
 
         try {
-            if (c == null || !c.moveToFirst()) {
+            if (cursor == null || !cursor.moveToFirst()) {
                 return;
             }
-            newData = LocalMediaData.PhotoData.buildFromCursor(context, c);
+            newData = LocalMediaData.PhotoData.buildFromCursor(mContext, cursor);
         } finally {
             // Ensure cursor is closed before returning
-            if (c != null) {
-                c.close();
+            if (cursor != null) {
+                cursor.close();
             }
         }
         int pos = findDataByContentUri(uri);
@@ -223,10 +225,10 @@ public class CameraDataAdapter implements LocalDataAdapter {
     }
 
     @Override
-    public boolean executeDeletion(Context c) {
+    public boolean executeDeletion() {
         if (mLocalDataToDelete == null) return false;
 
-        DeletionTask task = new DeletionTask(c);
+        DeletionTask task = new DeletionTask();
         task.execute(mLocalDataToDelete);
         mLocalDataToDelete = null;
         return true;
@@ -238,15 +240,14 @@ public class CameraDataAdapter implements LocalDataAdapter {
     }
 
     @Override
-    public void refresh(Context context, Uri contentUri,
-            boolean isInProgressSession) {
+    public void refresh(Uri contentUri, boolean isInProgressSession) {
         int pos = findDataByContentUri(contentUri);
         if (pos == -1) {
             return;
         }
 
         LocalData data = mImages.get(pos);
-        LocalData refreshedData = data.refresh(context);
+        LocalData refreshedData = data.refresh(mContext);
 
         // Wrap the data item if this represents a session that is in progress.
         if (isInProgressSession) {
@@ -303,13 +304,6 @@ public class CameraDataAdapter implements LocalDataAdapter {
     }
 
     private class QueryTask extends AsyncTask<ContentResolver, Void, LocalDataList> {
-        private Context mContext;
-
-        public QueryTask(Context context) {
-            super();
-            mContext = context;
-        }
-
         /**
          * Loads all the photo and video data in the camera folder in background
          * and combine them into one single list.
@@ -391,12 +385,6 @@ public class CameraDataAdapter implements LocalDataAdapter {
     }
 
     private class DeletionTask extends AsyncTask<LocalData, Void, Void> {
-        Context mContext;
-
-        DeletionTask(Context context) {
-            mContext = context;
-        }
-
         @Override
         protected Void doInBackground(LocalData... data) {
             for (int i = 0; i < data.length; i++) {
@@ -411,12 +399,6 @@ public class CameraDataAdapter implements LocalDataAdapter {
     }
 
     private class MetadataUpdateTask extends AsyncTask<Integer, Void, List<Integer> > {
-        Context mContext;
-
-        MetadataUpdateTask(Context context) {
-            mContext = context;
-        }
-
         @Override
         protected List<Integer> doInBackground(Integer... dataId) {
             List<Integer> updatedList = new ArrayList<Integer>();
index bb743b6..0ee6331 100644 (file)
@@ -32,7 +32,7 @@ public class FixedFirstDataAdapter extends AbstractLocalDataAdapterWrapper
         implements DataAdapter.Listener {
 
     @SuppressWarnings("unused")
-    private static final String TAG = "CAM_FixedFirstDataAdapter";
+    private static final String TAG = "FixedFirstDataAdapter";
 
     private LocalData mFirstData;
     private Listener mListener;
@@ -40,14 +40,16 @@ public class FixedFirstDataAdapter extends AbstractLocalDataAdapterWrapper
     /**
      * Constructor.
      *
+     * @param context Valid Android context.
      * @param wrappedAdapter The {@link LocalDataAdapter} to be wrapped.
-     * @param firstData      The {@link LocalData} to be placed at the first
-     *                       position.
+     * @param firstData The {@link LocalData} to be placed at the first
+     *            position.
      */
     public FixedFirstDataAdapter(
+            Context context,
             LocalDataAdapter wrappedAdapter,
             LocalData firstData) {
-        super(wrappedAdapter);
+        super(context, wrappedAdapter);
         if (firstData == null) {
             throw new AssertionError("data is null");
         }
@@ -63,9 +65,9 @@ public class FixedFirstDataAdapter extends AbstractLocalDataAdapterWrapper
     }
 
     @Override
-    public void removeData(Context context, int dataID) {
+    public void removeData(int dataID) {
         if (dataID > 0) {
-            mAdapter.removeData(context, dataID - 1);
+            mAdapter.removeData(dataID - 1);
         }
     }
 
@@ -185,11 +187,11 @@ public class FixedFirstDataAdapter extends AbstractLocalDataAdapterWrapper
     }
 
     @Override
-    public void updateMetadata(Context context, int dataId) {
+    public void updateMetadata(int dataId) {
         if (dataId > 0) {
-            mAdapter.updateMetadata(context, dataId);
+            mAdapter.updateMetadata(dataId);
         } else {
-            MetadataLoader.loadMetadata(context, mFirstData);
+            MetadataLoader.loadMetadata(mContext, mFirstData);
         }
     }
 
index 49c5279..6cda7d4 100644 (file)
@@ -35,13 +35,15 @@ public class FixedLastDataAdapter extends AbstractLocalDataAdapterWrapper {
     /**
      * Constructor.
      *
-     * @param wrappedAdapter  The {@link LocalDataAdapter} to be wrapped.
-     * @param lastData       The {@link LocalData} to be placed at the last position.
+     * @param context A valid Android context.
+     * @param wrappedAdapter The {@link LocalDataAdapter} to be wrapped.
+     * @param lastData The {@link LocalData} to be placed at the last position.
      */
     public FixedLastDataAdapter(
+            Context context,
             LocalDataAdapter wrappedAdapter,
             LocalData lastData) {
-        super(wrappedAdapter);
+        super(context, wrappedAdapter);
         if (lastData == null) {
             throw new AssertionError("data is null");
         }
@@ -68,9 +70,9 @@ public class FixedLastDataAdapter extends AbstractLocalDataAdapterWrapper {
     }
 
     @Override
-    public void removeData(Context context, int dataID) {
+    public void removeData(int dataID) {
         if (dataID < mAdapter.getTotalNumber()) {
-            mAdapter.removeData(context, dataID);
+            mAdapter.removeData(dataID);
         }
     }
 
@@ -147,11 +149,11 @@ public class FixedLastDataAdapter extends AbstractLocalDataAdapterWrapper {
     }
 
     @Override
-    public void updateMetadata(Context context, int dataId) {
+    public void updateMetadata(int dataId) {
         if (dataId < mAdapter.getTotalNumber()) {
-            mAdapter.updateMetadata(context, dataId);
+            mAdapter.updateMetadata(dataId);
         } else {
-            MetadataLoader.loadMetadata(context, mLastData);
+            MetadataLoader.loadMetadata(mContext, mLastData);
         }
     }
 
index e4c0e6f..8d63650 100644 (file)
@@ -16,7 +16,6 @@
 
 package com.android.camera.data;
 
-import android.content.Context;
 import android.net.Uri;
 
 import com.android.camera.filmstrip.DataAdapter;
@@ -24,29 +23,26 @@ import com.android.camera.filmstrip.DataAdapter;
 import java.util.List;
 
 /**
- * An interface which extends {@link com.android.camera.filmstrip.DataAdapter} and defines operations on
- * the data in the local camera folder.
+ * An interface which extends {@link com.android.camera.filmstrip.DataAdapter}
+ * and defines operations on the data in the local camera folder.
  */
 public interface LocalDataAdapter extends DataAdapter {
 
     public interface LocalDataListener {
         /**
-         * Metadata of a {@link com.android.camera.data.LocalData} is loaded
-         * on demand. Once the metadata is loaded this listener is notified.
+         * Metadata of a {@link com.android.camera.data.LocalData} is loaded on
+         * demand. Once the metadata is loaded this listener is notified.
          *
          * @param updatedData The IDs of the data whose metadata has been
-         *                    updated.
+         *            updated.
          */
         public void onMetadataUpdated(List<Integer> updatedData);
     }
 
     /**
      * Request for loading the local data.
-     *
-     * @param context The Android {@link android.content.Context} this data
-     *                adapter is used in.
      */
-    public void requestLoad(Context context);
+    public void requestLoad();
 
     /**
      * Returns the specified {@link LocalData}.
@@ -59,44 +55,38 @@ public interface LocalDataAdapter extends DataAdapter {
     /**
      * Remove the data in the local camera folder.
      *
-     * @param context       {@link Context} used to remove the data.
-     * @param dataID  ID of data to be deleted.
+     * @param dataID ID of data to be deleted.
      */
-    public void removeData(Context context, int dataID);
+    public void removeData(int dataID);
 
     /**
      * Add new local video data.
      *
-     * @param context The Android {@link android.content.Context} this data
-     *                adapter is used in.
-     * @param uri      {@link Uri} of the video.
+     * @param uri {@link Uri} of the video.
      */
-    public void addNewVideo(Context context, Uri uri);
+    public void addNewVideo(Uri uri);
 
     /**
      * Adds new local photo data.
      *
-     * @param context The Android {@link android.content.Context} this data
-     *                adapter is used in.
-     * @param uri       {@link Uri} of the photo.
+     * @param uri {@link Uri} of the photo.
      */
-    public void addNewPhoto(Context context, Uri uri);
+    public void addNewPhoto(Uri uri);
 
     /**
      * Refresh the data by {@link Uri}.
      *
-     * @param context The Android {@link android.content.Context}.
      * @param uri The {@link Uri} of the data to refresh.
      * @param isInProgressSession Whether this data item has a session in
      *            progress associated with it.
      */
-    public void refresh(Context context, Uri uri, boolean isInProgressSession);
+    public void refresh(Uri uri, boolean isInProgressSession);
 
     /**
      * Finds the {@link LocalData} of the specified content Uri.
      *
-     * @param uri  The content Uri of the {@link LocalData}.
-     * @return     The index of the data. {@code -1} if not found.
+     * @param uri The content Uri of the {@link LocalData}.
+     * @return The index of the data. {@code -1} if not found.
      */
     public int findDataByContentUri(Uri uri);
 
@@ -106,19 +96,18 @@ public interface LocalDataAdapter extends DataAdapter {
     public void flush();
 
     /**
-     * Executes the deletion task. Delete the data waiting in the deletion queue.
+     * Executes the deletion task. Delete the data waiting in the deletion
+     * queue.
      *
-     * @param context The {@link Context} from the caller.
-     * @return        {@code true} if task has been executed, {@code false}
-     *                otherwise.
+     * @return Whether the task has been executed
      */
-    public boolean executeDeletion(Context context);
+    public boolean executeDeletion();
 
     /**
      * Undo a deletion. If there is any data waiting to be deleted in the queue,
      * move it out of the deletion queue.
      *
-     * @return {@code true} if there are items in the queue, {@code false} otherwise.
+     * @return Whether there are items in the queue.
      */
     public boolean undoDataRemoval();
 
@@ -141,11 +130,9 @@ public interface LocalDataAdapter extends DataAdapter {
      * will be notified through
      * {@link com.android.camera.data.LocalDataAdapter.LocalDataListener}.
      *
-     * @param context The Android {@link android.content.Context} to update
-     *                the metadata.
      * @param dataId The ID of the data to update the metadata for.
      */
-    public void updateMetadata(Context context, int dataId);
+    public void updateMetadata(int dataId);
 
     /**
      * @return whether the metadata is already updated.
index ced8860..52d6c6b 100644 (file)
@@ -515,7 +515,7 @@ public abstract class LocalMediaData implements LocalData {
             protected void onPostExecute(Bitmap bitmap) {
                 super.onPostExecute(bitmap);
                 if (mNeedsRefresh && mAdapter != null) {
-                    mAdapter.refresh(mContext, getContentUri(), mIsInProgressSession);
+                    mAdapter.refresh(getContentUri(), mIsInProgressSession);
                 }
             }
         }