diff options
author | Chao Zhang <chaoz@codeaurora.org> | 2016-04-11 19:32:14 +0800 |
---|---|---|
committer | Gerrit - the friendly Code Review server <code-review@localhost> | 2016-04-19 19:50:13 -0700 |
commit | 21b854909858b17464153e7b1ba51d849659edd5 (patch) | |
tree | 063c6defb80721eb8843a56bed35ab865a8d9aa3 | |
parent | 33aceac8417557c7f63d2cab1f27a81fefbb9c8b (diff) | |
download | android_packages_apps_Gallery2-21b854909858b17464153e7b1ba51d849659edd5.tar.gz android_packages_apps_Gallery2-21b854909858b17464153e7b1ba51d849659edd5.tar.bz2 android_packages_apps_Gallery2-21b854909858b17464153e7b1ba51d849659edd5.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
-rwxr-xr-x | res/layout/photo_frame.xml | 22 | ||||
-rw-r--r-- | src/com/android/gallery3d/gadget/PhotoAppWidgetProvider.java | 67 |
2 files changed, 89 insertions, 0 deletions
diff --git a/res/layout/photo_frame.xml b/res/layout/photo_frame.xml index 63faf539d..ede942f6b 100755 --- a/res/layout/photo_frame.xml +++ b/res/layout/photo_frame.xml @@ -21,6 +21,28 @@ android:paddingBottom="23dp" android:paddingStart="12dp" android:paddingEnd="12dp"> + + <RelativeLayout + android:id="@+id/appwidget_empty_photo" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:visibility="gone"> + + <FrameLayout + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_centerInParent="true" + android:background="@drawable/appwidget_photo_border"> + + <TextView + android:id="@id/appwidget_photo_item" + android:layout_width="@dimen/stack_photo_width" + android:layout_height="@dimen/stack_photo_height" + android:gravity="center" + android:text="@string/appwidget_empty_text" + android:textColor="@android:color/black" /> + </FrameLayout> + </RelativeLayout> <ImageView android:id="@+id/photo" android:layout_gravity="center" android:layout_width="wrap_content" 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(); + } + } + } } |