Search in sources :

Example 1 with VideoPlayerActivityStarter

use of de.danoeh.antennapod.ui.appstartintent.VideoPlayerActivityStarter in project AntennaPod by AntennaPod.

the class WidgetUpdater method updateWidget.

/**
 * Update the widgets with the given parameters. Must be called in a background thread.
 */
public static void updateWidget(Context context, WidgetState widgetState) {
    if (!PlayerWidget.isEnabled(context) || widgetState == null) {
        return;
    }
    PendingIntent startMediaPlayer;
    if (widgetState.media != null && widgetState.media.getMediaType() == MediaType.VIDEO && !widgetState.isCasting) {
        startMediaPlayer = new VideoPlayerActivityStarter(context).getPendingIntent();
    } else {
        startMediaPlayer = new MainActivityStarter(context).withOpenPlayer().getPendingIntent();
    }
    RemoteViews views;
    views = new RemoteViews(context.getPackageName(), R.layout.player_widget);
    if (widgetState.media != null) {
        Bitmap icon;
        int iconSize = context.getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
        views.setOnClickPendingIntent(R.id.layout_left, startMediaPlayer);
        views.setOnClickPendingIntent(R.id.imgvCover, startMediaPlayer);
        try {
            icon = Glide.with(context).asBitmap().load(widgetState.media.getImageLocation()).apply(RequestOptions.diskCacheStrategyOf(ApGlideSettings.AP_DISK_CACHE_STRATEGY)).submit(iconSize, iconSize).get(500, TimeUnit.MILLISECONDS);
            views.setImageViewBitmap(R.id.imgvCover, icon);
        } catch (Throwable tr1) {
            try {
                icon = Glide.with(context).asBitmap().load(ImageResourceUtils.getFallbackImageLocation(widgetState.media)).apply(RequestOptions.diskCacheStrategyOf(ApGlideSettings.AP_DISK_CACHE_STRATEGY)).submit(iconSize, iconSize).get(500, TimeUnit.MILLISECONDS);
                views.setImageViewBitmap(R.id.imgvCover, icon);
            } catch (Throwable tr2) {
                Log.e(TAG, "Error loading the media icon for the widget", tr2);
                views.setImageViewResource(R.id.imgvCover, R.mipmap.ic_launcher_round);
            }
        }
        views.setTextViewText(R.id.txtvTitle, widgetState.media.getEpisodeTitle());
        views.setViewVisibility(R.id.txtvTitle, View.VISIBLE);
        views.setViewVisibility(R.id.txtNoPlaying, View.GONE);
        String progressString = getProgressString(widgetState.position, widgetState.duration, widgetState.playbackSpeed);
        if (progressString != null) {
            views.setViewVisibility(R.id.txtvProgress, View.VISIBLE);
            views.setTextViewText(R.id.txtvProgress, progressString);
        }
        if (widgetState.status == PlayerStatus.PLAYING) {
            views.setImageViewResource(R.id.butPlay, R.drawable.ic_widget_pause);
            views.setContentDescription(R.id.butPlay, context.getString(R.string.pause_label));
            views.setImageViewResource(R.id.butPlayExtended, R.drawable.ic_widget_pause);
            views.setContentDescription(R.id.butPlayExtended, context.getString(R.string.pause_label));
        } else {
            views.setImageViewResource(R.id.butPlay, R.drawable.ic_widget_play);
            views.setContentDescription(R.id.butPlay, context.getString(R.string.play_label));
            views.setImageViewResource(R.id.butPlayExtended, R.drawable.ic_widget_play);
            views.setContentDescription(R.id.butPlayExtended, context.getString(R.string.play_label));
        }
        views.setOnClickPendingIntent(R.id.butPlay, createMediaButtonIntent(context, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
        views.setOnClickPendingIntent(R.id.butPlayExtended, createMediaButtonIntent(context, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
        views.setOnClickPendingIntent(R.id.butRew, createMediaButtonIntent(context, KeyEvent.KEYCODE_MEDIA_REWIND));
        views.setOnClickPendingIntent(R.id.butFastForward, createMediaButtonIntent(context, KeyEvent.KEYCODE_MEDIA_FAST_FORWARD));
        views.setOnClickPendingIntent(R.id.butSkip, createMediaButtonIntent(context, KeyEvent.KEYCODE_MEDIA_NEXT));
    } else {
        // start the app if they click anything
        views.setOnClickPendingIntent(R.id.layout_left, startMediaPlayer);
        views.setOnClickPendingIntent(R.id.butPlay, startMediaPlayer);
        views.setOnClickPendingIntent(R.id.butPlayExtended, createMediaButtonIntent(context, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
        views.setViewVisibility(R.id.txtvProgress, View.GONE);
        views.setViewVisibility(R.id.txtvTitle, View.GONE);
        views.setViewVisibility(R.id.txtNoPlaying, View.VISIBLE);
        views.setImageViewResource(R.id.imgvCover, R.mipmap.ic_launcher_round);
        views.setImageViewResource(R.id.butPlay, R.drawable.ic_widget_play);
        views.setImageViewResource(R.id.butPlayExtended, R.drawable.ic_widget_play);
    }
    ComponentName playerWidget = new ComponentName(context, PlayerWidget.class);
    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    int[] widgetIds = manager.getAppWidgetIds(playerWidget);
    for (int id : widgetIds) {
        Bundle options = manager.getAppWidgetOptions(id);
        SharedPreferences prefs = context.getSharedPreferences(PlayerWidget.PREFS_NAME, Context.MODE_PRIVATE);
        int minWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
        int columns = getCellsForSize(minWidth);
        if (columns < 3) {
            views.setViewVisibility(R.id.layout_center, View.INVISIBLE);
        } else {
            views.setViewVisibility(R.id.layout_center, View.VISIBLE);
        }
        boolean showRewind = prefs.getBoolean(PlayerWidget.KEY_WIDGET_REWIND + id, false);
        boolean showFastForward = prefs.getBoolean(PlayerWidget.KEY_WIDGET_FAST_FORWARD + id, false);
        boolean showSkip = prefs.getBoolean(PlayerWidget.KEY_WIDGET_SKIP + id, false);
        if (showRewind || showSkip || showFastForward) {
            views.setInt(R.id.extendedButtonsContainer, "setVisibility", View.VISIBLE);
            views.setInt(R.id.butPlay, "setVisibility", View.GONE);
            views.setInt(R.id.butRew, "setVisibility", showRewind ? View.VISIBLE : View.GONE);
            views.setInt(R.id.butFastForward, "setVisibility", showFastForward ? View.VISIBLE : View.GONE);
            views.setInt(R.id.butSkip, "setVisibility", showSkip ? View.VISIBLE : View.GONE);
        }
        int backgroundColor = prefs.getInt(PlayerWidget.KEY_WIDGET_COLOR + id, PlayerWidget.DEFAULT_COLOR);
        views.setInt(R.id.widgetLayout, "setBackgroundColor", backgroundColor);
        manager.updateAppWidget(id, views);
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) Bundle(android.os.Bundle) AppWidgetManager(android.appwidget.AppWidgetManager) RemoteViews(android.widget.RemoteViews) Bitmap(android.graphics.Bitmap) MainActivityStarter(de.danoeh.antennapod.ui.appstartintent.MainActivityStarter) ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent) VideoPlayerActivityStarter(de.danoeh.antennapod.ui.appstartintent.VideoPlayerActivityStarter)

Aggregations

PendingIntent (android.app.PendingIntent)1 AppWidgetManager (android.appwidget.AppWidgetManager)1 ComponentName (android.content.ComponentName)1 SharedPreferences (android.content.SharedPreferences)1 Bitmap (android.graphics.Bitmap)1 Bundle (android.os.Bundle)1 RemoteViews (android.widget.RemoteViews)1 MainActivityStarter (de.danoeh.antennapod.ui.appstartintent.MainActivityStarter)1 VideoPlayerActivityStarter (de.danoeh.antennapod.ui.appstartintent.VideoPlayerActivityStarter)1