| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.annotation.StringRes; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.Context; |
| 22 |
import android.content.Intent; |
| 23 |
import android.content.pm.PackageManager; |
| 24 |
import android.content.pm.ResolveInfo; |
| 25 |
import android.content.res.Resources; |
| 26 |
import android.content.res.TypedArray; |
| 27 |
import android.database.DataSetObserver; |
| 28 |
import android.graphics.Color; |
| 29 |
import android.graphics.drawable.ColorDrawable; |
| 30 |
import android.graphics.drawable.Drawable; |
| 31 |
import android.util.AttributeSet; |
| 32 |
import android.util.Log; |
| 33 |
import android.view.ActionProvider; |
| 34 |
import android.view.LayoutInflater; |
| 35 |
import android.view.View; |
| 36 |
import android.view.ViewGroup; |
| 37 |
import android.view.ViewTreeObserver; |
| 38 |
import android.view.ViewTreeObserver.OnGlobalLayoutListener; |
| 39 |
import android.view.accessibility.AccessibilityNodeInfo; |
| 40 |
import android.widget.ActivityChooserModel.ActivityChooserModelClient; |
| 41 |
|
| 42 |
import com.android.internal.R; |
| 43 |
import com.android.internal.view.menu.ShowableListMenu; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public class ActivityChooserView extends ViewGroup implements ActivityChooserModelClient { |
| 69 |
|
| 70 |
private static final String LOG_TAG = "ActivityChooserView"; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
private final ActivityChooserViewAdapter mAdapter; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
private final Callbacks mCallbacks; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
private final LinearLayout mActivityChooserContent; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
private final Drawable mActivityChooserContentBackground; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
private final FrameLayout mExpandActivityOverflowButton; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
private final ImageView mExpandActivityOverflowButtonImage; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private final FrameLayout mDefaultActivityButton; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
private final ImageView mDefaultActivityButtonImage; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
private final int mListPopupMaxWidth; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
ActionProvider mProvider; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
private final DataSetObserver mModelDataSetOberver = new DataSetObserver() { |
| 126 |
|
| 127 |
@Override |
| 128 |
public void onChanged() { |
| 129 |
super.onChanged(); |
| 130 |
mAdapter.notifyDataSetChanged(); |
| 131 |
} |
| 132 |
@Override |
| 133 |
public void onInvalidated() { |
| 134 |
super.onInvalidated(); |
| 135 |
mAdapter.notifyDataSetInvalidated(); |
| 136 |
} |
| 137 |
}; |
| 138 |
|
| 139 |
private final OnGlobalLayoutListener mOnGlobalLayoutListener = new OnGlobalLayoutListener() { |
| 140 |
@Override |
| 141 |
public void onGlobalLayout() { |
| 142 |
if (isShowingPopup()) { |
| 143 |
if (!isShown()) { |
| 144 |
getListPopupWindow().dismiss(); |
| 145 |
} else { |
| 146 |
getListPopupWindow().show(); |
| 147 |
if (mProvider != null) { |
| 148 |
mProvider.subUiVisibilityChanged(true); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
}; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
private ListPopupWindow mListPopupWindow; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
private PopupWindow.OnDismissListener mOnDismissListener; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
private boolean mIsSelectingDefaultActivity; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
private int mInitialActivityCount = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_DEFAULT; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
private boolean mIsAttachedToWindow; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
private int mDefaultActionButtonContentDescription; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public ActivityChooserView(Context context) { |
| 191 |
this(context, null); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public ActivityChooserView(Context context, AttributeSet attrs) { |
| 201 |
this(context, attrs, 0); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public ActivityChooserView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 214 |
this(context, attrs, defStyleAttr, 0); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public ActivityChooserView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 231 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 232 |
|
| 233 |
TypedArray attributesArray = context.obtainStyledAttributes(attrs, |
| 234 |
R.styleable.ActivityChooserView, defStyleAttr, defStyleRes); |
| 235 |
saveAttributeDataForStyleable(context, R.styleable.ActivityChooserView, attrs, |
| 236 |
attributesArray, defStyleAttr, defStyleRes); |
| 237 |
|
| 238 |
mInitialActivityCount = attributesArray.getInt( |
| 239 |
R.styleable.ActivityChooserView_initialActivityCount, |
| 240 |
ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_DEFAULT); |
| 241 |
|
| 242 |
Drawable expandActivityOverflowButtonDrawable = attributesArray.getDrawable( |
| 243 |
R.styleable.ActivityChooserView_expandActivityOverflowButtonDrawable); |
| 244 |
|
| 245 |
attributesArray.recycle(); |
| 246 |
|
| 247 |
LayoutInflater inflater = LayoutInflater.from(mContext); |
| 248 |
inflater.inflate(R.layout.activity_chooser_view, this, true); |
| 249 |
|
| 250 |
mCallbacks = new Callbacks(); |
| 251 |
|
| 252 |
mActivityChooserContent = (LinearLayout) findViewById(R.id.activity_chooser_view_content); |
| 253 |
mActivityChooserContentBackground = mActivityChooserContent.getBackground(); |
| 254 |
|
| 255 |
mDefaultActivityButton = (FrameLayout) findViewById(R.id.default_activity_button); |
| 256 |
mDefaultActivityButton.setOnClickListener(mCallbacks); |
| 257 |
mDefaultActivityButton.setOnLongClickListener(mCallbacks); |
| 258 |
mDefaultActivityButtonImage = mDefaultActivityButton.findViewById(R.id.image); |
| 259 |
|
| 260 |
final FrameLayout expandButton = (FrameLayout) findViewById(R.id.expand_activities_button); |
| 261 |
expandButton.setOnClickListener(mCallbacks); |
| 262 |
expandButton.setAccessibilityDelegate(new AccessibilityDelegate() { |
| 263 |
@Override |
| 264 |
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { |
| 265 |
super.onInitializeAccessibilityNodeInfo(host, info); |
| 266 |
info.setCanOpenPopup(true); |
| 267 |
} |
| 268 |
}); |
| 269 |
expandButton.setOnTouchListener(new ForwardingListener(expandButton) { |
| 270 |
@Override |
| 271 |
public ShowableListMenu getPopup() { |
| 272 |
return getListPopupWindow(); |
| 273 |
} |
| 274 |
|
| 275 |
@Override |
| 276 |
protected boolean onForwardingStarted() { |
| 277 |
showPopup(); |
| 278 |
return true; |
| 279 |
} |
| 280 |
|
| 281 |
@Override |
| 282 |
protected boolean onForwardingStopped() { |
| 283 |
dismissPopup(); |
| 284 |
return true; |
| 285 |
} |
| 286 |
}); |
| 287 |
mExpandActivityOverflowButton = expandButton; |
| 288 |
|
| 289 |
mExpandActivityOverflowButtonImage = |
| 290 |
expandButton.findViewById(R.id.image); |
| 291 |
mExpandActivityOverflowButtonImage.setImageDrawable(expandActivityOverflowButtonDrawable); |
| 292 |
|
| 293 |
mAdapter = new ActivityChooserViewAdapter(); |
| 294 |
mAdapter.registerDataSetObserver(new DataSetObserver() { |
| 295 |
@Override |
| 296 |
public void onChanged() { |
| 297 |
super.onChanged(); |
| 298 |
updateAppearance(); |
| 299 |
} |
| 300 |
}); |
| 301 |
|
| 302 |
Resources resources = context.getResources(); |
| 303 |
mListPopupMaxWidth = Math.max(resources.getDisplayMetrics().widthPixels / 2, |
| 304 |
resources.getDimensionPixelSize(com.android.internal.R.dimen.config_prefDialogWidth)); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public void setActivityChooserModel(ActivityChooserModel dataModel) { |
| 311 |
mAdapter.setDataModel(dataModel); |
| 312 |
if (isShowingPopup()) { |
| 313 |
dismissPopup(); |
| 314 |
showPopup(); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
@UnsupportedAppUsage |
| 330 |
public void setExpandActivityOverflowButtonDrawable(Drawable drawable) { |
| 331 |
mExpandActivityOverflowButtonImage.setImageDrawable(drawable); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
public void setExpandActivityOverflowButtonContentDescription(@StringRes int resourceId) { |
| 345 |
CharSequence contentDescription = mContext.getString(resourceId); |
| 346 |
mExpandActivityOverflowButtonImage.setContentDescription(contentDescription); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public void setProvider(ActionProvider provider) { |
| 354 |
mProvider = provider; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
public boolean showPopup() { |
| 363 |
if (isShowingPopup() || !mIsAttachedToWindow) { |
| 364 |
return false; |
| 365 |
} |
| 366 |
mIsSelectingDefaultActivity = false; |
| 367 |
showPopupUnchecked(mInitialActivityCount); |
| 368 |
return true; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
private void showPopupUnchecked(int maxActivityCount) { |
| 377 |
if (mAdapter.getDataModel() == null) { |
| 378 |
throw new IllegalStateException("No data model. Did you call #setDataModel?"); |
| 379 |
} |
| 380 |
|
| 381 |
getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener); |
| 382 |
|
| 383 |
final boolean defaultActivityButtonShown = |
| 384 |
mDefaultActivityButton.getVisibility() == VISIBLE; |
| 385 |
|
| 386 |
final int activityCount = mAdapter.getActivityCount(); |
| 387 |
final int maxActivityCountOffset = defaultActivityButtonShown ? 1 : 0; |
| 388 |
if (maxActivityCount != ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED |
| 389 |
&& activityCount > maxActivityCount + maxActivityCountOffset) { |
| 390 |
mAdapter.setShowFooterView(true); |
| 391 |
mAdapter.setMaxActivityCount(maxActivityCount - 1); |
| 392 |
} else { |
| 393 |
mAdapter.setShowFooterView(false); |
| 394 |
mAdapter.setMaxActivityCount(maxActivityCount); |
| 395 |
} |
| 396 |
|
| 397 |
ListPopupWindow popupWindow = getListPopupWindow(); |
| 398 |
if (!popupWindow.isShowing()) { |
| 399 |
if (mIsSelectingDefaultActivity || !defaultActivityButtonShown) { |
| 400 |
mAdapter.setShowDefaultActivity(true, defaultActivityButtonShown); |
| 401 |
} else { |
| 402 |
mAdapter.setShowDefaultActivity(false, false); |
| 403 |
} |
| 404 |
final int contentWidth = Math.min(mAdapter.measureContentWidth(), mListPopupMaxWidth); |
| 405 |
popupWindow.setContentWidth(contentWidth); |
| 406 |
popupWindow.show(); |
| 407 |
if (mProvider != null) { |
| 408 |
mProvider.subUiVisibilityChanged(true); |
| 409 |
} |
| 410 |
popupWindow.getListView().setContentDescription(mContext.getString( |
| 411 |
R.string.activitychooserview_choose_application)); |
| 412 |
popupWindow.getListView().setSelector(new ColorDrawable(Color.TRANSPARENT)); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
public boolean dismissPopup() { |
| 422 |
if (isShowingPopup()) { |
| 423 |
getListPopupWindow().dismiss(); |
| 424 |
ViewTreeObserver viewTreeObserver = getViewTreeObserver(); |
| 425 |
if (viewTreeObserver.isAlive()) { |
| 426 |
viewTreeObserver.removeOnGlobalLayoutListener(mOnGlobalLayoutListener); |
| 427 |
} |
| 428 |
} |
| 429 |
return true; |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
public boolean isShowingPopup() { |
| 438 |
return getListPopupWindow().isShowing(); |
| 439 |
} |
| 440 |
|
| 441 |
@Override |
| 442 |
protected void onAttachedToWindow() { |
| 443 |
super.onAttachedToWindow(); |
| 444 |
ActivityChooserModel dataModel = mAdapter.getDataModel(); |
| 445 |
if (dataModel != null) { |
| 446 |
dataModel.registerObserver(mModelDataSetOberver); |
| 447 |
} |
| 448 |
mIsAttachedToWindow = true; |
| 449 |
} |
| 450 |
|
| 451 |
@Override |
| 452 |
protected void onDetachedFromWindow() { |
| 453 |
super.onDetachedFromWindow(); |
| 454 |
ActivityChooserModel dataModel = mAdapter.getDataModel(); |
| 455 |
if (dataModel != null) { |
| 456 |
dataModel.unregisterObserver(mModelDataSetOberver); |
| 457 |
} |
| 458 |
ViewTreeObserver viewTreeObserver = getViewTreeObserver(); |
| 459 |
if (viewTreeObserver.isAlive()) { |
| 460 |
viewTreeObserver.removeOnGlobalLayoutListener(mOnGlobalLayoutListener); |
| 461 |
} |
| 462 |
if (isShowingPopup()) { |
| 463 |
dismissPopup(); |
| 464 |
} |
| 465 |
mIsAttachedToWindow = false; |
| 466 |
} |
| 467 |
|
| 468 |
@Override |
| 469 |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 470 |
View child = mActivityChooserContent; |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
if (mDefaultActivityButton.getVisibility() != VISIBLE) { |
| 475 |
heightMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec), |
| 476 |
MeasureSpec.EXACTLY); |
| 477 |
} |
| 478 |
measureChild(child, widthMeasureSpec, heightMeasureSpec); |
| 479 |
setMeasuredDimension(child.getMeasuredWidth(), child.getMeasuredHeight()); |
| 480 |
} |
| 481 |
|
| 482 |
@Override |
| 483 |
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 484 |
mActivityChooserContent.layout(0, 0, right - left, bottom - top); |
| 485 |
if (!isShowingPopup()) { |
| 486 |
dismissPopup(); |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
public ActivityChooserModel getDataModel() { |
| 491 |
return mAdapter.getDataModel(); |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
public void setOnDismissListener(PopupWindow.OnDismissListener listener) { |
| 500 |
mOnDismissListener = listener; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
public void setInitialActivityCount(int itemCount) { |
| 512 |
mInitialActivityCount = itemCount; |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
public void setDefaultActionButtonContentDescription(@StringRes int resourceId) { |
| 526 |
mDefaultActionButtonContentDescription = resourceId; |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
private ListPopupWindow getListPopupWindow() { |
| 535 |
if (mListPopupWindow == null) { |
| 536 |
mListPopupWindow = new ListPopupWindow(getContext()); |
| 537 |
mListPopupWindow.setAdapter(mAdapter); |
| 538 |
mListPopupWindow.setAnchorView(ActivityChooserView.this); |
| 539 |
mListPopupWindow.setModal(true); |
| 540 |
mListPopupWindow.setOnItemClickListener(mCallbacks); |
| 541 |
mListPopupWindow.setOnDismissListener(mCallbacks); |
| 542 |
} |
| 543 |
return mListPopupWindow; |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
private void updateAppearance() { |
| 550 |
|
| 551 |
if (mAdapter.getCount() > 0) { |
| 552 |
mExpandActivityOverflowButton.setEnabled(true); |
| 553 |
} else { |
| 554 |
mExpandActivityOverflowButton.setEnabled(false); |
| 555 |
} |
| 556 |
|
| 557 |
final int activityCount = mAdapter.getActivityCount(); |
| 558 |
final int historySize = mAdapter.getHistorySize(); |
| 559 |
if (activityCount==1 || activityCount > 1 && historySize > 0) { |
| 560 |
mDefaultActivityButton.setVisibility(VISIBLE); |
| 561 |
ResolveInfo activity = mAdapter.getDefaultActivity(); |
| 562 |
PackageManager packageManager = mContext.getPackageManager(); |
| 563 |
mDefaultActivityButtonImage.setImageDrawable(activity.loadIcon(packageManager)); |
| 564 |
if (mDefaultActionButtonContentDescription != 0) { |
| 565 |
CharSequence label = activity.loadLabel(packageManager); |
| 566 |
String contentDescription = mContext.getString( |
| 567 |
mDefaultActionButtonContentDescription, label); |
| 568 |
mDefaultActivityButton.setContentDescription(contentDescription); |
| 569 |
} |
| 570 |
} else { |
| 571 |
mDefaultActivityButton.setVisibility(View.GONE); |
| 572 |
} |
| 573 |
|
| 574 |
if (mDefaultActivityButton.getVisibility() == VISIBLE) { |
| 575 |
mActivityChooserContent.setBackground(mActivityChooserContentBackground); |
| 576 |
} else { |
| 577 |
mActivityChooserContent.setBackground(null); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
private class Callbacks implements AdapterView.OnItemClickListener, |
| 585 |
View.OnClickListener, View.OnLongClickListener, PopupWindow.OnDismissListener { |
| 586 |
|
| 587 |
|
| 588 |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 589 |
ActivityChooserViewAdapter adapter = (ActivityChooserViewAdapter) parent.getAdapter(); |
| 590 |
final int itemViewType = adapter.getItemViewType(position); |
| 591 |
switch (itemViewType) { |
| 592 |
case ActivityChooserViewAdapter.ITEM_VIEW_TYPE_FOOTER: { |
| 593 |
showPopupUnchecked(ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED); |
| 594 |
} break; |
| 595 |
case ActivityChooserViewAdapter.ITEM_VIEW_TYPE_ACTIVITY: { |
| 596 |
dismissPopup(); |
| 597 |
if (mIsSelectingDefaultActivity) { |
| 598 |
|
| 599 |
if (position > 0) { |
| 600 |
mAdapter.getDataModel().setDefaultActivity(position); |
| 601 |
} |
| 602 |
} else { |
| 603 |
|
| 604 |
|
| 605 |
position = mAdapter.getShowDefaultActivity() ? position : position + 1; |
| 606 |
Intent launchIntent = mAdapter.getDataModel().chooseActivity(position); |
| 607 |
if (launchIntent != null) { |
| 608 |
launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); |
| 609 |
ResolveInfo resolveInfo = mAdapter.getDataModel().getActivity(position); |
| 610 |
startActivity(launchIntent, resolveInfo); |
| 611 |
} |
| 612 |
} |
| 613 |
} break; |
| 614 |
default: |
| 615 |
throw new IllegalArgumentException(); |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
public void onClick(View view) { |
| 621 |
if (view == mDefaultActivityButton) { |
| 622 |
dismissPopup(); |
| 623 |
ResolveInfo defaultActivity = mAdapter.getDefaultActivity(); |
| 624 |
final int index = mAdapter.getDataModel().getActivityIndex(defaultActivity); |
| 625 |
Intent launchIntent = mAdapter.getDataModel().chooseActivity(index); |
| 626 |
if (launchIntent != null) { |
| 627 |
launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); |
| 628 |
startActivity(launchIntent, defaultActivity); |
| 629 |
} |
| 630 |
} else if (view == mExpandActivityOverflowButton) { |
| 631 |
mIsSelectingDefaultActivity = false; |
| 632 |
showPopupUnchecked(mInitialActivityCount); |
| 633 |
} else { |
| 634 |
throw new IllegalArgumentException(); |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
@Override |
| 640 |
public boolean onLongClick(View view) { |
| 641 |
if (view == mDefaultActivityButton) { |
| 642 |
if (mAdapter.getCount() > 0) { |
| 643 |
mIsSelectingDefaultActivity = true; |
| 644 |
showPopupUnchecked(mInitialActivityCount); |
| 645 |
} |
| 646 |
} else { |
| 647 |
throw new IllegalArgumentException(); |
| 648 |
} |
| 649 |
return true; |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
public void onDismiss() { |
| 654 |
notifyOnDismissListener(); |
| 655 |
if (mProvider != null) { |
| 656 |
mProvider.subUiVisibilityChanged(false); |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
private void notifyOnDismissListener() { |
| 661 |
if (mOnDismissListener != null) { |
| 662 |
mOnDismissListener.onDismiss(); |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
private void startActivity(Intent intent, ResolveInfo resolveInfo) { |
| 667 |
try { |
| 668 |
mContext.startActivity(intent); |
| 669 |
} catch (RuntimeException re) { |
| 670 |
CharSequence appLabel = resolveInfo.loadLabel(mContext.getPackageManager()); |
| 671 |
String message = mContext.getString( |
| 672 |
R.string.activitychooserview_choose_application_error, appLabel); |
| 673 |
Log.e(LOG_TAG, message); |
| 674 |
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show(); |
| 675 |
} |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
private class ActivityChooserViewAdapter extends BaseAdapter { |
| 683 |
|
| 684 |
public static final int MAX_ACTIVITY_COUNT_UNLIMITED = Integer.MAX_VALUE; |
| 685 |
|
| 686 |
public static final int MAX_ACTIVITY_COUNT_DEFAULT = 4; |
| 687 |
|
| 688 |
private static final int ITEM_VIEW_TYPE_ACTIVITY = 0; |
| 689 |
|
| 690 |
private static final int ITEM_VIEW_TYPE_FOOTER = 1; |
| 691 |
|
| 692 |
private static final int ITEM_VIEW_TYPE_COUNT = 3; |
| 693 |
|
| 694 |
private ActivityChooserModel mDataModel; |
| 695 |
|
| 696 |
private int mMaxActivityCount = MAX_ACTIVITY_COUNT_DEFAULT; |
| 697 |
|
| 698 |
private boolean mShowDefaultActivity; |
| 699 |
|
| 700 |
private boolean mHighlightDefaultActivity; |
| 701 |
|
| 702 |
private boolean mShowFooterView; |
| 703 |
|
| 704 |
public void setDataModel(ActivityChooserModel dataModel) { |
| 705 |
ActivityChooserModel oldDataModel = mAdapter.getDataModel(); |
| 706 |
if (oldDataModel != null && isShown()) { |
| 707 |
oldDataModel.unregisterObserver(mModelDataSetOberver); |
| 708 |
} |
| 709 |
mDataModel = dataModel; |
| 710 |
if (dataModel != null && isShown()) { |
| 711 |
dataModel.registerObserver(mModelDataSetOberver); |
| 712 |
} |
| 713 |
notifyDataSetChanged(); |
| 714 |
} |
| 715 |
|
| 716 |
@Override |
| 717 |
public int getItemViewType(int position) { |
| 718 |
if (mShowFooterView && position == getCount() - 1) { |
| 719 |
return ITEM_VIEW_TYPE_FOOTER; |
| 720 |
} else { |
| 721 |
return ITEM_VIEW_TYPE_ACTIVITY; |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
@Override |
| 726 |
public int getViewTypeCount() { |
| 727 |
return ITEM_VIEW_TYPE_COUNT; |
| 728 |
} |
| 729 |
|
| 730 |
public int getCount() { |
| 731 |
int count = 0; |
| 732 |
int activityCount = mDataModel.getActivityCount(); |
| 733 |
if (!mShowDefaultActivity && mDataModel.getDefaultActivity() != null) { |
| 734 |
activityCount--; |
| 735 |
} |
| 736 |
count = Math.min(activityCount, mMaxActivityCount); |
| 737 |
if (mShowFooterView) { |
| 738 |
count++; |
| 739 |
} |
| 740 |
return count; |
| 741 |
} |
| 742 |
|
| 743 |
public Object getItem(int position) { |
| 744 |
final int itemViewType = getItemViewType(position); |
| 745 |
switch (itemViewType) { |
| 746 |
case ITEM_VIEW_TYPE_FOOTER: |
| 747 |
return null; |
| 748 |
case ITEM_VIEW_TYPE_ACTIVITY: |
| 749 |
if (!mShowDefaultActivity && mDataModel.getDefaultActivity() != null) { |
| 750 |
position++; |
| 751 |
} |
| 752 |
return mDataModel.getActivity(position); |
| 753 |
default: |
| 754 |
throw new IllegalArgumentException(); |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
public long getItemId(int position) { |
| 759 |
return position; |
| 760 |
} |
| 761 |
|
| 762 |
public View getView(int position, View convertView, ViewGroup parent) { |
| 763 |
final int itemViewType = getItemViewType(position); |
| 764 |
switch (itemViewType) { |
| 765 |
case ITEM_VIEW_TYPE_FOOTER: |
| 766 |
if (convertView == null || convertView.getId() != ITEM_VIEW_TYPE_FOOTER) { |
| 767 |
convertView = LayoutInflater.from(getContext()).inflate( |
| 768 |
R.layout.activity_chooser_view_list_item, parent, false); |
| 769 |
convertView.setId(ITEM_VIEW_TYPE_FOOTER); |
| 770 |
TextView titleView = convertView.findViewById(R.id.title); |
| 771 |
titleView.setText(mContext.getString( |
| 772 |
R.string.activity_chooser_view_see_all)); |
| 773 |
} |
| 774 |
return convertView; |
| 775 |
case ITEM_VIEW_TYPE_ACTIVITY: |
| 776 |
if (convertView == null || convertView.getId() != R.id.list_item) { |
| 777 |
convertView = LayoutInflater.from(getContext()).inflate( |
| 778 |
R.layout.activity_chooser_view_list_item, parent, false); |
| 779 |
} |
| 780 |
PackageManager packageManager = mContext.getPackageManager(); |
| 781 |
|
| 782 |
ImageView iconView = convertView.findViewById(R.id.icon); |
| 783 |
ResolveInfo activity = (ResolveInfo) getItem(position); |
| 784 |
iconView.setImageDrawable(activity.loadIcon(packageManager)); |
| 785 |
|
| 786 |
TextView titleView = convertView.findViewById(R.id.title); |
| 787 |
titleView.setText(activity.loadLabel(packageManager)); |
| 788 |
|
| 789 |
if (mShowDefaultActivity && position == 0 && mHighlightDefaultActivity) { |
| 790 |
convertView.setActivated(true); |
| 791 |
} else { |
| 792 |
convertView.setActivated(false); |
| 793 |
} |
| 794 |
return convertView; |
| 795 |
default: |
| 796 |
throw new IllegalArgumentException(); |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
public int measureContentWidth() { |
| 801 |
|
| 802 |
|
| 803 |
final int oldMaxActivityCount = mMaxActivityCount; |
| 804 |
mMaxActivityCount = MAX_ACTIVITY_COUNT_UNLIMITED; |
| 805 |
|
| 806 |
int contentWidth = 0; |
| 807 |
View itemView = null; |
| 808 |
|
| 809 |
final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 810 |
final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 811 |
final int count = getCount(); |
| 812 |
|
| 813 |
for (int i = 0; i < count; i++) { |
| 814 |
itemView = getView(i, itemView, null); |
| 815 |
itemView.measure(widthMeasureSpec, heightMeasureSpec); |
| 816 |
contentWidth = Math.max(contentWidth, itemView.getMeasuredWidth()); |
| 817 |
} |
| 818 |
|
| 819 |
mMaxActivityCount = oldMaxActivityCount; |
| 820 |
|
| 821 |
return contentWidth; |
| 822 |
} |
| 823 |
|
| 824 |
public void setMaxActivityCount(int maxActivityCount) { |
| 825 |
if (mMaxActivityCount != maxActivityCount) { |
| 826 |
mMaxActivityCount = maxActivityCount; |
| 827 |
notifyDataSetChanged(); |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
public ResolveInfo getDefaultActivity() { |
| 832 |
return mDataModel.getDefaultActivity(); |
| 833 |
} |
| 834 |
|
| 835 |
public void setShowFooterView(boolean showFooterView) { |
| 836 |
if (mShowFooterView != showFooterView) { |
| 837 |
mShowFooterView = showFooterView; |
| 838 |
notifyDataSetChanged(); |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
public int getActivityCount() { |
| 843 |
return mDataModel.getActivityCount(); |
| 844 |
} |
| 845 |
|
| 846 |
public int getHistorySize() { |
| 847 |
return mDataModel.getHistorySize(); |
| 848 |
} |
| 849 |
|
| 850 |
public ActivityChooserModel getDataModel() { |
| 851 |
return mDataModel; |
| 852 |
} |
| 853 |
|
| 854 |
public void setShowDefaultActivity(boolean showDefaultActivity, |
| 855 |
boolean highlightDefaultActivity) { |
| 856 |
if (mShowDefaultActivity != showDefaultActivity |
| 857 |
|| mHighlightDefaultActivity != highlightDefaultActivity) { |
| 858 |
mShowDefaultActivity = showDefaultActivity; |
| 859 |
mHighlightDefaultActivity = highlightDefaultActivity; |
| 860 |
notifyDataSetChanged(); |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
public boolean getShowDefaultActivity() { |
| 865 |
return mShowDefaultActivity; |
| 866 |
} |
| 867 |
} |
| 868 |
} |