Skip to content

Commit

Permalink
widget bug fix & code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearfog committed Aug 17, 2023
1 parent 4e40010 commit a43a064
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,58 @@

package org.nuclearfog.apollo.ui.widgets;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.widget.RemoteViews;

import androidx.annotation.Nullable;

import org.nuclearfog.apollo.service.MusicPlaybackService;

/**
* super class for all app widgets
*
* @author nuclearfog
*/
public abstract class AppWidgetBase extends AppWidgetProvider {

@SuppressLint("UnspecifiedImmutableFlag")
protected PendingIntent buildPendingIntent(Context context, String action, ComponentName serviceName) {
/**
* create pending intent used for playback control
*
* @param action type of playback control action used by {@link MusicPlaybackService}
* @return PendingIntent instance
*/
protected PendingIntent createPlaybackControlIntent(Context context, String action, ComponentName serviceName) {
Intent intent = new Intent(action);
intent.setComponent(serviceName);
intent.putExtra(MusicPlaybackService.EXTRA_FOREGROUND, false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
return PendingIntent.getService(context, 0, intent, 0);
return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
}

/**
* Check against {@link AppWidgetManager} if there are any instances of this
* widget.
*/
protected boolean hasInstances(Context context) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] mAppWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, getClass()));
return mAppWidgetIds.length > 0;
}

/**
*
*/
protected void pushUpdate(Context context, Class<?> widgetClass, @Nullable int[] appWidgetIds, RemoteViews views) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
if (appWidgetIds != null) {
appWidgetManager.updateAppWidget(appWidgetIds, views);
} else {
appWidgetManager.updateAppWidget(new ComponentName(context, widgetClass), views);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@

package org.nuclearfog.apollo.ui.widgets;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.widget.RemoteViews;

import org.nuclearfog.apollo.BuildConfig;
Expand Down Expand Up @@ -55,8 +53,7 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
@Override
public void notifyChange(MusicPlaybackService service, String what) {
if (hasInstances(service)) {
if (MusicPlaybackService.CHANGED_META.equals(what)
|| MusicPlaybackService.CHANGED_PLAYSTATE.equals(what)) {
if (MusicPlaybackService.CHANGED_META.equals(what) || MusicPlaybackService.CHANGED_PLAYSTATE.equals(what)) {
performUpdate(service, null);
}
}
Expand Down Expand Up @@ -92,7 +89,7 @@ public void performUpdate(MusicPlaybackService service, int[] appWidgetIds) {
// Link actions buttons to intents
linkButtons(service, appWidgetView, isPlaying);
// Update the app-widget
pushUpdate(service, appWidgetIds, appWidgetView);
pushUpdate(service, getClass(), appWidgetIds, appWidgetView);
}

/**
Expand All @@ -102,66 +99,29 @@ public void performUpdate(MusicPlaybackService service, int[] appWidgetIds) {
private void defaultAppWidget(Context context, int[] appWidgetIds) {
RemoteViews appWidgetViews = new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.app_widget_large);
linkButtons(context, appWidgetViews, false);
pushUpdate(context, appWidgetIds, appWidgetViews);
}

private void pushUpdate(Context context, int[] appWidgetIds, RemoteViews views) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
if (appWidgetIds != null) {
appWidgetManager.updateAppWidget(appWidgetIds, views);
} else {
appWidgetManager.updateAppWidget(new ComponentName(context, getClass()), views);
}
}

/**
* Check against {@link AppWidgetManager} if there are any instances of this
* widget.
*/
private boolean hasInstances(Context context) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] mAppWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, getClass()));
return mAppWidgetIds.length > 0;
pushUpdate(context, getClass(), appWidgetIds, appWidgetViews);
}

/**
* Link up various button actions using {@link PendingIntent}.
*
* @param playerActive True if player is active in background, which means
* widget click will launch {@link AudioPlayerActivity}
* @param playerActive True if player is active in background, which means widget click will launch {@link AudioPlayerActivity}
*/
@SuppressLint("UnspecifiedImmutableFlag")
private void linkButtons(Context context, RemoteViews views, boolean playerActive) {
Intent action;
PendingIntent pendingIntent;
int intentFlag = 0;

Intent action = new Intent(context, playerActive ? AudioPlayerActivity.class : HomeActivity.class);
ComponentName serviceName = new ComponentName(context, MusicPlaybackService.class);

if (playerActive) {
// Now playing
action = new Intent(context, AudioPlayerActivity.class);
} else {
// Home
action = new Intent(context, HomeActivity.class);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
intentFlag |= PendingIntent.FLAG_IMMUTABLE;
}
pendingIntent = PendingIntent.getActivity(context, 0, action, intentFlag);
//
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, action, PendingIntent.FLAG_IMMUTABLE);
views.setOnClickPendingIntent(R.id.app_widget_large_info_container, pendingIntent);
views.setOnClickPendingIntent(R.id.app_widget_large_image, pendingIntent);

// Previous track
pendingIntent = buildPendingIntent(context, MusicPlaybackService.ACTION_PREVIOUS, serviceName);
pendingIntent = createPlaybackControlIntent(context, MusicPlaybackService.ACTION_PREVIOUS, serviceName);
views.setOnClickPendingIntent(R.id.app_widget_large_previous, pendingIntent);

// Play and pause
pendingIntent = buildPendingIntent(context, MusicPlaybackService.ACTION_TOGGLEPAUSE, serviceName);
pendingIntent = createPlaybackControlIntent(context, MusicPlaybackService.ACTION_TOGGLEPAUSE, serviceName);
views.setOnClickPendingIntent(R.id.app_widget_large_play, pendingIntent);

// Next track
pendingIntent = buildPendingIntent(context, MusicPlaybackService.ACTION_NEXT, serviceName);
pendingIntent = createPlaybackControlIntent(context, MusicPlaybackService.ACTION_NEXT, serviceName);
views.setOnClickPendingIntent(R.id.app_widget_large_next, pendingIntent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@

package org.nuclearfog.apollo.ui.widgets;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.widget.RemoteViews;

import org.nuclearfog.apollo.BuildConfig;
Expand Down Expand Up @@ -119,7 +117,7 @@ public void performUpdate(MusicPlaybackService service, int[] appWidgetIds) {
// Link actions buttons to intents
linkButtons(service, appWidgetView, isPlaying);
// Update the app-widget
pushUpdate(service, appWidgetIds, appWidgetView);
pushUpdate(service, getClass(), appWidgetIds, appWidgetView);
}

/**
Expand All @@ -129,77 +127,35 @@ public void performUpdate(MusicPlaybackService service, int[] appWidgetIds) {
private void defaultAppWidget(Context context, int[] appWidgetIds) {
RemoteViews appWidgetViews = new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.app_widget_large_alternate);
linkButtons(context, appWidgetViews, false);
pushUpdate(context, appWidgetIds, appWidgetViews);
}

/**
*
*/
private void pushUpdate(Context context, int[] appWidgetIds, RemoteViews views) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
if (appWidgetIds != null) {
appWidgetManager.updateAppWidget(appWidgetIds, views);
} else {
appWidgetManager.updateAppWidget(new ComponentName(context, getClass()), views);
}
}

/**
* Check against {@link AppWidgetManager} if there are any instances of this
* widget.
*/
private boolean hasInstances(Context context) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] mAppWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, getClass()));
return mAppWidgetIds.length > 0;
pushUpdate(context, getClass(), appWidgetIds, appWidgetViews);
}

/**
* Link up various button actions using {@link PendingIntent}.
*
* @param playerActive True if player is active in background, which means
* widget click will launch {@link AudioPlayerActivity}
* @param playerActive True if player is active in background, which means widget click will launch {@link AudioPlayerActivity}
*/
@SuppressLint("UnspecifiedImmutableFlag")
private void linkButtons(Context context, RemoteViews views, boolean playerActive) {
Intent action;
PendingIntent pendingIntent;
int intentFlag = 0;

ComponentName serviceName = new ComponentName(context, MusicPlaybackService.class);

if (playerActive) {
// Now playing
action = new Intent(context, AudioPlayerActivity.class);
} else {
// Home
action = new Intent(context, HomeActivity.class);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
intentFlag |= PendingIntent.FLAG_IMMUTABLE;
}
pendingIntent = PendingIntent.getActivity(context, 0, action, intentFlag);
Intent action = new Intent(context, playerActive ? AudioPlayerActivity.class : HomeActivity.class);
//
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, action, PendingIntent.FLAG_IMMUTABLE);
views.setOnClickPendingIntent(R.id.app_widget_large_alternate_info_container, pendingIntent);
views.setOnClickPendingIntent(R.id.app_widget_large_alternate_image, pendingIntent);

// Shuffle modes
pendingIntent = buildPendingIntent(context, MusicPlaybackService.ACTION_SHUFFLE, serviceName);
pendingIntent = createPlaybackControlIntent(context, MusicPlaybackService.ACTION_SHUFFLE, serviceName);
views.setOnClickPendingIntent(R.id.app_widget_large_alternate_shuffle, pendingIntent);

// Previous track
pendingIntent = buildPendingIntent(context, MusicPlaybackService.ACTION_PREVIOUS, serviceName);
pendingIntent = createPlaybackControlIntent(context, MusicPlaybackService.ACTION_PREVIOUS, serviceName);
views.setOnClickPendingIntent(R.id.app_widget_large_alternate_previous, pendingIntent);

// Play and pause
pendingIntent = buildPendingIntent(context, MusicPlaybackService.ACTION_TOGGLEPAUSE, serviceName);
pendingIntent = createPlaybackControlIntent(context, MusicPlaybackService.ACTION_TOGGLEPAUSE, serviceName);
views.setOnClickPendingIntent(R.id.app_widget_large_alternate_play, pendingIntent);

// Next track
pendingIntent = buildPendingIntent(context, MusicPlaybackService.ACTION_NEXT, serviceName);
pendingIntent = createPlaybackControlIntent(context, MusicPlaybackService.ACTION_NEXT, serviceName);
views.setOnClickPendingIntent(R.id.app_widget_large_alternate_next, pendingIntent);

// Repeat modes
pendingIntent = buildPendingIntent(context, MusicPlaybackService.ACTION_REPEAT, serviceName);
pendingIntent = createPlaybackControlIntent(context, MusicPlaybackService.ACTION_REPEAT, serviceName);
views.setOnClickPendingIntent(R.id.app_widget_large_alternate_repeat, pendingIntent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@

package org.nuclearfog.apollo.ui.widgets;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.widget.RemoteViews;
Expand Down Expand Up @@ -74,7 +72,6 @@ public void performUpdate(MusicPlaybackService service, int[] appWidgetIds) {
CharSequence trackName = service.getTrackName();
CharSequence artistName = service.getArtistName();
Bitmap bitmap = service.getAlbumArt();

// Set the titles and artwork
if (TextUtils.isEmpty(trackName) && TextUtils.isEmpty(artistName)) {
appWidgetView.setViewVisibility(R.id.app_widget_small_info_container, View.INVISIBLE);
Expand All @@ -96,7 +93,7 @@ public void performUpdate(MusicPlaybackService service, int[] appWidgetIds) {
// Link actions buttons to intents
linkButtons(service, appWidgetView, isPlaying);
// Update the app-widget
pushUpdate(service, appWidgetIds, appWidgetView);
pushUpdate(service, getClass(), appWidgetIds, appWidgetView);
}

/**
Expand All @@ -107,69 +104,29 @@ private void defaultAppWidget(Context context, int[] appWidgetIds) {
RemoteViews appWidgetViews = new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.app_widget_small);
appWidgetViews.setViewVisibility(R.id.app_widget_small_info_container, View.INVISIBLE);
linkButtons(context, appWidgetViews, false);
pushUpdate(context, appWidgetIds, appWidgetViews);
}

/**
*
*/
private void pushUpdate(Context context, int[] appWidgetIds, RemoteViews views) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
if (appWidgetIds != null) {
appWidgetManager.updateAppWidget(appWidgetIds, views);
} else {
appWidgetManager.updateAppWidget(new ComponentName(context, getClass()), views);
}
}

/**
* Check against {@link AppWidgetManager} if there are any instances of this
* widget.
*/
private boolean hasInstances(Context context) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] mAppWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, getClass()));
return mAppWidgetIds.length > 0;
pushUpdate(context, getClass(), appWidgetIds, appWidgetViews);
}

/**
* Link up various button actions using {@link PendingIntent}.
*
* @param playerActive True if player is active in background, which means
* widget click will launch {@link AudioPlayerActivity}
* @param playerActive True if player is active in background, which means widget click will launch {@link AudioPlayerActivity}
*/
@SuppressLint("UnspecifiedImmutableFlag")
private void linkButtons(Context context, RemoteViews views, boolean playerActive) {
Intent action;
PendingIntent pendingIntent;
int intentFlag = 0;

ComponentName serviceName = new ComponentName(context, MusicPlaybackService.class);

if (playerActive) {
// Now playing
action = new Intent(context, AudioPlayerActivity.class);
} else {
// Home
action = new Intent(context, HomeActivity.class);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
intentFlag |= PendingIntent.FLAG_IMMUTABLE;
}
pendingIntent = PendingIntent.getActivity(context, 0, action, intentFlag);
Intent action = new Intent(context, playerActive ? AudioPlayerActivity.class : HomeActivity.class);
//
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, action, PendingIntent.FLAG_IMMUTABLE);
views.setOnClickPendingIntent(R.id.app_widget_small_info_container, pendingIntent);
views.setOnClickPendingIntent(R.id.app_widget_small_image, pendingIntent);

// Previous track
pendingIntent = buildPendingIntent(context, MusicPlaybackService.ACTION_PREVIOUS, serviceName);
pendingIntent = createPlaybackControlIntent(context, MusicPlaybackService.ACTION_PREVIOUS, serviceName);
views.setOnClickPendingIntent(R.id.app_widget_small_previous, pendingIntent);

// Play and pause
pendingIntent = buildPendingIntent(context, MusicPlaybackService.ACTION_TOGGLEPAUSE, serviceName);
pendingIntent = createPlaybackControlIntent(context, MusicPlaybackService.ACTION_TOGGLEPAUSE, serviceName);
views.setOnClickPendingIntent(R.id.app_widget_small_play, pendingIntent);

// Next track
pendingIntent = buildPendingIntent(context, MusicPlaybackService.ACTION_NEXT, serviceName);
pendingIntent = createPlaybackControlIntent(context, MusicPlaybackService.ACTION_NEXT, serviceName);
views.setOnClickPendingIntent(R.id.app_widget_small_next, pendingIntent);
}
}
Loading

0 comments on commit a43a064

Please sign in to comment.