[Android] Launch the Audio Player activity when started from a notification

Ludovic Fauvet git at videolan.org
Mon Jun 25 16:45:01 CEST 2012


android | branch: master | Ludovic Fauvet <etix at videolan.org> | Mon Jun 25 16:32:30 2012 +0200| [7251bfd4c480d63c42500f6b2d79125d51e3e708] | committer: Ludovic Fauvet

Launch the Audio Player activity when started from a notification

> http://git.videolan.org/gitweb.cgi/android.git/?a=commit;h=7251bfd4c480d63c42500f6b2d79125d51e3e708
---

 vlc-android/src/org/videolan/vlc/AudioService.java   |   12 +++++++-----
 .../src/org/videolan/vlc/gui/MainActivity.java       |    3 +--
 .../videolan/vlc/gui/audio/AudioPlayerActivity.java  |   18 ++++++++++++++++++
 3 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/AudioService.java b/vlc-android/src/org/videolan/vlc/AudioService.java
index bc12d1f..cf3422a 100644
--- a/vlc-android/src/org/videolan/vlc/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/AudioService.java
@@ -27,6 +27,7 @@ import java.util.List;
 import java.util.Stack;
 
 import org.videolan.vlc.gui.MainActivity;
+import org.videolan.vlc.gui.audio.AudioPlayerActivity;
 import org.videolan.vlc.interfaces.IAudioService;
 import org.videolan.vlc.interfaces.IAudioServiceCallback;
 import org.videolan.vlc.widget.VLCAppWidgetProvider;
@@ -62,6 +63,7 @@ public class AudioService extends Service {
     private static final String TAG = "VLC/AudioService";
 
     private static final int SHOW_PROGRESS = 0;
+    public static final String START_FROM_NOTIFICATION = "from_notification";
 
     private LibVLC mLibVLC;
     private ArrayList<Media> mMediaList;
@@ -143,8 +145,8 @@ public class AudioService extends Service {
                 } else if (!mLibVLC.isPlaying() && mCurrentMedia != null) {
                     play();
                 } else {
-                    Intent iVlc = new Intent(context, MainActivity.class);
-                    iVlc.putExtra(MainActivity.START_FROM_NOTIFICATION, "");
+                    Intent iVlc = new Intent(context, AudioPlayerActivity.class);
+                    iVlc.putExtra(START_FROM_NOTIFICATION, "");
                     iVlc.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                     context.startActivity(iVlc);
                 }
@@ -316,10 +318,10 @@ public class AudioService extends Service {
             mNotification = new Notification(R.drawable.icon, null,
                     System.currentTimeMillis());
         }
-        Intent notificationIntent = new Intent(this, MainActivity.class);
+        Intent notificationIntent = new Intent(this, AudioPlayerActivity.class);
         notificationIntent.setAction(Intent.ACTION_MAIN);
         notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
-        notificationIntent.putExtra(MainActivity.START_FROM_NOTIFICATION, "");
+        notificationIntent.putExtra(START_FROM_NOTIFICATION, "");
         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
         mNotification.setLatestEventInfo(this, mCurrentMedia.getTitle(),
                 mCurrentMedia.getArtist() + " - " + mCurrentMedia.getAlbum(), pendingIntent);
@@ -699,7 +701,7 @@ public class AudioService extends Service {
         Intent iForward = new Intent();
         iForward.setAction(VLCAppWidgetProvider.ACTION_WIDGET_FORWARD);
         Intent iVlc = new Intent(context, MainActivity.class);
-        iVlc.putExtra(MainActivity.START_FROM_NOTIFICATION, "");
+        iVlc.putExtra(START_FROM_NOTIFICATION, "");
 
         PendingIntent piBackward = PendingIntent.getBroadcast(context, 0, iBackward, PendingIntent.FLAG_UPDATE_CURRENT);
         PendingIntent piPlay = PendingIntent.getBroadcast(context, 0, iPlay, PendingIntent.FLAG_UPDATE_CURRENT);
diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index c53e001..a4252ff 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -78,7 +78,6 @@ public class MainActivity extends SherlockFragmentActivity {
 
     private static final int VIDEO_TAB = 0;
     private static final int AUDIO_TAB = 1;
-    public static final String START_FROM_NOTIFICATION = "from_notification";
     private static final String PREF_SHOW_INFO = "show_info";
 
     private ActionBar mActionBar;
@@ -186,7 +185,7 @@ public class MainActivity extends SherlockFragmentActivity {
         /* Restore last view */
         if(!mMediaLibraryActive)
             showDirectoryView();
-        else if (getIntent().hasExtra(START_FROM_NOTIFICATION) || mCurrentViewTab == AUDIO_TAB)
+        else if (mCurrentViewTab == AUDIO_TAB)
             showAudioTab();
         else
             showVideoTab();
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayerActivity.java
index 1932e3d..3501247 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayerActivity.java
@@ -20,13 +20,16 @@
 
 package org.videolan.vlc.gui.audio;
 
+import org.videolan.vlc.AudioService;
 import org.videolan.vlc.AudioServiceController;
 import org.videolan.vlc.R;
 import org.videolan.vlc.RepeatType;
 import org.videolan.vlc.Util;
+import org.videolan.vlc.gui.MainActivity;
 import org.videolan.vlc.interfaces.IAudioPlayer;
 
 import android.app.Activity;
+import android.content.Intent;
 import android.graphics.Bitmap;
 import android.media.AudioManager;
 import android.os.Bundle;
@@ -106,6 +109,21 @@ public class AudioPlayerActivity extends Activity implements IAudioPlayer {
     }
 
     @Override
+    public void onBackPressed() {
+        Bundle extras = getIntent().getExtras();
+
+        if (extras != null && extras.containsKey(AudioService.START_FROM_NOTIFICATION)) {
+            // Launched from notification (adding the MainActivity to the backstack)
+            Intent intent = new Intent(this, MainActivity.class);
+            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
+            startActivity(intent);
+            finish();
+        } else {
+            super.onBackPressed();
+        }
+    }
+
+    @Override
     public void update() {
         // Exit the player when there is no media
         if (!mAudioController.hasMedia()) {



More information about the Android mailing list