[Android] Only keep the service alive while playing This implies some changes on the widget , which assumed the service was always there

Sébastien Toque git at videolan.org
Sat May 25 16:33:45 CEST 2013


vlc-ports/android | branch: master | Sébastien Toque <xilasz at gmail.com> | Thu May 23 19:31:46 2013 +0200| [02ee423e8b11263f90f11a47bba3ebb46cee3457] | committer: Sébastien Toque

Only keep the service alive while playing This implies some changes on the widget, which assumed the service was always there

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=02ee423e8b11263f90f11a47bba3ebb46cee3457
---

 vlc-android/res/layout/vlcwidget.xml               |    7 +--
 vlc-android/res/values/strings.xml                 |    1 +
 vlc-android/src/org/videolan/vlc/AudioService.java |    8 ++-
 .../org/videolan/vlc/AudioServiceController.java   |    1 -
 .../videolan/vlc/widget/VLCAppWidgetProvider.java  |   53 ++++++++++++--------
 5 files changed, 45 insertions(+), 25 deletions(-)

diff --git a/vlc-android/res/layout/vlcwidget.xml b/vlc-android/res/layout/vlcwidget.xml
index 529d1e1..5f36f84 100644
--- a/vlc-android/res/layout/vlcwidget.xml
+++ b/vlc-android/res/layout/vlcwidget.xml
@@ -38,7 +38,7 @@
                 android:layout_gravity="center"
                 android:ellipsize="end"
                 android:singleLine="true"
-                android:text="@string/artist"
+                android:text=""
                 android:textColor="#888888"
                 android:textSize="14sp" />
 
@@ -49,7 +49,7 @@
                 android:layout_gravity="center"
                 android:ellipsize="end"
                 android:singleLine="true"
-                android:text="@string/title"
+                android:text="@string/widget_name"
                 android:textColor="#ffffff"
                 android:textSize="16sp" />
 
@@ -104,7 +104,8 @@
     <LinearLayout
         android:id="@+id/timeline_parent"
         android:layout_width="fill_parent"
-        android:layout_height="wrap_content" >
+        android:layout_height="wrap_content"
+        android:visibility="invisible" >
 
         <ProgressBar
             android:id="@+id/timeline"
diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index fc879ba..9e12a86 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -3,6 +3,7 @@
 
     <!-- Main VLC Interface -->
     <string name="app_name" translatable="false">VLC</string>
+    <string name="widget_name" translatable="false">VLC mini player</string>
     <string name="ok">OK</string>
     <string name="cancel">Cancel</string>
     <string name="other">Other</string>
diff --git a/vlc-android/src/org/videolan/vlc/AudioService.java b/vlc-android/src/org/videolan/vlc/AudioService.java
index 18afb62..74e1afb 100644
--- a/vlc-android/src/org/videolan/vlc/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/AudioService.java
@@ -90,6 +90,7 @@ public class AudioService extends Service {
     public static final String ACTION_REMOTE_STOP = "org.videolan.vlc.remote.Stop";
     public static final String ACTION_REMOTE_FORWARD = "org.videolan.vlc.remote.Forward";
     public static final String ACTION_REMOTE_LAST_PLAYLIST = "org.videolan.vlc.remote.LastPlaylist";
+    public static final String ACTION_WIDGET_INIT = "org.videolan.vlc.widget.INIT";
     public static final String ACTION_WIDGET_UPDATE = "org.videolan.vlc.widget.UPDATE";
     public static final String ACTION_WIDGET_UPDATE_POSITION = "org.videolan.vlc.widget.UPDATE_POSITION";
 
@@ -160,6 +161,7 @@ public class AudioService extends Service {
         filter.addAction(ACTION_REMOTE_STOP);
         filter.addAction(ACTION_REMOTE_FORWARD);
         filter.addAction(ACTION_REMOTE_LAST_PLAYLIST);
+        filter.addAction(ACTION_WIDGET_INIT);
         filter.addAction(Intent.ACTION_HEADSET_PLUG);
         filter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
         filter.addAction(VLCApplication.SLEEP_INTENT);
@@ -330,6 +332,8 @@ public class AudioService extends Service {
                 next();
             } else if (action.equalsIgnoreCase(ACTION_REMOTE_LAST_PLAYLIST)) {
                 loadLastPlaylist();
+            } else if (action.equalsIgnoreCase(ACTION_WIDGET_INIT)) {
+                updateWidget(context);
             }
 
             /*
@@ -577,6 +581,7 @@ public class AudioService extends Service {
                 notification = builder.build();
             }
 
+            startService(new Intent(this, AudioService.class));
             startForeground(3, notification);
         }
         catch (NoSuchMethodError e){
@@ -588,6 +593,7 @@ public class AudioService extends Service {
 
     private void hideNotification() {
         stopForeground(true);
+        stopSelf();
     }
 
     private void pause() {
@@ -1021,7 +1027,7 @@ public class AudioService extends Service {
             i.putExtra("artist", mCurrentMedia.getArtist());
         }
         else {
-            i.putExtra("title", "VLC mini player");
+            i.putExtra("title", context.getString(R.string.widget_name));
             i.putExtra("artist", "");
         }
         i.putExtra("isplaying", mLibVLC.isPlaying());
diff --git a/vlc-android/src/org/videolan/vlc/AudioServiceController.java b/vlc-android/src/org/videolan/vlc/AudioServiceController.java
index a79ce0f..de4f1f2 100644
--- a/vlc-android/src/org/videolan/vlc/AudioServiceController.java
+++ b/vlc-android/src/org/videolan/vlc/AudioServiceController.java
@@ -79,7 +79,6 @@ public class AudioServiceController implements IAudioPlayerControl {
 
         if (!mIsBound) {
             Intent service = new Intent(context, AudioService.class);
-            context.startService(service);
 
             SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
             final boolean enableHS = prefs.getBoolean("enable_headset_detection", true);
diff --git a/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.java b/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.java
index 28ee411..fd99184 100644
--- a/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.java
+++ b/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.java
@@ -39,6 +39,7 @@ public class VLCAppWidgetProvider extends AppWidgetProvider {
     public static final String ACTION_REMOTE_PLAYPAUSE = "org.videolan.vlc.remote.PlayPause";
     public static final String ACTION_REMOTE_STOP = "org.videolan.vlc.remote.Stop";
     public static final String ACTION_REMOTE_FORWARD = "org.videolan.vlc.remote.Forward";
+    public static final String ACTION_WIDGET_INIT = "org.videolan.vlc.widget.INIT";
     public static final String ACTION_WIDGET_UPDATE = "org.videolan.vlc.widget.UPDATE";
     public static final String ACTION_WIDGET_UPDATE_POSITION = "org.videolan.vlc.widget.UPDATE_POSITION";
 
@@ -51,40 +52,30 @@ public class VLCAppWidgetProvider extends AppWidgetProvider {
     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
         super.onUpdate(context, appWidgetManager, appWidgetIds);
 
-        Intent i = new Intent();
-        i.setClassName(VLC_PACKAGE, VLC_SERVICE);
-        context.startService(i);
+        /* init widget */
+        Intent i = new Intent(ACTION_WIDGET_INIT);
+        onReceive(context, i);
+
+        /* ask a refresh from the service if there is one */
+        i = new Intent(ACTION_WIDGET_INIT);
+        i.setPackage(VLC_PACKAGE);
+        context.sendBroadcast(i);
     }
 
     @Override
     public void onReceive(Context context, Intent intent) {
         String action = intent.getAction();
 
-        if (ACTION_WIDGET_UPDATE.equals(action)) {
+        if (ACTION_WIDGET_INIT.equals(action)) {
             RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.vlcwidget);
 
-            String title = intent.getStringExtra("title");
-            String artist = intent.getStringExtra("artist");
-            boolean isplaying = intent.getBooleanExtra("isplaying", false);
-            Bitmap cover = intent.getParcelableExtra("cover");
-
-            views.setTextViewText(R.id.songName, title);
-            views.setTextViewText(R.id.artist, artist);
-            views.setImageViewResource(R.id.play_pause, isplaying ? R.drawable.ic_pause : R.drawable.ic_play);
-            if (cover != null)
-                views.setImageViewBitmap(R.id.cover, cover);
-            else
-                views.setImageViewResource(R.id.cover, R.drawable.cone);
-
-            views.setViewVisibility(R.id.timeline_parent, artist != null && artist.length() > 0 ? View.VISIBLE : View.INVISIBLE);
-
             /* commands */
             Intent iBackward = new Intent(ACTION_REMOTE_BACKWARD);
             Intent iPlay = new Intent(ACTION_REMOTE_PLAYPAUSE);
             Intent iStop = new Intent(ACTION_REMOTE_STOP);
             Intent iForward = new Intent(ACTION_REMOTE_FORWARD);
             Intent iVlc = new Intent();
-            iVlc.setClassName(VLC_PACKAGE, isplaying ? VLC_PLAYER : VLC_MAIN);
+            iVlc.setClassName(VLC_PACKAGE, VLC_MAIN);
             iVlc.putExtra(START_FROM_NOTIFICATION, true);
 
             PendingIntent piBackward = PendingIntent.getBroadcast(context, 0, iBackward, PendingIntent.FLAG_UPDATE_CURRENT);
@@ -103,6 +94,28 @@ public class VLCAppWidgetProvider extends AppWidgetProvider {
             AppWidgetManager manager = AppWidgetManager.getInstance(context);
             manager.updateAppWidget(widget, views);
         }
+        else if (ACTION_WIDGET_UPDATE.equals(action)) {
+            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.vlcwidget);
+
+            String title = intent.getStringExtra("title");
+            String artist = intent.getStringExtra("artist");
+            boolean isplaying = intent.getBooleanExtra("isplaying", false);
+            Bitmap cover = intent.getParcelableExtra("cover");
+
+            views.setTextViewText(R.id.songName, title);
+            views.setTextViewText(R.id.artist, artist);
+            views.setImageViewResource(R.id.play_pause, isplaying ? R.drawable.ic_pause : R.drawable.ic_play);
+            if (cover != null)
+                views.setImageViewBitmap(R.id.cover, cover);
+            else
+                views.setImageViewResource(R.id.cover, R.drawable.cone);
+
+            views.setViewVisibility(R.id.timeline_parent, artist != null && artist.length() > 0 ? View.VISIBLE : View.INVISIBLE);
+
+            ComponentName widget = new ComponentName(context, VLCAppWidgetProvider.class);
+            AppWidgetManager manager = AppWidgetManager.getInstance(context);
+            manager.updateAppWidget(widget, views);
+        }
         else if (ACTION_WIDGET_UPDATE_POSITION.equals(action)) {
             RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.vlcwidget);
 



More information about the Android mailing list