OSDN Git Service

107cc7a6ef047d598e1e396750e6212d7fa6ee04
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / appwidgets / AppWidgetSmall.java
1 /*
2  * Copyright (C) 2012 Andrew Neal
3  * Copyright (C) 2014 The CyanogenMod Project
4  * Licensed under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
8  * or agreed to in writing, software distributed under the License is
9  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10  * KIND, either express or implied. See the License for the specific language
11  * governing permissions and limitations under the License.
12  */
13
14 package com.cyanogenmod.eleven.appwidgets;
15
16 import android.annotation.SuppressLint;
17 import android.app.PendingIntent;
18 import android.appwidget.AppWidgetManager;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.graphics.Bitmap;
23 import android.text.TextUtils;
24 import android.view.View;
25 import android.widget.RemoteViews;
26
27 import com.cyanogenmod.eleven.MusicPlaybackService;
28 import com.cyanogenmod.eleven.R;
29 import com.cyanogenmod.eleven.ui.activities.HomeActivity;
30 import com.cyanogenmod.eleven.utils.ApolloUtils;
31
32 /**
33  * 4x1 App-Widget
34  *
35  * @author Andrew Neal (andrewdneal@gmail.com)
36  */
37 @SuppressLint("NewApi")
38 public class AppWidgetSmall extends AppWidgetBase {
39
40     public static final String CMDAPPWIDGETUPDATE = "app_widget_small_update";
41
42     private static AppWidgetSmall mInstance;
43
44     public static synchronized AppWidgetSmall getInstance() {
45         if (mInstance == null) {
46             mInstance = new AppWidgetSmall();
47         }
48         return mInstance;
49     }
50
51     /**
52      * {@inheritDoc}
53      */
54     @Override
55     public void onUpdate(final Context context, final AppWidgetManager appWidgetManager,
56             final int[] appWidgetIds) {
57         defaultAppWidget(context, appWidgetIds);
58         final Intent updateIntent = new Intent(MusicPlaybackService.SERVICECMD);
59         updateIntent.putExtra(MusicPlaybackService.CMDNAME, AppWidgetSmall.CMDAPPWIDGETUPDATE);
60         updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
61         updateIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
62         context.sendBroadcast(updateIntent);
63     }
64
65     /**
66      * Initialize given widgets to default state, where we launch Music on
67      * default click and hide actions if service not running.
68      */
69     private void defaultAppWidget(final Context context, final int[] appWidgetIds) {
70         final RemoteViews appWidgetViews = new RemoteViews(context.getPackageName(),
71                 R.layout.app_widget_small);
72         appWidgetViews.setViewVisibility(R.id.app_widget_small_info_container, View.INVISIBLE);
73         linkButtons(context, appWidgetViews);
74         pushUpdate(context, appWidgetIds, appWidgetViews);
75     }
76
77     private void pushUpdate(final Context context, final int[] appWidgetIds, final RemoteViews views) {
78         final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
79         if (appWidgetIds != null) {
80             appWidgetManager.updateAppWidget(appWidgetIds, views);
81         } else {
82             appWidgetManager.updateAppWidget(new ComponentName(context, getClass()), views);
83         }
84     }
85
86     /**
87      * Check against {@link AppWidgetManager} if there are any instances of this
88      * widget.
89      */
90     private boolean hasInstances(final Context context) {
91         final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
92         final int[] mAppWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context,
93                 getClass()));
94         return mAppWidgetIds.length > 0;
95     }
96
97     /**
98      * Handle a change notification coming over from
99      * {@link MusicPlaybackService}
100      */
101     public void notifyChange(final MusicPlaybackService service, final String what) {
102         if (hasInstances(service)) {
103             if (MusicPlaybackService.META_CHANGED.equals(what)
104                     || MusicPlaybackService.PLAYSTATE_CHANGED.equals(what)) {
105                 performUpdate(service, null);
106             }
107         }
108     }
109
110     /**
111      * Update all active widget instances by pushing changes
112      */
113     public void performUpdate(final MusicPlaybackService service, final int[] appWidgetIds) {
114         final RemoteViews appWidgetView = new RemoteViews(service.getPackageName(),
115                 R.layout.app_widget_small);
116
117         final CharSequence trackName = service.getTrackName();
118         final CharSequence artistName = service.getArtistName();
119         final Bitmap bitmap = service.getAlbumArt(true).getBitmap();
120
121         // Set the titles and artwork
122         if (TextUtils.isEmpty(trackName) && TextUtils.isEmpty(artistName)) {
123             appWidgetView.setViewVisibility(R.id.app_widget_small_info_container, View.INVISIBLE);
124         } else {
125             appWidgetView.setViewVisibility(R.id.app_widget_small_info_container, View.VISIBLE);
126             appWidgetView.setTextViewText(R.id.app_widget_small_line_one, trackName);
127             appWidgetView.setTextViewText(R.id.app_widget_small_line_two, artistName);
128         }
129         appWidgetView.setImageViewBitmap(R.id.app_widget_small_image, bitmap);
130
131         // Set correct drawable for pause state
132         final boolean isPlaying = service.isPlaying();
133         if (isPlaying) {
134             appWidgetView.setImageViewResource(R.id.app_widget_small_play,
135                     R.drawable.btn_playback_pause);
136             appWidgetView.setContentDescription(R.id.app_widget_small_play,
137                     service.getString(R.string.accessibility_pause));
138         } else {
139             appWidgetView.setImageViewResource(R.id.app_widget_small_play,
140                     R.drawable.btn_playback_play);
141             appWidgetView.setContentDescription(R.id.app_widget_small_play,
142                     service.getString(R.string.accessibility_play));
143         }
144
145         // Link actions buttons to intents
146         linkButtons(service, appWidgetView);
147
148         // Update the app-widget
149         pushUpdate(service, appWidgetIds, appWidgetView);
150     }
151
152     /**
153      * Link up various button actions using {@link PendingIntents}.
154      *
155      */
156     private void linkButtons(final Context context, final RemoteViews views) {
157         Intent action;
158         PendingIntent pendingIntent;
159
160         final ComponentName serviceName = new ComponentName(context, MusicPlaybackService.class);
161
162         // Home
163         action = new Intent(context, HomeActivity.class);
164         action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
165         pendingIntent = PendingIntent.getActivity(context, 0, action, 0);
166         views.setOnClickPendingIntent(R.id.app_widget_small_info_container, pendingIntent);
167         views.setOnClickPendingIntent(R.id.app_widget_small_image, pendingIntent);
168
169         // Previous track
170         pendingIntent = buildPendingIntent(context, MusicPlaybackService.PREVIOUS_ACTION, serviceName);
171         views.setOnClickPendingIntent(R.id.app_widget_small_previous, pendingIntent);
172
173         // Play and pause
174         pendingIntent = buildPendingIntent(context, MusicPlaybackService.TOGGLEPAUSE_ACTION, serviceName);
175         views.setOnClickPendingIntent(R.id.app_widget_small_play, pendingIntent);
176
177         // Next track
178         pendingIntent = buildPendingIntent(context, MusicPlaybackService.NEXT_ACTION, serviceName);
179         views.setOnClickPendingIntent(R.id.app_widget_small_next, pendingIntent);
180     }
181
182 }