OSDN Git Service

Automatic translation import
[android-x86/packages-apps-Eleven.git] / src / com / andrew / apollo / appwidgets / RecentWidgetService.java
1 /*
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.
10  */
11
12 package com.andrew.apollo.appwidgets;
13
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;
22
23 import com.andrew.apollo.Config;
24 import com.andrew.apollo.R;
25 import com.andrew.apollo.cache.ImageCache;
26 import com.andrew.apollo.cache.ImageFetcher;
27 import com.andrew.apollo.provider.RecentStore;
28 import com.andrew.apollo.provider.RecentStore.RecentStoreColumns;
29
30 /**
31  * This class is used to build the recently listened list for the
32  * {@link RecentWidgetProvicer}.
33  * 
34  * @author Andrew Neal (andrewdneal@gmail.com)
35  */
36 @TargetApi(11)
37 public class RecentWidgetService extends RemoteViewsService {
38
39     /**
40      * {@inheritDoc}
41      */
42     @Override
43     public RemoteViewsFactory onGetViewFactory(final Intent intent) {
44         return new WidgetRemoteViewsFactory(getApplicationContext());
45     }
46
47     /**
48      * This is the factory that will provide data to the collection widget.
49      */
50     private static final class WidgetRemoteViewsFactory implements
51             RemoteViewsService.RemoteViewsFactory {
52         /**
53          * Number of views (ImageView and TextView)
54          */
55         private static final int VIEW_TYPE_COUNT = 2;
56
57         /**
58          * The context to use
59          */
60         private final Context mContext;
61
62         /**
63          * Image cache
64          */
65         private final ImageFetcher mFetcher;
66
67         /**
68          * Recents db
69          */
70         private final RecentStore mRecentsStore;
71
72         /**
73          * Cursor to use
74          */
75         private Cursor mCursor;
76
77         /**
78          * Remove views
79          */
80         private RemoteViews mViews;
81
82         /**
83          * Constructor of <code>WidgetRemoteViewsFactory</code>
84          * 
85          * @param context The {@link Context} to use.
86          */
87         public WidgetRemoteViewsFactory(final Context context) {
88             // Get the context
89             mContext = 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);
95         }
96
97         /**
98          * {@inheritDoc}
99          */
100         @Override
101         public int getCount() {
102             // Check for errors
103             if (mCursor == null || mCursor.isClosed() || mCursor.getCount() <= 0) {
104                 return 0;
105             }
106             return mCursor.getCount();
107         }
108
109         /**
110          * {@inheritDoc}
111          */
112         @Override
113         public long getItemId(final int position) {
114             return position;
115         }
116
117         /**
118          * {@inheritDoc}
119          */
120         @Override
121         public RemoteViews getViewAt(final int position) {
122             mCursor.moveToPosition(position);
123
124             // Create the remote views
125             mViews = new RemoteViews(mContext.getPackageName(), R.layout.app_widget_recents_items);
126
127             // Copy the album id
128             final long id = mCursor.getLong(mCursor
129                     .getColumnIndexOrThrow(RecentStoreColumns.ID));
130
131             // Copy the album name
132             final String albumName = mCursor.getString(mCursor
133                     .getColumnIndexOrThrow(RecentStoreColumns.ALBUMNAME));
134
135             // Copy the artist name
136             final String artist = mCursor.getString(mCursor
137                     .getColumnIndexOrThrow(RecentStoreColumns.ARTISTNAME));
138
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);
143             // Set the album art
144             Bitmap bitmap = mFetcher.getCachedArtwork(albumName, artist, id);
145             if (bitmap != null) {
146                 mViews.setImageViewBitmap(R.id.app_widget_recents_base_image, bitmap);
147             } else {
148                 mViews.setImageViewResource(R.id.app_widget_recents_base_image,
149                         R.drawable.default_artwork);
150             }
151
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);
162
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);
171             return mViews;
172         }
173
174         /**
175          * {@inheritDoc}
176          */
177         @Override
178         public int getViewTypeCount() {
179             return VIEW_TYPE_COUNT;
180         }
181
182         /**
183          * {@inheritDoc}
184          */
185         @Override
186         public boolean hasStableIds() {
187             return true;
188         }
189
190         /**
191          * {@inheritDoc}
192          */
193         @Override
194         public void onDataSetChanged() {
195             if (mCursor != null && !mCursor.isClosed()) {
196                 mCursor.close();
197                 mCursor = null;
198             }
199             mCursor = mRecentsStore.getReadableDatabase().query(
200                     RecentStoreColumns.NAME,
201                     new String[] {
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");
207         }
208
209         /**
210          * {@inheritDoc}
211          */
212         @Override
213         public void onDestroy() {
214             closeCursor();
215         }
216
217         /**
218          * {@inheritDoc}
219          */
220         @Override
221         public RemoteViews getLoadingView() {
222             // Nothing to do
223             return null;
224         }
225
226         /**
227          * {@inheritDoc}
228          */
229         @Override
230         public void onCreate() {
231             // Nothing to do
232         }
233
234         private void closeCursor() {
235             if (mCursor != null && !mCursor.isClosed()) {
236                 mCursor.close();
237                 mCursor = null;
238             }
239         }
240     }
241 }