OSDN Git Service

d2be599ee6449931ff3dba78da12ea49edcfd2ea
[android-x86/packages-apps-Eleven.git] / src / org / lineageos / eleven / appwidgets / AppWidgetLarge.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 org.lineageos.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.widget.RemoteViews;
24
25 import org.lineageos.eleven.MusicPlaybackService;
26 import org.lineageos.eleven.R;
27 import org.lineageos.eleven.ui.activities.HomeActivity;
28 import org.lineageos.eleven.utils.ApolloUtils;
29
30 /**
31  * 4x2 App-Widget
32  *
33  * @author Andrew Neal (andrewdneal@gmail.com)
34  */
35 @SuppressLint("NewApi")
36 public class AppWidgetLarge extends AppWidgetBase {
37
38     public static final String CMDAPPWIDGETUPDATE = "app_widget_large_update";
39
40     private static AppWidgetLarge mInstance;
41
42     public static synchronized AppWidgetLarge getInstance() {
43         if (mInstance == null) {
44             mInstance = new AppWidgetLarge();
45         }
46         return mInstance;
47     }
48
49     /**
50      * {@inheritDoc}
51      */
52     @Override
53     public void onUpdate(final Context context, final AppWidgetManager appWidgetManager,
54             final int[] appWidgetIds) {
55         defaultAppWidget(context, appWidgetIds);
56         final Intent updateIntent = new Intent(MusicPlaybackService.SERVICECMD);
57         updateIntent.putExtra(MusicPlaybackService.CMDNAME, AppWidgetLarge.CMDAPPWIDGETUPDATE);
58         updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
59         updateIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
60         context.sendBroadcast(updateIntent);
61     }
62
63     /**
64      * Initialize given widgets to default state, where we launch Music on
65      * default click and hide actions if service not running.
66      */
67     private void defaultAppWidget(final Context context, final int[] appWidgetIds) {
68         final RemoteViews appWidgetViews = new RemoteViews(context.getPackageName(),
69                 R.layout.app_widget_large);
70         linkButtons(context, appWidgetViews);
71         pushUpdate(context, appWidgetIds, appWidgetViews);
72     }
73
74     private void pushUpdate(final Context context, final int[] appWidgetIds, final RemoteViews views) {
75         final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
76         if (appWidgetIds != null) {
77             appWidgetManager.updateAppWidget(appWidgetIds, views);
78         } else {
79             appWidgetManager.updateAppWidget(new ComponentName(context, getClass()), views);
80         }
81     }
82
83     /**
84      * Check against {@link AppWidgetManager} if there are any instances of this
85      * widget.
86      */
87     private boolean hasInstances(final Context context) {
88         final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
89         final int[] mAppWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context,
90                 getClass()));
91         return mAppWidgetIds.length > 0;
92     }
93
94     /**
95      * Handle a change notification coming over from
96      * {@link MusicPlaybackService}
97      */
98     public void notifyChange(final MusicPlaybackService service, final String what) {
99         if (hasInstances(service)) {
100             if (MusicPlaybackService.META_CHANGED.equals(what)
101                     || MusicPlaybackService.PLAYSTATE_CHANGED.equals(what)) {
102                 performUpdate(service, null);
103             }
104         }
105     }
106
107     /**
108      * Update all active widget instances by pushing changes
109      */
110     public void performUpdate(final MusicPlaybackService service, final int[] appWidgetIds) {
111         final RemoteViews appWidgetView = new RemoteViews(service.getPackageName(),
112                 R.layout.app_widget_large);
113
114         final CharSequence trackName = service.getTrackName();
115         final CharSequence artistName = service.getArtistName();
116         final CharSequence albumName = service.getAlbumName();
117         final Bitmap bitmap = service.getAlbumArt(true).getBitmap();
118
119         // Set the titles and artwork
120         appWidgetView.setTextViewText(R.id.app_widget_large_line_one, trackName);
121         appWidgetView.setTextViewText(R.id.app_widget_large_line_two, artistName);
122         appWidgetView.setTextViewText(R.id.app_widget_large_line_three, albumName);
123         appWidgetView.setImageViewBitmap(R.id.app_widget_large_image, bitmap);
124
125         // Set correct drawable for pause state
126         final boolean isPlaying = service.isPlaying();
127         if (isPlaying) {
128             appWidgetView.setImageViewResource(R.id.app_widget_large_play,
129                     R.drawable.btn_playback_pause);
130             appWidgetView.setContentDescription(R.id.app_widget_large_play,
131                     service.getString(R.string.accessibility_pause));
132         } else {
133             appWidgetView.setImageViewResource(R.id.app_widget_large_play,
134                     R.drawable.btn_playback_play);
135             appWidgetView.setContentDescription(R.id.app_widget_large_play,
136                     service.getString(R.string.accessibility_play));
137         }
138
139         // Link actions buttons to intents
140         linkButtons(service, appWidgetView);
141
142         // Update the app-widget
143         pushUpdate(service, appWidgetIds, appWidgetView);
144     }
145
146     /**
147      * Link up various button actions using {@link PendingIntents}.
148      *
149      */
150     private void linkButtons(final Context context, final RemoteViews views) {
151         Intent action;
152         PendingIntent pendingIntent;
153
154         final ComponentName serviceName = new ComponentName(context, MusicPlaybackService.class);
155
156         // Home
157         action = new Intent(context, HomeActivity.class);
158         action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
159         pendingIntent = PendingIntent.getActivity(context, 0, action, 0);
160         views.setOnClickPendingIntent(R.id.app_widget_large_info_container, pendingIntent);
161         views.setOnClickPendingIntent(R.id.app_widget_large_image, pendingIntent);
162
163         // Previous track
164         pendingIntent = buildPendingIntent(context, MusicPlaybackService.PREVIOUS_ACTION, serviceName);
165         views.setOnClickPendingIntent(R.id.app_widget_large_previous, pendingIntent);
166
167         // Play and pause
168         pendingIntent = buildPendingIntent(context, MusicPlaybackService.TOGGLEPAUSE_ACTION, serviceName);
169         views.setOnClickPendingIntent(R.id.app_widget_large_play, pendingIntent);
170
171         // Next track
172         pendingIntent = buildPendingIntent(context, MusicPlaybackService.NEXT_ACTION, serviceName);
173         views.setOnClickPendingIntent(R.id.app_widget_large_next, pendingIntent);
174     }
175
176 }