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.
diff --git a/core/java/android/widget/ActivityChooserView.java b/core/java/android/widget/ActivityChooserView.java
index 45d73af..556414c 100644
--- a/core/java/android/widget/ActivityChooserView.java
+++ b/core/java/android/widget/ActivityChooserView.java
@@ -29,6 +29,8 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
+import android.view.ViewTreeObserver;
+import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ActivityChooserModel.ActivityChooserModelClient;
import com.android.internal.R;
@@ -115,6 +117,19 @@
}
};
+ private final OnGlobalLayoutListener mOnGlobalLayoutListener = new OnGlobalLayoutListener() {
+ @Override
+ public void onGlobalLayout() {
+ if (isShowingPopup()) {
+ if (!isShown()) {
+ getListPopupWindow().dismiss();
+ } else {
+ getListPopupWindow().show();
+ }
+ }
+ }
+ };
+
/**
* Popup window for showing the activity overflow list.
*/
@@ -261,6 +276,8 @@
throw new IllegalStateException("No data model. Did you call #setDataModel?");
}
+ getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
+
mAdapter.setMaxActivityCount(maxActivityCount);
final int activityCount = mAdapter.getActivityCount();
@@ -292,6 +309,10 @@
public boolean dismissPopup() {
if (isShowingPopup()) {
getListPopupWindow().dismiss();
+ ViewTreeObserver viewTreeObserver = getViewTreeObserver();
+ if (viewTreeObserver.isAlive()) {
+ viewTreeObserver.removeGlobalOnLayoutListener(mOnGlobalLayoutListener);
+ }
}
return true;
}
@@ -322,6 +343,10 @@
if (dataModel != null) {
dataModel.unregisterObserver(mModelDataSetOberver);
}
+ ViewTreeObserver viewTreeObserver = getViewTreeObserver();
+ if (viewTreeObserver.isAlive()) {
+ viewTreeObserver.removeGlobalOnLayoutListener(mOnGlobalLayoutListener);
+ }
mIsAttachedToWindow = false;
}
@@ -433,8 +458,10 @@
}
} else {
// The first item in the model is default action => adjust index
- Intent launchIntent = mAdapter.getDataModel().chooseActivity(position + 1);
- mContext.startActivity(launchIntent);
+ Intent launchIntent = mAdapter.getDataModel().chooseActivity(position + 1);
+ if (launchIntent != null) {
+ mContext.startActivity(launchIntent);
+ }
}
} break;
default:
@@ -449,7 +476,9 @@
ResolveInfo defaultActivity = mAdapter.getDefaultActivity();
final int index = mAdapter.getDataModel().getActivityIndex(defaultActivity);
Intent launchIntent = mAdapter.getDataModel().chooseActivity(index);
- mContext.startActivity(launchIntent);
+ if (launchIntent != null) {
+ mContext.startActivity(launchIntent);
+ }
} else if (view == mExpandActivityOverflowButton) {
mIsSelectingDefaultActivity = false;
showPopupUnchecked(mInitialActivityCount);
diff --git a/core/java/android/widget/AdapterView.java b/core/java/android/widget/AdapterView.java
index 9f5737e..f267458c 100644
--- a/core/java/android/widget/AdapterView.java
+++ b/core/java/android/widget/AdapterView.java
@@ -23,11 +23,11 @@
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.ContextMenu;
+import android.view.ContextMenu.ContextMenuInfo;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.ViewDebug;
import android.view.ViewGroup;
-import android.view.ContextMenu.ContextMenuInfo;
import android.view.accessibility.AccessibilityEvent;
@@ -278,6 +278,7 @@
*/
public boolean performItemClick(View view, int position, long id) {
view.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
+
if (mOnItemClickListener != null) {
playSoundEffect(SoundEffectConstants.CLICK);
mOnItemClickListener.onItemClick(this, view, position, id);
diff --git a/core/java/android/widget/ShareActionProvider.java b/core/java/android/widget/ShareActionProvider.java
index 665109a..6e29024 100644
--- a/core/java/android/widget/ShareActionProvider.java
+++ b/core/java/android/widget/ShareActionProvider.java
@@ -28,6 +28,7 @@
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.SubMenu;
import android.view.View;
+import android.widget.ActivityChooserModel.OnChooseActivityListener;
import com.android.internal.R;
@@ -73,6 +74,27 @@
public class ShareActionProvider extends ActionProvider {
/**
+ * Listener for the event of selecting a share target.
+ */
+ public interface OnShareTargetSelectedListener {
+
+ /**
+ * Called when a share target has been selected. The client can
+ * decide whether to handle the intent or rely on the default
+ * behavior which is launching it.
+ * <p>
+ * <strong>Note:</strong> Modifying the intent is not permitted and
+ * any changes to the latter will be ignored.
+ * </p>
+ *
+ * @param source The source of the notification.
+ * @param intent The intent for launching the chosen share target.
+ * @return Whether the client has handled the intent.
+ */
+ public boolean onShareTargetSelected(ShareActionProvider source, Intent intent);
+ }
+
+ /**
* The default for the maximal number of activities shown in the sub-menu.
*/
private static final int DEFAULT_INITIAL_ACTIVITY_COUNT = 4;
@@ -103,6 +125,10 @@
*/
private String mShareHistoryFileName = DEFAULT_SHARE_HISTORY_FILE_NAME;
+ private OnShareTargetSelectedListener mOnShareTargetSelectedListener;
+
+ private OnChooseActivityListener mOnChooseActivityListener;
+
/**
* Creates a new instance.
*
@@ -114,6 +140,21 @@
}
/**
+ * Sets a listener to be notified when a share target has been selected.
+ * The listener can optionally decide to handle the selection and
+ * not rely on the default behavior which is to launch the activity.
+ * <p>
+ * <strong>Note:</strong> If you choose the backing share history file
+ * you will still be notified in this callback.
+ * </p>
+ * @param listener The listener.
+ */
+ public void setOnShareTargetSelectedListener(OnShareTargetSelectedListener listener) {
+ mOnShareTargetSelectedListener = listener;
+ setActivityChooserPolicyIfNeeded();
+ }
+
+ /**
* {@inheritDoc}
*/
@Override
@@ -192,6 +233,7 @@
*/
public void setShareHistoryFileName(String shareHistoryFile) {
mShareHistoryFileName = shareHistoryFile;
+ setActivityChooserPolicyIfNeeded();
}
/**
@@ -229,8 +271,39 @@
mShareHistoryFileName);
final int itemId = item.getItemId();
Intent launchIntent = dataModel.chooseActivity(itemId);
- mContext.startActivity(launchIntent);
+ if (launchIntent != null) {
+ mContext.startActivity(launchIntent);
+ }
return true;
}
}
+
+ /**
+ * Set the activity chooser policy of the model backed by the current
+ * share history file if needed which is if there is a registered callback.
+ */
+ private void setActivityChooserPolicyIfNeeded() {
+ if (mOnShareTargetSelectedListener == null) {
+ return;
+ }
+ if (mOnChooseActivityListener == null) {
+ mOnChooseActivityListener = new ShareAcitivityChooserModelPolicy();
+ }
+ ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mShareHistoryFileName);
+ dataModel.setOnChooseActivityListener(mOnChooseActivityListener);
+ }
+
+ /**
+ * Policy that delegates to the {@link OnShareTargetSelectedListener}, if such.
+ */
+ private class ShareAcitivityChooserModelPolicy implements OnChooseActivityListener {
+ @Override
+ public boolean onChooseActivity(ActivityChooserModel host, Intent intent) {
+ if (mOnShareTargetSelectedListener != null) {
+ return mOnShareTargetSelectedListener.onShareTargetSelected(
+ ShareActionProvider.this, intent);
+ }
+ return false;
+ }
+ }
}