Java Code Examples for android.content.Intent.parseUri()

The following are Jave code examples for showing how to use parseUri() of the android.content.Intent class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: XinFramework   File: WebModel.java   View Source Code Vote up 7 votes
private static boolean handleIntentUrl(WebView view, String url) {
    if (TextUtils.isEmpty(url) || !url.startsWith("intent://"))

        return false;
    try {
        Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
        ResolveInfo info = PackageUtil.getResolveInfo(view.getContext(), intent);
        if (info != null) {
            if (!(view.getContext() instanceof Activity))
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            view.getContext().startActivity(intent);
            return true;
        }
    } catch (URISyntaxException e) {
        Log.printStackTrace(e);
    }


    return false;
}
 
Example 2
Project: nativead   File: IntentUtils.java   View Source Code Vote up 7 votes
/**
 * 从uri中解析出Intent
 *
 * @param context 上下文
 * @param uri     uri
 * @param flags   intentFlags
 *
 * @return Intent
 */
public static Intent getIntentFromUri(Context context, String uri, int flags) {
	try {
		Intent intent = Intent.parseUri(uri, flags);
		if (intent == null) {
			return null;
		}
		List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent, 0);
		if (list == null || list.isEmpty()) {
			return null;
		}
		if (!(context instanceof Activity)) {
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		}
		return intent;
	} catch (Throwable e) {
		DLog.e(e);
	}
	return null;
}
 
Example 3
Project: GravityBox   File: ModStatusBar.java   View Source Code Vote up 7 votes
private static void launchClockAction(String uri) {
    if (mContext == null) return;

    try {
        final Intent intent = Intent.parseUri(uri, 0);
        if (intent != null) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            mContext.startActivity(intent);
            if (mPhoneStatusBar != null) {
                XposedHelpers.callMethod(mPhoneStatusBar, "animateCollapsePanels");
            }
        }
    } catch (ActivityNotFoundException e) {
        log("Error launching assigned app for long-press on clock: " + e.getMessage());
    } catch (Throwable t) {
        XposedBridge.log(t);
    }
}
 
Example 4
Project: GravityBox   File: ModHwKeys.java   View Source Code Vote up 7 votes
private static void launchCustomApp(String uri) {
    if (uri == null) {
        try {
            Handler handler = (Handler) XposedHelpers.getObjectField(mPhoneWindowManager, "mHandler");
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(mContext, mStrCustomAppNone, Toast.LENGTH_SHORT).show();
                }
            });
        } catch (Throwable t) {
        }
        return;
    }

    try {
        Intent i = Intent.parseUri(uri, 0);
        launchCustomApp(i);
    } catch (URISyntaxException e) {
        log("launchCustomApp: error parsing uri: " + e.getMessage());
    }
}
 
Example 5
Project: SimpleUILauncher   File: DefaultLayoutParser.java   View Source Code Vote up 6 votes
@Override
protected Intent parseIntent(XmlResourceParser parser) {
    String uri = null;
    try {
        uri = getAttributeValue(parser, ATTR_URI);
        return Intent.parseUri(uri, 0);
    } catch (URISyntaxException e) {
        Log.w(TAG, "Shortcut has malformed uri: " + uri);
        return null; // Oh well
    }
}
 
Example 6
Project: LaunchEnr   File: LoaderCursor.java   View Source Code Vote up 6 votes
public Intent parseIntent() {
    String intentDescription = getString(intentIndex);
    try {
        return TextUtils.isEmpty(intentDescription) ?
                null : Intent.parseUri(intentDescription, 0);
    } catch (URISyntaxException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 7
Project: LaunchEnr   File: GridSizeMigrationTask.java   View Source Code Vote up 6 votes
/**
 * Verifies if the intent should be restored.
 */
private void verifyIntent(String intentStr) throws Exception {
    Intent intent = Intent.parseUri(intentStr, 0);
    if (intent.getComponent() != null) {
        verifyPackage(intent.getComponent().getPackageName());
    } else if (intent.getPackage() != null) {
        // Only verify package if the component was null.
        verifyPackage(intent.getPackage());
    }
}
 
Example 8
Project: LaunchEnr   File: ImportDataTask.java   View Source Code Vote up 6 votes
@Override
public long insertAndCheck(SQLiteDatabase db, ContentValues values) {
    if (mExistingItems.size() >= mRequiredSize) {
        // No need to add more items.
        return 0;
    }
    Intent intent;
    try {
        intent = Intent.parseUri(values.getAsString(Favorites.INTENT), 0);
    } catch (URISyntaxException e) {
        return 0;
    }
    String pkg = getPackage(intent);
    if (pkg == null || mExisitingApps.contains(pkg)) {
        // The item does not target an app or is already in hotseat.
        return 0;
    }
    mExisitingApps.add(pkg);

    // find next vacant spot.
    long screen = 0;
    while (mExistingItems.get(screen) != null) {
        screen++;
    }
    mExistingItems.put(screen, intent);
    values.put(Favorites.SCREEN, screen);
    mOutOps.add(ContentProviderOperation.newInsert(Favorites.CONTENT_URI).withValues(values).build());
    return 0;
}
 
Example 9
Project: sflauncher   File: App.java   View Source Code Vote up 6 votes
public static BitmapShortcut from(String ident){
    String[] parts = ident.split("\\|");
    if (parts.length < 4) return null;
    try {
        String uri = decodeText(parts[1]);
        Intent intent = Intent.parseUri(uri, Intent.URI_INTENT_SCHEME);
        return new BitmapShortcut(parts[2],decodeBitmap(parts[3]), intent, parts[3]);
    } catch (URISyntaxException ignored) {
    }
    return null;
}
 
Example 10
Project: garras   File: AlipayDonate.java   View Source Code Vote up 6 votes
private static void startIntentUrl(Activity activity, String intentFullUrl) {
    try {
        Intent intent = Intent.parseUri(intentFullUrl, Intent.URI_INTENT_SCHEME);
        activity.startActivity(intent);
    } catch (URISyntaxException | ActivityNotFoundException e) {
        Timber.e(e);
    }
}
 
Example 11
Project: chromium-for-android-56-debug-video   File: ExternalNavigationHandler.java   View Source Code Vote up 6 votes
/**
 * @return Whether the |url| could be handled by an external application on the system.
 */
public boolean canExternalAppHandleUrl(String url) {
    if (url.startsWith(SCHEME_WTAI_MC)) return true;
    try {
        Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
        if (intent.getPackage() != null) return true;

        List<ResolveInfo> resolvingInfos = mDelegate.queryIntentActivities(intent);
        if (resolvingInfos != null && resolvingInfos.size() > 0) return true;
    } catch (Exception ex) {
        // Ignore the error.
        Log.w(TAG, "Bad URI %s", url, ex);
    }
    return false;
}
 
Example 12
Project: FlickLauncher   File: GridSizeMigrationTask.java   View Source Code Vote up 6 votes
/**
 * Verifies if the intent should be restored.
 */
private void verifyIntent(String intentStr) throws Exception {
    Intent intent = Intent.parseUri(intentStr, 0);
    if (intent.getComponent() != null) {
        verifyPackage(intent.getComponent().getPackageName());
    } else if (intent.getPackage() != null) {
        // Only verify package if the component was null.
        verifyPackage(intent.getPackage());
    }
}
 
Example 13
Project: letv   File: VipOrderDetailActivity.java   View Source Code Vote up 6 votes
private void redirectAndStartAlipay(String redirectUrl) {
    LogInfo.log("CRL 支付宝支付 跳转并打开支付宝钱包 == " + redirectUrl);
    LogInfo.log("CRL 支付宝支付 跳转并打开支付宝钱包 isPackageAvailable== " + isPackageAvailable(this, "com.eg.android.AlipayGphone"));
    String requestUrl = "";
    if (isPackageAvailable(this, "com.eg.android.AlipayGphone")) {
        try {
            requestUrl = ALIPAY_SCHEME_HEADER + URLEncoder.encode(redirectUrl, "UTF-8");
            LogInfo.log("CRL 支付宝支付 跳转并打开支付宝钱包 requestUrl== " + requestUrl);
            Intent intent = null;
            try {
                intent = Intent.parseUri(requestUrl, 1);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            startActivity(intent);
            return;
        } catch (UnsupportedEncodingException e2) {
            e2.printStackTrace();
            return;
        }
    }
    try {
        requestUrl = ALIPAY_HTTP_HEADER + URLEncoder.encode(ALIPAY_HTTP_SCHEME_KEY + URLEncoder.encode(redirectUrl, "UTF-8"), "UTF-8");
        LogInfo.log("CRL 支付宝支付 跳转并打开支付宝钱包 requestUrl== " + requestUrl);
    } catch (UnsupportedEncodingException e22) {
        e22.printStackTrace();
    }
    if (!TextUtils.isEmpty(requestUrl)) {
        new LetvWebViewActivityConfig(this).launch(requestUrl, "", true, false);
    }
    finish();
}
 
Example 14
Project: SimpleUILauncher   File: GridSizeMigrationTask.java   View Source Code Vote up 6 votes
/**
 * Verifies if the intent should be restored.
 */
private void verifyIntent(String intentStr) throws Exception {
    Intent intent = Intent.parseUri(intentStr, 0);
    if (intent.getComponent() != null) {
        verifyPackage(intent.getComponent().getPackageName());
    } else if (intent.getPackage() != null) {
        // Only verify package if the component was null.
        verifyPackage(intent.getPackage());
    }
}
 
Example 15
Project: LaunchEnr   File: DefaultLayoutParser.java   View Source Code Vote up 5 votes
@Override
protected long invalidPackageOrClass(XmlResourceParser parser) {
    final String uri = getAttributeValue(parser, ATTR_URI);
    if (TextUtils.isEmpty(uri)) {
        return -1;
    }

    final Intent metaIntent;
    try {
        metaIntent = Intent.parseUri(uri, 0);
    } catch (URISyntaxException e) {
        e.printStackTrace();
        return -1;
    }

    ResolveInfo resolved = mPackageManager.resolveActivity(metaIntent,
            PackageManager.MATCH_DEFAULT_ONLY);
    final List<ResolveInfo> appList = mPackageManager.queryIntentActivities(
            metaIntent, PackageManager.MATCH_DEFAULT_ONLY);

    // Verify that the result is an app and not just the resolver dialog asking which
    // app to use.
    if (wouldLaunchResolverActivity(resolved, appList)) {
        // If only one of the results is a system app then choose that as the default.
        final ResolveInfo systemApp = getSingleSystemActivity(appList);
        if (systemApp == null) {
            // There is no logical choice for this meta-favorite, so rather than making
            // a bad choice just add nothing.
            return -1;
        }
        resolved = systemApp;
    }
    final ActivityInfo info = resolved.activityInfo;
    final Intent intent = mPackageManager.getLaunchIntentForPackage(info.packageName);
    if (intent == null) {
        return -1;
    }
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
            Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

    return addShortcut(info.loadLabel(mPackageManager).toString(), intent,
            Favorites.ITEM_TYPE_APPLICATION);
}
 
Example 16
Project: LaunchEnr   File: LauncherProvider.java   View Source Code Vote up 5 votes
/**
 * Replaces all shortcuts of type {@link Favorites#ITEM_TYPE_SHORTCUT} which have a valid
 * launcher activity target with {@link Favorites#ITEM_TYPE_APPLICATION}.
 */
@Thunk void convertShortcutsToLauncherActivities(SQLiteDatabase db) {
    db.beginTransaction();
    Cursor c = null;
    SQLiteStatement updateStmt = null;

    try {
        // Only consider the primary user as other users can't have a shortcut.
        long userSerial = getDefaultUserSerial();
        c = db.query(Favorites.TABLE_NAME, new String[] {
                Favorites._ID,
                Favorites.INTENT,
            }, "itemType=" + Favorites.ITEM_TYPE_SHORTCUT + " AND profileId=" + userSerial,
            null, null, null, null);

        updateStmt = db.compileStatement("UPDATE favorites SET itemType="
                + Favorites.ITEM_TYPE_APPLICATION + " WHERE _id=?");

        final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
        final int intentIndex = c.getColumnIndexOrThrow(Favorites.INTENT);

        while (c.moveToNext()) {
            String intentDescription = c.getString(intentIndex);
            Intent intent;
            try {
                intent = Intent.parseUri(intentDescription, 0);
            } catch (URISyntaxException e) {
                e.printStackTrace();
                continue;
            }

            if (!Utilities.isLauncherAppTarget(intent)) {
                continue;
            }

            long id = c.getLong(idIndex);
            updateStmt.bindLong(1, id);
            updateStmt.executeUpdateDelete();
        }
        db.setTransactionSuccessful();
    } catch (SQLException ex) {
        ex.printStackTrace();
    } finally {
        db.endTransaction();
        if (c != null) {
            c.close();
        }
        if (updateStmt != null) {
            updateStmt.close();
        }
    }
}
 
Example 17
Project: SimpleUILauncher   File: ImportDataTask.java   View Source Code Vote up 5 votes
@Override
public long insertAndCheck(SQLiteDatabase db, ContentValues values) {
    if (mExistingItems.size() >= mRequiredSize) {
        // No need to add more items.
        return 0;
    }
    Intent intent;
    try {
        intent = Intent.parseUri(values.getAsString(Favorites.INTENT), 0);
    } catch (URISyntaxException e) {
        return 0;
    }
    String pkg = getPackage(intent);
    if (pkg == null || mExisitingApps.contains(pkg)) {
        // The item does not target an app or is already in hotseat.
        return 0;
    }
    mExisitingApps.add(pkg);

    // find next vacant spot.
    long screen = 0;
    while (mExistingItems.get(screen) != null) {
        screen++;
    }
    mExistingItems.put(screen, intent);
    values.put(Favorites.SCREEN, screen);
    mOutOps.add(ContentProviderOperation.newInsert(Favorites.CONTENT_URI).withValues(values).build());
    return 0;
}
 
Example 18
Project: FlickLauncher   File: InstallShortcutReceiver.java   View Source Code Vote up 5 votes
private static PendingInstallShortcutInfo decode(String encoded, Context context) {
    try {
        JSONObject object = (JSONObject) new JSONTokener(encoded).nextValue();
        Intent launcherIntent = Intent.parseUri(object.getString(LAUNCH_INTENT_KEY), 0);

        if (object.optBoolean(APP_SHORTCUT_TYPE_KEY)) {
            // The is an internal launcher target shortcut.
            UserHandleCompat user = UserManagerCompat.getInstance(context)
                    .getUserForSerialNumber(object.getLong(USER_HANDLE_KEY));
            if (user == null) {
                return null;
            }

            LauncherActivityInfoCompat info = LauncherAppsCompat.getInstance(context)
                    .resolveActivity(launcherIntent, user);
            return info == null ? null : new PendingInstallShortcutInfo(info, context);
        }

        Intent data = new Intent();
        data.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);
        data.putExtra(Intent.EXTRA_SHORTCUT_NAME, object.getString(NAME_KEY));

        String iconBase64 = object.optString(ICON_KEY);
        String iconResourceName = object.optString(ICON_RESOURCE_NAME_KEY);
        String iconResourcePackageName = object.optString(ICON_RESOURCE_PACKAGE_NAME_KEY);
        if (iconBase64 != null && !iconBase64.isEmpty()) {
            byte[] iconArray = Base64.decode(iconBase64, Base64.DEFAULT);
            Bitmap b = BitmapFactory.decodeByteArray(iconArray, 0, iconArray.length);
            data.putExtra(Intent.EXTRA_SHORTCUT_ICON, b);
        } else if (iconResourceName != null && !iconResourceName.isEmpty()) {
            Intent.ShortcutIconResource iconResource =
                new Intent.ShortcutIconResource();
            iconResource.resourceName = iconResourceName;
            iconResource.packageName = iconResourcePackageName;
            data.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
        }

        return new PendingInstallShortcutInfo(data, context);
    } catch (JSONException | URISyntaxException e) {
        Log.d(TAG, "Exception reading shortcut to add: " + e);
    }
    return null;
}
 
Example 19
Project: firefox-tv   File: IntentUtils.java   View Source Code Vote up 5 votes
/**
 * Find and open the appropriate app for a given Uri. If appropriate, let the user select between
 * multiple supported apps. Returns a boolean indicating whether the URL was handled. A fallback
 * URL will be opened in the supplied WebView if appropriate (in which case the URL was handled,
 * and true will also be returned). If not handled, we should  fall back to webviews error handling
 * (which ends up calling our error handling code as needed).
 *
 * Note: this method "leaks" the target Uri to Android before asking the user whether they
 * want to use an external app to open the uri. Ultimately the OS can spy on anything we're
 * doing in the app, so this isn't an actual "bug".
 */
public static boolean handleExternalUri(final Context context, final IWebView webView, final String uri) {
    // This code is largely based on Fennec's ExternalIntentDuringPrivateBrowsingPromptFragment.java
    final Intent intent;
    try {
        intent = Intent.parseUri(uri, 0);
    } catch (URISyntaxException e) {
        // Let the browser handle the url (i.e. let the browser show it's unsupported protocol /
        // invalid URL page).
        return false;
    }

    // Since we're a browser:
    intent.addCategory(Intent.CATEGORY_BROWSABLE);

    final PackageManager packageManager = context.getPackageManager();

    // This is where we "leak" the uri to the OS. If we're using the system webview, then the OS
    // already knows that we're opening this uri. Even if we're using GeckoView, the OS can still
    // see what domains we're visiting, so there's no loss of privacy here:
    final List<ResolveInfo> matchingActivities = packageManager.queryIntentActivities(intent, 0);

    if (matchingActivities.size() == 0) {
        return handleUnsupportedLink(context, webView, intent);
    } else if (matchingActivities.size() == 1) {
        final ResolveInfo info;

        if (matchingActivities.size() == 1) {
            info = matchingActivities.get(0);
        } else {
            // Ordering isn't guaranteed if there is more than one available activity - hence
            // we fetch the default (this code isn't currently run because we handle the > 1
            // case separately, but would be needed if we ever decide to prefer the default
            // app for the > 1 case.
            info = packageManager.resolveActivity(intent, 0);
        }
        final CharSequence externalAppTitle = info.loadLabel(packageManager);

        showConfirmationDialog(context, intent, context.getString(R.string.external_app_prompt_title), R.string.external_app_prompt, externalAppTitle);
        return true;
    } else { // matchingActivities.size() > 1
        // By explicitly showing the chooser, we can avoid having a (default) app from opening
        // the link immediately. This isn't perfect - we'd prefer to highlight the default app,
        // but it's not clear if there's any way of doing that. An alternative
        // would be to reuse the same dialog as for the single-activity case, and offer
        // a "open in other app this time" button if we have more than one matchingActivity.
        final String chooserTitle = context.getString(R.string.external_multiple_apps_matched_exit);
        final Intent chooserIntent = Intent.createChooser(intent, chooserTitle);
        context.startActivity(chooserIntent);

        return true;
    }
}
 
Example 20
Project: GravityBox   File: AppPickerPreference.java   View Source Code Vote up 5 votes
private AppInfo getAppInfoFromValue(String value) {
    AppInfo appInfo = new AppInfo();
    if (value == null) return appInfo;

    try {
        Intent intent = Intent.parseUri(value, 0);

        int iconResId = intent.getStringExtra("iconResName") != null ?
                mResources.getIdentifier(intent.getStringExtra("iconResName"),
                        "drawable", mContext.getPackageName()) : 0;
        if (iconResId != 0) {
            appInfo.icon = mResources.getDrawable(iconResId);
        } else if (intent.hasExtra("icon")) {
            final String appIconPath = intent.getStringExtra("icon");
            if (appIconPath != null) {
                File f = new File(appIconPath);
                if (f.exists() && f.canRead()) {
                    FileInputStream fis = new FileInputStream(f);
                    appInfo.icon = new BitmapDrawable(mResources, BitmapFactory.decodeStream(fis));
                    fis.close();
                }
            }
        }

        int mode = intent.getIntExtra("mode", MODE_APP);
        if (mode == MODE_APP) {
            ComponentName cn = intent.getComponent();
            ActivityInfo ai = mPackageManager.getActivityInfo(cn, 0);
            appInfo.name = (ai.loadLabel(mPackageManager).toString());
            if (appInfo.icon == null) {
                appInfo.icon = ai.loadIcon(mPackageManager);
            }
        } else if (mode == MODE_SHORTCUT) {
            appInfo.name = intent.getStringExtra("prefLabel");
        }
        return appInfo;
    } catch (Exception e) {
        e.printStackTrace();
        return appInfo;
    }
}