Пример #1
0
  private void showSharePopup() {
    Intent intent = getShareIntent();

    Activity parent = getActivity();
    PackageManager packageManager = parent.getPackageManager();

    // Get a list of sharable options.
    List<ResolveInfo> shareOptions =
        packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);

    if (shareOptions.size() == 0) {
      return;
    }
    ArrayList<CharSequence> shareOptionTitles = new ArrayList<CharSequence>();
    ArrayList<Drawable> shareOptionIcons = new ArrayList<Drawable>();
    ArrayList<CharSequence> shareOptionThreeTitles = new ArrayList<CharSequence>();
    ArrayList<Drawable> shareOptionThreeIcons = new ArrayList<Drawable>();
    ArrayList<String> shareOptionPackageNames = new ArrayList<String>();
    ArrayList<String> shareOptionClassNames = new ArrayList<String>();

    for (int option_i = 0; option_i < shareOptions.size(); option_i++) {
      ResolveInfo option = shareOptions.get(option_i);
      CharSequence label = option.loadLabel(packageManager);
      Drawable icon = option.loadIcon(packageManager);
      shareOptionTitles.add(label);
      shareOptionIcons.add(icon);
      if (shareOptions.size() > 4 && option_i < 3) {
        shareOptionThreeTitles.add(label);
        shareOptionThreeIcons.add(icon);
      }
      shareOptionPackageNames.add(option.activityInfo.packageName);
      shareOptionClassNames.add(option.activityInfo.name);
    }
    if (shareOptionTitles.size() > 4) {
      shareOptionThreeTitles.add(getResources().getString(R.string.see_all));
      shareOptionThreeIcons.add(getResources().getDrawable(android.R.color.transparent));
    }

    if (mSharePopup != null) {
      mSharePopup.dismiss();
      mSharePopup = null;
    }
    mSharePopup = new ListPopupWindow(parent);
    mSharePopup.setAnchorView(mShareButton);
    mSharePopup.setModal(true);
    // This adapter to show the rest will be used to quickly repopulate if "See all..." is hit.
    ImageLabelAdapter showAllAdapter =
        new ImageLabelAdapter(
            parent,
            R.layout.popup_window_item,
            shareOptionTitles,
            shareOptionIcons,
            shareOptionPackageNames,
            shareOptionClassNames);
    if (shareOptionTitles.size() > 4) {
      mSharePopup.setAdapter(
          new ImageLabelAdapter(
              parent,
              R.layout.popup_window_item,
              shareOptionThreeTitles,
              shareOptionThreeIcons,
              shareOptionPackageNames,
              shareOptionClassNames,
              showAllAdapter));
    } else {
      mSharePopup.setAdapter(showAllAdapter);
    }

    mSharePopup.setOnItemClickListener(
        new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            CharSequence label = ((TextView) view.findViewById(R.id.title)).getText();
            if (label.equals(getResources().getString(R.string.see_all))) {
              mSharePopup.setAdapter(((ImageLabelAdapter) parent.getAdapter()).getShowAllAdapter());
              mSharePopup.show();
              return;
            }

            Intent intent = getShareIntent();
            ImageLabelAdapter adapter = (ImageLabelAdapter) parent.getAdapter();
            String packageName = adapter.getPackageName(position);
            String className = adapter.getClassName(position);
            intent.setClassName(packageName, className);
            startActivity(intent);
          }
        });
    mSharePopup.setOnDismissListener(
        new OnDismissListener() {
          @Override
          public void onDismiss() {
            mSharePopup = null;
          }
        });
    mSharePopup.setWidth((int) getResources().getDimension(R.dimen.popup_window_width));
    mSharePopup.show();
  }
Пример #2
0
  /**
   * Creates and shows the app menu anchored to the specified view.
   *
   * @param context The context of the AppMenu (ensure the proper theme is set on this context).
   * @param anchorView The anchor {@link View} of the {@link ListPopupWindow}.
   * @param isByPermanentButton Whether or not permanent hardware button triggered it. (oppose to
   *     software button or keyboard).
   * @param screenRotation Current device screen rotation.
   * @param visibleDisplayFrame The display area rect in which AppMenu is supposed to fit in.
   * @param screenHeight Current device screen height.
   * @param footerResourceId The resource id for a view to add to the end of the menu list. Can be 0
   *     if no such view is required.
   */
  void show(
      Context context,
      View anchorView,
      boolean isByPermanentButton,
      int screenRotation,
      Rect visibleDisplayFrame,
      int screenHeight,
      int footerResourceId) {
    mPopup = new ListPopupWindow(context, null, android.R.attr.popupMenuStyle);
    mPopup.setModal(true);
    mPopup.setAnchorView(anchorView);
    mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);

    int footerHeight = 0;
    if (footerResourceId != 0) {
      mPopup.setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW);
      View promptView = LayoutInflater.from(context).inflate(footerResourceId, null);
      mPopup.setPromptView(promptView);
      int measureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
      promptView.measure(measureSpec, measureSpec);
      footerHeight = promptView.getMeasuredHeight();
    }
    mPopup.setOnDismissListener(
        new OnDismissListener() {
          @Override
          public void onDismiss() {
            if (mPopup.getAnchorView() instanceof ImageButton) {
              ((ImageButton) mPopup.getAnchorView()).setSelected(false);
            }

            if (mMenuItemEnterAnimator != null) mMenuItemEnterAnimator.cancel();

            mHandler.appMenuDismissed();
            mHandler.onMenuVisibilityChanged(false);
          }
        });

    // Some OEMs don't actually let us change the background... but they still return the
    // padding of the new background, which breaks the menu height.  If we still have a
    // drawable here even though our style says @null we should use this padding instead...
    Drawable originalBgDrawable = mPopup.getBackground();

    // Need to explicitly set the background here.  Relying on it being set in the style caused
    // an incorrectly drawn background.
    if (isByPermanentButton) {
      mPopup.setBackgroundDrawable(
          ApiCompatibilityUtils.getDrawable(context.getResources(), R.drawable.menu_bg));
    } else {
      mPopup.setBackgroundDrawable(
          ApiCompatibilityUtils.getDrawable(context.getResources(), R.drawable.edge_menu_bg));
      mPopup.setAnimationStyle(R.style.OverflowMenuAnim);
    }

    // Turn off window animations for low end devices, and on Android M, which has built-in menu
    // animations.
    if (SysUtils.isLowEndDevice() || Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      mPopup.setAnimationStyle(0);
    }

    Rect bgPadding = new Rect();
    mPopup.getBackground().getPadding(bgPadding);

    int popupWidth =
        context.getResources().getDimensionPixelSize(R.dimen.menu_width)
            + bgPadding.left
            + bgPadding.right;

    mPopup.setWidth(popupWidth);

    mCurrentScreenRotation = screenRotation;
    mIsByPermanentButton = isByPermanentButton;

    // Extract visible items from the Menu.
    int numItems = mMenu.size();
    List<MenuItem> menuItems = new ArrayList<MenuItem>();
    for (int i = 0; i < numItems; ++i) {
      MenuItem item = mMenu.getItem(i);
      if (item.isVisible()) {
        menuItems.add(item);
      }
    }

    Rect sizingPadding = new Rect(bgPadding);
    if (isByPermanentButton && originalBgDrawable != null) {
      Rect originalPadding = new Rect();
      originalBgDrawable.getPadding(originalPadding);
      sizingPadding.top = originalPadding.top;
      sizingPadding.bottom = originalPadding.bottom;
    }

    // A List adapter for visible items in the Menu. The first row is added as a header to the
    // list view.
    mAdapter = new AppMenuAdapter(this, menuItems, LayoutInflater.from(context));
    mPopup.setAdapter(mAdapter);

    setMenuHeight(menuItems.size(), visibleDisplayFrame, screenHeight, sizingPadding, footerHeight);
    setPopupOffset(mPopup, mCurrentScreenRotation, visibleDisplayFrame, sizingPadding);
    mPopup.setOnItemClickListener(this);
    mPopup.show();
    mPopup.getListView().setItemsCanFocus(true);
    mPopup.getListView().setOnKeyListener(this);

    mHandler.onMenuVisibilityChanged(true);

    if (mVerticalFadeDistance > 0) {
      mPopup.getListView().setVerticalFadingEdgeEnabled(true);
      mPopup.getListView().setFadingEdgeLength(mVerticalFadeDistance);
    }

    // Don't animate the menu items for low end devices.
    if (!SysUtils.isLowEndDevice() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      mPopup
          .getListView()
          .addOnLayoutChangeListener(
              new View.OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(
                    View v,
                    int left,
                    int top,
                    int right,
                    int bottom,
                    int oldLeft,
                    int oldTop,
                    int oldRight,
                    int oldBottom) {
                  mPopup.getListView().removeOnLayoutChangeListener(this);
                  runMenuItemEnterAnimations();
                }
              });
    }
  }
Пример #3
0
  /**
   * Set listener for window dismissed. This listener will only be fired if the quickaction dialog
   * is dismissed by clicking outside the dialog or clicking on sticky item.
   */
  @Override
  public void setOnDismissListener(PopupMenu.OnDismissListener listener) {
    mWindow.setOnDismissListener(listener != null ? this : null);

    mDismissListener = listener;
  }