Example usage for android.net Uri isHierarchical

List of usage examples for android.net Uri isHierarchical

Introduction

In this page you can find the example usage for android.net Uri isHierarchical.

Prototype

public abstract boolean isHierarchical();

Source Link

Document

Returns true if this URI is hierarchical like "http://google.com".

Usage

From source file:com.scvngr.levelup.core.net.AbstractRequest.java

/**
 * Returns the portion of an absolute URL before the query string. E.g.
 * {@code http://example.com/search?q=kittens} would return {@code "http://example.com/search"}.
 *
 * @param url the URL to strip./*from   ww  w . ja va  2  s  . c o m*/
 * @return the URL stripped of any query parameters, including the '?'.
 * @throws IllegalArgumentException if the URI passed in isn't an absolute URL.
 */
@NonNull
private static String stripQueryParameters(final Uri url) throws IllegalArgumentException {
    if (!url.isAbsolute() || !url.isHierarchical()) {
        throw new IllegalArgumentException("Request URI must be an absolute URL");
    }

    return NullUtils.nonNullContract(url.buildUpon().query(null).build().toString());
}

From source file:org.transdroid.core.gui.navigation.NavigationHelper.java

/**
 * Analyses a torrent http or magnet URI and tries to come up with a reasonable human-readable name.
 * @param rawTorrentUri The raw http:// or magnet: link to the torrent
 * @return A best-guess, reasonably long name for the linked torrent
 *///from  ww  w . ja  v  a  2s.co m
public static String extractNameFromUri(Uri rawTorrentUri) {

    if (rawTorrentUri.getScheme() == null) {
        // Probably an incorrect URI; just return the whole thing
        return rawTorrentUri.toString();
    }

    if (rawTorrentUri.getScheme().equals("magnet")) {
        // Magnet links might have a dn (display name) parameter
        String dn = getQueryParameter(rawTorrentUri, "dn");
        if (dn != null && !dn.equals("")) {
            return dn;
        }
        // If not, try to return the hash that is specified as xt (exact topci)
        String xt = getQueryParameter(rawTorrentUri, "xt");
        if (xt != null && !xt.equals("")) {
            return xt;
        }
    }

    if (rawTorrentUri.isHierarchical()) {
        String path = rawTorrentUri.getPath();
        if (path != null) {
            if (path.contains("/")) {
                path = path.substring(path.lastIndexOf("/"));
            }
            return path;
        }
    }

    // No idea what to do with this; return as is
    return rawTorrentUri.toString();
}

From source file:uk.org.rivernile.edinburghbustracker.android.fragments.general.EnterStopCodeFragment.java

/**
 * {@inheritDoc}//from   www . jav a  2 s  .c o  m
 */
@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    final Activity activity = getActivity();

    // The result code signified success.
    if (resultCode == Activity.RESULT_OK) {
        // Make sure there is a data Intent. There have been some crashes
        // caused by this being null.
        if (data == null) {
            Toast.makeText(activity, R.string.enterstopcode_scan_error, Toast.LENGTH_LONG).show();
            return;
        }

        // Get the data from the Intent.
        final Uri uri = Uri.parse(data.getStringExtra("SCAN_RESULT"));
        // We can only handle hierarchical URIs. Make sure it is so.
        if (!uri.isHierarchical()) {
            // Tell the user the URI was invalid.
            Toast.makeText(activity, R.string.enterstopcode_invalid_qrcode, Toast.LENGTH_LONG).show();
            return;
        }

        // Get the busStopCode parameter from the URI.
        final String stopCode = uri.getQueryParameter("busStopCode");
        // Do basic sanity checking on the parameter.
        if (stopCode != null && stopCode.length() > 0) {
            // Set the EditText to the value of this stop code.
            txt.setText(stopCode);
            // Launch bus times.
            callbacks.onShowBusTimes(stopCode);
        } else {
            Toast.makeText(activity, R.string.enterstopcode_invalid_qrcode, Toast.LENGTH_LONG).show();
        }
    }
}

From source file:id.ridon.keude.Keude.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    fdroidApp = ((KeudeApp) getApplication());
    fdroidApp.applyTheme(this);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.fdroid);/*from  www. j  a v  a  2 s . co m*/
    createViews();

    getTabManager().createTabs();

    // Start a search by just typing
    setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);

    Intent i = getIntent();
    Uri data = i.getData();
    String appid = null;
    if (data != null) {
        if (data.isHierarchical()) {
            // http(s)://f-droid.org/repository/browse?fdid=app.id
            appid = data.getQueryParameter("fdid");
        }
    } else if (i.hasExtra(EXTRA_TAB_UPDATE)) {
        boolean showUpdateTab = i.getBooleanExtra(EXTRA_TAB_UPDATE, false);
        if (showUpdateTab) {
            getTabManager().selectTab(2);
        }
    }
    if (appid != null && appid.length() > 0) {
        Intent call = new Intent(this, AppDetails.class);
        call.putExtra(AppDetails.EXTRA_APPID, appid);
        startActivityForResult(call, REQUEST_APPDETAILS);
    }

    Uri uri = AppProvider.getContentUri();
    getContentResolver().registerContentObserver(uri, true, new AppObserver());
}

From source file:net.openid.appauthdemo.Configuration.java

@NonNull
Uri getRequiredConfigUri(String propName) throws InvalidConfigurationException {
    String uriStr = getRequiredConfigString(propName);
    Uri uri;
    try {/*from   w  ww .j a  v a 2 s  .  c  o m*/
        uri = Uri.parse(uriStr);
    } catch (Throwable ex) {
        throw new InvalidConfigurationException(propName + " could not be parsed", ex);
    }

    if (!uri.isHierarchical() || !uri.isAbsolute()) {
        throw new InvalidConfigurationException(propName + " must be hierarchical and absolute");
    }

    if (!TextUtils.isEmpty(uri.getEncodedUserInfo())) {
        throw new InvalidConfigurationException(propName + " must not have user info");
    }

    if (!TextUtils.isEmpty(uri.getEncodedQuery())) {
        throw new InvalidConfigurationException(propName + " must not have query parameters");
    }

    if (!TextUtils.isEmpty(uri.getEncodedFragment())) {
        throw new InvalidConfigurationException(propName + " must not have a fragment");
    }

    return uri;
}

From source file:com.vuze.android.remote.VuzeEasyTrackerOld.java

public Map<String, String> getReferrerMapFromUri(Uri uri) {

    MapBuilder paramMap = new MapBuilder();

    // If no URI, return an empty Map.
    if (uri == null) {
        return paramMap.build();
    }//from w  ww. ja  va2 s.c o m

    try {
        // Source is the only required campaign field. No need to continue if not
        // present.
        if (uri.isHierarchical() && uri.getQueryParameter(CAMPAIGN_SOURCE_PARAM) != null) {

            // MapBuilder.setCampaignParamsFromUrl parses Google Analytics campaign
            // ("UTM") parameters from a string URL into a Map that can be set on
            // the Tracker.
            paramMap.setCampaignParamsFromUrl(uri.toString());

            // If no source parameter, set authority to source and medium to
            // "referral".
        } else if (uri.getAuthority() != null && uri.getAuthority().length() > 0) {

            paramMap.set(Fields.CAMPAIGN_MEDIUM, "referral");
            paramMap.set(Fields.CAMPAIGN_SOURCE, uri.getAuthority());

        } else if (uri.getScheme() != null) {
            paramMap.set(Fields.CAMPAIGN_MEDIUM, uri.getScheme());
        }
    } catch (Throwable t) {
        // I found: java.lang.UnsupportedOperationException: This isn't a hierarchical URI.
        // Fixed above with isHeirarchical, but who knows what other throws there are
        if (AndroidUtils.DEBUG) {
            t.printStackTrace();
        }
    }

    return paramMap.build();
}

From source file:com.jtechme.apphub.FDroid.java

private void handleSearchOrAppViewIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        performSearch(query);/*from www .j  av a2s .c  om*/
        return;
    }

    final Uri data = intent.getData();
    if (data == null) {
        return;
    }

    final String scheme = data.getScheme();
    final String path = data.getPath();
    String packageName = null;
    String query = null;
    if (data.isHierarchical()) {
        final String host = data.getHost();
        if (host == null) {
            return;
        }
        switch (host) {
        case "f-droid.org":
            if (path.startsWith("/repository/browse")) {
                // http://f-droid.org/repository/browse?fdfilter=search+query
                query = UriCompat.getQueryParameter(data, "fdfilter");

                // http://f-droid.org/repository/browse?fdid=packageName
                packageName = UriCompat.getQueryParameter(data, "fdid");
            } else if (path.startsWith("/app")) {
                // http://f-droid.org/app/packageName
                packageName = data.getLastPathSegment();
                if ("app".equals(packageName)) {
                    packageName = null;
                }
            }
            break;
        case "details":
            // market://details?id=app.id
            packageName = UriCompat.getQueryParameter(data, "id");
            break;
        case "search":
            // market://search?q=query
            query = UriCompat.getQueryParameter(data, "q");
            break;
        case "play.google.com":
            if (path.startsWith("/store/apps/details")) {
                // http://play.google.com/store/apps/details?id=app.id
                packageName = UriCompat.getQueryParameter(data, "id");
            } else if (path.startsWith("/store/search")) {
                // http://play.google.com/store/search?q=foo
                query = UriCompat.getQueryParameter(data, "q");
            }
            break;
        case "apps":
        case "amazon.com":
        case "www.amazon.com":
            // amzn://apps/android?p=app.id
            // http://amazon.com/gp/mas/dl/android?s=app.id
            packageName = UriCompat.getQueryParameter(data, "p");
            query = UriCompat.getQueryParameter(data, "s");
            break;
        }
    } else if ("fdroid.app".equals(scheme)) {
        // fdroid.app:app.id
        packageName = data.getSchemeSpecificPart();
    } else if ("fdroid.search".equals(scheme)) {
        // fdroid.search:query
        query = data.getSchemeSpecificPart();
    }

    if (!TextUtils.isEmpty(query)) {
        // an old format for querying via packageName
        if (query.startsWith("pname:"))
            packageName = query.split(":")[1];

        // sometimes, search URLs include pub: or other things before the query string
        if (query.contains(":"))
            query = query.split(":")[1];
    }

    if (!TextUtils.isEmpty(packageName)) {
        Utils.debugLog(TAG, "FDroid launched via app link for '" + packageName + "'");
        Intent intentToInvoke = new Intent(this, AppDetails.class);
        intentToInvoke.putExtra(AppDetails.EXTRA_APPID, packageName);
        startActivity(intentToInvoke);
    } else if (!TextUtils.isEmpty(query)) {
        Utils.debugLog(TAG, "FDroid launched via search link for '" + query + "'");
        performSearch(query);
    }
}

From source file:org.fdroid.fdroid.FDroid.java

private void handleSearchOrAppViewIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        performSearch(query);//from   w ww .  j a  va 2  s  .  com
        return;
    }

    final Uri data = intent.getData();
    if (data == null) {
        return;
    }

    final String scheme = data.getScheme();
    final String path = data.getPath();
    String packageName = null;
    String query = null;
    if (data.isHierarchical()) {
        final String host = data.getHost();
        if (host == null) {
            return;
        }
        switch (host) {
        case "f-droid.org":
            if (path.startsWith("/repository/browse")) {
                // http://f-droid.org/repository/browse?fdfilter=search+query
                query = UriCompat.getQueryParameter(data, "fdfilter");

                // http://f-droid.org/repository/browse?fdid=packageName
                packageName = UriCompat.getQueryParameter(data, "fdid");
            } else if (path.startsWith("/app")) {
                // http://f-droid.org/app/packageName
                packageName = data.getLastPathSegment();
                if ("app".equals(packageName)) {
                    packageName = null;
                }
            }
            break;
        case "details":
            // market://details?id=app.id
            packageName = UriCompat.getQueryParameter(data, "id");
            break;
        case "search":
            // market://search?q=query
            query = UriCompat.getQueryParameter(data, "q");
            break;
        case "play.google.com":
            if (path.startsWith("/store/apps/details")) {
                // http://play.google.com/store/apps/details?id=app.id
                packageName = UriCompat.getQueryParameter(data, "id");
            } else if (path.startsWith("/store/search")) {
                // http://play.google.com/store/search?q=foo
                query = UriCompat.getQueryParameter(data, "q");
            }
            break;
        case "apps":
        case "amazon.com":
        case "www.amazon.com":
            // amzn://apps/android?p=app.id
            // http://amazon.com/gp/mas/dl/android?s=app.id
            packageName = UriCompat.getQueryParameter(data, "p");
            query = UriCompat.getQueryParameter(data, "s");
            break;
        }
    } else if ("fdroid.app".equals(scheme)) {
        // fdroid.app:app.id
        packageName = data.getSchemeSpecificPart();
    } else if ("fdroid.search".equals(scheme)) {
        // fdroid.search:query
        query = data.getSchemeSpecificPart();
    }

    if (!TextUtils.isEmpty(query)) {
        // an old format for querying via packageName
        if (query.startsWith("pname:")) {
            packageName = query.split(":")[1];
        }

        // sometimes, search URLs include pub: or other things before the query string
        if (query.contains(":")) {
            query = query.split(":")[1];
        }
    }

    if (!TextUtils.isEmpty(packageName)) {
        Utils.debugLog(TAG, "FDroid launched via app link for '" + packageName + "'");
        Intent intentToInvoke = new Intent(this, AppDetails.class);
        intentToInvoke.putExtra(AppDetails.EXTRA_APPID, packageName);
        startActivity(intentToInvoke);
        finish();
    } else if (!TextUtils.isEmpty(query)) {
        Utils.debugLog(TAG, "FDroid launched via search link for '" + query + "'");
        performSearch(query);
    }
}

From source file:com.yeldi.yeldibazaar.AppDetails.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("lightTheme", false))
        setTheme(R.style.AppThemeLight);

    super.onCreate(savedInstanceState);
    ActionBarCompat abCompat = ActionBarCompat.create(this);
    abCompat.setDisplayHomeAsUpEnabled(true);

    setContentView(R.layout.appdetails);

    Intent i = getIntent();/*  ww w. j av a 2 s  .c o  m*/
    appid = "";
    Uri data = getIntent().getData();
    if (data != null) {
        if (data.isHierarchical()) {
            if (data.getHost().equals("details")) {
                // market://details?id=app.id
                appid = data.getQueryParameter("id");
            } else {
                // https://f-droid.org/app/app.id
                appid = data.getLastPathSegment();
            }
        } else {
            // fdroid.app:app.id (old scheme)
            appid = data.getEncodedSchemeSpecificPart();
        }
        Log.d("FDroid", "AppDetails launched from link, for '" + appid + "'");
    } else if (!i.hasExtra("appid")) {
        Log.d("FDroid", "No application ID in AppDetails!?");
    } else {
        appid = i.getStringExtra("appid");
    }

    // Set up the list...
    headerView = new LinearLayout(this);
    ListView lv = (ListView) findViewById(android.R.id.list);
    lv.addHeaderView(headerView);
    ApkListAdapter la = new ApkListAdapter(this, null);
    setListAdapter(la);

    mPm = getPackageManager();
    // Get the preferences we're going to use in this Activity...
    AppDetails old = (AppDetails) getLastNonConfigurationInstance();
    if (old != null) {
        copyState(old);
    } else {
        if (!reset()) {
            finish();
            return;
        }
        resetRequired = false;
    }
    startViews();

}

From source file:com.microsoft.services.msa.AuthorizationRequest.java

/**
 * Called when the end uri is loaded./*from   www  . ja v a  2 s . c o  m*/
 *
 * This method will read the uri's query parameters and fragment, and respond with the
 * appropriate action.
 *
 * @param endUri that was loaded
 */
private void onEndUri(Uri endUri) {
    // If we are on an end uri, the response could either be in
    // the fragment or the query parameters. The response could
    // either be successful or it could contain an error.
    // Check all situations and call the listener's appropriate callback.
    // Callback the listener on the UI/main thread. We could call it right away since
    // we are on the UI/main thread, but it is probably better that we finish up with
    // the WebView code before we callback on the listener.
    boolean hasFragment = endUri.getFragment() != null;
    boolean hasQueryParameters = endUri.getQuery() != null;
    boolean invalidUri = !hasFragment && !hasQueryParameters;
    boolean isHierarchical = endUri.isHierarchical();

    // check for an invalid uri, and leave early
    if (invalidUri) {
        this.onInvalidUri();
        return;
    }

    if (hasFragment) {
        Map<String, String> fragmentParameters = AuthorizationRequest.getFragmentParametersMap(endUri);

        boolean isSuccessfulResponse = fragmentParameters.containsKey(OAuth.ACCESS_TOKEN)
                && fragmentParameters.containsKey(OAuth.TOKEN_TYPE);
        if (isSuccessfulResponse) {

            //                SharedPreferences preferences = activity.getSharedPreferences("csPrivateSpace", Context.MODE_PRIVATE);
            //                SharedPreferences.Editor editor = preferences.edit();
            //                editor.putString("funUserID", fragmentParameters.get("user_id"));
            //                editor.apply();

            this.onAccessTokenResponse(fragmentParameters);
            return;
        }

        String error = fragmentParameters.get(OAuth.ERROR);
        if (error != null) {
            String errorDescription = fragmentParameters.get(OAuth.ERROR_DESCRIPTION);
            String errorUri = fragmentParameters.get(OAuth.ERROR_URI);
            this.onError(error, errorDescription, errorUri);
            return;
        }
    }

    if (hasQueryParameters && isHierarchical) {
        String code = endUri.getQueryParameter(OAuth.CODE);
        if (code != null) {
            this.onAuthorizationResponse(code);
            return;
        }

        String error = endUri.getQueryParameter(OAuth.ERROR);
        if (error != null) {
            String errorDescription = endUri.getQueryParameter(OAuth.ERROR_DESCRIPTION);
            String errorUri = endUri.getQueryParameter(OAuth.ERROR_URI);
            this.onError(error, errorDescription, errorUri);
            return;
        }
    }

    if (hasQueryParameters && !isHierarchical) {
        String[] pairs = endUri.getQuery().split("&|=");
        for (int i = 0; i < pairs.length; i = +2) {
            if (pairs[i].equals(OAuth.CODE)) {
                this.onAuthorizationResponse(pairs[i + 1]);
                return;
            }
        }
    }

    // if the code reaches this point, the uri was invalid
    // because it did not contain either a successful response
    // or an error in either the queryParameter or the fragment
    this.onInvalidUri();
}