gt:     if (recipientId != null) {
context: /*
   * Copyright (C) 2014-2017 Open Whisper Systems
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   */
  
  package org.thoughtcrime.securesms.sharing;
  
  import android.content.Intent;
  import android.net.Uri;
  import android.os.Bundle;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.ImageView;
  import android.widget.Toast;
  
  import androidx.annotation.NonNull;
  import androidx.annotation.Nullable;
  import androidx.annotation.WorkerThread;
  import androidx.appcompat.app.ActionBar;
  import androidx.appcompat.app.AlertDialog;
  import androidx.appcompat.widget.Toolbar;
  import androidx.constraintlayout.widget.ConstraintLayout;
  import androidx.core.content.pm.ShortcutInfoCompat;
  import androidx.core.content.pm.ShortcutManagerCompat;
  import androidx.core.util.Consumer;
  import androidx.lifecycle.ViewModelProviders;
  import androidx.recyclerview.widget.RecyclerView;
  
  import com.annimon.stream.Collectors;
  import com.annimon.stream.Stream;
  
  import org.signal.core.util.logging.Log;
  import org.thoughtcrime.securesms.ContactSelectionListFragment;
  import org.thoughtcrime.securesms.PassphraseRequiredActivity;
  import org.thoughtcrime.securesms.R;
  import org.thoughtcrime.securesms.components.SearchToolbar;
  import org.thoughtcrime.securesms.contacts.ContactsCursorLoader.DisplayMode;
  import org.thoughtcrime.securesms.conversation.ConversationIntents;
  import org.thoughtcrime.securesms.database.SignalDatabase;
  import org.thoughtcrime.securesms.database.ThreadDatabase;
  import org.thoughtcrime.securesms.mediasend.Media;
  import org.thoughtcrime.securesms.mediasend.v2.MediaSelectionActivity;
  import org.thoughtcrime.securesms.recipients.Recipient;
  import org.thoughtcrime.securesms.recipients.RecipientId;
  import org.thoughtcrime.securesms.sharing.interstitial.ShareInterstitialActivity;
  import org.thoughtcrime.securesms.util.ConversationUtil;
  import org.thoughtcrime.securesms.util.DynamicLanguage;
  import org.thoughtcrime.securesms.util.DynamicNoActionBarTheme;
  import org.thoughtcrime.securesms.util.DynamicTheme;
  import org.thoughtcrime.securesms.util.FeatureFlags;
  import org.thoughtcrime.securesms.util.LifecycleDisposable;
  import org.thoughtcrime.securesms.util.Util;
  import org.thoughtcrime.securesms.util.ViewUtil;
  import org.thoughtcrime.securesms.util.concurrent.SimpleTask;
  import org.thoughtcrime.securesms.util.views.SimpleProgressDialog;
  import org.whispersystems.libsignal.util.guava.Optional;
  
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Map;
  import java.util.Objects;
  import java.util.Set;
  import java.util.concurrent.atomic.AtomicReference;
  
  import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
  import io.reactivex.rxjava3.schedulers.Schedulers;
  
  /**
   * Entry point for sharing content into the app.
   *
   * Handles contact selection when necessary, but also serves as an entry point for when the contact
   * is known (such as choosing someone in a direct share).
   */
  public class ShareActivity extends PassphraseRequiredActivity
      implements ContactSelectionListFragment.OnContactSelectedListener,
      ContactSelectionListFragment.OnSelectionLimitReachedListener
  {
    private static final String TAG = Log.tag(ShareActivity.class);
  
    private static final short RESULT_TEXT_CONFIRMATION  = 1;
    private static final short RESULT_MEDIA_CONFIRMATION = 2;
  
    public static final String EXTRA_THREAD_ID          = "thread_id";
    public static final String EXTRA_RECIPIENT_ID       = "recipient_id";
    public static final String EXTRA_DISTRIBUTION_TYPE  = "distribution_type";
  
    private final DynamicTheme    dynamicTheme    = new DynamicNoActionBarTheme();
    private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
  
    private ConstraintLayout             shareContainer;
    private ContactSelectionListFragment contactsFragment;
    private SearchToolbar                searchToolbar;
    private ImageView                    searchAction;
    private View                         shareConfirm;
    private RecyclerView                 contactsRecycler;
    private View                         contactsRecyclerDivider;
    private ShareSelectionAdapter        adapter;
    private boolean                      disallowMultiShare;
  
    private ShareIntents.Args args;
    private ShareViewModel    viewModel;
  
    private final LifecycleDisposable disposables = new LifecycleDisposable();
  
    @Override
    protected void onPreCreate() {
      dynamicTheme.onCreate(this);
      dynamicLanguage.onCreate(this);
    }
  
    @Override
    protected void onCreate(Bundle icicle, boolean ready) {
      setContentView(R.layout.share_activity);
  
      disposables.bindTo(getLifecycle());
  
      initializeArgs();
      initializeViewModel();
      initializeMedia();
      initializeIntent();
      initializeToolbar();
      initializeResources();
      initializeSearch();
    }
  
    @Override
    public void onResume() {
      Log.i(TAG, "onResume()");
      super.onResume();
      dynamicTheme.onResume(this);
      dynamicLanguage.onResume(this);
      handleDirectShare();
    }
  
    @Override
    public void onStop() {
      super.onStop();
  
      if (!isFinishing() && !viewModel.isMultiShare()) {
        finish();
      }
    }
  
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      if (item.getItemId() == android.R.id.home) {
        onBackPressed();
        return true;
      } else {
        return super.onOptionsItemSelected(item);
      }
    }
  
    @Override
    public void onBackPressed() {
      if (searchToolbar.isVisible()) searchToolbar.collapse();
      else                           super.onBackPressed();
    }
  
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
      if (resultCode == RESULT_OK) {
        switch (requestCode) {
          case RESULT_MEDIA_CONFIRMATION:
          case RESULT_TEXT_CONFIRMATION:
            viewModel.onSuccessfulShare();
            finish();
            break;
          default:
            super.onActivityResult(requestCode, resultCode, data);
        }
      } else {
        shareConfirm.setClickable(true);
        super.onActivityResult(requestCode, resultCode, data);
      }
    }
  
    @Override
    public void onBeforeContactSelected(Optional<RecipientId> recipientId, String number, java.util.function.Consumer<Boolean> callback) {
      if (disallowMultiShare) {
        Toast.makeText(this, R.string.ShareActivity__sharing_to_multiple_chats_is, Toast.LENGTH_LONG).show();
        callback.accept(false);
      } else {
        disposables.add(viewModel.onContactSelected(new ShareContact(recipientId, number))
                                 .subscribeOn(Schedulers.io())
                                 .observeOn(AndroidSchedulers.mainThread())
                                 .subscribe(result -> {
                                   switch (result) {
                                     case TRUE:
                                       callback.accept(true);
                                       break;
                                     case FALSE:
                                       callback.accept(false);
                                       break;
                                     case FALSE_AND_SHOW_PERMISSION_TOAST:
                                       Toast.makeText(this, R.string.ShareActivity_you_do_not_have_permission_to_send_to_this_group, Toast.LENGTH_SHORT).show();
                                       callback.accept(false);
                                       break;
                                     case FALSE_AND_SHOW_SMS_MULTISELECT_TOAST:
                                       Toast.makeText(this, R.string.ShareActivity__sharing_to_multiple_chats_is, Toast.LENGTH_LONG).show();
                                       callback.accept(false);
                                       break;
                                   }
                                 }));
      }
    }
  
    @Override
    public void onContactDeselected(@NonNull Optional<RecipientId> recipientId, String number) {
      viewModel.onContactDeselected(new ShareContact(recipientId, number));
    }
  
    @Override
    public void onSelectionChanged() {
    }
  
    private void animateInSelection() {
      contactsRecyclerDivider.animate()
                             .alpha(1f)
                             .translationY(0);
      contactsRecycler.animate()
                      .alpha(1f)
                      .translationY(0);
    }
  
    private void animateOutSelection() {
      contactsRecyclerDivider.animate()
                             .alpha(0f)
                             .translationY(ViewUtil.dpToPx(48));
      contactsRecycler.animate()
                      .alpha(0f)
                      .translationY(ViewUtil.dpToPx(48));
    }
  
    private void initializeIntent() {
      if (!getIntent().hasExtra(ContactSelectionListFragment.DISPLAY_MODE)) {
        int mode = DisplayMode.FLAG_PUSH | DisplayMode.FLAG_ACTIVE_GROUPS | DisplayMode.FLAG_SELF | DisplayMode.FLAG_HIDE_NEW;
  
        if (Util.isDefaultSmsProvider(this))  {
          mode |= DisplayMode.FLAG_SMS;
        }
  
        mode |= DisplayMode.FLAG_HIDE_GROUPS_V1;
  
        getIntent().putExtra(ContactSelectionListFragment.DISPLAY_MODE, mode);
      }
  
      getIntent().putExtra(ContactSelectionListFragment.REFRESHABLE, false);
      getIntent().putExtra(ContactSelectionListFragment.RECENTS, true);
      getIntent().putExtra(ContactSelectionListFragment.SELECTION_LIMITS, FeatureFlags.shareSelectionLimit());
      getIntent().putExtra(ContactSelectionListFragment.HIDE_COUNT, true);
      getIntent().putExtra(ContactSelectionListFragment.DISPLAY_CHIPS, false);
      getIntent().putExtra(ContactSelectionListFragment.CAN_SELECT_SELF, true);
      getIntent().putExtra(ContactSelectionListFragment.RV_CLIP, false);
      getIntent().putExtra(ContactSelectionListFragment.RV_PADDING_BOTTOM, ViewUtil.dpToPx(48));
    }
  
    private void handleDirectShare() {
      boolean isDirectShare      = getIntent().hasExtra(ShortcutManagerCompat.EXTRA_SHORTCUT_ID);
      boolean intentHasRecipient = getIntent().hasExtra(EXTRA_RECIPIENT_ID);
  
      if (intentHasRecipient) {
        handleDestination();
      } else if (isDirectShare) {
        String  extraShortcutId = getIntent().getStringExtra(ShortcutManagerCompat.EXTRA_SHORTCUT_ID);
        SimpleTask.run(getLifecycle(),
            () -> getDirectShareExtras(extraShortcutId),
            extras -> {
              if (extras != null) {
                addShortcutExtrasToIntent(extras);
                handleDestination();
              }
            }
        );
      }
    }
  
    /**
     * @param extraShortcutId EXTRA_SHORTCUT_ID String as included in direct share intent
     * @return shortcutExtras or null
     */
    @WorkerThread
    private @Nullable Bundle getDirectShareExtras(@NonNull String extraShortcutId) {
      Bundle shortcutExtras = getShortcutExtrasFor(extraShortcutId);
      if (shortcutExtras == null) {
        shortcutExtras = createExtrasFromExtraShortcutId(extraShortcutId);
      }
      return shortcutExtras;
    }
  
    /**
     * Search for dynamic shortcut originally declared in {@link ConversationUtil} and return extras
     *
     * @param extraShortcutId EXTRA_SHORTCUT_ID String as included in direct share intent
     * @return shortcutExtras or null
     */
    @WorkerThread
    private @Nullable Bundle getShortcutExtrasFor(@NonNull String extraShortcutId) {
      List<ShortcutInfoCompat> shortcuts = ShortcutManagerCompat.getDynamicShortcuts(this);
      for (ShortcutInfoCompat shortcutInfo : shortcuts) {
        if (extraShortcutId.equals(shortcutInfo.getId())) {
          return shortcutInfo.getIntent().getExtras();
        }
      }
      return null;
    }
  
    /**
     * @param extraShortcutId EXTRA_SHORTCUT_ID string as included in direct share intent
     */
    @WorkerThread
    private @Nullable Bundle createExtrasFromExtraShortcutId(@NonNull String extraShortcutId) {
      Bundle      extras           = new Bundle();
      RecipientId recipientId      = ConversationUtil.getRecipientId(extraShortcutId);
      Long        threadId         = null;
      int         distributionType = ThreadDatabase.DistributionTypes.DEFAULT;
  
  