2 * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
3 * (the "License"); you may not use this file except in compliance with the
4 * License. You may obtain a copy of the License at
5 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6 * or agreed to in writing, software distributed under the License is
7 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8 * KIND, either express or implied. See the License for the specific language
9 * governing permissions and limitations under the License.
12 package com.cyngn.eleven.appwidgets;
14 import android.annotation.TargetApi;
15 import android.content.Context;
16 import android.content.Intent;
17 import android.database.Cursor;
18 import android.graphics.Bitmap;
19 import android.os.Bundle;
20 import android.widget.RemoteViews;
21 import android.widget.RemoteViewsService;
23 import com.cyngn.eleven.Config;
24 import com.cyngn.eleven.R;
25 import com.cyngn.eleven.cache.ImageCache;
26 import com.cyngn.eleven.cache.ImageFetcher;
27 import com.cyngn.eleven.provider.RecentStore;
28 import com.cyngn.eleven.provider.RecentStore.RecentStoreColumns;
31 * This class is used to build the recently listened list for the
32 * {@link RecentWidgetProvicer}.
34 * @author Andrew Neal (andrewdneal@gmail.com)
37 public class RecentWidgetService extends RemoteViewsService {
43 public RemoteViewsFactory onGetViewFactory(final Intent intent) {
44 return new WidgetRemoteViewsFactory(getApplicationContext());
48 * This is the factory that will provide data to the collection widget.
50 private static final class WidgetRemoteViewsFactory implements
51 RemoteViewsService.RemoteViewsFactory {
53 * Number of views (ImageView and TextView)
55 private static final int VIEW_TYPE_COUNT = 2;
60 private final Context mContext;
65 private final ImageFetcher mFetcher;
70 private final RecentStore mRecentsStore;
75 private Cursor mCursor;
80 private RemoteViews mViews;
83 * Constructor of <code>WidgetRemoteViewsFactory</code>
85 * @param context The {@link Context} to use.
87 public WidgetRemoteViewsFactory(final Context context) {
90 // Initialze the image cache
91 mFetcher = ImageFetcher.getInstance(context);
92 mFetcher.setImageCache(ImageCache.getInstance(context));
93 // Initialze the recents store
94 mRecentsStore = RecentStore.getInstance(context);
101 public int getCount() {
103 if (mCursor == null || mCursor.isClosed() || mCursor.getCount() <= 0) {
106 return mCursor.getCount();
113 public long getItemId(final int position) {
121 public RemoteViews getViewAt(final int position) {
122 mCursor.moveToPosition(position);
124 // Create the remote views
125 mViews = new RemoteViews(mContext.getPackageName(), R.layout.app_widget_recents_items);
128 final long id = mCursor.getLong(mCursor
129 .getColumnIndexOrThrow(RecentStoreColumns.ID));
131 // Copy the album name
132 final String albumName = mCursor.getString(mCursor
133 .getColumnIndexOrThrow(RecentStoreColumns.ALBUMNAME));
135 // Copy the artist name
136 final String artist = mCursor.getString(mCursor
137 .getColumnIndexOrThrow(RecentStoreColumns.ARTISTNAME));
139 // Set the album names
140 mViews.setTextViewText(R.id.app_widget_recents_line_one, albumName);
141 // Set the artist names
142 mViews.setTextViewText(R.id.app_widget_recents_line_two, artist);
144 Bitmap bitmap = mFetcher.getCachedArtwork(albumName, artist, id);
145 if (bitmap != null) {
146 mViews.setImageViewBitmap(R.id.app_widget_recents_base_image, bitmap);
148 mViews.setImageViewResource(R.id.app_widget_recents_base_image,
149 R.drawable.default_artwork);
152 // Open the profile of the touched album
153 final Intent profileIntent = new Intent();
154 final Bundle profileExtras = new Bundle();
155 profileExtras.putLong(Config.ID, id);
156 profileExtras.putString(Config.NAME, albumName);
157 profileExtras.putString(Config.ARTIST_NAME, artist);
158 profileExtras.putString(RecentWidgetProvider.SET_ACTION,
159 RecentWidgetProvider.OPEN_PROFILE);
160 profileIntent.putExtras(profileExtras);
161 mViews.setOnClickFillInIntent(R.id.app_widget_recents_items, profileIntent);
163 // Play the album when the artwork is touched
164 final Intent playAlbum = new Intent();
165 final Bundle playAlbumExtras = new Bundle();
166 playAlbumExtras.putLong(Config.ID, id);
167 playAlbumExtras.putString(RecentWidgetProvider.SET_ACTION,
168 RecentWidgetProvider.PLAY_ALBUM);
169 playAlbum.putExtras(playAlbumExtras);
170 mViews.setOnClickFillInIntent(R.id.app_widget_recents_base_image, playAlbum);
178 public int getViewTypeCount() {
179 return VIEW_TYPE_COUNT;
186 public boolean hasStableIds() {
194 public void onDataSetChanged() {
195 if (mCursor != null && !mCursor.isClosed()) {
199 mCursor = mRecentsStore.getReadableDatabase().query(
200 RecentStoreColumns.NAME,
202 RecentStoreColumns.ID + " as id", RecentStoreColumns.ID,
203 RecentStoreColumns.ALBUMNAME, RecentStoreColumns.ARTISTNAME,
204 RecentStoreColumns.ALBUMSONGCOUNT, RecentStoreColumns.ALBUMYEAR,
205 RecentStoreColumns.TIMEPLAYED
206 }, null, null, null, null, RecentStoreColumns.TIMEPLAYED + " DESC");
213 public void onDestroy() {
221 public RemoteViews getLoadingView() {
230 public void onCreate() {
234 private void closeCursor() {
235 if (mCursor != null && !mCursor.isClosed()) {