Commit e9e52313 authored by Geoffrey Métais's avatar Geoffrey Métais

Fix widgets commands

parent 77ddbbca
......@@ -129,26 +129,6 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
* Remote / headset control events
*/
when (action) {
ACTION_REMOTE_PLAYPAUSE -> {
if (!playlistManager.hasCurrentMedia()) loadLastAudioPlaylist()
else if (isPlaying) pause()
else play()
}
ACTION_REMOTE_PLAY -> if (!isPlaying && playlistManager.hasCurrentMedia()) play()
ACTION_REMOTE_PAUSE -> if (playlistManager.hasCurrentMedia()) pause()
ACTION_REMOTE_BACKWARD -> previous(false)
ACTION_REMOTE_STOP,
VLCApplication.SLEEP_INTENT -> stop()
ACTION_REMOTE_FORWARD -> next()
ACTION_REMOTE_LAST_PLAYLIST -> loadLastAudioPlaylist()
ACTION_REMOTE_LAST_VIDEO_PLAYLIST -> playlistManager.loadLastPlaylist(PLAYLIST_TYPE_VIDEO)
ACTION_REMOTE_SWITCH_VIDEO -> {
removePopup()
if (hasMedia()) {
currentMediaWrapper!!.removeFlags(MediaWrapper.MEDIA_FORCE_AUDIO)
playlistManager.switchToVideo()
}
}
VLCAppWidgetProvider.ACTION_WIDGET_INIT -> updateWidget()
VLCAppWidgetProvider.ACTION_WIDGET_ENABLED , VLCAppWidgetProvider.ACTION_WIDGET_DISABLED -> updateHasWidget()
ACTION_CAR_MODE_EXIT -> MediaSessionBrowser.unbindExtensionConnection()
......@@ -477,21 +457,11 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
val filter = IntentFilter().apply {
priority = Integer.MAX_VALUE
addAction(ACTION_REMOTE_BACKWARD)
addAction(ACTION_REMOTE_PLAYPAUSE)
addAction(ACTION_REMOTE_PLAY)
addAction(ACTION_REMOTE_PAUSE)
addAction(ACTION_REMOTE_STOP)
addAction(ACTION_REMOTE_FORWARD)
addAction(ACTION_REMOTE_LAST_PLAYLIST)
addAction(ACTION_REMOTE_LAST_VIDEO_PLAYLIST)
addAction(ACTION_REMOTE_SWITCH_VIDEO)
addAction(VLCAppWidgetProvider.ACTION_WIDGET_INIT)
addAction(VLCAppWidgetProvider.ACTION_WIDGET_ENABLED)
addAction(VLCAppWidgetProvider.ACTION_WIDGET_DISABLED)
addAction(Intent.ACTION_HEADSET_PLUG)
addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY)
addAction(VLCApplication.SLEEP_INTENT)
addAction(ACTION_CAR_MODE_EXIT)
}
registerReceiver(receiver, filter)
......@@ -523,15 +493,27 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
ACTION_REMOTE_PLAYPAUSE,
ACTION_REMOTE_PLAY,
ACTION_REMOTE_LAST_PLAYLIST -> {
if (playlistManager.hasCurrentMedia()) play()
else loadLastAudioPlaylist()
if (playlistManager.hasCurrentMedia()) {
if (isPlaying) pause()
else play()
} else loadLastAudioPlaylist()
}
ACTION_REMOTE_BACKWARD -> previous(false)
ACTION_REMOTE_FORWARD -> next()
ACTION_REMOTE_STOP -> stop()
ACTION_PLAY_FROM_SEARCH -> {
if (!this::mediaSession.isInitialized) initMediaSession()
val extras = intent.getBundleExtra(EXTRA_SEARCH_BUNDLE)
mediaSession.controller.transportControls
.playFromSearch(extras.getString(SearchManager.QUERY), extras)
}
ACTION_REMOTE_SWITCH_VIDEO -> {
removePopup()
if (hasMedia()) {
currentMediaWrapper!!.removeFlags(MediaWrapper.MEDIA_FORCE_AUDIO)
playlistManager.switchToVideo()
}
}
}
return Service.START_NOT_STICKY
}
......
......@@ -60,8 +60,6 @@ public class VLCApplication extends Application {
public final static String ACTION_MEDIALIBRARY_READY = "VLC/VLCApplication";
private static volatile Application instance;
public final static String SLEEP_INTENT = Strings.buildPkgString("SleepIntent");
public static Calendar sPlayerSleepTime = null;
private static SimpleArrayMap<String, WeakReference<Object>> sDataMap = new SimpleArrayMap<>();
......
......@@ -63,7 +63,7 @@ public class SleepTimerDialog extends PickTimeFragment {
Calendar sleepTime = Calendar.getInstance();
sleepTime.setTimeInMillis(sleepTime.getTimeInMillis() + interval);
sleepTime.set(Calendar.SECOND, 0);
PlayerOptionsDelegateKt.setSleep(sleepTime);
PlayerOptionsDelegateKt.setSleep(requireContext(), sleepTime);
}
dismiss();
......
......@@ -175,7 +175,7 @@ class PlayerOptionsDelegate(val activity: AppCompatActivity, val service: Playba
if (VLCApplication.sPlayerSleepTime == null)
showFragment(ID_SLEEP)
else {
setSleep(null)
activity.setSleep(null)
initSleep()
}
}
......@@ -411,10 +411,10 @@ class PlayerOptionsDelegate(val activity: AppCompatActivity, val service: Playba
}
}
fun setSleep(time: Calendar?) {
val alarmMgr = VLCApplication.getAppContext().getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(VLCApplication.SLEEP_INTENT)
val sleepPendingIntent = PendingIntent.getBroadcast(VLCApplication.getAppContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
fun Context.setSleep(time: Calendar?) {
val alarmMgr = applicationContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(SLEEP_INTENT)
val sleepPendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
if (time != null) alarmMgr.set(AlarmManager.RTC_WAKEUP, time.timeInMillis, sleepPendingIntent)
else alarmMgr.cancel(sleepPendingIntent)
......
......@@ -38,6 +38,7 @@ import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import org.jetbrains.annotations.NotNull;
import org.videolan.libvlc.IVLCVout;
import org.videolan.libvlc.Media;
import org.videolan.libvlc.MediaPlayer;
......@@ -47,6 +48,7 @@ import org.videolan.vlc.R;
import org.videolan.vlc.gui.preferences.PreferencesActivity;
import org.videolan.vlc.gui.view.PopupLayout;
import org.videolan.vlc.util.Constants;
import org.videolan.vlc.util.KextensionsKt;
import org.videolan.vlc.util.Settings;
import androidx.core.app.NotificationCompat;
......@@ -81,7 +83,7 @@ public class PopupManager implements PlaybackService.Callback, GestureDetector.O
if (mRootView == null) return;
mService.removeCallback(this);
final IVLCVout vlcVout = mService.getVout();
vlcVout.detachViews();
if (vlcVout != null) vlcVout.detachViews();
mRootView.close();
mRootView = null;
}
......@@ -203,10 +205,10 @@ public class PopupManager implements PlaybackService.Callback, GestureDetector.O
public void update() {}
@Override
public void onMediaEvent(Media.Event event) {}
public void onMediaEvent(@NotNull Media.Event event) {}
@Override
public void onMediaPlayerEvent(MediaPlayer.Event event) {
public void onMediaPlayerEvent(@NotNull MediaPlayer.Event event) {
switch (event.type) {
case MediaPlayer.Event.Playing:
if (mRootView != null) {
......@@ -278,8 +280,7 @@ public class PopupManager implements PlaybackService.Callback, GestureDetector.O
@SuppressWarnings("deprecation")
private void showNotification() {
final PendingIntent piStop = PendingIntent.getBroadcast(mService, 0,
new Intent(Constants.ACTION_REMOTE_STOP), PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent piStop = KextensionsKt.getPendingIntent(mService, new Intent(Constants.ACTION_REMOTE_STOP));
final NotificationCompat.Builder builder = new NotificationCompat.Builder(mService)
.setSmallIcon(R.drawable.ic_notif_video)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
......@@ -292,9 +293,9 @@ public class PopupManager implements PlaybackService.Callback, GestureDetector.O
//Switch
final Intent notificationIntent = new Intent(Constants.ACTION_REMOTE_SWITCH_VIDEO);
final PendingIntent piExpand = PendingIntent.getBroadcast(mService, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent piExpand = KextensionsKt.getPendingIntent(mService, notificationIntent);
//PLay Pause
final PendingIntent piPlay = PendingIntent.getBroadcast(mService, 0, new Intent(Constants.ACTION_REMOTE_PLAYPAUSE), PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent piPlay = KextensionsKt.getPendingIntent(mService, new Intent(Constants.ACTION_REMOTE_PLAYPAUSE));
if (mService.isPlaying())
builder.addAction(R.drawable.ic_popup_pause, mService.getString(R.string.pause), piPlay);
......
......@@ -344,7 +344,7 @@ public class VideoPlayerActivity extends AppCompatActivity implements IPlaybackS
editor.apply();
final IntentFilter filter = new IntentFilter();
filter.addAction(VLCApplication.SLEEP_INTENT);
filter.addAction(Constants.SLEEP_INTENT);
registerReceiver(mReceiver, filter);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
......@@ -864,7 +864,7 @@ public class VideoPlayerActivity extends AppCompatActivity implements IPlaybackS
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (VLCApplication.SLEEP_INTENT.equalsIgnoreCase(action)) exitOK();
if (Constants.SLEEP_INTENT.equalsIgnoreCase(action)) exitOK();
}
};
......
......@@ -49,11 +49,9 @@ const val ACTIVITY_RESULT_SECONDARY = 3
@JvmField val EXTRA_SEARCH_BUNDLE = "${ACTION_REMOTE_GENERIC}extra_search_bundle"
@JvmField val ACTION_PLAY_FROM_SEARCH = "${ACTION_REMOTE_GENERIC}play_from_search"
@JvmField val ACTION_REMOTE_SWITCH_VIDEO = "${ACTION_REMOTE_GENERIC}SwitchToVideo"
@JvmField val ACTION_REMOTE_LAST_VIDEO_PLAYLIST = "${ACTION_REMOTE_GENERIC}LastVideoPlaylist"
@JvmField val ACTION_REMOTE_LAST_PLAYLIST = "${ACTION_REMOTE_GENERIC}LastPlaylist"
@JvmField val ACTION_REMOTE_FORWARD = "${ACTION_REMOTE_GENERIC}Forward"
@JvmField val ACTION_REMOTE_STOP = "${ACTION_REMOTE_GENERIC}Stop"
@JvmField val ACTION_REMOTE_PAUSE = "${ACTION_REMOTE_GENERIC}Pause"
@JvmField val ACTION_REMOTE_PLAYPAUSE = "${ACTION_REMOTE_GENERIC}PlayPause"
@JvmField val ACTION_REMOTE_PLAY = "${ACTION_REMOTE_GENERIC}Play"
@JvmField val ACTION_REMOTE_BACKWARD = "${ACTION_REMOTE_GENERIC}Backward"
......@@ -82,6 +80,7 @@ const val ACTION_PAUSE_SCAN = "action_pause_scan"
@JvmField val PLAY_FROM_VIDEOGRID = "gui.video.PLAY_FROM_VIDEOGRID".buildPkgString()
@JvmField val PLAY_FROM_SERVICE = "gui.video.PLAY_FROM_SERVICE".buildPkgString()
@JvmField val EXIT_PLAYER = "gui.video.EXIT_PLAYER".buildPkgString()
@JvmField val SLEEP_INTENT = "SleepIntent".buildPkgString()
const val PLAY_EXTRA_ITEM_LOCATION = "item_location"
const val PLAY_EXTRA_SUBTITLES_LOCATION = "subtitles_location"
const val PLAY_EXTRA_ITEM_TITLE = "title"
......
package org.videolan.vlc.util
import android.annotation.TargetApi
import android.app.Activity
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.res.Resources
import android.net.Uri
import android.os.Build
import android.util.DisplayMetrics
import android.widget.TextView
import androidx.appcompat.widget.AppCompatTextView
......@@ -17,6 +21,7 @@ import androidx.lifecycle.*
import androidx.preference.PreferenceManager
import kotlinx.coroutines.*
import org.videolan.libvlc.Media
import org.videolan.libvlc.util.AndroidUtil
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.MediaWrapper
import org.videolan.tools.SingletonHolder
......@@ -172,3 +177,9 @@ fun Activity.getScreenWidth() : Int {
val dm = DisplayMetrics().also { windowManager.defaultDisplay.getMetrics(it) }
return dm.widthPixels
}
@TargetApi(Build.VERSION_CODES.O)
fun Context.getPendingIntent(iPlay: Intent): PendingIntent {
return if (AndroidUtil.isOOrLater) PendingIntent.getForegroundService(applicationContext, 0, iPlay, PendingIntent.FLAG_UPDATE_CURRENT)
else PendingIntent.getService(applicationContext, 0, iPlay, PendingIntent.FLAG_UPDATE_CURRENT)
}
......@@ -20,13 +20,16 @@
package org.videolan.vlc.util;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.widget.Toast;
......
......@@ -35,9 +35,11 @@ import android.view.View;
import android.widget.RemoteViews;
import org.videolan.vlc.BuildConfig;
import org.videolan.vlc.PlaybackService;
import org.videolan.vlc.R;
import org.videolan.vlc.StartActivity;
import org.videolan.vlc.gui.helpers.AudioUtil;
import org.videolan.vlc.util.KextensionsKt;
import org.videolan.vlc.util.Strings;
import org.videolan.vlc.util.WorkersKt;
......@@ -73,7 +75,7 @@ abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
@Override
public void onReceive(final Context context, final Intent intent) {
final String action = intent.getAction();
if (!action.startsWith(ACTION_WIDGET_PREFIX)) {
if (action == null || !action.startsWith(ACTION_WIDGET_PREFIX)) {
super.onReceive(context, intent);
return;
}
......@@ -83,16 +85,17 @@ abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
if (!partial) {
/* commands */
final Intent iBackward = new Intent(ACTION_REMOTE_BACKWARD);
final Intent iPlay = new Intent(ACTION_REMOTE_PLAYPAUSE);
final Intent iStop = new Intent(ACTION_REMOTE_STOP);
final Intent iForward = new Intent(ACTION_REMOTE_FORWARD);
final Intent iVlc = new Intent(context, StartActivity.class);
final PendingIntent piBackward = PendingIntent.getBroadcast(context, 0, iBackward, PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent piPlay = PendingIntent.getBroadcast(context, 0, iPlay, PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent piStop = PendingIntent.getBroadcast(context, 0, iStop, PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent piForward = PendingIntent.getBroadcast(context, 0, iForward, PendingIntent.FLAG_UPDATE_CURRENT);
final Context appCtx = context.getApplicationContext();
final Intent iBackward = new Intent(ACTION_REMOTE_BACKWARD, null, appCtx, PlaybackService.class);
final Intent iPlay = new Intent(ACTION_REMOTE_PLAYPAUSE, null, appCtx, PlaybackService.class);
final Intent iStop = new Intent(ACTION_REMOTE_STOP, null, appCtx, PlaybackService.class);
final Intent iForward = new Intent(ACTION_REMOTE_FORWARD, null, appCtx, PlaybackService.class);
final Intent iVlc = new Intent(appCtx, StartActivity.class);
final PendingIntent piBackward = KextensionsKt.getPendingIntent(context, iBackward);
final PendingIntent piPlay = KextensionsKt.getPendingIntent(context, iPlay);
final PendingIntent piStop = KextensionsKt.getPendingIntent(context, iStop);
final PendingIntent piForward = KextensionsKt.getPendingIntent(context, iForward);
final PendingIntent piVlc = PendingIntent.getActivity(context, 0, iVlc, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.backward, piBackward);
......@@ -126,10 +129,8 @@ abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
WorkersKt.runOnMainThread(new Runnable() {
@Override
public void run() {
if (cover != null)
views.setImageViewBitmap(R.id.cover, cover);
else
views.setImageViewResource(R.id.cover, R.drawable.icon);
if (cover != null) views.setImageViewBitmap(R.id.cover, cover);
else views.setImageViewResource(R.id.cover, R.drawable.icon);
views.setProgressBar(R.id.timeline, 100, 0, false);
applyUpdate(context, views, partial);
}
......@@ -149,12 +150,10 @@ abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void applyUpdate(Context context, RemoteViews views, boolean partial) {
ComponentName widget = new ComponentName(context, this.getClass());
AppWidgetManager manager = AppWidgetManager.getInstance(context);
if (partial)
manager.partiallyUpdateAppWidget(manager.getAppWidgetIds(widget), views);
else
manager.updateAppWidget(widget, views);
final ComponentName widget = new ComponentName(context, this.getClass());
final AppWidgetManager manager = AppWidgetManager.getInstance(context);
if (partial) manager.partiallyUpdateAppWidget(manager.getAppWidgetIds(widget), views);
else manager.updateAppWidget(widget, views);
}
abstract protected int getLayout();
......@@ -164,12 +163,12 @@ abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
context.sendBroadcast(new Intent(ACTION_WIDGET_ENABLED));
context.sendBroadcast(new Intent(ACTION_WIDGET_ENABLED, null, context.getApplicationContext(), PlaybackService.class));
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
context.sendBroadcast(new Intent(ACTION_WIDGET_DISABLED));
context.sendBroadcast(new Intent(ACTION_WIDGET_DISABLED, null, context.getApplicationContext(), PlaybackService.class));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment