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

The following examples show how to use android.net.Uri#getEncodedQuery() . 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: Floo.java    From Floo with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a map of the unique names of all query parameters. Iterating
 * over the set will return the names in order of their first occurrence.
 *
 * @param uri The URI to transform.
 * @return A map of decoded names.
 */
@NonNull
private static Map<String, String> encodedQueryParameters(@NonNull final Uri uri) {
  final HashMap<String, String> map = new HashMap<>();
  final String query = uri.getEncodedQuery();
  if (query != null) {

    String[] ands = query.split("&");
    for (String and : ands) {
      int splitIndex = and.indexOf("=");
      if (splitIndex != -1) {
        map.put(and.substring(0, splitIndex), and.substring(splitIndex + 1));
      } else {
        Log.w("ParametersParseError", "query: " + query);
      }
    }
  }
  return map;
}
 
Example 2
Source File: HttpClient.java    From Dashchan with Apache License 2.0 6 votes vote down vote up
URL encodeUri(Uri uri) throws MalformedURLException {
	StringBuilder uriStringBuilder = new StringBuilder();
	uriStringBuilder.append(uri.getScheme()).append("://");
	String host = IDN.toASCII(uri.getHost());
	uriStringBuilder.append(host);
	int port = uri.getPort();
	if (port != -1) {
		uriStringBuilder.append(':').append(port);
	}
	String path = uri.getEncodedPath();
	if (!StringUtils.isEmpty(path)) {
		encodeUriAppend(uriStringBuilder, path);
	}
	String query = uri.getEncodedQuery();
	if (!StringUtils.isEmpty(query)) {
		uriStringBuilder.append('?');
		encodeUriAppend(uriStringBuilder, query);
	}
	return new URL(uriStringBuilder.toString());
}
 
Example 3
Source File: UriCompact.java    From ActivityRouter with Apache License 2.0 6 votes vote down vote up
/**
 * Call Uri#getQueryParameterNames() below api 11.
 *
 * @param uri Uri
 * @return Set
 */
public static Set<String> getQueryParameterNames(Uri uri) {
    String query = uri.getEncodedQuery();
    if (query == null) {
        return Collections.emptySet();
    }

    Set<String> names = new LinkedHashSet<String>();
    int start = 0;
    do {
        int next = query.indexOf('&', start);
        int end = (next == -1) ? query.length() : next;

        int separator = query.indexOf('=', start);
        if (separator > end || separator == -1) {
            separator = end;
        }

        String name = query.substring(start, separator);
        names.add(Uri.decode(name));
        // Move start to end of name.
        start = end + 1;
    } while (start < query.length());

    return Collections.unmodifiableSet(names);
}
 
Example 4
Source File: NewConversationActivity.java    From deltachat-android with GNU General Public License v3.0 5 votes vote down vote up
private Map<String, String> getMailtoQueryMap(Uri uri) {
  Map<String, String> mailtoQueryMap = new HashMap<>();
  String query =  uri.getEncodedQuery();
  if (query != null && !query.isEmpty()) {
    String[] queryArray = query.split(QUERY_SEPARATOR);
    for(String queryEntry : queryArray) {
      String[] queryEntryArray = queryEntry.split(KEY_VALUE_SEPARATOR);
      mailtoQueryMap.put(queryEntryArray[0], URLDecoder.decode(queryEntryArray[1]));
    }
  }
  return mailtoQueryMap;
}
 
Example 5
Source File: CompatUri.java    From android-oauth-client with Apache License 2.0 5 votes vote down vote up
static Set<String> getQueryParameterNames(Uri uri) {
    if (uri.isOpaque()) {
        throw new UnsupportedOperationException(NOT_HIERARCHICAL);
    }

    String query = uri.getEncodedQuery();
    if (query == null) {
        return Collections.emptySet();
    }

    Set<String> names = new LinkedHashSet<String>();
    int start = 0;
    do {
        int next = query.indexOf('&', start);
        int end = (next == -1) ? query.length() : next;

        int separator = query.indexOf('=', start);
        if (separator > end || separator == -1) {
            separator = end;
        }

        String name = query.substring(start, separator);
        names.add(Uri.decode(name));

        // Move start to end of name
        start = end + 1;
    } while (start < query.length());

    return Collections.unmodifiableSet(names);
}
 
Example 6
Source File: UriUtil.java    From android-lite-http with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a set of the unique names of all query parameters. Iterating
 * over the set will return the names in order of their first occurrence.
 *
 * @return a set of decoded names
 * @throws UnsupportedOperationException if this isn't a hierarchical URI
 */
public static Set<String> getQueryParameterNames(Uri uri) {
    if (uri.isOpaque()) {
        return Collections.emptySet();
    }

    String query = uri.getEncodedQuery();
    if (query == null) {
        return Collections.emptySet();
    }

    Set<String> names = new LinkedHashSet<String>();
    int start = 0;
    do {
        int next = query.indexOf('&', start);
        int end = (next == -1) ? query.length() : next;

        int separator = query.indexOf('=', start);
        if (separator > end || separator == -1) {
            separator = end;
        }

        String name = query.substring(start, separator);
        names.add(name);
        // Move start to end of name.
        start = end + 1;
    } while (start < query.length());

    return Collections.unmodifiableSet(names);
}
 
Example 7
Source File: UriUtils.java    From arca-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Set<String> getQueryParameterNames(final Uri uri) {
	if (uri.isOpaque()) {
		throw new UnsupportedOperationException("This isn't a hierarchical URI.");
	}

	final String query = uri.getEncodedQuery();
	if (query == null) {
		return Collections.emptySet();
	}

	final Set<String> names = new LinkedHashSet<String>();
	int start = 0;
	do {
		final int next = query.indexOf('&', start);
		final int end = (next == -1) ? query.length() : next;

		int separator = query.indexOf('=', start);
		if (separator > end || separator == -1) {
			separator = end;
		}

		final String name = query.substring(start, separator);
		names.add(Uri.decode(name));

		// Move start to end of name.
		start = end + 1;
	} while (start < query.length());

	return Collections.unmodifiableSet(names);
}
 
Example 8
Source File: UriMatchers.java    From android-test with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a set of the unique names of all query parameters. Iterating over the set will return
 * the names in order of their first occurrence.
 *
 * <p>This method was added to Uri class in android api 11. So, for working with API level less
 * then that we had to re-implement it.
 *
 * @throws UnsupportedOperationException if this isn't a hierarchical URI
 * @return a set of decoded names
 */
// VisibleForTesting
static Set<String> getQueryParameterNames(Uri uri) {
  checkArgument(!uri.isOpaque(), "This isn't a hierarchical URI.");
  String query = uri.getEncodedQuery();
  if (query == null) {
    return Collections.emptySet();
  }

  Set<String> names = new LinkedHashSet<String>();
  int start = 0;
  do {
    int next = query.indexOf('&', start);
    int end = (next == -1) ? query.length() : next;

    int separator = query.indexOf('=', start);
    if (separator > end || separator == -1) {
      separator = end;
    }

    String name = query.substring(start, separator);
    names.add(Uri.decode(name));

    // Move start to end of name.
    start = end + 1;
  } while (start < query.length());

  return Collections.unmodifiableSet(names);
}
 
Example 9
Source File: CompatUri.java    From android-java-connect-rest-sample with MIT License 5 votes vote down vote up
/**
 * Returns a set of the unique names of all query parameters. Iterating
 * over the set will return the names in order of their first occurrence.
 * Extracted from Uri#getQueryParameterNames() API 22.
 *
 * @throws UnsupportedOperationException if this isn't a hierarchical URI
 * @see Uri#getQueryParameterNames()
 *
 * @return a set of decoded names
 */
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static Set<String> getQueryParameterNames(Uri uri) {
    if (uri.isOpaque()) {
        throw new UnsupportedOperationException(NOT_HIERARCHICAL);
    }

    String query = uri.getEncodedQuery();
    if (query == null) {
        return Collections.emptySet();
    }

    Set<String> names = new LinkedHashSet<>();
    int start = 0;
    do {
        int next = query.indexOf('&', start);
        int end = (next == -1) ? query.length() : next;

        int separator = query.indexOf('=', start);
        if (separator > end || separator == -1) {
            separator = end;
        }

        String name = query.substring(start, separator);
        names.add(Uri.decode(name));

        // Move start to end of name.
        start = end + 1;
    } while (start < query.length());

    return Collections.unmodifiableSet(names);
}
 
Example 10
Source File: CompatUri.java    From droidddle with Apache License 2.0 5 votes vote down vote up
public static Set<String> getQueryParameterNames(Uri uri) {
    if (uri.isOpaque()) {
        throw new UnsupportedOperationException(NOT_HIERARCHICAL);
    }

    String query = uri.getEncodedQuery();
    if (query == null) {
        return Collections.emptySet();
    }

    Set<String> names = new LinkedHashSet<String>();
    int start = 0;
    do {
        int next = query.indexOf('&', start);
        int end = (next == -1) ? query.length() : next;

        int separator = query.indexOf('=', start);
        if (separator > end || separator == -1) {
            separator = end;
        }

        String name = query.substring(start, separator);
        names.add(Uri.decode(name));

        // Move start to end of name
        start = end + 1;
    } while (start < query.length());

    return Collections.unmodifiableSet(names);
}
 
Example 11
Source File: QchatSchemeActivity.java    From imsdk-android with MIT License 5 votes vote down vote up
public static Set<String> getQueryParameterNames(Uri uri) {
    if (uri.isOpaque()) {
        throw new UnsupportedOperationException("This isn't a hierarchical URI.");
    }

    String query = uri.getEncodedQuery();
    if (query == null) {
        return Collections.emptySet();
    }

    Set<String> names = new LinkedHashSet<String>();
    int start = 0;
    do {
        int next = query.indexOf('&', start);
        int end = next == -1 ? query.length() : next;

        int separator = query.indexOf('=', start);
        if (separator > end || separator == -1) {
            separator = end;
        }

        String name = query.substring(start, separator);
        names.add(Uri.decode(name));

        // Move start to end of name.
        start = end + 1;
    } while (start < query.length());

    return Collections.unmodifiableSet(names);
}
 
Example 12
Source File: ExternalStorageProviderProxy.java    From island with Apache License 2.0 5 votes vote down vote up
private static Uri replaceAuthorityInUri(final Uri uri, final String authority) {
	final Uri.Builder builder = new Uri.Builder().scheme(uri.getScheme()).authority(authority).encodedPath(uri.getEncodedPath());
	final String query = uri.getEncodedQuery();
	if (query != null) builder.encodedQuery(query);
	final String fragment = uri.getEncodedFragment();
	if (fragment != null) builder.encodedFragment(fragment);
	return builder.build();
}
 
Example 13
Source File: ShuttleProvider.java    From island with Apache License 2.0 5 votes vote down vote up
private static Uri replaceAuthorityInUri(final Uri uri, final String authority) {
	final Uri.Builder builder = new Uri.Builder().scheme(uri.getScheme()).authority(authority).encodedPath(uri.getEncodedPath());
	final String query = uri.getEncodedQuery();
	if (query != null) builder.encodedQuery(query);
	final String fragment = uri.getEncodedFragment();
	if (fragment != null) builder.encodedFragment(fragment);
	return builder.build();
}
 
Example 14
Source File: CompatUri.java    From mirror with Apache License 2.0 5 votes vote down vote up
static Set<String> getQueryParameterNames(Uri uri) {
    if (uri.isOpaque()) {
        throw new UnsupportedOperationException(NOT_HIERARCHICAL);
    }

    String query = uri.getEncodedQuery();
    if (query == null) {
        return Collections.emptySet();
    }

    Set<String> names = new LinkedHashSet<>();
    int start = 0;
    do {
        int next = query.indexOf('&', start);
        int end = (next == -1) ? query.length() : next;

        int separator = query.indexOf('=', start);
        if (separator > end || separator == -1) {
            separator = end;
        }

        String name = query.substring(start, separator);
        names.add(Uri.decode(name));

        // Move start to end of name
        start = end + 1;
    } while (start < query.length());

    return Collections.unmodifiableSet(names);
}
 
Example 15
Source File: WebPlayerActivity.java    From mv-android-client with Apache License 2.0 5 votes vote down vote up
private static Uri.Builder appendQuery(Uri.Builder builder, String query) {
    Uri current = builder.build();
    String oldQuery = current.getEncodedQuery();
    if (oldQuery != null && oldQuery.length() > 0) {
        query = oldQuery + "&" + query;
    }
    return builder.encodedQuery(query);
}
 
Example 16
Source File: UriUtils.java    From JIMU with Apache License 2.0 5 votes vote down vote up
public static Set<String> getQueryParameterNames(Uri uri) {
    String query = uri.getEncodedQuery();
    if (query == null) {
        return Collections.emptySet();
    }

    Set<String> names = new LinkedHashSet<String>();
    int start = 0;
    do {
        int next = query.indexOf('&', start);
        int end = (next == -1) ? query.length() : next;

        int separator = query.indexOf('=', start);
        if (separator > end || separator == -1) {
            separator = end;
        }

        String name = query.substring(start, separator);
        try {
            names.add(URLDecoder.decode(name, "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        start = end + 1;
    } while (start < query.length());

    return Collections.unmodifiableSet(names);
}
 
Example 17
Source File: General.java    From RedReader with GNU General Public License v3.0 5 votes vote down vote up
public static Set<String> getUriQueryParameterNames(final Uri uri) {

		if(uri.isOpaque()) {
			throw new UnsupportedOperationException("This isn't a hierarchical URI.");
		}

		final String query = uri.getEncodedQuery();
		if(query == null) {
			return Collections.emptySet();
		}

		final Set<String> names = new LinkedHashSet<>();
		int pos = 0;
		while(pos < query.length()) {
			int next = query.indexOf('&', pos);
			int end = (next == -1) ? query.length() : next;

			int separator = query.indexOf('=', pos);
			if (separator > end || separator == -1) {
				separator = end;
			}

			String name = query.substring(pos, separator);
			names.add(Uri.decode(name));

			// Move start to end of name.
			pos = end + 1;
		}

		return Collections.unmodifiableSet(names);
	}
 
Example 18
Source File: Bundle.java    From Small with Apache License 2.0 4 votes vote down vote up
private Boolean matchesRule(Uri uri) {
    /* e.g.
     *  input
     *      - uri: http://base/abc.html
     *      - self.uri: http://base
     *      - self.rules: abc.html -> AbcController
     *  output
     *      - target => AbcController
     */
    String uriString = uri.toString();
    if (this.uriString == null || !uriString.startsWith(this.uriString)) return false;

    String srcPath = uriString.substring(this.uriString.length());
    String srcQuery = uri.getEncodedQuery();
    if (srcQuery != null) {
        srcPath = srcPath.substring(0, srcPath.length() - srcQuery.length() - 1);
    }

    String dstPath = null;
    String dstQuery = srcQuery;

    for (String key : this.rules.keySet()) {
        // TODO: regex match and replace
        if (key.equals(srcPath)) dstPath = this.rules.get(key);
        if (dstPath != null) break;
    }
    if (dstPath == null) return false;

    int index = dstPath.indexOf("?");
    if (index > 0) {
        if (dstQuery != null) {
            dstQuery = dstQuery + "&" + dstPath.substring(index + 1);
        } else {
            dstQuery = dstPath.substring(index + 1);
        }
        dstPath = dstPath.substring(0, index);
    }

    this.path = dstPath;
    this.query = dstQuery;
    return true;
}
 
Example 19
Source File: UriUtil.java    From android-lite-http with Apache License 2.0 4 votes vote down vote up
/**
 * Searches the query string for parameter values with the given key.
 *
 * @param key which will be encoded
 * @return a list of decoded values
 * @throws UnsupportedOperationException if this isn't a hierarchical URI
 * @throws NullPointerException          if key is null
 */
public static List<String> getQueryParameters(Uri uri, String key) {
    if (uri.isOpaque()) {
        return Collections.emptyList();
    }
    if (key == null) {
        throw new NullPointerException("key");
    }

    String query = uri.getEncodedQuery();
    if (query == null) {
        return Collections.emptyList();
    }

    ArrayList<String> values = new ArrayList<String>();

    int start = 0;
    do {
        int nextAmpersand = query.indexOf('&', start);
        int end = nextAmpersand != -1 ? nextAmpersand : query.length();

        int separator = query.indexOf('=', start);
        if (separator > end || separator == -1) {
            separator = end;
        }

        if (separator - start == key.length()
                && query.regionMatches(start, key, 0, key.length())) {
            if (separator == end) {
                values.add("");
            } else {
                values.add(query.substring(separator + 1, end));
            }
        }

        // Move start to end of name.
        if (nextAmpersand != -1) {
            start = nextAmpersand + 1;
        } else {
            break;
        }
    } while (true);

    return Collections.unmodifiableList(values);
}
 
Example 20
Source File: BarcodeData.java    From xmrwallet with Apache License 2.0 4 votes vote down vote up
/**
 * Parse and decode a monero scheme string. It is here because it needs to validate the data.
 *
 * @param uri String containing a monero URL
 * @return BarcodeData object or null if uri not valid
 */

static public BarcodeData parseMoneroUri(String uri) {
    Timber.d("parseMoneroUri=%s", uri);

    if (uri == null) return null;

    if (!uri.startsWith(XMR_SCHEME)) return null;

    String noScheme = uri.substring(XMR_SCHEME.length());
    Uri monero = Uri.parse(noScheme);
    Map<String, String> parms = new HashMap<>();
    String query = monero.getEncodedQuery();
    if (query != null) {
        String[] args = query.split("&");
        for (String arg : args) {
            String[] namevalue = arg.split("=");
            if (namevalue.length == 0) {
                continue;
            }
            parms.put(Uri.decode(namevalue[0]).toLowerCase(),
                    namevalue.length > 1 ? Uri.decode(namevalue[1]) : "");
        }
    }
    String address = monero.getPath();

    String paymentId = parms.get(XMR_PAYMENTID);
    // no support for payment ids!
    if (paymentId != null) {
        Timber.e("no support for payment ids!");
        return null;
    }

    String description = parms.get(XMR_DESCRIPTION);
    String amount = parms.get(XMR_AMOUNT);
    if (amount != null) {
        try {
            Double.parseDouble(amount);
        } catch (NumberFormatException ex) {
            Timber.d(ex.getLocalizedMessage());
            return null; // we have an amount but its not a number!
        }
    }

    if (!Wallet.isAddressValid(address)) {
        Timber.d("address invalid");
        return null;
    }
    return new BarcodeData(Asset.XMR, address, paymentId, description, amount);
}