mirror of
https://github.com/antebudimir/tempus.git
synced 2026-01-01 01:53:31 +00:00
feat: Add home screen music playback widget
Introduces a new app widget for music playback control and display. Adds widget provider classes, update manager, view factory, and related resources (layouts, colors, strings, XML). Integrates widget updates with MediaService to reflect current playback state. Updates AndroidManifest to register the widget.
This commit is contained in:
parent
2e29e9537a
commit
cc0e264a17
20 changed files with 746 additions and 2 deletions
|
|
@ -0,0 +1,82 @@
|
|||
package com.cappielloantonio.tempo.widget;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.appwidget.AppWidgetProvider;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
import com.cappielloantonio.tempo.R;
|
||||
import android.app.TaskStackBuilder;
|
||||
import android.app.PendingIntent;
|
||||
import com.cappielloantonio.tempo.ui.activity.MainActivity;
|
||||
import android.util.Log;
|
||||
|
||||
public class WidgetProvider extends AppWidgetProvider {
|
||||
private static final String TAG = "TempoWidget";
|
||||
public static final String ACT_PLAY_PAUSE = "tempo.widget.PLAY_PAUSE";
|
||||
public static final String ACT_NEXT = "tempo.widget.NEXT";
|
||||
public static final String ACT_PREV = "tempo.widget.PREV";
|
||||
|
||||
@Override public void onUpdate(Context ctx, AppWidgetManager mgr, int[] ids) {
|
||||
for (int id : ids) {
|
||||
RemoteViews rv = WidgetUpdateManager.chooseBuild(ctx, id);
|
||||
attachIntents(ctx, rv, id);
|
||||
mgr.updateAppWidget(id, rv);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void onReceive(Context ctx, Intent intent) {
|
||||
super.onReceive(ctx, intent);
|
||||
String a = intent.getAction();
|
||||
Log.d(TAG, "onReceive action=" + a);
|
||||
if (ACT_PLAY_PAUSE.equals(a) || ACT_NEXT.equals(a) || ACT_PREV.equals(a)) {
|
||||
WidgetActions.dispatchToMediaSession(ctx, a);
|
||||
} else if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(a)) {
|
||||
WidgetUpdateManager.refreshFromController(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, android.os.Bundle newOptions) {
|
||||
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
|
||||
RemoteViews rv = WidgetUpdateManager.chooseBuild(context, appWidgetId);
|
||||
attachIntents(context, rv, appWidgetId);
|
||||
appWidgetManager.updateAppWidget(appWidgetId, rv);
|
||||
WidgetUpdateManager.refreshFromController(context);
|
||||
}
|
||||
|
||||
public static void attachIntents(Context ctx, RemoteViews rv) {
|
||||
attachIntents(ctx, rv, 0);
|
||||
}
|
||||
|
||||
public static void attachIntents(Context ctx, RemoteViews rv, int requestCodeBase) {
|
||||
PendingIntent playPause = PendingIntent.getBroadcast(
|
||||
ctx,
|
||||
requestCodeBase + 0,
|
||||
new Intent(ctx, WidgetProvider4x1.class).setAction(ACT_PLAY_PAUSE),
|
||||
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT
|
||||
);
|
||||
PendingIntent next = PendingIntent.getBroadcast(
|
||||
ctx,
|
||||
requestCodeBase + 1,
|
||||
new Intent(ctx, WidgetProvider4x1.class).setAction(ACT_NEXT),
|
||||
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT
|
||||
);
|
||||
PendingIntent prev = PendingIntent.getBroadcast(
|
||||
ctx,
|
||||
requestCodeBase + 2,
|
||||
new Intent(ctx, WidgetProvider4x1.class).setAction(ACT_PREV),
|
||||
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT
|
||||
);
|
||||
|
||||
rv.setOnClickPendingIntent(R.id.btn_play_pause, playPause);
|
||||
rv.setOnClickPendingIntent(R.id.btn_next, next);
|
||||
rv.setOnClickPendingIntent(R.id.btn_prev, prev);
|
||||
|
||||
PendingIntent launch = TaskStackBuilder.create(ctx)
|
||||
.addNextIntentWithParentStack(new Intent(ctx, MainActivity.class))
|
||||
.getPendingIntent(requestCodeBase + 10, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
rv.setOnClickPendingIntent(R.id.root, launch);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue