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.
14 package org.lineageos.eleven.appwidgets;
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;
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;
33 * @author Andrew Neal (andrewdneal@gmail.com)
35 @SuppressLint("NewApi")
36 public class AppWidgetLarge extends AppWidgetBase {
38 public static final String CMDAPPWIDGETUPDATE = "app_widget_large_update";
40 private static AppWidgetLarge mInstance;
42 public static synchronized AppWidgetLarge getInstance() {
43 if (mInstance == null) {
44 mInstance = new AppWidgetLarge();
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);
64 * Initialize given widgets to default state, where we launch Music on
65 * default click and hide actions if service not running.
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);
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);
79 appWidgetManager.updateAppWidget(new ComponentName(context, getClass()), views);
84 * Check against {@link AppWidgetManager} if there are any instances of this
87 private boolean hasInstances(final Context context) {
88 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
89 final int[] mAppWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context,
91 return mAppWidgetIds.length > 0;
95 * Handle a change notification coming over from
96 * {@link MusicPlaybackService}
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);
108 * Update all active widget instances by pushing changes
110 public void performUpdate(final MusicPlaybackService service, final int[] appWidgetIds) {
111 final RemoteViews appWidgetView = new RemoteViews(service.getPackageName(),
112 R.layout.app_widget_large);
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();
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);
125 // Set correct drawable for pause state
126 final boolean isPlaying = service.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));
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));
139 // Link actions buttons to intents
140 linkButtons(service, appWidgetView);
142 // Update the app-widget
143 pushUpdate(service, appWidgetIds, appWidgetView);
147 * Link up various button actions using {@link PendingIntents}.
150 private void linkButtons(final Context context, final RemoteViews views) {
152 PendingIntent pendingIntent;
154 final ComponentName serviceName = new ComponentName(context, MusicPlaybackService.class);
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);
164 pendingIntent = buildPendingIntent(context, MusicPlaybackService.PREVIOUS_ACTION, serviceName);
165 views.setOnClickPendingIntent(R.id.app_widget_large_previous, pendingIntent);
168 pendingIntent = buildPendingIntent(context, MusicPlaybackService.TOGGLEPAUSE_ACTION, serviceName);
169 views.setOnClickPendingIntent(R.id.app_widget_large_play, pendingIntent);
172 pendingIntent = buildPendingIntent(context, MusicPlaybackService.NEXT_ACTION, serviceName);
173 views.setOnClickPendingIntent(R.id.app_widget_large_next, pendingIntent);