mirror of
https://github.com/antebudimir/tempus.git
synced 2026-01-01 01:53:31 +00:00
feat: Make all objects in Tempo references for quick access
This commit is contained in:
parent
89fa38f5a0
commit
6c637dcbcb
21 changed files with 1030 additions and 35 deletions
|
|
@ -5,17 +5,20 @@ import android.appwidget.AppWidgetManager;
|
|||
import android.appwidget.AppWidgetProvider;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.text.TextUtils;
|
||||
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;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
public class WidgetProvider extends AppWidgetProvider {
|
||||
private static final String TAG = "TempoWidget";
|
||||
public static final String ACT_PLAY_PAUSE = "tempo.widget.PLAY_PAUSE";
|
||||
|
|
@ -28,7 +31,7 @@ public class WidgetProvider extends AppWidgetProvider {
|
|||
public void onUpdate(Context ctx, AppWidgetManager mgr, int[] ids) {
|
||||
for (int id : ids) {
|
||||
RemoteViews rv = WidgetUpdateManager.chooseBuild(ctx, id);
|
||||
attachIntents(ctx, rv, id);
|
||||
attachIntents(ctx, rv, id, null, null, null);
|
||||
mgr.updateAppWidget(id, rv);
|
||||
}
|
||||
}
|
||||
|
|
@ -50,16 +53,23 @@ public class WidgetProvider extends AppWidgetProvider {
|
|||
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);
|
||||
attachIntents(context, rv, appWidgetId, null, null, null);
|
||||
appWidgetManager.updateAppWidget(appWidgetId, rv);
|
||||
WidgetUpdateManager.refreshFromController(context);
|
||||
}
|
||||
|
||||
public static void attachIntents(Context ctx, RemoteViews rv) {
|
||||
attachIntents(ctx, rv, 0);
|
||||
attachIntents(ctx, rv, 0, null, null, null);
|
||||
}
|
||||
|
||||
public static void attachIntents(Context ctx, RemoteViews rv, int requestCodeBase) {
|
||||
attachIntents(ctx, rv, requestCodeBase, null, null, null);
|
||||
}
|
||||
|
||||
public static void attachIntents(Context ctx, RemoteViews rv, int requestCodeBase,
|
||||
String songLink,
|
||||
String albumLink,
|
||||
String artistLink) {
|
||||
PendingIntent playPause = PendingIntent.getBroadcast(
|
||||
ctx,
|
||||
requestCodeBase + 0,
|
||||
|
|
@ -97,9 +107,31 @@ public class WidgetProvider extends AppWidgetProvider {
|
|||
rv.setOnClickPendingIntent(R.id.btn_shuffle, shuffle);
|
||||
rv.setOnClickPendingIntent(R.id.btn_repeat, repeat);
|
||||
|
||||
PendingIntent launch = TaskStackBuilder.create(ctx)
|
||||
.addNextIntentWithParentStack(new Intent(ctx, MainActivity.class))
|
||||
.getPendingIntent(requestCodeBase + 10, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
PendingIntent launch = buildMainActivityPendingIntent(ctx, requestCodeBase + 10, null);
|
||||
rv.setOnClickPendingIntent(R.id.root, launch);
|
||||
|
||||
PendingIntent songPending = buildMainActivityPendingIntent(ctx, requestCodeBase + 20, songLink);
|
||||
PendingIntent artistPending = buildMainActivityPendingIntent(ctx, requestCodeBase + 21, artistLink);
|
||||
PendingIntent albumPending = buildMainActivityPendingIntent(ctx, requestCodeBase + 22, albumLink);
|
||||
|
||||
PendingIntent fallback = launch;
|
||||
rv.setOnClickPendingIntent(R.id.album_art, songPending != null ? songPending : fallback);
|
||||
rv.setOnClickPendingIntent(R.id.title, songPending != null ? songPending : fallback);
|
||||
rv.setOnClickPendingIntent(R.id.subtitle,
|
||||
artistPending != null ? artistPending : (songPending != null ? songPending : fallback));
|
||||
rv.setOnClickPendingIntent(R.id.album, albumPending != null ? albumPending : fallback);
|
||||
}
|
||||
|
||||
private static PendingIntent buildMainActivityPendingIntent(Context ctx, int requestCode, @Nullable String link) {
|
||||
Intent intent;
|
||||
if (!TextUtils.isEmpty(link)) {
|
||||
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link), ctx, MainActivity.class);
|
||||
} else {
|
||||
intent = new Intent(ctx, MainActivity.class);
|
||||
}
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx);
|
||||
stackBuilder.addNextIntentWithParentStack(intent);
|
||||
return stackBuilder.getPendingIntent(requestCode, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,8 +4,9 @@ import android.appwidget.AppWidgetManager;
|
|||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.text.TextUtils;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.bumptech.glide.request.target.CustomTarget;
|
||||
import com.bumptech.glide.request.transition.Transition;
|
||||
|
|
@ -17,6 +18,7 @@ import androidx.media3.session.MediaController;
|
|||
import androidx.media3.session.SessionToken;
|
||||
|
||||
import com.cappielloantonio.tempo.service.MediaService;
|
||||
import com.cappielloantonio.tempo.util.AssetLinkUtil;
|
||||
import com.cappielloantonio.tempo.util.MusicUtil;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import com.google.common.util.concurrent.MoreExecutors;
|
||||
|
|
@ -34,7 +36,10 @@ public final class WidgetUpdateManager {
|
|||
boolean shuffleEnabled,
|
||||
int repeatMode,
|
||||
long positionMs,
|
||||
long durationMs) {
|
||||
long durationMs,
|
||||
String songLink,
|
||||
String albumLink,
|
||||
String artistLink) {
|
||||
if (TextUtils.isEmpty(title)) title = ctx.getString(R.string.widget_not_playing);
|
||||
if (TextUtils.isEmpty(artist)) artist = ctx.getString(R.string.widget_placeholder_subtitle);
|
||||
if (TextUtils.isEmpty(album)) album = "";
|
||||
|
|
@ -46,7 +51,7 @@ public final class WidgetUpdateManager {
|
|||
for (int id : ids) {
|
||||
android.widget.RemoteViews rv = choosePopulate(ctx, title, artist, album, art, playing,
|
||||
timing.elapsedText, timing.totalText, timing.progress, shuffleEnabled, repeatMode, id);
|
||||
WidgetProvider.attachIntents(ctx, rv, id);
|
||||
WidgetProvider.attachIntents(ctx, rv, id, songLink, albumLink, artistLink);
|
||||
mgr.updateAppWidget(id, rv);
|
||||
}
|
||||
}
|
||||
|
|
@ -56,7 +61,7 @@ public final class WidgetUpdateManager {
|
|||
int[] ids = mgr.getAppWidgetIds(new ComponentName(ctx, WidgetProvider4x1.class));
|
||||
for (int id : ids) {
|
||||
android.widget.RemoteViews rv = chooseBuild(ctx, id);
|
||||
WidgetProvider.attachIntents(ctx, rv, id);
|
||||
WidgetProvider.attachIntents(ctx, rv, id, null, null, null);
|
||||
mgr.updateAppWidget(id, rv);
|
||||
}
|
||||
}
|
||||
|
|
@ -70,7 +75,10 @@ public final class WidgetUpdateManager {
|
|||
boolean shuffleEnabled,
|
||||
int repeatMode,
|
||||
long positionMs,
|
||||
long durationMs) {
|
||||
long durationMs,
|
||||
String songLink,
|
||||
String albumLink,
|
||||
String artistLink) {
|
||||
final Context appCtx = ctx.getApplicationContext();
|
||||
final String t = TextUtils.isEmpty(title) ? appCtx.getString(R.string.widget_not_playing) : title;
|
||||
final String a = TextUtils.isEmpty(artist) ? appCtx.getString(R.string.widget_placeholder_subtitle) : artist;
|
||||
|
|
@ -79,6 +87,9 @@ public final class WidgetUpdateManager {
|
|||
final boolean sh = shuffleEnabled;
|
||||
final int rep = repeatMode;
|
||||
final TimingInfo timing = createTimingInfo(positionMs, durationMs);
|
||||
final String songLinkFinal = songLink;
|
||||
final String albumLinkFinal = albumLink;
|
||||
final String artistLinkFinal = artistLink;
|
||||
|
||||
if (!TextUtils.isEmpty(coverArtId)) {
|
||||
CustomGlideRequest.loadAlbumArtBitmap(
|
||||
|
|
@ -93,7 +104,7 @@ public final class WidgetUpdateManager {
|
|||
for (int id : ids) {
|
||||
android.widget.RemoteViews rv = choosePopulate(appCtx, t, a, alb, resource, p,
|
||||
timing.elapsedText, timing.totalText, timing.progress, sh, rep, id);
|
||||
WidgetProvider.attachIntents(appCtx, rv, id);
|
||||
WidgetProvider.attachIntents(appCtx, rv, id, songLinkFinal, albumLinkFinal, artistLinkFinal);
|
||||
mgr.updateAppWidget(id, rv);
|
||||
}
|
||||
}
|
||||
|
|
@ -105,7 +116,7 @@ public final class WidgetUpdateManager {
|
|||
for (int id : ids) {
|
||||
android.widget.RemoteViews rv = choosePopulate(appCtx, t, a, alb, null, p,
|
||||
timing.elapsedText, timing.totalText, timing.progress, sh, rep, id);
|
||||
WidgetProvider.attachIntents(appCtx, rv, id);
|
||||
WidgetProvider.attachIntents(appCtx, rv, id, songLinkFinal, albumLinkFinal, artistLinkFinal);
|
||||
mgr.updateAppWidget(id, rv);
|
||||
}
|
||||
}
|
||||
|
|
@ -117,7 +128,7 @@ public final class WidgetUpdateManager {
|
|||
for (int id : ids) {
|
||||
android.widget.RemoteViews rv = choosePopulate(appCtx, t, a, alb, null, p,
|
||||
timing.elapsedText, timing.totalText, timing.progress, sh, rep, id);
|
||||
WidgetProvider.attachIntents(appCtx, rv, id);
|
||||
WidgetProvider.attachIntents(appCtx, rv, id, songLinkFinal, albumLinkFinal, artistLinkFinal);
|
||||
mgr.updateAppWidget(id, rv);
|
||||
}
|
||||
}
|
||||
|
|
@ -133,6 +144,7 @@ public final class WidgetUpdateManager {
|
|||
MediaController c = future.get();
|
||||
androidx.media3.common.MediaItem mi = c.getCurrentMediaItem();
|
||||
String title = null, artist = null, album = null, coverId = null;
|
||||
String songLink = null, albumLink = null, artistLink = null;
|
||||
if (mi != null && mi.mediaMetadata != null) {
|
||||
if (mi.mediaMetadata.title != null) title = mi.mediaMetadata.title.toString();
|
||||
if (mi.mediaMetadata.artist != null)
|
||||
|
|
@ -140,10 +152,26 @@ public final class WidgetUpdateManager {
|
|||
if (mi.mediaMetadata.albumTitle != null)
|
||||
album = mi.mediaMetadata.albumTitle.toString();
|
||||
if (mi.mediaMetadata.extras != null) {
|
||||
Bundle extras = mi.mediaMetadata.extras;
|
||||
if (title == null) title = mi.mediaMetadata.extras.getString("title");
|
||||
if (artist == null) artist = mi.mediaMetadata.extras.getString("artist");
|
||||
if (album == null) album = mi.mediaMetadata.extras.getString("album");
|
||||
coverId = mi.mediaMetadata.extras.getString("coverArtId");
|
||||
coverId = extras.getString("coverArtId");
|
||||
|
||||
songLink = extras.getString("assetLinkSong");
|
||||
if (songLink == null) {
|
||||
songLink = AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_SONG, extras.getString("id"));
|
||||
}
|
||||
|
||||
albumLink = extras.getString("assetLinkAlbum");
|
||||
if (albumLink == null) {
|
||||
albumLink = AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_ALBUM, extras.getString("albumId"));
|
||||
}
|
||||
|
||||
artistLink = extras.getString("assetLinkArtist");
|
||||
if (artistLink == null) {
|
||||
artistLink = AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_ARTIST, extras.getString("artistId"));
|
||||
}
|
||||
}
|
||||
}
|
||||
long position = c.getCurrentPosition();
|
||||
|
|
@ -159,7 +187,10 @@ public final class WidgetUpdateManager {
|
|||
c.getShuffleModeEnabled(),
|
||||
c.getRepeatMode(),
|
||||
position,
|
||||
duration);
|
||||
duration,
|
||||
songLink,
|
||||
albumLink,
|
||||
artistLink);
|
||||
c.release();
|
||||
} catch (ExecutionException | InterruptedException ignored) {
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue