2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.gadget;
19 import android.appwidget.AppWidgetManager;
20 import android.content.Intent;
21 import android.graphics.Bitmap;
22 import android.net.Uri;
23 import android.widget.RemoteViews;
24 import android.widget.RemoteViewsService;
26 import com.android.gallery3d.R;
27 import com.android.gallery3d.app.GalleryApp;
28 import com.android.gallery3d.data.ContentListener;
29 import com.android.gallery3d.data.DataManager;
30 import com.android.gallery3d.data.MediaSet;
31 import com.android.gallery3d.data.Path;
33 public class WidgetService extends RemoteViewsService {
35 @SuppressWarnings("unused")
36 private static final String TAG = "GalleryAppWidgetService";
38 public static final String EXTRA_WIDGET_TYPE = "widget-type";
39 public static final String EXTRA_ALBUM_PATH = "album-path";
42 public RemoteViewsFactory onGetViewFactory(Intent intent) {
43 int id = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
44 AppWidgetManager.INVALID_APPWIDGET_ID);
45 int type = intent.getIntExtra(EXTRA_WIDGET_TYPE, 0);
46 String albumPath = intent.getStringExtra(EXTRA_ALBUM_PATH);
48 return new PhotoRVFactory((GalleryApp) getApplicationContext(),
52 private static class EmptySource implements WidgetSource {
60 public Bitmap getImage(int index) {
61 throw new UnsupportedOperationException();
65 public Uri getContentUri(int index) {
66 throw new UnsupportedOperationException();
70 public void setContentListener(ContentListener listener) {}
73 public void reload() {}
76 public void close() {}
79 private static class PhotoRVFactory implements
80 RemoteViewsService.RemoteViewsFactory, ContentListener {
82 private final int mAppWidgetId;
83 private final int mType;
84 private final String mAlbumPath;
85 private final GalleryApp mApp;
87 private WidgetSource mSource;
89 public PhotoRVFactory(GalleryApp app, int id, int type, String albumPath) {
93 mAlbumPath = albumPath;
97 public void onCreate() {
98 if (mType == WidgetDatabaseHelper.TYPE_ALBUM) {
99 Path path = Path.fromString(mAlbumPath);
100 DataManager manager = mApp.getDataManager();
101 MediaSet mediaSet = (MediaSet) manager.getMediaObject(path);
102 mSource = mediaSet == null
104 : new MediaSetSource(mediaSet);
106 mSource = new LocalPhotoSource(mApp.getAndroidContext());
108 mSource.setContentListener(this);
109 AppWidgetManager.getInstance(mApp.getAndroidContext())
110 .notifyAppWidgetViewDataChanged(
111 mAppWidgetId, R.id.appwidget_stack_view);
115 public void onDestroy() {
120 public int getCount() {
121 return mSource.size();
124 public long getItemId(int position) {
128 public int getViewTypeCount() {
132 public boolean hasStableIds() {
136 public RemoteViews getLoadingView() {
137 RemoteViews rv = new RemoteViews(
138 mApp.getAndroidContext().getPackageName(),
139 R.layout.appwidget_loading_item);
140 rv.setProgressBar(R.id.appwidget_loading_item, 0, 0, true);
144 public RemoteViews getViewAt(int position) {
145 Bitmap bitmap = mSource.getImage(position);
146 if (bitmap == null) return getLoadingView();
147 RemoteViews views = new RemoteViews(
148 mApp.getAndroidContext().getPackageName(),
149 R.layout.appwidget_photo_item);
150 views.setImageViewBitmap(R.id.appwidget_photo_item, bitmap);
151 views.setOnClickFillInIntent(R.id.appwidget_photo_item, new Intent()
152 .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
153 .setData(mSource.getContentUri(position)));
158 public void onDataSetChanged() {
163 public void onContentDirty() {
164 AppWidgetManager.getInstance(mApp.getAndroidContext())
165 .notifyAppWidgetViewDataChanged(
166 mAppWidgetId, R.id.appwidget_stack_view);