summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorChao Zhang <chaoz@codeaurora.org>2016-04-11 19:32:14 +0800
committerGerrit - the friendly Code Review server <code-review@localhost>2016-04-14 22:47:22 -0700
commit2a5e936116cd6adcc4a461091e101b3704d4e6c0 (patch)
tree642dc37c2ea377af3057fc034e8e08f2cb267fc1 /src
parent0e760ee5222ade8f3d0aaa4321d707fb76b90552 (diff)
downloadandroid_packages_apps_Gallery2-2a5e936116cd6adcc4a461091e101b3704d4e6c0.tar.gz
android_packages_apps_Gallery2-2a5e936116cd6adcc4a461091e101b3704d4e6c0.tar.bz2
android_packages_apps_Gallery2-2a5e936116cd6adcc4a461091e101b3704d4e6c0.zip
Gallery2: fix photo widget has not sync after delete the photo.
Use a ContentObserver to observe uri's change. If uri is deleted, show a empty view in widght. Change-Id: I11f0632ed96958a74b6aa2cdd9c2f1d88c99e94e CRs-Fixed: 981663
Diffstat (limited to 'src')
-rw-r--r--src/com/android/gallery3d/gadget/PhotoAppWidgetProvider.java67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/com/android/gallery3d/gadget/PhotoAppWidgetProvider.java b/src/com/android/gallery3d/gadget/PhotoAppWidgetProvider.java
index 58466bf01..91e5cbeb6 100644
--- a/src/com/android/gallery3d/gadget/PhotoAppWidgetProvider.java
+++ b/src/com/android/gallery3d/gadget/PhotoAppWidgetProvider.java
@@ -22,10 +22,14 @@ import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
+import android.database.ContentObserver;
+import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
+import android.os.Handler;
import android.util.Log;
+import android.view.View;
import android.widget.RemoteViews;
import com.android.gallery3d.R;
@@ -33,9 +37,14 @@ import com.android.gallery3d.common.ApiHelper;
import com.android.gallery3d.gadget.WidgetDatabaseHelper.Entry;
import com.android.gallery3d.onetimeinitializer.GalleryWidgetMigrator;
+import java.util.ArrayList;
+import java.util.List;
+
public class PhotoAppWidgetProvider extends AppWidgetProvider {
private static final String TAG = "WidgetProvider";
+ private static List<PhotoUriContentObserver> mPhotoUriObservers = new ArrayList<>();
+ private static Handler mContentObserverHandler = new Handler();
static RemoteViews buildWidget(Context context, int id, Entry entry) {
@@ -44,6 +53,12 @@ public class PhotoAppWidgetProvider extends AppWidgetProvider {
case WidgetDatabaseHelper.TYPE_SHUFFLE:
return buildStackWidget(context, id, entry);
case WidgetDatabaseHelper.TYPE_SINGLE_PHOTO:
+ PhotoUriContentObserver photoUriObserver =
+ new PhotoUriContentObserver(context, mContentObserverHandler, entry, id);
+ photoUriObserver.setTag(id);
+ mPhotoUriObservers.add(photoUriObserver);
+ context.getContentResolver().registerContentObserver(Uri.parse(entry.imageUri),
+ false, photoUriObserver);
return buildFrameWidget(context, id, entry);
}
throw new RuntimeException("invalid type - " + entry.type);
@@ -132,8 +147,60 @@ public class PhotoAppWidgetProvider extends AppWidgetProvider {
// Clean deleted photos out of our database
WidgetDatabaseHelper helper = new WidgetDatabaseHelper(context);
for (int appWidgetId : appWidgetIds) {
+ PhotoUriContentObserver contentObserver = getContentObserver(appWidgetId);
+ if (contentObserver != null) {
+ context.getContentResolver().unregisterContentObserver(contentObserver);
+ mPhotoUriObservers.remove(contentObserver);
+ }
helper.deleteEntry(appWidgetId);
}
helper.close();
}
+
+ private PhotoUriContentObserver getContentObserver(int appWidgetId) {
+ for (PhotoUriContentObserver contentObserver : mPhotoUriObservers) {
+ if (appWidgetId == contentObserver.getTag()) {
+ return contentObserver;
+ }
+ }
+ return null;
+ }
+
+ private static class PhotoUriContentObserver extends ContentObserver {
+ private int mId;
+ private int mTag;
+ private Context mContext;
+ private Entry mEntry;
+
+ public void setTag(int tag) {
+ this.mTag = tag;
+ }
+
+ public int getTag() {
+ return mTag;
+ }
+
+ public PhotoUriContentObserver(Context context, Handler handler, Entry entry, int id) {
+ super(handler);
+ mContext = context;
+ mEntry = entry;
+ mId = id;
+ }
+
+ public void onChange(boolean selfChange) {
+ super.onChange(selfChange);
+ Uri uri = Uri.parse(mEntry.imageUri);
+ Cursor cursor = mContext.getContentResolver().query(uri, null, null,
+ null, "_id ASC LIMIT 1");
+ if (cursor != null) {
+ if (cursor.getCount() == 0) {
+ RemoteViews views = buildFrameWidget(mContext, mId, mEntry);
+ views.setViewVisibility(R.id.appwidget_empty_photo, View.VISIBLE);
+ views.setViewVisibility(R.id.photo, View.GONE);
+ AppWidgetManager.getInstance(mContext).updateAppWidget(mId, views);
+ }
+ cursor.close();
+ }
+ }
+ }
}