WidgetService.java revision a2cb30b688131380cab763a1b4da25f15a9fa45a
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.gallery3d.gadget;
18
19import android.annotation.TargetApi;
20import android.appwidget.AppWidgetManager;
21import android.content.Intent;
22import android.graphics.Bitmap;
23import android.net.Uri;
24import android.widget.RemoteViews;
25import android.widget.RemoteViewsService;
26
27import com.android.gallery3d.R;
28import com.android.gallery3d.app.GalleryApp;
29import com.android.gallery3d.common.ApiHelper;
30import com.android.gallery3d.data.ContentListener;
31import com.android.gallery3d.data.DataManager;
32import com.android.gallery3d.data.MediaSet;
33import com.android.gallery3d.data.Path;
34
35@TargetApi(ApiHelper.VERSION_CODES.HONEYCOMB)
36public class WidgetService extends RemoteViewsService {
37
38    @SuppressWarnings("unused")
39    private static final String TAG = "GalleryAppWidgetService";
40
41    public static final String EXTRA_WIDGET_TYPE = "widget-type";
42    public static final String EXTRA_ALBUM_PATH = "album-path";
43
44    @Override
45    public RemoteViewsFactory onGetViewFactory(Intent intent) {
46        int id = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
47                AppWidgetManager.INVALID_APPWIDGET_ID);
48        int type = intent.getIntExtra(EXTRA_WIDGET_TYPE, 0);
49        String albumPath = intent.getStringExtra(EXTRA_ALBUM_PATH);
50
51        return new PhotoRVFactory((GalleryApp) getApplicationContext(),
52                id, type, albumPath);
53    }
54
55    private static class EmptySource implements WidgetSource {
56
57        @Override
58        public int size() {
59            return 0;
60        }
61
62        @Override
63        public Bitmap getImage(int index) {
64            throw new UnsupportedOperationException();
65        }
66
67        @Override
68        public Uri getContentUri(int index) {
69            throw new UnsupportedOperationException();
70        }
71
72        @Override
73        public void setContentListener(ContentListener listener) {}
74
75        @Override
76        public void reload() {}
77
78        @Override
79        public void close() {}
80    }
81
82    private static class PhotoRVFactory implements
83            RemoteViewsService.RemoteViewsFactory, ContentListener {
84
85        private final int mAppWidgetId;
86        private final int mType;
87        private final String mAlbumPath;
88        private final GalleryApp mApp;
89
90        private WidgetSource mSource;
91
92        public PhotoRVFactory(GalleryApp app, int id, int type, String albumPath) {
93            mApp = app;
94            mAppWidgetId = id;
95            mType = type;
96            mAlbumPath = albumPath;
97        }
98
99        @Override
100        public void onCreate() {
101            if (mType == WidgetDatabaseHelper.TYPE_ALBUM) {
102                Path path = Path.fromString(mAlbumPath);
103                DataManager manager = mApp.getDataManager();
104                MediaSet mediaSet = (MediaSet) manager.getMediaObject(path);
105                mSource = mediaSet == null
106                        ? new EmptySource()
107                        : new MediaSetSource(mediaSet);
108            } else {
109                mSource = new LocalPhotoSource(mApp.getAndroidContext());
110            }
111            mSource.setContentListener(this);
112            AppWidgetManager.getInstance(mApp.getAndroidContext())
113                    .notifyAppWidgetViewDataChanged(
114                    mAppWidgetId, R.id.appwidget_stack_view);
115        }
116
117        @Override
118        public void onDestroy() {
119            mSource.close();
120            mSource = null;
121        }
122
123        public int getCount() {
124            return mSource.size();
125        }
126
127        public long getItemId(int position) {
128            return position;
129        }
130
131        public int getViewTypeCount() {
132            return 1;
133        }
134
135        public boolean hasStableIds() {
136            return true;
137        }
138
139        public RemoteViews getLoadingView() {
140            RemoteViews rv = new RemoteViews(
141                    mApp.getAndroidContext().getPackageName(),
142                    R.layout.appwidget_loading_item);
143            rv.setProgressBar(R.id.appwidget_loading_item, 0, 0, true);
144            return rv;
145        }
146
147        public RemoteViews getViewAt(int position) {
148            Bitmap bitmap = mSource.getImage(position);
149            if (bitmap == null) return getLoadingView();
150            RemoteViews views = new RemoteViews(
151                    mApp.getAndroidContext().getPackageName(),
152                    R.layout.appwidget_photo_item);
153            views.setImageViewBitmap(R.id.appwidget_photo_item, bitmap);
154            views.setOnClickFillInIntent(R.id.appwidget_photo_item, new Intent()
155                    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
156                    .setData(mSource.getContentUri(position)));
157            return views;
158        }
159
160        @Override
161        public void onDataSetChanged() {
162            mSource.reload();
163        }
164
165        @Override
166        public void onContentDirty() {
167            AppWidgetManager.getInstance(mApp.getAndroidContext())
168                    .notifyAppWidgetViewDataChanged(
169                    mAppWidgetId, R.id.appwidget_stack_view);
170        }
171    }
172}
173