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; 31 32@TargetApi(ApiHelper.VERSION_CODES.HONEYCOMB) 33public 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 PhotoRVFactory implements 53 RemoteViewsService.RemoteViewsFactory, ContentListener { 54 55 private final int mAppWidgetId; 56 private final int mType; 57 private final String mAlbumPath; 58 private final GalleryApp mApp; 59 60 private WidgetSource mSource; 61 62 public PhotoRVFactory(GalleryApp app, int id, int type, String albumPath) { 63 mApp = app; 64 mAppWidgetId = id; 65 mType = type; 66 mAlbumPath = albumPath; 67 } 68 69 @Override 70 public void onCreate() { 71 if (mType == WidgetDatabaseHelper.TYPE_ALBUM) { 72 mSource = new MediaSetSource(mApp.getDataManager(), mAlbumPath); 73 } else { 74 mSource = new LocalPhotoSource(mApp.getAndroidContext()); 75 } 76 mSource.setContentListener(this); 77 AppWidgetManager.getInstance(mApp.getAndroidContext()) 78 .notifyAppWidgetViewDataChanged( 79 mAppWidgetId, R.id.appwidget_stack_view); 80 } 81 82 @Override 83 public void onDestroy() { 84 mSource.close(); 85 mSource = null; 86 } 87 88 @Override 89 public int getCount() { 90 return mSource.size(); 91 } 92 93 @Override 94 public long getItemId(int position) { 95 return position; 96 } 97 98 @Override 99 public int getViewTypeCount() { 100 return 2; 101 } 102 103 @Override 104 public boolean hasStableIds() { 105 return true; 106 } 107 108 @Override 109 public RemoteViews getLoadingView() { 110 RemoteViews rv = new RemoteViews( 111 mApp.getAndroidContext().getPackageName(), 112 R.layout.appwidget_loading_item); 113 rv.setProgressBar(R.id.appwidget_loading_item, 0, 0, true); 114 return rv; 115 } 116 117 @Override 118 public RemoteViews getViewAt(int position) { 119 Bitmap bitmap = mSource.getImage(position); 120 if (bitmap == null) return getLoadingView(); 121 RemoteViews views = new RemoteViews( 122 mApp.getAndroidContext().getPackageName(), 123 R.layout.appwidget_photo_item); 124 views.setImageViewBitmap(R.id.appwidget_photo_item, bitmap); 125 views.setOnClickFillInIntent(R.id.appwidget_photo_item, new Intent() 126 .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) 127 .setData(mSource.getContentUri(position))); 128 return views; 129 } 130 131 @Override 132 public void onDataSetChanged() { 133 mSource.reload(); 134 } 135 136 @Override 137 public void onContentDirty() { 138 AppWidgetManager.getInstance(mApp.getAndroidContext()) 139 .notifyAppWidgetViewDataChanged( 140 mAppWidgetId, R.id.appwidget_stack_view); 141 } 142 } 143} 144