Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit e16711d4 authored by Chao Zhang's avatar Chao Zhang Committed by Michael Bestas
Browse files

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
parent 33475780
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -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"
+67 −0
Original line number Diff line number Diff line
@@ -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();
            }
        }
    }
}