Java Code Examples for android.net.Uri#isHierarchical()

The following examples show how to use android.net.Uri#isHierarchical() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomMatchers.java    From OpenYOLO-Android with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean matchesSafely(Uri item) {
    if (!item.isAbsolute()
            || !item.isHierarchical()
            || TextUtils.isEmpty(item.getScheme())
            || TextUtils.isEmpty(item.getAuthority())) {
        return false;
    }

    if (!mPermittedSchemes.isEmpty() && !mPermittedSchemes.contains(item.getScheme())) {
        return false;
    }

    if (mAllowPathQueryOrFragment) {
        return true;
    }

    return TextUtils.isEmpty(item.getPath())
            && item.getQuery() == null
            && item.getFragment() == null;
}
 
Example 2
Source File: Utils.java    From Android-RecurrencePicker with Apache License 2.0 6 votes vote down vote up
/**
 * If the given intent specifies a time (in milliseconds since the epoch),
 * then that time is returned. Otherwise, the current time is returned.
 */
public static final long timeFromIntentInMillis(Intent intent) {
    // If the time was specified, then use that. Otherwise, use the current
    // time.
    Uri data = intent.getData();
    long millis = intent.getLongExtra(EXTRA_EVENT_BEGIN_TIME, -1);
    if (millis == -1 && data != null && data.isHierarchical()) {
        List<String> path = data.getPathSegments();
        if (path.size() == 2 && path.get(0).equals("time")) {
            try {
                millis = Long.valueOf(data.getLastPathSegment());
            } catch (NumberFormatException e) {
                Log.i("Calendar", "timeFromIntentInMillis: Data existed but no valid time "
                        + "found. Using current time.");
            }
        }
    }
    if (millis <= 0) {
        millis = System.currentTimeMillis();
    }
    return millis;
}
 
Example 3
Source File: WXPageActivity.java    From yanxuan-weex-demo with MIT License 5 votes vote down vote up
private String getUrl(Uri uri) {
  String url = uri.toString();
  String scheme = uri.getScheme();
  if (uri.isHierarchical()) {
    if (TextUtils.equals(scheme, "http") || TextUtils.equals(scheme, "https")) {
      String weexTpl = uri.getQueryParameter(Constants.WEEX_TPL_KEY);
      if (!TextUtils.isEmpty(weexTpl)) {
        url = weexTpl;
      }
    }
  }
  return url;
}
 
Example 4
Source File: JsonParser.java    From okta-sdk-appauth-android with Apache License 2.0 5 votes vote down vote up
/**
 * Gets a Uri from the JSON object with the given name.
 *
 * @param propName The name of the JSON property
 * @return The Uri value of the property with the given name
 * @throws InvalidJsonDocumentException When the property is missing or the value is empty or
 *     blank
 */
@NonNull
public Uri getRequiredUri(String propName)
        throws InvalidJsonDocumentException {
    String uriStr = getRequiredString(propName);
    Uri uri;
    try {
        uri = Uri.parse(uriStr);
    } catch (Throwable ex) {
        throw new InvalidJsonDocumentException(propName + " could not be parsed", ex);
    }

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

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

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

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

    return uri;
}
 
Example 5
Source File: LaunchOtherAppActivity.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
@Override
public void onClick(View v) {
    TextView textView = findViewById(v.getId());
    String result = textView.getText().toString().trim();

    Uri uri = Uri.parse(result);

    StringBuffer sb = new StringBuffer();
    sb.append("uri==").append(uri).append("\n");
    sb.append("scheme==").append(uri.getScheme()).append("\n");
    sb.append("host==").append(uri.getHost()).append("\n");
    sb.append("path==").append(uri.getPath()).append("\n");
    sb.append("pathSegment==").append(Arrays.toString(uri.getPathSegments().toArray())).append("\n");
    sb.append("query==").append(uri.getQuery()).append("\n");

    if (uri.isHierarchical()) {
        Optional.ofNullable(uri.getQueryParameterNames())
                .ifPresent(strings -> sb.append("QueryParameterNames==").append(strings.toString()).append("\n"));
    }


    Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();

    if (v.getId() == R.id.custom_scheme_and_host) {
        Intent intent = new Intent();
        intent.setAction("my_action");
        intent.setData(Uri.parse(result));
        ActivityInfo activityInfo = intent.resolveActivityInfo(getPackageManager(), PackageManager.MATCH_DEFAULT_ONLY);
        Optional.ofNullable(activityInfo)
                .ifPresent( activityInfo1 -> {
                    startActivity(intent);});
    }

}
 
Example 6
Source File: WXPageActivity.java    From WeexOne with MIT License 5 votes vote down vote up
private String getUrl(Uri uri) {
  String url = uri.toString();
  String scheme = uri.getScheme();
  if (uri.isHierarchical()) {
    if (TextUtils.equals(scheme, "http") || TextUtils.equals(scheme, "https")) {
      String weexTpl = uri.getQueryParameter(Constants.WEEX_TPL_KEY);
      if (!TextUtils.isEmpty(weexTpl)) {
        url = weexTpl;
      }
    }
  }
  return url;
}
 
Example 7
Source File: Configuration.java    From AppAuth-Android with Apache License 2.0 5 votes vote down vote up
@NonNull
Uri getRequiredConfigUri(String propName)
        throws InvalidConfigurationException {
    String uriStr = getRequiredConfigString(propName);
    Uri uri;
    try {
        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;
}
 
Example 8
Source File: AddRepoIntentService.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
public static String normalizeUrl(Uri uri) throws URISyntaxException {
    if (!uri.isAbsolute()) {
        throw new URISyntaxException(uri.toString(), "Must provide an absolute URI for repositories");
    }
    if (!uri.isHierarchical()) {
        throw new URISyntaxException(uri.toString(), "Must provide an hierarchical URI for repositories");
    }
    if ("content".equals(uri.getScheme())) {
        return uri.toString();
    }
    String path = uri.getPath();
    if (path != null) {
        path = path.replaceAll("//*/", "/"); // Collapse multiple forward slashes into 1.
        if (path.length() > 0 && path.charAt(path.length() - 1) == '/') {
            path = path.substring(0, path.length() - 1);
        }
    }
    String scheme = uri.getScheme();
    String host = uri.getHost();
    if (TextUtils.isEmpty(scheme) || TextUtils.isEmpty(host)) {
        return uri.toString();
    }
    return new URI(scheme.toLowerCase(Locale.ENGLISH),
            uri.getUserInfo(),
            host.toLowerCase(Locale.ENGLISH),
            uri.getPort(),
            path,
            uri.getQuery(),
            uri.getFragment()).normalize().toString();
}