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

The following examples show how to use android.net.Uri#normalizeScheme() . 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: NdefRecord.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new NDEF Record containing a URI.<p>
 * Use this method to encode a URI (or URL) into an NDEF Record.<p>
 * Uses the well known URI type representation: {@link #TNF_WELL_KNOWN}
 * and {@link #RTD_URI}. This is the most efficient encoding
 * of a URI into NDEF.<p>
 * The uri parameter will be normalized with
 * {@link Uri#normalizeScheme} to set the scheme to lower case to
 * follow Android best practices for intent filtering.
 * However the unchecked exception
 * {@link IllegalArgumentException} may be thrown if the uri
 * parameter has serious problems, for example if it is empty, so always
 * catch this exception if you are passing user-generated data into this
 * method.<p>
 *
 * Reference specification: NFCForum-TS-RTD_URI_1.0
 *
 * @param uri URI to encode.
 * @return an NDEF Record containing the URI
 * @throws IllegalArugmentException if the uri is empty or invalid
 */
public static NdefRecord createUri(Uri uri) {
    if (uri == null) throw new NullPointerException("uri is null");

    uri = uri.normalizeScheme();
    String uriString = uri.toString();
    if (uriString.length() == 0) throw new IllegalArgumentException("uri is empty");

    byte prefix = 0;
    for (int i = 1; i < URI_PREFIX_MAP.length; i++) {
        if (uriString.startsWith(URI_PREFIX_MAP[i])) {
            prefix = (byte) i;
            uriString = uriString.substring(URI_PREFIX_MAP[i].length());
            break;
        }
    }
    byte[] uriBytes = uriString.getBytes(StandardCharsets.UTF_8);
    byte[] recordBytes = new byte[uriBytes.length + 1];
    recordBytes[0] = prefix;
    System.arraycopy(uriBytes, 0, recordBytes, 1, uriBytes.length);
    return new NdefRecord(TNF_WELL_KNOWN, RTD_URI, null, recordBytes);
}
 
Example 2
Source File: LinkUtil.java    From Slide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Corrects mistakes users might make when typing URLs, e.g. case sensitivity in the scheme
 * and converts to Uri
 *
 * @param url URL to correct
 * @return corrected as a Uri
 */
public static Uri formatURL(String url) {
    if (url.startsWith("//")) {
        url = "https:" + url;
    }
    if (url.startsWith("/")) {
        url = "https://reddit.com" + url;
    }
    if (!url.contains("://") && !url.startsWith("mailto:")) {
        url = "http://" + url;
    }

    Uri uri = Uri.parse(url);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        return uri.normalizeScheme();
    } else {
        return uri;
    }
}
 
Example 3
Source File: NdefRecord.java    From effective_android_sample with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new NDEF Record containing a URI.<p>
 * Use this method to encode a URI (or URL) into an NDEF Record.<p>
 * Uses the well known URI type representation: {@link #TNF_WELL_KNOWN}
 * and {@link #RTD_URI}. This is the most efficient encoding
 * of a URI into NDEF.<p>
 * The uri parameter will be normalized with
 * {@link Uri#normalizeScheme} to set the scheme to lower case to
 * follow Android best practices for intent filtering.
 * However the unchecked exception
 * {@link IllegalArgumentException} may be thrown if the uri
 * parameter has serious problems, for example if it is empty, so always
 * catch this exception if you are passing user-generated data into this
 * method.<p>
 *
 * Reference specification: NFCForum-TS-RTD_URI_1.0
 *
 * @param uri URI to encode.
 * @return an NDEF Record containing the URI
 * @throws IllegalArugmentException if the uri is empty or invalid
 */
public static NdefRecord createUri(Uri uri) {
    if (uri == null) throw new NullPointerException("uri is null");

    uri = uri.normalizeScheme();
    String uriString = uri.toString();
    if (uriString.length() == 0) throw new IllegalArgumentException("uri is empty");

    byte prefix = 0;
    for (int i = 1; i < URI_PREFIX_MAP.length; i++) {
        if (uriString.startsWith(URI_PREFIX_MAP[i])) {
            prefix = (byte) i;
            uriString = uriString.substring(URI_PREFIX_MAP[i].length());
            break;
        }
    }
    byte[] uriBytes = uriString.getBytes(Charsets.UTF_8);
    byte[] recordBytes = new byte[uriBytes.length + 1];
    recordBytes[0] = prefix;
    System.arraycopy(uriBytes, 0, recordBytes, 1, uriBytes.length);
    return new NdefRecord(TNF_WELL_KNOWN, RTD_URI, null, recordBytes);
}