Adding a callback to ShareActionProvider + make ActivityChooserView popup hide on action bar hide.

1. Added a callback to ShareActionProvider allowing clients to be notified
   when an activity is chosed given them a change to say update their UI and
   also decide whether to handle the activity launch themselves.

2. The popup of the ActivityChooserView was not hiding on hiding the action bar.

bug:5094963
bug:5095004

Change-Id: I6c8e8cc1f22d07e707e2218eb108f9101417b23b
diff --git a/core/java/android/widget/ActivityChooserModel.java b/core/java/android/widget/ActivityChooserModel.java
index 4b0a6da..9fea506 100644
--- a/core/java/android/widget/ActivityChooserModel.java
+++ b/core/java/android/widget/ActivityChooserModel.java
@@ -131,6 +131,30 @@
     }
 
     /**
+     * Listener for choosing an activity.
+     */
+    public interface OnChooseActivityListener {
+
+        /**
+         * Called when an activity has been chosen. The client can decide whether
+         * an activity can be chosen and if so the caller of
+         * {@link ActivityChooserModel#chooseActivity(int)} will receive and {@link Intent}
+         * for launching it.
+         * <p>
+         * <strong>Note:</strong> Modifying the intent is not permitted and
+         *     any changes to the latter will be ignored.
+         * </p>
+         *
+         * @param host The listener's host model.
+         * @param intent The intent for launching the chosen activity.
+         * @return Whether the intent is handled and should not be delivered to clients.
+         *
+         * @see ActivityChooserModel#chooseActivity(int)
+         */
+        public boolean onChooseActivity(ActivityChooserModel host, Intent intent);
+    }
+
+    /**
      * Flag for selecting debug mode.
      */
     private static final boolean DEBUG = false;
@@ -287,6 +311,11 @@
     private final Handler mHandler = new Handler();
 
     /**
+     * Policy for controlling how the model handles chosen activities.
+     */
+    private OnChooseActivityListener mActivityChoserModelPolicy;
+
+    /**
      * Gets the data model backed by the contents of the provided file with historical data.
      * Note that only one data model is backed by a given file, thus multiple calls with
      * the same file name will return the same model instance. If no such instance is present
@@ -426,9 +455,11 @@
      * the client solely to let additional customization before the start.
      * </p>
      *
-     * @return Whether adding succeeded.
+     * @return An {@link Intent} for launching the activity or null if the
+     *         policy has consumed the intent.
      *
      * @see HistoricalRecord
+     * @see OnChooseActivityListener
      */
     public Intent chooseActivity(int index) {
         ActivityResolveInfo chosenActivity = mActivites.get(index);
@@ -436,17 +467,37 @@
         ComponentName chosenName = new ComponentName(
                 chosenActivity.resolveInfo.activityInfo.packageName,
                 chosenActivity.resolveInfo.activityInfo.name);
-        HistoricalRecord historicalRecord = new HistoricalRecord(chosenName,
-                System.currentTimeMillis(), DEFAULT_HISTORICAL_RECORD_WEIGHT);
-        addHisoricalRecord(historicalRecord);
 
         Intent choiceIntent = new Intent(mIntent);
         choiceIntent.setComponent(chosenName);
 
+        if (mActivityChoserModelPolicy != null) {
+            // Do not allow the policy to change the intent.
+            Intent choiceIntentCopy = new Intent(choiceIntent);
+            final boolean handled = mActivityChoserModelPolicy.onChooseActivity(this,
+                    choiceIntentCopy);
+            if (handled) {
+                return null;
+            }
+        }
+
+        HistoricalRecord historicalRecord = new HistoricalRecord(chosenName,
+                System.currentTimeMillis(), DEFAULT_HISTORICAL_RECORD_WEIGHT);
+        addHisoricalRecord(historicalRecord);
+
         return choiceIntent;
     }
 
     /**
+     * Sets the listener for choosing an activity.
+     *
+     * @param listener The listener.
+     */
+    public void setOnChooseActivityListener(OnChooseActivityListener listener) {
+        mActivityChoserModelPolicy = listener;
+    }
+
+    /**
      * Gets the default activity, The default activity is defined as the one
      * with highest rank i.e. the first one in the list of activities that can
      * handle the intent.