content: package allen.town.focus.twitter.utils;
  
  import static androidx.browser.customtabs.CustomTabsIntent.SHARE_STATE_ON;
  
  import android.app.PendingIntent;
  import android.content.ActivityNotFoundException;
  import android.content.Context;
  import android.content.Intent;
  import android.net.Uri;
  
  import androidx.browser.customtabs.CustomTabsIntent;
  
  import allen.town.focus.twitter.api.APIKeys;
  import allen.town.focus.twitter.R;
  import allen.town.focus.twitter.settings.AppSettings;
  import allen.town.focus.twitter.activities.BrowserActivity;
  
  import java.util.Random;
  
  import allen.town.focus_common.util.ThemeUtils;
  import code.name.monkey.appthemehelper.ThemeStore;
  import xyz.klinker.android.article.ArticleIntent;
  
  /**
   * Used to handle URLs.
   *
   * Will redirect some to the external browser automatically (play store, youtube, twitter, periscope, meerkat)
   * Others will attempt to load a custom tab
   * If the tab fails, then it will open up the in app browser intent.
   *
   * This does not do anything with warming up the custom intent or starting a session.
   * It will simply display the page.
   */
  public class WebIntentBuilder {
  
      private static boolean JUST_RAN = false;
  
      private static final String PLAY_STORE = "play.google.com";
      private static final String YOUTUBE = "youtu";
      private static final String TWITTER = "twitter.com";
      private static final String PERISCOPE = "periscope.tv";
      private static final String MEERKAT = "mkr.tv";
      private static final String NEOGAF = "neogaf.com";
      private static final String FACEBOOK = "facebook.com";
      private static final String PIXIV = "pixiv.net";
  
      private static final String[] ALWAYS_EXTERNAL = new String[] {
              PLAY_STORE,
              YOUTUBE,
              TWITTER,
              PERISCOPE,
              MEERKAT,
              NEOGAF,
              FACEBOOK,
              PIXIV
      };
  
      private Context context;
      private AppSettings settings;
  
      private String webpage;
      private boolean forceExternal;
  
      private Intent intent;
      private CustomTabsIntent customTab;
      private ArticleIntent articleIntent;
  
      public WebIntentBuilder(Context context) {
          this.context = context;
          this.settings = AppSettings.getInstance(context);
      }
  
      public WebIntentBuilder setUrl(String url) {
          this.webpage = url;
          return this;
      }
  
      public WebIntentBuilder setShouldForceExternal(boolean forceExternal) {
          this.forceExternal = forceExternal;
          return this;
      }
  
      public WebIntentBuilder build() {
          if (webpage == null) {
              throw new RuntimeException("URL cannot be null.");
          }
  
          if (forceExternal || shouldAlwaysForceExternal(webpage) || settings.browserSelection.equals("external")) {
              // request the external browser
              intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webpage));
              intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          } else if (settings.browserSelection.equals("custom_tab")) {
              // add the share action
              Intent shareIntent = new Intent(Intent.ACTION_SEND);
              String extraText = webpage;
              shareIntent.putExtra(Intent.EXTRA_TEXT, extraText);
              shareIntent.setType("text/plain");
              Random random = new Random();
              PendingIntent pendingIntent = PendingIntent.getActivity(context, random.nextInt(Integer.MAX_VALUE), shareIntent, Utils.withImmutability(PendingIntent.FLAG_UPDATE_CURRENT));
  
              customTab = new CustomTabsIntent.Builder(null)
                      .setShowTitle(true)
                      .setShareState(SHARE_STATE_ON)
                      .enableUrlBarHiding()
                      .setToolbarColor(ThemeUtils.getColorFromAttr(context,R.attr.colorSurface))
                      .build();
          } else {
              // fallback to in app browser
              intent = new Intent(context, BrowserActivity.class);
              intent.putExtra("url", webpage);
              intent.setFlags(0);
          }
  
          return this;
      }
  
      public void start() {
          if (customTab != null) {
              customTab.launchUrl(context, Uri.parse(webpage));
          } else if (articleIntent != null) {
              if (!JUST_RAN) {
                  articleIntent.launchUrl(context, Uri.parse(webpage));
              }
  
              WebIntentBuilder.JUST_RAN = true;
              new Thread(() -> {
                  try {
                      Thread.sleep(1000);
                  } catch (InterruptedException e) { }
  
                  WebIntentBuilder.JUST_RAN = false;
              }).start();
          } else {
              try {
                  context.startActivity(intent);
              } catch (ActivityNotFoundException e) {
                  e.printStackTrace();
              }
          }
      }
  
      private boolean shouldAlwaysForceExternal(String url) {
          for (String s : ALWAYS_EXTERNAL)
              if (url.contains(s))
                  return true;
  
          return false;
      }
  }
  