OSDN Git Service

Add special case for Gallery2 when opening image files.
authorMatt Garnes <matt@cyngn.com>
Sat, 17 Jan 2015 08:58:20 +0000 (16:58 +0800)
committerMatt Garnes <matt@cyngn.com>
Sun, 18 Jan 2015 15:15:28 +0000 (15:15 +0000)
Gallery2 will change the back button behavior when opening a media URI
to a specific image with FLAG_ACTIVITY_NEW_TASK. Do not use this flag
for intents resolving to Gallery2 for media URIs.

Change-Id: I26d36536bf071403bfd6d8430c2656f1b3ec3449

src/com/cyanogenmod/filemanager/ui/policy/IntentsActionPolicy.java

index 026e31c..5dcab23 100644 (file)
@@ -27,6 +27,7 @@ import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.net.Uri;
 import android.os.Bundle;
+import android.provider.MediaStore;
 import android.util.Log;
 import android.widget.Toast;
 
@@ -83,6 +84,11 @@ public final class IntentsActionPolicy extends ActionsPolicy {
             "com.cyanogenmod.filemanager.category.EDITOR"; //$NON-NLS-1$
 
     /**
+     * The package name of Gallery2.
+     */
+    public static final String GALLERY2_PACKAGE = "com.android.gallery3d";
+
+    /**
      * Method that opens a {@link FileSystemObject} with the default registered application
      * by the system, or ask the user for select a registered application.
      *
@@ -484,8 +490,15 @@ public final class IntentsActionPolicy extends ActionsPolicy {
             }
             intent.setAction(a);
         } else {
-            // Create a new stack for the activity
-            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+            // Opening image files with Gallery2 will behave incorrectly when started
+            // as a new task. We want to be able to return to CMFM with the back button.
+            if (!(Intent.ACTION_VIEW.equals(intent.getAction())
+                  && isGallery2(ri)
+                  && intent.getData() != null
+                  && MediaStore.AUTHORITY.equals(intent.getData().getAuthority()))) {
+                // Create a new stack for the activity
+                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+            }
             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         }
 
@@ -578,6 +591,10 @@ public final class IntentsActionPolicy extends ActionsPolicy {
                         IntentsActionPolicy.CATEGORY_INTERNAL_VIEWER, false);
     }
 
+    public static final boolean isGallery2(ResolveInfo ri) {
+        return GALLERY2_PACKAGE.equals(ri.activityInfo.packageName);
+    }
+
     /**
      * Method that retrieve the finds the preferred activity, if one exists. In case
      * of multiple preferred activity exists the try to choose the better