Example usage for android.net Uri encode

List of usage examples for android.net Uri encode

Introduction

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

Prototype

public static String encode(String s) 

Source Link

Document

Encodes characters in the given string as '%'-escaped octets using the UTF-8 scheme.

Usage

From source file:org.kontalk.util.SystemUtils.java

public static Uri lookupPhoneNumber(Context context, String phoneNumber) {
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cur = context.getContentResolver().query(uri,
            new String[] { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.LOOKUP_KEY }, null,
            null, null);/* www  .ja v  a 2 s. co m*/
    if (cur != null) {
        try {
            if (cur.moveToNext()) {
                long id = cur.getLong(0);
                String lookupKey = cur.getString(1);
                return ContactsContract.Contacts.getLookupUri(id, lookupKey);
            }
        } finally {
            cur.close();
        }
    }

    return null;
}

From source file:org.samcrow.ridgesurvey.data.UploadService.java

/**
 * Writes URL-encoded form data to a PrintStream
 *
 * @param data the key-value pairs to write, not URL encoded
 * @param out  the stream to write to/*from www.j a  v a2 s.c  o m*/
 */
private static void writeFormEncodedData(@NonNull Map<String, String> data, @NonNull PrintStream out) {
    Objects.requireAllNonNull(data, out);
    int i = 0;
    for (Map.Entry<String, String> entry : data.entrySet()) {
        out.print(Uri.encode(entry.getKey()));
        out.print("=");
        out.print(Uri.encode(entry.getValue()));
        // Add the separator after each entry except the last
        if (i < data.size() - 1) {
            out.print("&");
        }
        i++;
    }
}

From source file:com.phonemetra.lockclock.weather.YahooWeatherProvider.java

@Override
public WeatherInfo getWeatherInfo(Location location, boolean metric) {
    String language = getLanguage();
    String params = String.format(Locale.US, "\"%f %f\" and locale=\"%s\"", location.getLatitude(),
            location.getLongitude(), language);
    String url = URL_PLACEFINDER + Uri.encode(params);
    JSONObject results = fetchResults(url);
    if (results == null) {
        return null;
    }//  w w w .  j  a v a2  s . co m

    try {
        JSONObject result = results.getJSONObject("Result");
        String woeid = result.getString("woeid");

        String city = null;
        for (String name : PLACE_NAMES) {
            if (!result.isNull(name)) {
                city = result.getString(name);
                if (Log.isLoggable(TAG, Log.VERBOSE)) {
                    Log.v(TAG,
                            String.format(Locale.US, "Placefinder for location %f %f " + "matched %s using %s",
                                    location.getLatitude(), location.getLongitude(), city, name));
                }
                break;
            }
        }

        // The city name in the placefinder result is HTML encoded :-(
        if (city != null) {
            city = Html.fromHtml(city).toString();
        } else {
            Log.w(TAG, "Can not resolve place name for " + location);
        }

        Log.d(TAG, "Resolved location " + location + " to " + city + " (" + woeid + ")");

        WeatherInfo info = getWeatherInfo(woeid, city, metric);
        if (info != null) {
            return info;
        }
    } catch (JSONException e) {
        Log.e(TAG, "Received malformed placefinder data (location=" + location + ", lang=" + language + ")", e);
    }

    return null;
}

From source file:com.android.dialer.calllog.ContactInfoHelper.java

/**
 * Determines the contact information for the given SIP address.
 * <p>/*www  .  j  a v  a 2  s  .com*/
 * It returns the contact info if found.
 * <p>
 * If no contact corresponds to the given SIP address, returns {@link ContactInfo#EMPTY}.
 * <p>
 * If the lookup fails for some other reason, it returns null.
 */
private ContactInfo queryContactInfoForSipAddress(String sipAddress) {
    if (TextUtils.isEmpty(sipAddress)) {
        return null;
    }
    final ContactInfo info;

    // "contactNumber" is a SIP address, so use the PhoneLookup table with the SIP parameter.
    Uri.Builder uriBuilder = PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI.buildUpon();
    uriBuilder.appendPath(Uri.encode(sipAddress));
    uriBuilder.appendQueryParameter(PhoneLookup.QUERY_PARAMETER_SIP_ADDRESS, "1");
    return lookupContactFromUri(uriBuilder.build());
}

From source file:fr.tvbarthel.attempt.googlyzooapp.MainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_license) {
        (new LicenseDialogFragment()).show(getFragmentManager(), "dialog_license");
        return true;
    } else if (id == R.id.action_more_projects) {
        (new MoreProjectDialogFragment()).show(getFragmentManager(), "dialog_more_projects");
        return true;
    } else if (id == R.id.action_about) {
        (new AboutDialogFragment()).show(getFragmentManager(), "dialog_about");
        return true;
    } else if (id == R.id.action_support) {
        startActivity(new Intent(this, SupportActivity.class));
        return true;
    } else if (id == R.id.action_contact_us) {
        String contactUriString = getString(R.string.contact_send_to_uri,
                Uri.encode(getString(R.string.contact_mail)),
                Uri.encode(getString(R.string.contact_default_subject)));
        Uri contactUri = Uri.parse(contactUriString);
        Intent sendMail = new Intent(Intent.ACTION_SENDTO);
        sendMail.setData(contactUri);/*from   ww w. j a va2s  . c  om*/
        startActivity(sendMail);
        return true;
    } else if (id == R.id.action_camera) {
        if (mRoundedOverlay.getVisibility() != View.VISIBLE) {
            mRoundedOverlay.open();
        } else {
            hideAdditionalContent();
        }
        return true;
    }
    return super.onOptionsItemSelected(item);
}

From source file:com.considine.letseat.DistanceDetailFragment.java

/**
 * Constructs a geo scheme Uri from a postal address.
 * //from  www .j a  v a 2  s .  c o  m
 * @param postalAddress A postal address.
 * @return the geo:// Uri for the postal address.
 */
private Uri constructGeoUri(String postalAddress) {
    // Concatenates the geo:// prefix to the postal address. The postal
    // address must be
    // converted to Uri format and encoded for special characters.
    return Uri.parse(GEO_URI_SCHEME_PREFIX + Uri.encode(postalAddress));
}

From source file:com.librelio.activity.StartupActivity.java

private String getAdvertisingImageURL() {
    Log.d(TAG, "Will get advertising image");
    Log.d(TAG, "Advertising url" + getString(R.string.get_advertising_image_url));
    Log.d(TAG, "Client name" + Uri.encode(LibrelioApplication.getClientName(self())));

    return new StringBuilder(getString(R.string.get_advertising_image_url))
            .append(getString(R.string.get_advertising_image_end)).toString()
            .replace(PARAM_CLIENT, Uri.encode(LibrelioApplication.getClientName(self())))
            .replace(PARAM_APP, Uri.encode(LibrelioApplication.getMagazineName(self())));
}

From source file:com.geniusgithub.contact.contact.calllog.ContactInfoHelper.java

/**
 * Determines the contact information for the given phone number.
 * <p>/*from   w ww . j  av a 2s  .  co  m*/
 * It returns the contact info if found.
 * <p>
 * If no contact corresponds to the given phone number, returns {@link ContactInfo#EMPTY}.
 * <p>
 * If the lookup fails for some other reason, it returns null.
 */
private ContactInfo queryContactInfoForPhoneNumber(String number, String countryIso) {
    String contactNumber = number;
    if (!TextUtils.isEmpty(countryIso)) {
        // Normalize the number: this is needed because the PhoneLookup query below does not
        // accept a country code as an input.
        String numberE164 = PhoneNumberUtils.formatNumberToE164(number, countryIso);
        if (!TextUtils.isEmpty(numberE164)) {
            // Only use it if the number could be formatted to E164.
            contactNumber = numberE164;
        }
    }

    // The "contactNumber" is a regular phone number, so use the PhoneLookup table.
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(contactNumber));
    ContactInfo info = lookupContactFromUri(uri);
    if (info != null && info != ContactInfo.EMPTY) {
        info.formattedNumber = formatPhoneNumber(number, null, countryIso);
    } else if (mCachedNumberLookupService != null) {
        CachedContactInfo cacheInfo = mCachedNumberLookupService.lookupCachedContactFromNumber(mContext,
                number);
        if (cacheInfo != null) {
            info = cacheInfo.getContactInfo().isBadData ? null : cacheInfo.getContactInfo();
        } else {
            info = null;
        }
    }
    return info;
}

From source file:org.smssecure.smssecure.contacts.ContactAccessor.java

public List<String> getNumbersForThreadSearchFilter(Context context, String constraint) {
    LinkedList<String> numberList = new LinkedList<>();
    Cursor cursor = null;//from w w w.ja v  a 2s  . com

    try {
        cursor = context.getContentResolver().query(
                Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(constraint)), null, null, null, null);

        while (cursor != null && cursor.moveToNext()) {
            numberList.add(cursor.getString(cursor.getColumnIndexOrThrow(Phone.NUMBER)));
        }

    } finally {
        if (cursor != null)
            cursor.close();
    }

    GroupDatabase.Reader reader = null;
    GroupRecord record;

    try {
        reader = DatabaseFactory.getGroupDatabase(context).getGroupsFilteredByTitle(constraint);

        while ((record = reader.getNext()) != null) {
            numberList.add(record.getEncodedId());
        }
    } finally {
        if (reader != null)
            reader.close();
    }

    return numberList;
}

From source file:com.fa.mastodon.activity.LoginActivity.java

/**
 * Chain together the key-value pairs into a query string, for either appending to a URL or
 * as the content of an HTTP request.//from  w  w w  .j  a  v  a 2  s  .c o m
 */
@NonNull
private static String toQueryString(Map<String, String> parameters) {
    StringBuilder s = new StringBuilder();
    String between = "";
    for (Map.Entry<String, String> entry : parameters.entrySet()) {
        s.append(between);
        s.append(Uri.encode(entry.getKey()));
        s.append("=");
        s.append(Uri.encode(entry.getValue()));
        between = "&";
    }
    return s.toString();
}