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 17 package com.android.gallery3d.gadget; 18 19 import com.android.gallery3d.R; 20 import com.android.gallery3d.app.GalleryApp; 21 import com.android.gallery3d.data.ContentListener; 22 import com.android.gallery3d.data.DataManager; 23 import com.android.gallery3d.data.MediaSet; 24 import com.android.gallery3d.data.Path; 25 26 import android.appwidget.AppWidgetManager; 27 import android.content.Intent; 28 import android.graphics.Bitmap; 29 import android.net.Uri; 30 import android.widget.RemoteViews; 31 import android.widget.RemoteViewsService; 32 33 public class WidgetService extends RemoteViewsService { 34 35 @SuppressWarnings("unused") 36 private static final String TAG = "GalleryAppWidgetService"; 37 38 public static final String EXTRA_WIDGET_TYPE = "widget-type"; 39 public static final String EXTRA_ALBUM_PATH = "album-path"; 40 41 @Override 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); 47 48 return new PhotoRVFactory((GalleryApp) getApplicationContext(), 49 id, type, albumPath); 50 } 51 52 private static class EmptySource implements WidgetSource { 53 54 @Override 55 public int size() { 56 return 0; 57 } 58 59 @Override 60 public Bitmap getImage(int index) { 61 throw new UnsupportedOperationException(); 62 } 63 64 @Override 65 public Uri getContentUri(int index) { 66 throw new UnsupportedOperationException(); 67 } 68 69 @Override 70 public void setContentListener(ContentListener listener) {} 71 72 @Override 73 public void reload() {} 74 75 @Override 76 public void close() {} 77 } 78 79 private static class PhotoRVFactory implements 80 RemoteViewsService.RemoteViewsFactory, ContentListener { 81 82 private final int mAppWidgetId; 83 private final int mType; 84 private final String mAlbumPath; 85 private final GalleryApp mApp; 86 87 private WidgetSource mSource; 88 89 public PhotoRVFactory(GalleryApp app, int id, int type, String albumPath) { 90 mApp = app; 91 mAppWidgetId = id; 92 mType = type; 93 mAlbumPath = albumPath; 94 } 95 96 @Override 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 103 ? new EmptySource() 104 : new MediaSetSource(mediaSet); 105 } else { 106 mSource = new LocalPhotoSource(mApp.getAndroidContext()); 107 } 108 mSource.setContentListener(this); 109 AppWidgetManager.getInstance(mApp.getAndroidContext()) 110 .notifyAppWidgetViewDataChanged( 111 mAppWidgetId, R.id.appwidget_stack_view); 112 } 113 114 @Override 115 public void onDestroy() { 116 mSource.close(); 117 mSource = null; 118 } 119 120 public int getCount() { 121 return mSource.size(); 122 } 123 124 public long getItemId(int position) { 125 return position; 126 } 127 128 public int getViewTypeCount() { 129 return 1; 130 } 131 132 public boolean hasStableIds() { 133 return true; 134 } 135 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); 141 return rv; 142 } 143 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))); 154 return views; 155 } 156 157 @Override 158 public void onDataSetChanged() { 159 mSource.reload(); 160 } 161 162 @Override 163 public void onContentDirty() { 164 AppWidgetManager.getInstance(mApp.getAndroidContext()) 165 .notifyAppWidgetViewDataChanged( 166 mAppWidgetId, R.id.appwidget_stack_view); 167 } 168 } 169 } 170