From 340e2611de6d54516e222597585dbe7968a9915d Mon Sep 17 00:00:00 2001 From: Svetoslav Ganov Date: Sat, 5 May 2012 17:08:39 -0700 Subject: Showing default activity in activity chooser view only if enough space. 1. While a default target is very convenient, having two targets for the widget on a phone takes too much space and the apps do not have room to add other essential affordances to the action bar. Making the default activity show only of the screen is large enough - the action bar accommodates at least four targets i.e. 500dip. 2. Fixed a bug where changing the backing model of an ActivityChooserView when the view is not shown did not remove the observer for the old model, hence the View can get into a state where it responds to changes of two models while presenting only one. Also in such a case the view would leak since the singleton model will keep a handle to it. 3. Updated the documentation of share action provider to explain how a a developer can change the backing history file and refresh the UI based on the historical data in that file. bug:6447692 Change-Id: Id5c9e54cd5df322ded8574ba028cb680e628243b --- core/java/android/widget/ActivityChooserView.java | 46 ++++++++++++++++++----- 1 file changed, 37 insertions(+), 9 deletions(-) (limited to 'core/java/android/widget/ActivityChooserView.java') diff --git a/core/java/android/widget/ActivityChooserView.java b/core/java/android/widget/ActivityChooserView.java index be6b4e2f5c76..0c0bb1ed5d66 100644 --- a/core/java/android/widget/ActivityChooserView.java +++ b/core/java/android/widget/ActivityChooserView.java @@ -20,8 +20,10 @@ import com.android.internal.R; import android.content.Context; import android.content.Intent; +import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; +import android.content.res.Configuration; import android.content.res.Resources; import android.content.res.TypedArray; import android.database.DataSetObserver; @@ -173,6 +175,11 @@ public class ActivityChooserView extends ViewGroup implements ActivityChooserMod */ private int mDefaultActionButtonContentDescription; + /** + * Whether this view has a default activity affordance. + */ + private boolean mHasDefaultActivity; + /** * Create a new instance. * @@ -245,6 +252,8 @@ public class ActivityChooserView extends ViewGroup implements ActivityChooserMod Resources resources = context.getResources(); mListPopupMaxWidth = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(com.android.internal.R.dimen.config_prefDialogWidth)); + + updateHasDefaultActivity(); } /** @@ -258,6 +267,21 @@ public class ActivityChooserView extends ViewGroup implements ActivityChooserMod } } + @Override + protected void onConfigurationChanged(Configuration newConfig) { + Configuration oldConfig = mContext.getResources().getConfiguration(); + final int changed = oldConfig.diff(newConfig); + if ((changed & ActivityInfo.CONFIG_SCREEN_SIZE) != 0 + || (changed & ActivityInfo.CONFIG_ORIENTATION) != 0) { + updateHasDefaultActivity(); + } + } + + private void updateHasDefaultActivity() { + mHasDefaultActivity = mContext.getResources().getBoolean( + R.bool.activity_chooser_view_has_default_activity); + } + /** * Sets the background for the button that expands the activity * overflow list. @@ -383,7 +407,8 @@ public class ActivityChooserView extends ViewGroup implements ActivityChooserMod protected void onAttachedToWindow() { super.onAttachedToWindow(); ActivityChooserModel dataModel = mAdapter.getDataModel(); - if (dataModel != null) { + if (dataModel != null + && !dataModel.isRegisteredObserver(mModelDataSetOberver)) { dataModel.registerObserver(mModelDataSetOberver); } mIsAttachedToWindow = true; @@ -393,7 +418,8 @@ public class ActivityChooserView extends ViewGroup implements ActivityChooserMod protected void onDetachedFromWindow() { super.onDetachedFromWindow(); ActivityChooserModel dataModel = mAdapter.getDataModel(); - if (dataModel != null) { + if (dataModel != null + && dataModel.isRegisteredObserver(mModelDataSetOberver)) { dataModel.unregisterObserver(mModelDataSetOberver); } ViewTreeObserver viewTreeObserver = getViewTreeObserver(); @@ -496,7 +522,7 @@ public class ActivityChooserView extends ViewGroup implements ActivityChooserMod // Default activity button. final int activityCount = mAdapter.getActivityCount(); final int historySize = mAdapter.getHistorySize(); - if (activityCount > 0 && historySize > 0) { + if (mHasDefaultActivity && activityCount > 0 && historySize > 0) { mDefaultActivityButton.setVisibility(VISIBLE); ResolveInfo activity = mAdapter.getDefaultActivity(); PackageManager packageManager = mContext.getPackageManager(); @@ -512,9 +538,9 @@ public class ActivityChooserView extends ViewGroup implements ActivityChooserMod } // Activity chooser content. if (mDefaultActivityButton.getVisibility() == VISIBLE) { - mActivityChooserContent.setBackgroundDrawable(mActivityChooserContentBackground); + mActivityChooserContent.setBackground(mActivityChooserContentBackground); } else { - mActivityChooserContent.setBackgroundDrawable(null); + mActivityChooserContent.setBackground(null); } } @@ -577,7 +603,7 @@ public class ActivityChooserView extends ViewGroup implements ActivityChooserMod // OnLongClickListener#onLongClick @Override public boolean onLongClick(View view) { - if (view == mDefaultActivityButton) { + if (mHasDefaultActivity && view == mDefaultActivityButton) { if (mAdapter.getCount() > 0) { mIsSelectingDefaultActivity = true; showPopupUnchecked(mInitialActivityCount); @@ -630,14 +656,16 @@ public class ActivityChooserView extends ViewGroup implements ActivityChooserMod public void setDataModel(ActivityChooserModel dataModel) { ActivityChooserModel oldDataModel = mAdapter.getDataModel(); - if (oldDataModel != null && isShown()) { + if (oldDataModel != null) { oldDataModel.unregisterObserver(mModelDataSetOberver); } mDataModel = dataModel; - if (dataModel != null && isShown()) { + if (dataModel != null) { dataModel.registerObserver(mModelDataSetOberver); + notifyDataSetChanged(); + } else { + notifyDataSetInvalidated(); } - notifyDataSetChanged(); } @Override -- cgit v1.2.3