Search in sources :

Example 1 with CustomTabsIntent

use of androidx.browser.customtabs.CustomTabsIntent in project plaid by nickbutcher.

the class CustomTabActivityHelper method openCustomTab.

/**
 * Opens the URL on a Custom Tab if possible; otherwise falls back to opening it via
 * {@code Intent.ACTION_VIEW}
 *
 * @param activity The host activity
 * @param customTabsIntent a CustomTabsIntent to be used if Custom Tabs is available
 * @param uri the Uri to be opened
 */
public static void openCustomTab(Activity activity, CustomTabsIntent customTabsIntent, Uri uri) {
    String packageName = CustomTabsHelper.getPackageNameToUse(activity);
    // Custom Tabs installed. So, we fallback to a view intent
    if (packageName != null) {
        customTabsIntent.intent.setPackage(packageName);
        customTabsIntent.launchUrl(activity, uri);
    } else {
        activity.startActivity(new Intent(Intent.ACTION_VIEW, uri));
    }
}
Also used : CustomTabsIntent(androidx.browser.customtabs.CustomTabsIntent) Intent(android.content.Intent)

Example 2 with CustomTabsIntent

use of androidx.browser.customtabs.CustomTabsIntent in project Tusky by Vavassor.

the class LinkHelper method openLinkInCustomTab.

/**
 * tries to open a link in a custom tab
 * falls back to browser if not possible
 *
 * @param uri the uri to open
 * @param context context
 */
public static void openLinkInCustomTab(Uri uri, Context context) {
    int toolbarColor = ThemeUtils.getColor(context, R.attr.colorSurface);
    int navigationbarColor = ThemeUtils.getColor(context, android.R.attr.navigationBarColor);
    int navigationbarDividerColor = ThemeUtils.getColor(context, R.attr.dividerColor);
    CustomTabColorSchemeParams colorSchemeParams = new CustomTabColorSchemeParams.Builder().setToolbarColor(toolbarColor).setNavigationBarColor(navigationbarColor).setNavigationBarDividerColor(navigationbarDividerColor).build();
    CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().setDefaultColorSchemeParams(colorSchemeParams).setShowTitle(true).build();
    try {
        customTabsIntent.launchUrl(context, uri);
    } catch (ActivityNotFoundException e) {
        Log.w("LinkHelper", "Activity was not found for intent " + customTabsIntent);
        openLinkInBrowser(uri, context);
    }
}
Also used : CustomTabsIntent(androidx.browser.customtabs.CustomTabsIntent) ActivityNotFoundException(android.content.ActivityNotFoundException) CustomTabColorSchemeParams(androidx.browser.customtabs.CustomTabColorSchemeParams) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 3 with CustomTabsIntent

use of androidx.browser.customtabs.CustomTabsIntent in project kcanotify by antest1.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_vpn_main);
    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    assetManager = getAssets();
    dbHelper = new KcaDBHelper(getApplicationContext(), null, KCANOTIFY_DB_VERSION);
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(R.string.app_name);
    prefs.edit().putBoolean(PREF_SVC_ENABLED, KcaService.getServiceStatus()).apply();
    prefs.edit().putBoolean(PREF_VPN_ENABLED, KcaVpnService.checkOn()).apply();
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    downloader = KcaUtils.getInfoDownloader(getApplicationContext());
    int sniffer_mode = Integer.parseInt(getStringPreferences(getApplicationContext(), PREF_SNIFFER_MODE));
    vpnbtn = findViewById(R.id.vpnbtn);
    vpnbtn.setTextOff(getStringWithLocale(R.string.ma_vpn_toggleoff));
    vpnbtn.setTextOn(getStringWithLocale(R.string.ma_vpn_toggleon));
    vpnbtn.setText("PASSIVE");
    vpnbtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                JsonObject statProperties = new JsonObject();
                try {
                    final Intent prepare = VpnService.prepare(MainActivity.this);
                    if (prepare == null) {
                        // Log.i(TAG, "Prepare done");
                        onActivityResult(REQUEST_VPN, RESULT_OK, null);
                    } else {
                        startActivityForResult(prepare, REQUEST_VPN);
                    }
                    statProperties.addProperty("is_success", true);
                } catch (Throwable ex) {
                    // Prepare failed
                    statProperties.addProperty("is_success", false);
                    Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                }
                sendUserAnalytics(getApplicationContext(), START_SNIFFER, statProperties);
            } else {
                KcaVpnService.stop(VPN_STOP_REASON, MainActivity.this);
                prefs.edit().putBoolean(PREF_VPN_ENABLED, false).apply();
                sendUserAnalytics(getApplicationContext(), END_SNIFFER, null);
            }
        }
    });
    if (sniffer_mode == SNIFFER_ACTIVE) {
        vpnbtn.setEnabled(true);
    } else {
        vpnbtn.setEnabled(false);
    }
    Intent serviceIntent = new Intent(MainActivity.this, KcaService.class);
    svcbtn = findViewById(R.id.svcbtn);
    svcbtn.setTextOff(getStringWithLocale(R.string.ma_svc_toggleoff));
    svcbtn.setTextOn(getStringWithLocale(R.string.ma_svc_toggleon));
    svcbtn.setOnCheckedChangeListener((buttonView, isChecked) -> {
        if (isChecked) {
            if (!prefs.getBoolean(PREF_SVC_ENABLED, false)) {
                sendUserAnalytics(getApplicationContext(), START_SERVICE, null);
                loadTranslationData(getApplicationContext());
                serviceIntent.setAction(KcaService.KCASERVICE_START);
                if (Build.VERSION.SDK_INT >= 26) {
                    startForegroundService(serviceIntent);
                } else {
                    startService(serviceIntent);
                }
            }
        } else {
            serviceIntent.setAction(KcaService.KCASERVICE_STOP);
            startService(serviceIntent);
            sendUserAnalytics(getApplicationContext(), END_SERVICE, null);
            prefs.edit().putBoolean(PREF_SVC_ENABLED, false).apply();
        }
    });
    kcbtn = findViewById(R.id.kcbtn);
    kcbtn.setOnClickListener(v -> {
        String kcApp = getStringPreferences(getApplicationContext(), PREF_KC_PACKAGE);
        Intent kcIntent = getKcIntent(getApplicationContext());
        boolean is_kca_installed = false;
        if (!BuildConfig.DEBUG)
            is_kca_installed = (kcIntent != null);
        JsonObject statProperties = new JsonObject();
        statProperties.addProperty("browser", kcApp);
        statProperties.addProperty("sniffer", sniffer_mode);
        statProperties.addProperty("enabled", is_kca_installed ? 1 : 0);
        sendUserAnalytics(getApplicationContext(), RUN_KANCOLLE, statProperties);
        if (is_kca_installed) {
            kcIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(kcIntent);
            finish();
        } else {
            if (kcApp.equals(KC_PACKAGE_NAME)) {
                Toast.makeText(getApplicationContext(), getString(R.string.ma_toast_kancolle_not_installed), Toast.LENGTH_LONG).show();
            } else if (kcApp.equals(GOTO_PACKAGE_NAME)) {
                Toast.makeText(getApplicationContext(), getString(R.string.ma_toast_gotobrowser_not_installed), Toast.LENGTH_LONG).show();
            }
        }
    });
    kctoolbtn = findViewById(R.id.kcatoolbtn);
    kctoolbtn.setOnClickListener(v -> {
        Intent intent = new Intent(MainActivity.this, ToolsActivity.class);
        startActivity(intent);
    });
    kctoolbtn.setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.white), PorterDuff.Mode.SRC_ATOP);
    kcafairybtn = findViewById(R.id.kcafairybtn);
    boolean is_random_fairy = getBooleanPreferences(getApplicationContext(), PREF_FAIRY_RANDOM);
    if (is_random_fairy) {
        kcafairybtn.setImageResource(R.mipmap.ic_help);
    } else {
        String fairyIdValue = getStringPreferences(getApplicationContext(), PREF_FAIRY_ICON);
        String fairyPath = "noti_icon_".concat(fairyIdValue);
        KcaUtils.setFairyImageFromStorage(getApplicationContext(), fairyPath, kcafairybtn, 24);
    }
    kcafairybtn.setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.white), PorterDuff.Mode.SRC_ATOP);
    kcafairybtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(getApplicationContext())) {
            // Can not draw overlays: pass
            } else if (KcaService.getServiceStatus() && sHandler != null) {
                Bundle bundle = new Bundle();
                bundle.putString("url", KCA_API_FAIRY_RETURN);
                bundle.putString("data", "");
                Message sMsg = sHandler.obtainMessage();
                sMsg.setData(bundle);
                sHandler.sendMessage(sMsg);
            }
        }
    });
    textWarn = findViewById(R.id.textMainWarn);
    textWarn.setVisibility(View.GONE);
    String main_html = "";
    try {
        String locale = getStringPreferences(getApplicationContext(), PREF_KCA_LANGUAGE);
        InputStream ais = assetManager.open("main-".concat(getResourceLocaleCode(locale)).concat(".html"));
        byte[] bytes = ByteStreams.toByteArray(ais);
        main_html = new String(bytes);
    } catch (IOException e) {
        main_html = "Error loading html file.";
    }
    textMainUpdate = findViewById(R.id.textMainUpdate);
    textMainUpdate.setOnClickListener(v -> {
        startActivity(new Intent(this, UpdateCheckActivity.class));
    });
    textDescription = findViewById(R.id.textDescription);
    Spanned fromHtml = HtmlCompat.fromHtml(getApplicationContext(), main_html, 0);
    textDescription.setMovementMethod(LinkMovementMethod.getInstance());
    textDescription.setText(fromHtml);
    // Linkify.addLinks(textDescription, Linkify.WEB_URLS);
    backPressCloseHandler = new BackPressCloseHandler(this);
    textMaintenance = findViewById(R.id.textMaintenance);
    String maintenanceInfo = dbHelper.getValue(DB_KEY_KCMAINTNC);
    if (maintenanceInfo != null && maintenanceInfo.trim().length() > 0) {
        try {
            JsonArray maintenance_data = (new JsonParser().parse(maintenanceInfo)).getAsJsonArray();
            String mt_start = maintenance_data.get(0).getAsString();
            String mt_end = maintenance_data.get(1).getAsString();
            if (!mt_start.equals("")) {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z", Locale.US);
                Date start_date = df.parse(mt_start);
                Date end_date = df.parse(mt_end);
                SimpleDateFormat out_df = df;
                if (Build.VERSION.SDK_INT >= 18) {
                    out_df = new SimpleDateFormat(DateFormat.getBestDateTimePattern(Locale.getDefault(), "EEE, dd MMM yyyy HH:mm Z"), Locale.getDefault());
                }
                boolean is_passed = end_date.getTime() < System.currentTimeMillis();
                boolean before_maintenance = System.currentTimeMillis() < start_date.getTime();
                if (before_maintenance) {
                    textMaintenance.setText(KcaUtils.format(getStringWithLocale(R.string.ma_nextmaintenance), out_df.format(start_date)));
                } else if (!is_passed) {
                    textMaintenance.setText(KcaUtils.format(getStringWithLocale(R.string.ma_endmaintenance), out_df.format(end_date)));
                }
                textMaintenance.setVisibility(View.VISIBLE);
            } else {
                textMaintenance.setVisibility(View.GONE);
            }
        } catch (ParseException | IllegalStateException e) {
            textMaintenance.setText(getStringFromException(e));
            textMaintenance.setVisibility(View.VISIBLE);
        }
    } else {
        textMaintenance.setVisibility(View.GONE);
    }
    String locale = getStringPreferences(getApplicationContext(), PREF_KCA_LANGUAGE);
    ImageView specialImage = findViewById(R.id.special_image);
    specialImage.setImageResource(R.mipmap.special_image);
    specialImage.setVisibility(View.GONE);
    specialImage.setOnClickListener(v -> v.setVisibility(View.GONE));
    textSpecial = findViewById(R.id.textSpecial);
    textSpecial.setText(getStringWithLocale(R.string.special_message));
    textSpecial.setOnClickListener(v -> {
        specialImage.setImageResource(R.mipmap.special_image);
        specialImage.setVisibility(View.VISIBLE);
        sendUserAnalytics(getApplicationContext(), OPEN_PIC, null);
    });
    textSpecial2 = findViewById(R.id.textSpecial2);
    textSpecial2.setText(getStringWithLocale(R.string.notification_message));
    textSpecial2.setOnClickListener(v -> {
        String locale1 = getStringPreferences(getApplicationContext(), PREF_KCA_LANGUAGE);
        String locale_code = getResourceLocaleCode(locale1);
        String url = KcaUtils.format("http://luckyjervis.com/noti.html", locale_code);
        CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
        intentBuilder.setShowTitle(true);
        intentBuilder.setToolbarColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
        intentBuilder.enableUrlBarHiding();
        final CustomTabsIntent customTabsIntent = intentBuilder.build();
        final List<ResolveInfo> customTabsApps = getPackageManager().queryIntentActivities(customTabsIntent.intent, 0);
        if (customTabsApps.size() > 0) {
            customTabsIntent.launchUrl(MainActivity.this, Uri.parse(url));
        } else {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(browserIntent);
        }
    });
}
Also used : Message(android.os.Message) JsonObject(com.google.gson.JsonObject) ResolveInfo(android.content.pm.ResolveInfo) ImageView(android.widget.ImageView) JsonParser(com.google.gson.JsonParser) Bundle(android.os.Bundle) InputStream(java.io.InputStream) CustomTabsIntent(androidx.browser.customtabs.CustomTabsIntent) Intent(android.content.Intent) KcaUtils.getKcIntent(com.antest1.kcanotify.KcaUtils.getKcIntent) IOException(java.io.IOException) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) Spanned(android.text.Spanned) Date(java.util.Date) JsonArray(com.google.gson.JsonArray) CustomTabsIntent(androidx.browser.customtabs.CustomTabsIntent) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) CompoundButton(android.widget.CompoundButton)

Aggregations

CustomTabsIntent (androidx.browser.customtabs.CustomTabsIntent)3 Intent (android.content.Intent)2 ActivityNotFoundException (android.content.ActivityNotFoundException)1 ResolveInfo (android.content.pm.ResolveInfo)1 Bundle (android.os.Bundle)1 Message (android.os.Message)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 Spanned (android.text.Spanned)1 View (android.view.View)1 CompoundButton (android.widget.CompoundButton)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 CustomTabColorSchemeParams (androidx.browser.customtabs.CustomTabColorSchemeParams)1 KcaUtils.getKcIntent (com.antest1.kcanotify.KcaUtils.getKcIntent)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ParseException (java.text.ParseException)1