2020
2121import android .content .Context ;
2222import android .content .Intent ;
23+ import android .content .pm .ActivityInfo ;
2324import android .content .pm .PackageManager ;
2425import android .content .pm .ResolveInfo ;
26+ import android .content .res .Configuration ;
2527import android .content .res .Resources ;
2628import android .content .res .TypedArray ;
2729import android .database .DataSetObserver ;
@@ -173,6 +175,11 @@ public void onGlobalLayout() {
173175 */
174176 private int mDefaultActionButtonContentDescription ;
175177
178+ /**
179+ * Whether this view has a default activity affordance.
180+ */
181+ private boolean mHasDefaultActivity ;
182+
176183 /**
177184 * Create a new instance.
178185 *
@@ -245,6 +252,8 @@ public void onChanged() {
245252 Resources resources = context .getResources ();
246253 mListPopupMaxWidth = Math .max (resources .getDisplayMetrics ().widthPixels / 2 ,
247254 resources .getDimensionPixelSize (com .android .internal .R .dimen .config_prefDialogWidth ));
255+
256+ updateHasDefaultActivity ();
248257 }
249258
250259 /**
@@ -258,6 +267,21 @@ public void setActivityChooserModel(ActivityChooserModel dataModel) {
258267 }
259268 }
260269
270+ @ Override
271+ protected void onConfigurationChanged (Configuration newConfig ) {
272+ Configuration oldConfig = mContext .getResources ().getConfiguration ();
273+ final int changed = oldConfig .diff (newConfig );
274+ if ((changed & ActivityInfo .CONFIG_SCREEN_SIZE ) != 0
275+ || (changed & ActivityInfo .CONFIG_ORIENTATION ) != 0 ) {
276+ updateHasDefaultActivity ();
277+ }
278+ }
279+
280+ private void updateHasDefaultActivity () {
281+ mHasDefaultActivity = mContext .getResources ().getBoolean (
282+ R .bool .activity_chooser_view_has_default_activity );
283+ }
284+
261285 /**
262286 * Sets the background for the button that expands the activity
263287 * overflow list.
@@ -383,7 +407,8 @@ public boolean isShowingPopup() {
383407 protected void onAttachedToWindow () {
384408 super .onAttachedToWindow ();
385409 ActivityChooserModel dataModel = mAdapter .getDataModel ();
386- if (dataModel != null ) {
410+ if (dataModel != null
411+ && !dataModel .isRegisteredObserver (mModelDataSetOberver )) {
387412 dataModel .registerObserver (mModelDataSetOberver );
388413 }
389414 mIsAttachedToWindow = true ;
@@ -393,7 +418,8 @@ protected void onAttachedToWindow() {
393418 protected void onDetachedFromWindow () {
394419 super .onDetachedFromWindow ();
395420 ActivityChooserModel dataModel = mAdapter .getDataModel ();
396- if (dataModel != null ) {
421+ if (dataModel != null
422+ && dataModel .isRegisteredObserver (mModelDataSetOberver )) {
397423 dataModel .unregisterObserver (mModelDataSetOberver );
398424 }
399425 ViewTreeObserver viewTreeObserver = getViewTreeObserver ();
@@ -496,7 +522,7 @@ private void updateAppearance() {
496522 // Default activity button.
497523 final int activityCount = mAdapter .getActivityCount ();
498524 final int historySize = mAdapter .getHistorySize ();
499- if (activityCount > 0 && historySize > 0 ) {
525+ if (mHasDefaultActivity && activityCount > 0 && historySize > 0 ) {
500526 mDefaultActivityButton .setVisibility (VISIBLE );
501527 ResolveInfo activity = mAdapter .getDefaultActivity ();
502528 PackageManager packageManager = mContext .getPackageManager ();
@@ -512,9 +538,9 @@ private void updateAppearance() {
512538 }
513539 // Activity chooser content.
514540 if (mDefaultActivityButton .getVisibility () == VISIBLE ) {
515- mActivityChooserContent .setBackgroundDrawable (mActivityChooserContentBackground );
541+ mActivityChooserContent .setBackground (mActivityChooserContentBackground );
516542 } else {
517- mActivityChooserContent .setBackgroundDrawable (null );
543+ mActivityChooserContent .setBackground (null );
518544 }
519545 }
520546
@@ -577,7 +603,7 @@ public void onClick(View view) {
577603 // OnLongClickListener#onLongClick
578604 @ Override
579605 public boolean onLongClick (View view ) {
580- if (view == mDefaultActivityButton ) {
606+ if (mHasDefaultActivity && view == mDefaultActivityButton ) {
581607 if (mAdapter .getCount () > 0 ) {
582608 mIsSelectingDefaultActivity = true ;
583609 showPopupUnchecked (mInitialActivityCount );
@@ -630,14 +656,16 @@ private class ActivityChooserViewAdapter extends BaseAdapter {
630656
631657 public void setDataModel (ActivityChooserModel dataModel ) {
632658 ActivityChooserModel oldDataModel = mAdapter .getDataModel ();
633- if (oldDataModel != null && isShown () ) {
659+ if (oldDataModel != null ) {
634660 oldDataModel .unregisterObserver (mModelDataSetOberver );
635661 }
636662 mDataModel = dataModel ;
637- if (dataModel != null && isShown () ) {
663+ if (dataModel != null ) {
638664 dataModel .registerObserver (mModelDataSetOberver );
665+ notifyDataSetChanged ();
666+ } else {
667+ notifyDataSetInvalidated ();
639668 }
640- notifyDataSetChanged ();
641669 }
642670
643671 @ Override
0 commit comments