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 android.annotation.TargetApi; 20 import android.appwidget.AppWidgetManager; 21 import android.content.Intent; 22 import android.graphics.Bitmap; 23 import android.net.Uri; 24 import android.widget.RemoteViews; 25 import android.widget.RemoteViewsService; 26 27 import com.android.gallery3d.R; 28 import com.android.gallery3d.app.GalleryApp; 29 import com.android.gallery3d.common.ApiHelper; 30 import com.android.gallery3d.data.ContentListener; 31 import com.android.gallery3d.data.DataManager; 32 import com.android.gallery3d.data.MediaSet; 33 import com.android.gallery3d.data.Path; 34 35 @TargetApi(ApiHelper.VERSION_CODES.HONEYCOMB) 36 public 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 @Override 124 public int getCount() { 125 return mSource.size(); 126 } 127 128 @Override 129 public long getItemId(int position) { 130 return position; 131 } 132 133 @Override 134 public int getViewTypeCount() { 135 return 1; 136 } 137 138 @Override 139 public boolean hasStableIds() { 140 return true; 141 } 142 143 @Override 144 public RemoteViews getLoadingView() { 145 RemoteViews rv = new RemoteViews( 146 mApp.getAndroidContext().getPackageName(), 147 R.layout.appwidget_loading_item); 148 rv.setProgressBar(R.id.appwidget_loading_item, 0, 0, true); 149 return rv; 150 } 151 152 @Override 153 public RemoteViews getViewAt(int position) { 154 Bitmap bitmap = mSource.getImage(position); 155 if (bitmap == null) return getLoadingView(); 156 RemoteViews views = new RemoteViews( 157 mApp.getAndroidContext().getPackageName(), 158 R.layout.appwidget_photo_item); 159 views.setImageViewBitmap(R.id.appwidget_photo_item, bitmap); 160 views.setOnClickFillInIntent(R.id.appwidget_photo_item, new Intent() 161 .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) 162 .setData(mSource.getContentUri(position))); 163 return views; 164 } 165 166 @Override 167 public void onDataSetChanged() { 168 mSource.reload(); 169 } 170 171 @Override 172 public void onContentDirty() { 173 AppWidgetManager.getInstance(mApp.getAndroidContext()) 174 .notifyAppWidgetViewDataChanged( 175 mAppWidgetId, R.id.appwidget_stack_view); 176 } 177 } 178 } 179