Search in sources :

Example 26 with VisibleForTesting

use of org.chromium.base.VisibleForTesting in project AndroidChromium by JackyAndroid.

the class IntentUtils method safePutBinderExtra.

/**
     * Inserts a {@link Binder} value into an Intent as an extra.
     *
     * Uses {@link BundleCompat#putBinder()}, but doesn't throw exceptions.
     *
     * @param intent Intent to put the binder into.
     * @param name Key.
     * @param binder Binder object.
     */
@VisibleForTesting
public static void safePutBinderExtra(Intent intent, String name, IBinder binder) {
    if (intent == null)
        return;
    Bundle bundle = new Bundle();
    try {
        BundleCompat.putBinder(bundle, name, binder);
    } catch (Throwable t) {
        // Catches parceling exceptions.
        Log.e(TAG, "putBinder failed on bundle " + bundle);
    }
    intent.putExtras(bundle);
}
Also used : Bundle(android.os.Bundle) VisibleForTesting(org.chromium.base.VisibleForTesting)

Example 27 with VisibleForTesting

use of org.chromium.base.VisibleForTesting in project AndroidChromium by JackyAndroid.

the class UrlUtilities method validateIntentUrl.

/**
     * @param url An Android intent:// URL to validate.
     *
     * @throws URISyntaxException if url is not a valid Android intent://
     * URL, as specified at
     * https://developer.chrome.com/multidevice/android/intents#syntax.
     */
@VisibleForTesting
public static boolean validateIntentUrl(String url) {
    if (url == null) {
        Log.d(TAG, "url was null");
        return false;
    }
    URI parsed;
    try {
        parsed = new URI(url);
    } catch (URISyntaxException e) {
        // specific case.
        if (url.indexOf("intent:#Intent;") == 0) {
            return validateIntentUrl(url.replace("intent:#Intent;", "intent://foo/#Intent;"));
        }
        Log.d(TAG, "Could not parse url '%s': %s", url, e.toString());
        return false;
    }
    String scheme = parsed.getScheme();
    if (scheme == null || !scheme.equals("intent")) {
        Log.d(TAG, "scheme was not 'intent'");
        return false;
    }
    String hostname = parsed.getHost();
    if (hostname == null) {
        Log.d(TAG, "hostname was null for '%s'", url);
        return false;
    }
    Matcher m = DNS_HOSTNAME_PATTERN.matcher(hostname);
    if (!m.matches()) {
        Log.d(TAG, "hostname did not match DNS_HOSTNAME_PATTERN");
        return false;
    }
    String path = parsed.getPath();
    if (path == null || (!path.isEmpty() && !path.equals("/"))) {
        Log.d(TAG, "path was null or not \"/\"");
        return false;
    }
    // We need to get the raw, unparsed, un-URL-decoded fragment.
    // parsed.getFragment() returns a URL-decoded fragment, which can
    // interfere with lexing and parsing Intent extras correctly. Therefore,
    // we handle the fragment "manually", but first assert that it
    // URL-decodes correctly.
    int fragmentStart = url.indexOf('#');
    if (fragmentStart == -1 || fragmentStart == url.length() - 1) {
        Log.d(TAG, "Could not find '#'");
        return false;
    }
    String fragment = url.substring(url.indexOf('#') + 1);
    try {
        String f = parsed.getFragment();
        if (f == null) {
            Log.d(TAG, "Could not get fragment from parsed URL");
            return false;
        }
        if (!URLDecoder.decode(fragment, "UTF-8").equals(f)) {
            Log.d(TAG, "Parsed fragment does not equal lexed fragment");
            return false;
        }
    } catch (UnsupportedEncodingException e) {
        Log.d(TAG, e.toString());
        return false;
    }
    // Now lex and parse the correctly-encoded fragment.
    String[] parts = fragment.split(";");
    if (parts.length < 3 || !parts[0].equals("Intent") || !parts[parts.length - 1].equals("end")) {
        Log.d(TAG, "Invalid fragment (not enough parts, lacking Intent, or lacking end)");
        return false;
    }
    boolean seenPackage = false;
    boolean seenAction = false;
    boolean seenCategory = false;
    boolean seenComponent = false;
    boolean seenScheme = false;
    for (int i = 1; i < parts.length - 1; ++i) {
        // This is OK *only* because no valid package, action, category,
        // component, or scheme contains (unencoded) "=".
        String[] pair = parts[i].split("=");
        if (2 != pair.length) {
            Log.d(TAG, "Invalid key=value pair '%s'", parts[i]);
            return false;
        }
        m = JAVA_PACKAGE_NAME_PATTERN.matcher(pair[1]);
        if (pair[0].equals("package")) {
            if (seenPackage || !m.matches()) {
                Log.d(TAG, "Invalid package '%s'", pair[1]);
                return false;
            }
            seenPackage = true;
        } else if (pair[0].equals("action")) {
            if (seenAction || !m.matches()) {
                Log.d(TAG, "Invalid action '%s'", pair[1]);
                return false;
            }
            seenAction = true;
        } else if (pair[0].equals("category")) {
            if (seenCategory || !m.matches()) {
                Log.d(TAG, "Invalid category '%s'", pair[1]);
                return false;
            }
            seenCategory = true;
        } else if (pair[0].equals("component")) {
            Matcher componentMatcher = ANDROID_COMPONENT_NAME_PATTERN.matcher(pair[1]);
            if (seenComponent || !componentMatcher.matches()) {
                Log.d(TAG, "Invalid component '%s'", pair[1]);
                return false;
            }
            seenComponent = true;
        } else if (pair[0].equals("scheme")) {
            if (seenScheme)
                return false;
            Matcher schemeMatcher = URL_SCHEME_PATTERN.matcher(pair[1]);
            if (!schemeMatcher.matches()) {
                Log.d(TAG, "Invalid scheme '%s'", pair[1]);
                return false;
            }
            seenScheme = true;
        } else {
            // are blobs to us.
            continue;
        }
    }
    return true;
}
Also used : Matcher(java.util.regex.Matcher) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) VisibleForTesting(org.chromium.base.VisibleForTesting)

Example 28 with VisibleForTesting

use of org.chromium.base.VisibleForTesting in project AndroidChromium by JackyAndroid.

the class ChromeTabbedActivity method maybeMergeTabs.

/**
     * Merges tabs from a second ChromeTabbedActivity instance if necesssary and calls
     * finishAndRemoveTask() on the other activity.
     */
@TargetApi(Build.VERSION_CODES.M)
@VisibleForTesting
public void maybeMergeTabs() {
    if (!FeatureUtilities.isTabModelMergingEnabled())
        return;
    Class<?> otherWindowActivityClass = MultiWindowUtils.getInstance().getOpenInOtherWindowActivity(this);
    // 1. Find the other activity's task if it's still running so that it can be removed from
    //    Android recents.
    ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.AppTask> appTasks = activityManager.getAppTasks();
    ActivityManager.AppTask otherActivityTask = null;
    for (ActivityManager.AppTask task : appTasks) {
        if (task.getTaskInfo() == null || task.getTaskInfo().baseActivity == null)
            continue;
        String baseActivity = task.getTaskInfo().baseActivity.getClassName();
        if (baseActivity.equals(otherWindowActivityClass.getName())) {
            otherActivityTask = task;
        }
    }
    if (otherActivityTask != null) {
        for (WeakReference<Activity> activityRef : ApplicationStatus.getRunningActivities()) {
            Activity activity = activityRef.get();
            if (activity == null)
                continue;
            // starts, causing some duplicate work to be done. Avoid the redundancy.
            if (activity.getClass().equals(otherWindowActivityClass)) {
                ((ChromeTabbedActivity) activity).saveState();
                break;
            }
        }
        // 3. Kill the other activity's task to remove it from Android recents.
        otherActivityTask.finishAndRemoveTask();
    }
    // 4. Ask TabPersistentStore to merge state.
    RecordUserAction.record("Android.MergeState.Live");
    mTabModelSelectorImpl.mergeState();
    setMergedInstanceTaskId(getTaskId());
}
Also used : AppTask(android.app.ActivityManager.AppTask) AppTask(android.app.ActivityManager.AppTask) ChromeLauncherActivity(org.chromium.chrome.browser.document.ChromeLauncherActivity) FirstRunActivity(org.chromium.chrome.browser.firstrun.FirstRunActivity) Activity(android.app.Activity) ActivityManager(android.app.ActivityManager) VisibleForTesting(org.chromium.base.VisibleForTesting) TargetApi(android.annotation.TargetApi)

Example 29 with VisibleForTesting

use of org.chromium.base.VisibleForTesting in project AndroidChromium by JackyAndroid.

the class BluetoothChooserDialog method show.

/**
     * Show the BluetoothChooserDialog.
     */
@VisibleForTesting
void show() {
    // Emphasize the origin.
    Profile profile = Profile.getLastUsedProfile();
    SpannableString origin = new SpannableString(mOrigin);
    OmniboxUrlEmphasizer.emphasizeUrl(origin, mActivity.getResources(), profile, mSecurityLevel, false, true, true);
    // Construct a full string and replace the origin text with emphasized version.
    SpannableString title = new SpannableString(mActivity.getString(R.string.bluetooth_dialog_title, mOrigin));
    int start = title.toString().indexOf(mOrigin);
    TextUtils.copySpansFrom(origin, 0, origin.length(), Object.class, title, start);
    String message = mActivity.getString(R.string.bluetooth_not_found);
    SpannableString noneFound = SpanApplier.applySpans(message, new SpanInfo("<link>", "</link>", new BluetoothClickableSpan(LinkType.RESTART_SEARCH, mActivity)));
    SpannableString searching = SpanApplier.applySpans(mActivity.getString(R.string.bluetooth_searching), new SpanInfo("<link>", "</link>", new BluetoothClickableSpan(LinkType.EXPLAIN_BLUETOOTH, mActivity)));
    String positiveButton = mActivity.getString(R.string.bluetooth_confirm_button);
    SpannableString statusIdleNoneFound = SpanApplier.applySpans(mActivity.getString(R.string.bluetooth_not_seeing_it_idle_none_found), new SpanInfo("<link>", "</link>", new BluetoothClickableSpan(LinkType.EXPLAIN_BLUETOOTH, mActivity)));
    SpannableString statusActive = SpanApplier.applySpans(mActivity.getString(R.string.bluetooth_not_seeing_it), new SpanInfo("<link>", "</link>", new BluetoothClickableSpan(LinkType.EXPLAIN_BLUETOOTH, mActivity)));
    SpannableString statusIdleSomeFound = SpanApplier.applySpans(mActivity.getString(R.string.bluetooth_not_seeing_it_idle_some_found), new SpanInfo("<link1>", "</link1>", new BluetoothClickableSpan(LinkType.EXPLAIN_BLUETOOTH, mActivity)), new SpanInfo("<link2>", "</link2>", new BluetoothClickableSpan(LinkType.RESTART_SEARCH, mActivity)));
    ItemChooserDialog.ItemChooserLabels labels = new ItemChooserDialog.ItemChooserLabels(title, searching, noneFound, statusActive, statusIdleNoneFound, statusIdleSomeFound, positiveButton);
    mItemChooserDialog = new ItemChooserDialog(mActivity, this, labels);
    mActivity.registerReceiver(mLocationModeBroadcastReceiver, new IntentFilter(LocationManager.MODE_CHANGED_ACTION));
    mIsLocationModeChangedReceiverRegistered = true;
}
Also used : SpannableString(android.text.SpannableString) IntentFilter(android.content.IntentFilter) SpanInfo(org.chromium.ui.text.SpanApplier.SpanInfo) SpannableString(android.text.SpannableString) Profile(org.chromium.chrome.browser.profiles.Profile) VisibleForTesting(org.chromium.base.VisibleForTesting)

Example 30 with VisibleForTesting

use of org.chromium.base.VisibleForTesting in project AndroidChromium by JackyAndroid.

the class BluetoothChooserDialog method notifyAdapterTurnedOff.

@VisibleForTesting
@CalledByNative
void notifyAdapterTurnedOff() {
    SpannableString adapterOffMessage = SpanApplier.applySpans(mActivity.getString(R.string.bluetooth_adapter_off), new SpanInfo("<link>", "</link>", new BluetoothClickableSpan(LinkType.ADAPTER_OFF, mActivity)));
    SpannableString adapterOffStatus = SpanApplier.applySpans(mActivity.getString(R.string.bluetooth_adapter_off_help), new SpanInfo("<link>", "</link>", new BluetoothClickableSpan(LinkType.ADAPTER_OFF_HELP, mActivity)));
    mItemChooserDialog.setErrorState(adapterOffMessage, adapterOffStatus);
}
Also used : SpannableString(android.text.SpannableString) SpanInfo(org.chromium.ui.text.SpanApplier.SpanInfo) VisibleForTesting(org.chromium.base.VisibleForTesting) CalledByNative(org.chromium.base.annotations.CalledByNative)

Aggregations

VisibleForTesting (org.chromium.base.VisibleForTesting)52 Intent (android.content.Intent)6 IOException (java.io.IOException)6 SharedPreferences (android.content.SharedPreferences)5 JSONObject (org.json.JSONObject)4 PendingIntent (android.app.PendingIntent)3 SpannableString (android.text.SpannableString)3 BufferedReader (java.io.BufferedReader)3 Matcher (java.util.regex.Matcher)3 CalledByNative (org.chromium.base.annotations.CalledByNative)3 AccountManagerHelper (org.chromium.components.signin.AccountManagerHelper)3 SpanInfo (org.chromium.ui.text.SpanApplier.SpanInfo)3 SuppressLint (android.annotation.SuppressLint)2 IntentFilter (android.content.IntentFilter)2 Paint (android.graphics.Paint)2 File (java.io.File)2 FileReader (java.io.FileReader)2 HttpURLConnection (java.net.HttpURLConnection)2 ArrayList (java.util.ArrayList)2 Formatter (java.util.Formatter)2