blob: b2f3113de9b6ad6d22b3c1e100dee5d70136db7f [file] [log] [blame]
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.gallery3d.gadget;
import android.annotation.TargetApi;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
//import android.drm.DrmHelper;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
import com.android.gallery3d.R;
import com.android.gallery3d.app.GalleryApp;
import com.android.gallery3d.common.ApiHelper;
import com.android.gallery3d.data.ContentListener;
@TargetApi(ApiHelper.VERSION_CODES.HONEYCOMB)
public class WidgetService extends RemoteViewsService {
@SuppressWarnings("unused")
private static final String TAG = "GalleryAppWidgetService";
public static final String EXTRA_WIDGET_TYPE = "widget-type";
public static final String EXTRA_ALBUM_PATH = "album-path";
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
int id = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
int type = intent.getIntExtra(EXTRA_WIDGET_TYPE, 0);
String albumPath = intent.getStringExtra(EXTRA_ALBUM_PATH);
return new PhotoRVFactory((GalleryApp) getApplicationContext(),
id, type, albumPath);
}
private static class PhotoRVFactory implements
RemoteViewsService.RemoteViewsFactory, ContentListener {
private final int mAppWidgetId;
private final int mType;
private final String mAlbumPath;
private final GalleryApp mApp;
private WidgetSource mSource;
public PhotoRVFactory(GalleryApp app, int id, int type, String albumPath) {
mApp = app;
mAppWidgetId = id;
mType = type;
mAlbumPath = albumPath;
}
@Override
public void onCreate() {
if (mType == WidgetDatabaseHelper.TYPE_ALBUM) {
mSource = new MediaSetSource(mApp.getDataManager(), mAlbumPath);
} else {
mSource = new LocalPhotoSource(mApp.getAndroidContext());
}
mSource.setContentListener(this);
AppWidgetManager.getInstance(mApp.getAndroidContext())
.notifyAppWidgetViewDataChanged(
mAppWidgetId, R.id.appwidget_stack_view);
}
@Override
public void onDestroy() {
mSource.close();
mSource = null;
}
@Override
public int getCount() {
return mSource.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public RemoteViews getLoadingView() {
RemoteViews rv = new RemoteViews(
mApp.getAndroidContext().getPackageName(),
R.layout.appwidget_loading_item);
rv.setProgressBar(R.id.appwidget_loading_item, 0, 0, true);
return rv;
}
@Override
public RemoteViews getViewAt(int position) {
Bitmap bitmap = null;
try {
bitmap = mSource.getImage(position);
} catch (UnsupportedOperationException e){
// catch exception here to avoid FC
}
boolean isDrm = false;
// if (DrmHelper.isDrmFile(DrmHelper.getFilePath(
// mApp.getAndroidContext(), mSource.getContentUri(position)))) {
// isDrm = true;
// }
if (isDrm) {
if (bitmap == null) {
RemoteViews rv = new RemoteViews(mApp.getAndroidContext()
.getPackageName(),
R.layout.appwidget_drm_empty_item);
rv.setOnClickFillInIntent(
R.id.appwidget_photo_item,
new Intent().setFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP).setData(
mSource.getContentUri(position)));
return rv;
}
} else {
if (bitmap == null) {
return getLoadingView();
}
}
RemoteViews views = new RemoteViews(
mApp.getAndroidContext().getPackageName(),
R.layout.appwidget_photo_item);
views.setImageViewBitmap(R.id.appwidget_photo_item, bitmap);
views.setOnClickFillInIntent(R.id.appwidget_photo_item, new Intent()
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.setData(mSource.getContentUri(position)));
if (isDrm) {
views.setViewVisibility(R.id.drm_icon, View.VISIBLE);
} else {
views.setViewVisibility(R.id.drm_icon, View.GONE);
}
return views;
}
@Override
public void onDataSetChanged() {
mSource.reload();
}
@Override
public void onContentDirty() {
AppWidgetManager.getInstance(mApp.getAndroidContext())
.notifyAppWidgetViewDataChanged(
mAppWidgetId, R.id.appwidget_stack_view);
}
}
}