Fix issue #6717667: expanded notification actions don't work on the lock screen

FLAG_ACTIVITY_CLOSE_SYSTEM_DIALOGS was a mistake.

Instead, and the infrastructure for the status bar to take care
of closing and hiding things itself when you press these buttons,
just like it does for the main Intent of the notification.

Bug: 6717667
Change-Id: I1b22186e0cedc05f46a1a3ec78053a72afaf61b1
diff --git a/core/java/android/app/ActivityManagerNative.java b/core/java/android/app/ActivityManagerNative.java
index 2ed93f4..e12fa19 100644
--- a/core/java/android/app/ActivityManagerNative.java
+++ b/core/java/android/app/ActivityManagerNative.java
@@ -1617,6 +1617,16 @@
             return true;
         }
 
+        case IS_INTENT_SENDER_AN_ACTIVITY_TRANSACTION: {
+            data.enforceInterface(IActivityManager.descriptor);
+            IIntentSender r = IIntentSender.Stub.asInterface(
+                data.readStrongBinder());
+            boolean res = isIntentSenderAnActivity(r);
+            reply.writeNoException();
+            reply.writeInt(res ? 1 : 0);
+            return true;
+        }
+
         case UPDATE_PERSISTENT_CONFIGURATION_TRANSACTION: {
             data.enforceInterface(IActivityManager.descriptor);
             Configuration config = Configuration.CREATOR.createFromParcel(data);
@@ -3753,6 +3763,19 @@
         return res;
     }
 
+    public boolean isIntentSenderAnActivity(IIntentSender sender) throws RemoteException {
+        Parcel data = Parcel.obtain();
+        Parcel reply = Parcel.obtain();
+        data.writeInterfaceToken(IActivityManager.descriptor);
+        data.writeStrongBinder(sender.asBinder());
+        mRemote.transact(IS_INTENT_SENDER_AN_ACTIVITY_TRANSACTION, data, reply, 0);
+        reply.readException();
+        boolean res = reply.readInt() != 0;
+        data.recycle();
+        reply.recycle();
+        return res;
+    }
+
     public void updatePersistentConfiguration(Configuration values) throws RemoteException
     {
         Parcel data = Parcel.obtain();
diff --git a/core/java/android/app/IActivityManager.java b/core/java/android/app/IActivityManager.java
index 609a047..031e39b 100644
--- a/core/java/android/app/IActivityManager.java
+++ b/core/java/android/app/IActivityManager.java
@@ -338,6 +338,8 @@
 
     public boolean isIntentSenderTargetedToPackage(IIntentSender sender) throws RemoteException;
 
+    public boolean isIntentSenderAnActivity(IIntentSender sender) throws RemoteException;
+
     public void updatePersistentConfiguration(Configuration values) throws RemoteException;
 
     public long[] getProcessPss(int[] pids) throws RemoteException;
@@ -603,4 +605,5 @@
     int FINISH_ACTIVITY_AFFINITY_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+148;
     int GET_LAUNCHED_FROM_UID_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+149;
     int UNSTABLE_PROVIDER_DIED_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+150;
+    int IS_INTENT_SENDER_AN_ACTIVITY_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+151;
 }
diff --git a/core/java/android/app/PendingIntent.java b/core/java/android/app/PendingIntent.java
index aa366b6..8adc8a2 100644
--- a/core/java/android/app/PendingIntent.java
+++ b/core/java/android/app/PendingIntent.java
@@ -631,6 +631,20 @@
     }
 
     /**
+     * @hide
+     * Check whether this PendingIntent will launch an Activity.
+     */
+    public boolean isActivity() {
+        try {
+            return ActivityManagerNative.getDefault()
+                .isIntentSenderAnActivity(mTarget);
+        } catch (RemoteException e) {
+            // Should never happen.
+            return false;
+        }
+    }
+
+    /**
      * Comparison operator on two PendingIntent objects, such that true
      * is returned then they both represent the same operation from the
      * same package.  This allows you to use {@link #getActivity},
diff --git a/core/java/android/content/Intent.java b/core/java/android/content/Intent.java
index 76dfac4..3fdf451 100644
--- a/core/java/android/content/Intent.java
+++ b/core/java/android/content/Intent.java
@@ -3105,17 +3105,6 @@
      */
     public static final int FLAG_ACTIVITY_TASK_ON_HOME = 0X00004000;
     /**
-     * If set in an Intent passed to {@link Context#startActivity Context.startActivity()},
-     * upon starting the activity the system will also clear any system dialogs that
-     * are currently shown.  This is intended primarily for any actions that are
-     * associated with buttons in a notification: tapping on the button to launch
-     * the activity needs to also dismiss the notification window (which is one
-     * of the system dialogs); setting this flag on the Intent associated with that
-     * action will ensure that and other system dialogs are dismissed so that the
-     * user arrives in the new activity.
-     */
-    public static final int FLAG_ACTIVITY_CLOSE_SYSTEM_DIALOGS = 0X00002000;
-    /**
      * If set, when sending a broadcast only registered receivers will be
      * called -- no BroadcastReceiver components will be launched.
      */
diff --git a/core/java/android/widget/RemoteViews.java b/core/java/android/widget/RemoteViews.java
index beb87cd..e29000d 100644
--- a/core/java/android/widget/RemoteViews.java
+++ b/core/java/android/widget/RemoteViews.java
@@ -125,7 +125,10 @@
      * setting on click extras and setting on click pending intents. The former is enabled,
      * and the latter disabled when this flag is true.
      */
-     private boolean mIsWidgetCollectionChild = false;
+    private boolean mIsWidgetCollectionChild = false;
+
+    private static final OnClickHandler DEFAULT_ON_CLICK_HANDLER = new OnClickHandler();
+    private OnClickHandler mOnClickHandler = DEFAULT_ON_CLICK_HANDLER;
 
     /**
      * This annotation indicates that a subclass of View is alllowed to be used
@@ -149,29 +152,9 @@
         }
     }
 
-    /**
-     * Base class for all actions that can be performed on an 
-     * inflated view.
-     *
-     *  SUBCLASSES MUST BE IMMUTABLE SO CLONE WORKS!!!!!
-     */
-    private abstract static class Action implements Parcelable {
-        public abstract void apply(View root, ViewGroup rootParent) throws ActionException;
-
-        public int describeContents() {
-            return 0;
-        }
-
-        /**
-         * Overridden by each class to report on it's own memory usage
-         */
-        public void updateMemoryUsageEstimate(MemoryUsageCounter counter) {
-            // We currently only calculate Bitmap memory usage, so by default, don't do anything
-            // here
-            return;
-        }
-
-        protected boolean startIntentSafely(View view, PendingIntent pendingIntent,
+    /** @hide */
+    public static class OnClickHandler {
+        public boolean onClickHandler(View view, PendingIntent pendingIntent,
                 Intent fillInIntent) {
             try {
                 // TODO: Unregister this handler if PendingIntent.FLAG_ONE_SHOT?
@@ -193,6 +176,30 @@
             }
             return true;
         }
+    }
+
+    /**
+     * Base class for all actions that can be performed on an
+     * inflated view.
+     *
+     *  SUBCLASSES MUST BE IMMUTABLE SO CLONE WORKS!!!!!
+     */
+    private abstract static class Action implements Parcelable {
+        public abstract void apply(RemoteViews owner, View root,
+                ViewGroup rootParent) throws ActionException;
+
+        public int describeContents() {
+            return 0;
+        }
+
+        /**
+         * Overridden by each class to report on it's own memory usage
+         */
+        public void updateMemoryUsageEstimate(MemoryUsageCounter counter) {
+            // We currently only calculate Bitmap memory usage, so by default, don't do anything
+            // here
+            return;
+        }
 
         public void setBitmapCache(BitmapCache bitmapCache) {
             // Do nothing
@@ -222,7 +229,7 @@
         }
 
         @Override
-        public void apply(View root, ViewGroup rootParent) {
+        public void apply(RemoteViews owner, View root, ViewGroup rootParent) {
             final View view = root.findViewById(viewId);
             if (!(view instanceof AdapterView<?>)) return;
 
@@ -253,7 +260,7 @@
         }
 
         @Override
-        public void apply(View root, ViewGroup rootParent) {
+        public void apply(RemoteViews owner, View root, ViewGroup rootParent) {
             final View target = root.findViewById(viewId);
             if (target == null) return;
 
@@ -265,6 +272,7 @@
             if (target == root) {
                 target.setTagInternal(com.android.internal.R.id.fillInIntent, fillInIntent);
             } else if (target != null && fillInIntent != null) {
+                final OnClickHandler clicker = owner.mOnClickHandler;
                 OnClickListener listener = new OnClickListener() {
                     public void onClick(View v) {
                         // Insure that this view is a child of an AdapterView
@@ -302,7 +310,7 @@
                         rect.bottom = (int) ((pos[1] + v.getHeight()) * appScale + 0.5f);
 
                         fillInIntent.setSourceBounds(rect);
-                        startIntentSafely(v, pendingIntent, fillInIntent);
+                        clicker.onClickHandler(v, pendingIntent, fillInIntent);
                     }
 
                 };
@@ -334,13 +342,14 @@
         }
 
         @Override
-        public void apply(View root, ViewGroup rootParent) {
+        public void apply(RemoteViews owner, View root, ViewGroup rootParent) {
             final View target = root.findViewById(viewId);
             if (target == null) return;
 
             // If the view isn't an AdapterView, setting a PendingIntent template doesn't make sense
             if (target instanceof AdapterView<?>) {
                 AdapterView<?> av = (AdapterView<?>) target;
+                final OnClickHandler clicker = owner.mOnClickHandler;
                 // The PendingIntent template is stored in the view's tag.
                 OnItemClickListener listener = new OnItemClickListener() {
                     public void onItemClick(AdapterView<?> parent, View view,
@@ -380,7 +389,7 @@
 
                             final Intent intent = new Intent();
                             intent.setSourceBounds(rect);
-                            startIntentSafely(view, pendingIntentTemplate, fillInIntent);
+                            clicker.onClickHandler(view, pendingIntentTemplate, fillInIntent);
                         }
                     }
                 };
@@ -417,7 +426,7 @@
         }
 
         @Override
-        public void apply(View root, ViewGroup rootParent) {
+        public void apply(RemoteViews owner, View root, ViewGroup rootParent) {
             final View target = root.findViewById(viewId);
             if (target == null) return;
 
@@ -485,7 +494,7 @@
         }
 
         @Override
-        public void apply(View root, ViewGroup rootParent) {
+        public void apply(RemoteViews owner, View root, ViewGroup rootParent) {
             final View target = root.findViewById(viewId);
             if (target == null) return;
 
@@ -506,6 +515,7 @@
 
             if (target != null) {
                 // If the pendingIntent is null, we clear the onClickListener
+                final OnClickHandler clicker = owner.mOnClickHandler;
                 OnClickListener listener = null;
                 if (pendingIntent != null) {
                     listener = new OnClickListener() {
@@ -525,7 +535,7 @@
     
                             final Intent intent = new Intent();
                             intent.setSourceBounds(rect);
-                            startIntentSafely(v, pendingIntent, intent);
+                            clicker.onClickHandler(v, pendingIntent, intent);
                         }
                     };
                 }
@@ -592,7 +602,7 @@
         }
         
         @Override
-        public void apply(View root, ViewGroup rootParent) {
+        public void apply(RemoteViews owner, View root, ViewGroup rootParent) {
             final View target = root.findViewById(viewId);
             if (target == null) return;
             
@@ -652,7 +662,7 @@
         }
 
         @Override
-        public void apply(View root, ViewGroup rootParent) {
+        public void apply(RemoteViews owner, View root, ViewGroup rootParent) {
             final View view = root.findViewById(viewId);
             if (view == null) return;
 
@@ -776,10 +786,11 @@
         }
 
         @Override
-        public void apply(View root, ViewGroup rootParent) throws ActionException {
+        public void apply(RemoteViews owner, View root,
+                ViewGroup rootParent) throws ActionException {
             ReflectionAction ra = new ReflectionAction(viewId, methodName, ReflectionAction.BITMAP,
                     bitmap);
-            ra.apply(root, rootParent);
+            ra.apply(owner, root, rootParent);
         }
 
         @Override
@@ -995,7 +1006,7 @@
         }
 
         @Override
-        public void apply(View root, ViewGroup rootParent) {
+        public void apply(RemoteViews owner, View root, ViewGroup rootParent) {
             final View view = root.findViewById(viewId);
             if (view == null) return;
 
@@ -1097,13 +1108,13 @@
         }
 
         @Override
-        public void apply(View root, ViewGroup rootParent) {
+        public void apply(RemoteViews owner, View root, ViewGroup rootParent) {
             final Context context = root.getContext();
             final ViewGroup target = (ViewGroup) root.findViewById(viewId);
             if (target == null) return;
             if (nestedViews != null) {
                 // Inflate nested views and add as children
-                target.addView(nestedViews.apply(context, target));
+                target.addView(nestedViews.apply(owner, context, target));
             } else {
                 // Clear all children when nested views omitted
                 target.removeAllViews();
@@ -1164,7 +1175,7 @@
         }
 
         @Override
-        public void apply(View root, ViewGroup rootParent) {
+        public void apply(RemoteViews owner, View root, ViewGroup rootParent) {
             final Context context = root.getContext();
             final TextView target = (TextView) root.findViewById(viewId);
             if (target == null) return;
@@ -1206,7 +1217,7 @@
         }
 
         @Override
-        public void apply(View root, ViewGroup rootParent) {
+        public void apply(RemoteViews owner, View root, ViewGroup rootParent) {
             final Context context = root.getContext();
             final TextView target = (TextView) root.findViewById(viewId);
             if (target == null) return;
@@ -1250,7 +1261,7 @@
         }
 
         @Override
-        public void apply(View root, ViewGroup rootParent) {
+        public void apply(RemoteViews owner, View root, ViewGroup rootParent) {
             final Context context = root.getContext();
             final View target = root.findViewById(viewId);
             if (target == null) return;
@@ -2088,6 +2099,11 @@
         return this;
     }
 
+    /** @hide */
+    public void setOnClickHandler(OnClickHandler handler) {
+        mOnClickHandler = handler;
+    }
+
     /**
      * Inflates the view hierarchy represented by this object and applies
      * all of the actions.
@@ -2100,6 +2116,10 @@
      * @return The inflated view hierarchy
      */
     public View apply(Context context, ViewGroup parent) {
+        return apply(this, context, parent);
+    }
+
+    View apply(RemoteViews owner, Context context, ViewGroup parent) {
         RemoteViews rvToApply = getRemoteViewsToApply(context);
 
         View result;
@@ -2114,7 +2134,7 @@
 
         result = inflater.inflate(rvToApply.getLayoutId(), parent, false);
 
-        rvToApply.performApply(result, parent);
+        rvToApply.performApply(owner, result, parent);
 
         return result;
     }
@@ -2141,15 +2161,15 @@
         }
 
         prepareContext(context);
-        rvToApply.performApply(v, (ViewGroup) v.getParent());
+        rvToApply.performApply(this, v, (ViewGroup) v.getParent());
     }
 
-    private void performApply(View v, ViewGroup parent) {
+    private void performApply(RemoteViews owner, View v, ViewGroup parent) {
         if (mActions != null) {
             final int count = mActions.size();
             for (int i = 0; i < count; i++) {
                 Action a = mActions.get(i);
-                a.apply(v, parent);
+                a.apply(owner, v, parent);
             }
         }
     }