content: package androidx.appcompat.widget;
  
  import android.content.Context;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.content.pm.ResolveInfo;
  import android.os.Build;
  import android.util.TypedValue;
  import android.view.MenuItem;
  import android.view.SubMenu;
  import android.view.View;
  import androidx.appcompat.R;
  import androidx.appcompat.content.res.AppCompatResources;
  import androidx.appcompat.widget.ActivityChooserModel;
  import androidx.core.view.ActionProvider;
  
  public class ShareActionProvider extends ActionProvider {
      private static final int DEFAULT_INITIAL_ACTIVITY_COUNT = 4;
      public static final String DEFAULT_SHARE_HISTORY_FILE_NAME = "share_history.xml";
      final Context mContext;
      private int mMaxShownActivityCount = 4;
      private ActivityChooserModel.OnChooseActivityListener mOnChooseActivityListener;
      private final ShareMenuItemOnMenuItemClickListener mOnMenuItemClickListener = new ShareMenuItemOnMenuItemClickListener();
      OnShareTargetSelectedListener mOnShareTargetSelectedListener;
      String mShareHistoryFileName = DEFAULT_SHARE_HISTORY_FILE_NAME;
  
      public interface OnShareTargetSelectedListener {
          boolean onShareTargetSelected(ShareActionProvider shareActionProvider, Intent intent);
      }
  
      private class ShareActivityChooserModelPolicy implements ActivityChooserModel.OnChooseActivityListener {
          ShareActivityChooserModelPolicy() {
          }
  
          public boolean onChooseActivity(ActivityChooserModel host, Intent intent) {
              if (ShareActionProvider.this.mOnShareTargetSelectedListener == null) {
                  return false;
              }
              ShareActionProvider.this.mOnShareTargetSelectedListener.onShareTargetSelected(ShareActionProvider.this, intent);
              return false;
          }
      }
  
      private class ShareMenuItemOnMenuItemClickListener implements MenuItem.OnMenuItemClickListener {
          ShareMenuItemOnMenuItemClickListener() {
          }
  
          public boolean onMenuItemClick(MenuItem item) {
              Intent chooseActivity = ActivityChooserModel.get(ShareActionProvider.this.mContext, ShareActionProvider.this.mShareHistoryFileName).chooseActivity(item.getItemId());
              if (chooseActivity == null) {
                  return true;
              }
              String action = chooseActivity.getAction();
              if ("android.intent.action.SEND".equals(action) || "android.intent.action.SEND_MULTIPLE".equals(action)) {
                  ShareActionProvider.this.updateIntent(chooseActivity);
              }
              ShareActionProvider.this.mContext.startActivity(chooseActivity);
              return true;
          }
      }
  
      public ShareActionProvider(Context context) {
          super(context);
          this.mContext = context;
      }
  
      private void setActivityChooserPolicyIfNeeded() {
          if (this.mOnShareTargetSelectedListener != null) {
              if (this.mOnChooseActivityListener == null) {
                  this.mOnChooseActivityListener = new ShareActivityChooserModelPolicy();
              }
              ActivityChooserModel.get(this.mContext, this.mShareHistoryFileName).setOnChooseActivityListener(this.mOnChooseActivityListener);
          }
      }
  
      public boolean hasSubMenu() {
          return true;
      }
  
      public View onCreateActionView() {
          ActivityChooserView activityChooserView = new ActivityChooserView(this.mContext);
          if (!activityChooserView.isInEditMode()) {
              activityChooserView.setActivityChooserModel(ActivityChooserModel.get(this.mContext, this.mShareHistoryFileName));
          }
          TypedValue typedValue = new TypedValue();
          this.mContext.getTheme().resolveAttribute(R.attr.actionModeShareDrawable, typedValue, true);
          activityChooserView.setExpandActivityOverflowButtonDrawable(AppCompatResources.getDrawable(this.mContext, typedValue.resourceId));
          activityChooserView.setProvider(this);
          activityChooserView.setDefaultActionButtonContentDescription(R.string.abc_shareactionprovider_share_with_application);
          activityChooserView.setExpandActivityOverflowButtonContentDescription(R.string.abc_shareactionprovider_share_with);
          return activityChooserView;
      }
  
      public void onPrepareSubMenu(SubMenu subMenu) {
          subMenu.clear();
          ActivityChooserModel activityChooserModel = ActivityChooserModel.get(this.mContext, this.mShareHistoryFileName);
          PackageManager packageManager = this.mContext.getPackageManager();
          int activityCount = activityChooserModel.getActivityCount();
          int min = Math.min(activityCount, this.mMaxShownActivityCount);
          for (int i = 0; i < min; i++) {
              ResolveInfo activity = activityChooserModel.getActivity(i);
              subMenu.add(0, i, i, activity.loadLabel(packageManager)).setIcon(activity.loadIcon(packageManager)).setOnMenuItemClickListener(this.mOnMenuItemClickListener);
          }
          if (min < activityCount) {
              SubMenu addSubMenu = subMenu.addSubMenu(0, min, min, this.mContext.getString(R.string.abc_activity_chooser_view_see_all));
              for (int i2 = 0; i2 < activityCount; i2++) {
                  ResolveInfo activity2 = activityChooserModel.getActivity(i2);
                  addSubMenu.add(0, i2, i2, activity2.loadLabel(packageManager)).setIcon(activity2.loadIcon(packageManager)).setOnMenuItemClickListener(this.mOnMenuItemClickListener);
              }
          }
      }
  
      public void setOnShareTargetSelectedListener(OnShareTargetSelectedListener listener) {
          this.mOnShareTargetSelectedListener = listener;
          setActivityChooserPolicyIfNeeded();
      }
  
      public void setShareHistoryFileName(String shareHistoryFile) {
          this.mShareHistoryFileName = shareHistoryFile;
          setActivityChooserPolicyIfNeeded();
      }
  
      public void setShareIntent(Intent shareIntent) {
          if (shareIntent != null) {
              String action = shareIntent.getAction();
              if ("android.intent.action.SEND".equals(action) || "android.intent.action.SEND_MULTIPLE".equals(action)) {
                  updateIntent(shareIntent);
              }
          }
          ActivityChooserModel.get(this.mContext, this.mShareHistoryFileName).setIntent(shareIntent);
      }
  
      /* access modifiers changed from: package-private */
      public void updateIntent(Intent intent) {
          if (Build.VERSION.SDK_INT >= 21) {
              intent.addFlags(134742016);
          } else {
              intent.addFlags(524288);
          }
      }
  }
  