List of usage examples for android.net Uri encode
public static String encode(String s)
From source file:com.piusvelte.sonet.SonetCreatePost.java
@Override public void onClick(View v) { if (v == mSend) { if (!mAccountsService.isEmpty()) { final ProgressDialog loadingDialog = new ProgressDialog(this); final AsyncTask<Void, String, Void> asyncTask = new AsyncTask<Void, String, Void>() { @Override/*from w w w . jav a2s . c o m*/ protected Void doInBackground(Void... arg0) { Iterator<Map.Entry<Long, Integer>> entrySet = mAccountsService.entrySet().iterator(); while (entrySet.hasNext()) { Map.Entry<Long, Integer> entry = entrySet.next(); final long accountId = entry.getKey(); final int service = entry.getValue(); final String placeId = mAccountsLocation.get(accountId); // post or comment! Cursor account = getContentResolver().query( Accounts.getContentUri(SonetCreatePost.this), new String[] { Accounts._ID, Accounts.TOKEN, Accounts.SECRET }, Accounts._ID + "=?", new String[] { Long.toString(accountId) }, null); if (account.moveToFirst()) { final String serviceName = Sonet.getServiceName(getResources(), service); publishProgress(serviceName); String message; SonetOAuth sonetOAuth; HttpPost httpPost; String response = null; switch (service) { case TWITTER: sonetOAuth = new SonetOAuth(BuildConfig.TWITTER_KEY, BuildConfig.TWITTER_SECRET, mSonetCrypto.Decrypt( account.getString(account.getColumnIndex(Accounts.TOKEN))), mSonetCrypto.Decrypt( account.getString(account.getColumnIndex(Accounts.SECRET)))); // limit tweets to 140, breaking up the message if necessary message = mMessage.getText().toString(); while (message.length() > 0) { final String send; if (message.length() > 140) { // need to break on a word int end = 0; int nextSpace = 0; for (int i = 0, i2 = message.length(); i < i2; i++) { end = nextSpace; if (message.substring(i, i + 1).equals(" ")) { nextSpace = i; } } // in case there are no spaces, just break on 140 if (end == 0) { end = 140; } send = message.substring(0, end); message = message.substring(end + 1); } else { send = message; message = ""; } httpPost = new HttpPost(String.format(TWITTER_UPDATE, TWITTER_BASE_URL)); // resolve Error 417 Expectation by Twitter httpPost.getParams().setBooleanParameter("http.protocol.expect-continue", false); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair(Sstatus, send)); if (placeId != null) { params.add(new BasicNameValuePair("place_id", placeId)); params.add(new BasicNameValuePair("lat", mLat)); params.add(new BasicNameValuePair("long", mLong)); } try { httpPost.setEntity(new UrlEncodedFormEntity(params)); response = SonetHttpClient.httpResponse(mHttpClient, sonetOAuth.getSignedRequest(httpPost)); } catch (UnsupportedEncodingException e) { Log.e(TAG, e.toString()); } publishProgress(serviceName, getString(response != null ? R.string.success : R.string.failure)); } break; case FACEBOOK: // handle tags StringBuilder tags = null; if (mAccountsTags.containsKey(accountId)) { String[] accountTags = mAccountsTags.get(accountId); if ((accountTags != null) && (accountTags.length > 0)) { tags = new StringBuilder(); tags.append("["); String tag_format; if (mPhotoPath != null) tag_format = "{\"tag_uid\":\"%s\",\"x\":0,\"y\":0}"; else tag_format = "%s"; for (int i = 0, l = accountTags.length; i < l; i++) { if (i > 0) tags.append(","); tags.append(String.format(tag_format, accountTags[i])); } tags.append("]"); } } if (mPhotoPath != null) { // upload photo // uploading a photo takes a long time, have the service handle it Intent i = Sonet.getPackageIntent( SonetCreatePost.this.getApplicationContext(), PhotoUploadService.class); i.setAction(Sonet.ACTION_UPLOAD); i.putExtra(Accounts.TOKEN, account.getString(account.getColumnIndex(Accounts.TOKEN))); i.putExtra(Widgets.INSTANT_UPLOAD, mPhotoPath); i.putExtra(Statuses.MESSAGE, mMessage.getText().toString()); i.putExtra(Splace, placeId); if (tags != null) i.putExtra(Stags, tags.toString()); startService(i); publishProgress(serviceName + " photo"); } else { // regular post httpPost = new HttpPost(String.format(FACEBOOK_POST, FACEBOOK_BASE_URL, Saccess_token, mSonetCrypto.Decrypt(account .getString(account.getColumnIndex(Accounts.TOKEN))))); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair(Smessage, mMessage.getText().toString())); if (placeId != null) params.add(new BasicNameValuePair(Splace, placeId)); if (tags != null) params.add(new BasicNameValuePair(Stags, tags.toString())); try { httpPost.setEntity(new UrlEncodedFormEntity(params)); response = SonetHttpClient.httpResponse(mHttpClient, httpPost); } catch (UnsupportedEncodingException e) { Log.e(TAG, e.toString()); } publishProgress(serviceName, getString(response != null ? R.string.success : R.string.failure)); } break; case MYSPACE: sonetOAuth = new SonetOAuth(BuildConfig.MYSPACE_KEY, BuildConfig.MYSPACE_SECRET, mSonetCrypto.Decrypt( account.getString(account.getColumnIndex(Accounts.TOKEN))), mSonetCrypto.Decrypt( account.getString(account.getColumnIndex(Accounts.SECRET)))); try { HttpPut httpPut = new HttpPut( String.format(MYSPACE_URL_STATUSMOOD, MYSPACE_BASE_URL)); httpPut.setEntity(new StringEntity(String.format(MYSPACE_STATUSMOOD_BODY, mMessage.getText().toString()))); response = SonetHttpClient.httpResponse(mHttpClient, sonetOAuth.getSignedRequest(httpPut)); } catch (IOException e) { Log.e(TAG, e.toString()); } // warn users about myspace permissions if (response != null) { publishProgress(serviceName, getString(R.string.success)); } else { publishProgress(serviceName, getString(R.string.failure) + " " + getString(R.string.myspace_permissions_message)); } break; case FOURSQUARE: try { message = URLEncoder.encode(mMessage.getText().toString(), "UTF-8"); if (placeId != null) { if (message != null) { httpPost = new HttpPost(String.format(FOURSQUARE_CHECKIN, FOURSQUARE_BASE_URL, placeId, message, mLat, mLong, mSonetCrypto.Decrypt(account.getString( account.getColumnIndex(Accounts.TOKEN))))); } else { httpPost = new HttpPost(String.format(FOURSQUARE_CHECKIN_NO_SHOUT, FOURSQUARE_BASE_URL, placeId, mLat, mLong, mSonetCrypto.Decrypt(account.getString( account.getColumnIndex(Accounts.TOKEN))))); } } else { httpPost = new HttpPost(String.format(FOURSQUARE_CHECKIN_NO_VENUE, FOURSQUARE_BASE_URL, message, mSonetCrypto.Decrypt(account .getString(account.getColumnIndex(Accounts.TOKEN))))); } response = SonetHttpClient.httpResponse(mHttpClient, httpPost); } catch (UnsupportedEncodingException e) { Log.e(TAG, e.toString()); } publishProgress(serviceName, getString(response != null ? R.string.success : R.string.failure)); break; case LINKEDIN: sonetOAuth = new SonetOAuth(BuildConfig.LINKEDIN_KEY, BuildConfig.LINKEDIN_SECRET, mSonetCrypto.Decrypt( account.getString(account.getColumnIndex(Accounts.TOKEN))), mSonetCrypto.Decrypt( account.getString(account.getColumnIndex(Accounts.SECRET)))); try { httpPost = new HttpPost(String.format(LINKEDIN_POST, LINKEDIN_BASE_URL)); httpPost.setEntity(new StringEntity(String.format(LINKEDIN_POST_BODY, "", mMessage.getText().toString()))); httpPost.addHeader(new BasicHeader("Content-Type", "application/xml")); response = SonetHttpClient.httpResponse(mHttpClient, sonetOAuth.getSignedRequest(httpPost)); } catch (IOException e) { Log.e(TAG, e.toString()); } publishProgress(serviceName, getString(response != null ? R.string.success : R.string.failure)); break; case IDENTICA: sonetOAuth = new SonetOAuth(BuildConfig.IDENTICA_KEY, BuildConfig.IDENTICA_SECRET, mSonetCrypto.Decrypt( account.getString(account.getColumnIndex(Accounts.TOKEN))), mSonetCrypto.Decrypt( account.getString(account.getColumnIndex(Accounts.SECRET)))); // limit tweets to 140, breaking up the message if necessary message = mMessage.getText().toString(); while (message.length() > 0) { final String send; if (message.length() > 140) { // need to break on a word int end = 0; int nextSpace = 0; for (int i = 0, i2 = message.length(); i < i2; i++) { end = nextSpace; if (message.substring(i, i + 1).equals(" ")) { nextSpace = i; } } // in case there are no spaces, just break on 140 if (end == 0) { end = 140; } send = message.substring(0, end); message = message.substring(end + 1); } else { send = message; message = ""; } httpPost = new HttpPost(String.format(IDENTICA_UPDATE, IDENTICA_BASE_URL)); // resolve Error 417 Expectation by Twitter httpPost.getParams().setBooleanParameter("http.protocol.expect-continue", false); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair(Sstatus, send)); if (placeId != null) { params.add(new BasicNameValuePair("place_id", placeId)); params.add(new BasicNameValuePair("lat", mLat)); params.add(new BasicNameValuePair("long", mLong)); } try { httpPost.setEntity(new UrlEncodedFormEntity(params)); response = SonetHttpClient.httpResponse(mHttpClient, sonetOAuth.getSignedRequest(httpPost)); } catch (UnsupportedEncodingException e) { Log.e(TAG, e.toString()); } publishProgress(serviceName, getString(response != null ? R.string.success : R.string.failure)); } break; case CHATTER: // need to get an updated access_token response = SonetHttpClient.httpResponse(mHttpClient, new HttpPost(String.format( CHATTER_URL_ACCESS, BuildConfig.CHATTER_KEY, mSonetCrypto.Decrypt( account.getString(account.getColumnIndex(Accounts.TOKEN)))))); if (response != null) { try { JSONObject jobj = new JSONObject(response); if (jobj.has("instance_url") && jobj.has(Saccess_token)) { httpPost = new HttpPost(String.format(CHATTER_URL_POST, jobj.getString("instance_url"), Uri.encode(mMessage.getText().toString()))); httpPost.setHeader("Authorization", "OAuth " + jobj.getString(Saccess_token)); response = SonetHttpClient.httpResponse(mHttpClient, httpPost); } } catch (JSONException e) { Log.e(TAG, serviceName + ":" + e.toString()); Log.e(TAG, response); } } publishProgress(serviceName, getString(response != null ? R.string.success : R.string.failure)); break; } } account.close(); } return null; } @Override protected void onProgressUpdate(String... params) { if (params.length == 1) { loadingDialog.setMessage(String.format(getString(R.string.sending), params[0])); } else { (Toast.makeText(SonetCreatePost.this, params[0] + " " + params[1], Toast.LENGTH_LONG)) .show(); } } @Override protected void onPostExecute(Void result) { if (loadingDialog.isShowing()) loadingDialog.dismiss(); finish(); } }; loadingDialog.setMessage(getString(R.string.loading)); loadingDialog.setCancelable(true); loadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { if (!asyncTask.isCancelled()) asyncTask.cancel(true); } }); loadingDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, getString(android.R.string.cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); loadingDialog.show(); asyncTask.execute(); } else (Toast.makeText(SonetCreatePost.this, "no accounts selected", Toast.LENGTH_LONG)).show(); } }
From source file:org.videolan.vlc.PlaybackService.java
private synchronized void saveMediaList() { StringBuilder locations = new StringBuilder(); for (int i = 0; i < mMediaListPlayer.getMediaList().size(); i++) locations.append(" ").append(Uri.encode(mMediaListPlayer.getMediaList().getMRL(i))); //We save a concatenated String because putStringSet is APIv11. SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit(); editor.putString("media_list", locations.toString().trim()); Util.commitPreferences(editor); }
From source file:com.android.bluetooth.map.BluetoothMapContent.java
private String getContactNameFromPhone(String phone) { String name = ""; if (TextUtils.isEmpty(phone)) { return name; }//from ww w. j av a2 s.c o m Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone)); String[] projection = { Contacts._ID, Contacts.DISPLAY_NAME }; String selection = Contacts.IN_VISIBLE_GROUP + "=1"; String orderBy = Contacts.DISPLAY_NAME + " ASC"; Cursor c = mResolver.query(uri, projection, selection, null, orderBy); try { if (c != null && c.getCount() >= 1) { c.moveToFirst(); name = c.getString(c.getColumnIndex(Contacts.DISPLAY_NAME)); } ; } finally { close(c); } return name; }
From source file:com.android.contacts.activities.PeopleActivity.java
/** * Share all contacts that are currently selected in mAllFragment. This method is pretty * inefficient for handling large numbers of contacts. I don't expect this to be a problem. *//* w w w .j ava2 s .co m*/ private void shareSelectedContacts() { final StringBuilder uriListBuilder = new StringBuilder(); for (Long contactId : mAllFragment.getSelectedContactIds()) { final Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId); final Uri lookupUri = Contacts.getLookupUri(getContentResolver(), contactUri); if (lookupUri == null) { continue; } final List<String> pathSegments = lookupUri.getPathSegments(); if (pathSegments.size() < 2) { continue; } final String lookupKey = pathSegments.get(pathSegments.size() - 2); if (uriListBuilder.length() > 0) { uriListBuilder.append(':'); } uriListBuilder.append(Uri.encode(lookupKey)); } if (uriListBuilder.length() == 0) { return; } final Uri uri = Uri.withAppendedPath(Contacts.CONTENT_MULTI_VCARD_URI, Uri.encode(uriListBuilder.toString())); final Intent intent = new Intent(Intent.ACTION_SEND); intent.setType(Contacts.CONTENT_VCARD_TYPE); intent.putExtra(Intent.EXTRA_STREAM, uri); ImplicitIntentsUtil.startActivityOutsideApp(this, intent); }
From source file:com.shafiq.myfeedle.core.MyfeedleCreatePost.java
@Override public void onClick(View v) { if (v == mSend) { if (!mAccountsService.isEmpty()) { final ProgressDialog loadingDialog = new ProgressDialog(this); final AsyncTask<Void, String, Void> asyncTask = new AsyncTask<Void, String, Void>() { @Override/*from www . j ava 2 s.c o m*/ protected Void doInBackground(Void... arg0) { Iterator<Map.Entry<Long, Integer>> entrySet = mAccountsService.entrySet().iterator(); while (entrySet.hasNext()) { Map.Entry<Long, Integer> entry = entrySet.next(); final long accountId = entry.getKey(); final int service = entry.getValue(); final String placeId = mAccountsLocation.get(accountId); // post or comment! Cursor account = getContentResolver().query( Accounts.getContentUri(MyfeedleCreatePost.this), new String[] { Accounts._ID, Accounts.TOKEN, Accounts.SECRET }, Accounts._ID + "=?", new String[] { Long.toString(accountId) }, null); if (account.moveToFirst()) { final String serviceName = Myfeedle.getServiceName(getResources(), service); publishProgress(serviceName); String message; MyfeedleOAuth myfeedleOAuth; HttpPost httpPost; String response = null; switch (service) { case TWITTER: myfeedleOAuth = new MyfeedleOAuth(TWITTER_KEY, TWITTER_SECRET, mMyfeedleCrypto.Decrypt( account.getString(account.getColumnIndex(Accounts.TOKEN))), mMyfeedleCrypto.Decrypt( account.getString(account.getColumnIndex(Accounts.SECRET)))); // limit tweets to 140, breaking up the message if necessary message = mMessage.getText().toString(); while (message.length() > 0) { final String send; if (message.length() > 140) { // need to break on a word int end = 0; int nextSpace = 0; for (int i = 0, i2 = message.length(); i < i2; i++) { end = nextSpace; if (message.substring(i, i + 1).equals(" ")) { nextSpace = i; } } // in case there are no spaces, just break on 140 if (end == 0) { end = 140; } send = message.substring(0, end); message = message.substring(end + 1); } else { send = message; message = ""; } httpPost = new HttpPost(String.format(TWITTER_UPDATE, TWITTER_BASE_URL)); // resolve Error 417 Expectation by Twitter httpPost.getParams().setBooleanParameter("http.protocol.expect-continue", false); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair(Sstatus, send)); if (placeId != null) { params.add(new BasicNameValuePair("place_id", placeId)); params.add(new BasicNameValuePair("lat", mLat)); params.add(new BasicNameValuePair("long", mLong)); } try { httpPost.setEntity(new UrlEncodedFormEntity(params)); response = MyfeedleHttpClient.httpResponse(mHttpClient, myfeedleOAuth.getSignedRequest(httpPost)); } catch (UnsupportedEncodingException e) { Log.e(TAG, e.toString()); } publishProgress(serviceName, getString(response != null ? R.string.success : R.string.failure)); } break; case FACEBOOK: // handle tags StringBuilder tags = null; if (mAccountsTags.containsKey(accountId)) { String[] accountTags = mAccountsTags.get(accountId); if ((accountTags != null) && (accountTags.length > 0)) { tags = new StringBuilder(); tags.append("["); String tag_format; if (mPhotoPath != null) tag_format = "{\"tag_uid\":\"%s\",\"x\":0,\"y\":0}"; else tag_format = "%s"; for (int i = 0, l = accountTags.length; i < l; i++) { if (i > 0) tags.append(","); tags.append(String.format(tag_format, accountTags[i])); } tags.append("]"); } } if (mPhotoPath != null) { // upload photo // uploading a photo takes a long time, have the service handle it Intent i = Myfeedle.getPackageIntent( MyfeedleCreatePost.this.getApplicationContext(), PhotoUploadService.class); i.setAction(Myfeedle.ACTION_UPLOAD); i.putExtra(Accounts.TOKEN, account.getString(account.getColumnIndex(Accounts.TOKEN))); i.putExtra(Widgets.INSTANT_UPLOAD, mPhotoPath); i.putExtra(Statuses.MESSAGE, mMessage.getText().toString()); i.putExtra(Splace, placeId); if (tags != null) i.putExtra(Stags, tags.toString()); startService(i); publishProgress(serviceName + " photo"); } else { // regular post httpPost = new HttpPost(String.format(FACEBOOK_POST, FACEBOOK_BASE_URL, Saccess_token, mMyfeedleCrypto.Decrypt(account .getString(account.getColumnIndex(Accounts.TOKEN))))); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair(Smessage, mMessage.getText().toString())); if (placeId != null) params.add(new BasicNameValuePair(Splace, placeId)); if (tags != null) params.add(new BasicNameValuePair(Stags, tags.toString())); try { httpPost.setEntity(new UrlEncodedFormEntity(params)); response = MyfeedleHttpClient.httpResponse(mHttpClient, httpPost); } catch (UnsupportedEncodingException e) { Log.e(TAG, e.toString()); } publishProgress(serviceName, getString(response != null ? R.string.success : R.string.failure)); } break; case MYSPACE: myfeedleOAuth = new MyfeedleOAuth(MYSPACE_KEY, MYSPACE_SECRET, mMyfeedleCrypto.Decrypt( account.getString(account.getColumnIndex(Accounts.TOKEN))), mMyfeedleCrypto.Decrypt( account.getString(account.getColumnIndex(Accounts.SECRET)))); try { HttpPut httpPut = new HttpPut( String.format(MYSPACE_URL_STATUSMOOD, MYSPACE_BASE_URL)); httpPut.setEntity(new StringEntity(String.format(MYSPACE_STATUSMOOD_BODY, mMessage.getText().toString()))); response = MyfeedleHttpClient.httpResponse(mHttpClient, myfeedleOAuth.getSignedRequest(httpPut)); } catch (IOException e) { Log.e(TAG, e.toString()); } // warn users about myspace permissions if (response != null) { publishProgress(serviceName, getString(R.string.success)); } else { publishProgress(serviceName, getString(R.string.failure) + " " + getString(R.string.myspace_permissions_message)); } break; case FOURSQUARE: try { message = URLEncoder.encode(mMessage.getText().toString(), "UTF-8"); if (placeId != null) { if (message != null) { httpPost = new HttpPost(String.format(FOURSQUARE_CHECKIN, FOURSQUARE_BASE_URL, placeId, message, mLat, mLong, mMyfeedleCrypto.Decrypt(account.getString( account.getColumnIndex(Accounts.TOKEN))))); } else { httpPost = new HttpPost(String.format(FOURSQUARE_CHECKIN_NO_SHOUT, FOURSQUARE_BASE_URL, placeId, mLat, mLong, mMyfeedleCrypto.Decrypt(account.getString( account.getColumnIndex(Accounts.TOKEN))))); } } else { httpPost = new HttpPost(String.format(FOURSQUARE_CHECKIN_NO_VENUE, FOURSQUARE_BASE_URL, message, mMyfeedleCrypto.Decrypt(account .getString(account.getColumnIndex(Accounts.TOKEN))))); } response = MyfeedleHttpClient.httpResponse(mHttpClient, httpPost); } catch (UnsupportedEncodingException e) { Log.e(TAG, e.toString()); } publishProgress(serviceName, getString(response != null ? R.string.success : R.string.failure)); break; case LINKEDIN: myfeedleOAuth = new MyfeedleOAuth(LINKEDIN_KEY, LINKEDIN_SECRET, mMyfeedleCrypto.Decrypt( account.getString(account.getColumnIndex(Accounts.TOKEN))), mMyfeedleCrypto.Decrypt( account.getString(account.getColumnIndex(Accounts.SECRET)))); try { httpPost = new HttpPost(String.format(LINKEDIN_POST, LINKEDIN_BASE_URL)); httpPost.setEntity(new StringEntity(String.format(LINKEDIN_POST_BODY, "", mMessage.getText().toString()))); httpPost.addHeader(new BasicHeader("Content-Type", "application/xml")); response = MyfeedleHttpClient.httpResponse(mHttpClient, myfeedleOAuth.getSignedRequest(httpPost)); } catch (IOException e) { Log.e(TAG, e.toString()); } publishProgress(serviceName, getString(response != null ? R.string.success : R.string.failure)); break; case IDENTICA: myfeedleOAuth = new MyfeedleOAuth(IDENTICA_KEY, IDENTICA_SECRET, mMyfeedleCrypto.Decrypt( account.getString(account.getColumnIndex(Accounts.TOKEN))), mMyfeedleCrypto.Decrypt( account.getString(account.getColumnIndex(Accounts.SECRET)))); // limit tweets to 140, breaking up the message if necessary message = mMessage.getText().toString(); while (message.length() > 0) { final String send; if (message.length() > 140) { // need to break on a word int end = 0; int nextSpace = 0; for (int i = 0, i2 = message.length(); i < i2; i++) { end = nextSpace; if (message.substring(i, i + 1).equals(" ")) { nextSpace = i; } } // in case there are no spaces, just break on 140 if (end == 0) { end = 140; } send = message.substring(0, end); message = message.substring(end + 1); } else { send = message; message = ""; } httpPost = new HttpPost(String.format(IDENTICA_UPDATE, IDENTICA_BASE_URL)); // resolve Error 417 Expectation by Twitter httpPost.getParams().setBooleanParameter("http.protocol.expect-continue", false); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair(Sstatus, send)); if (placeId != null) { params.add(new BasicNameValuePair("place_id", placeId)); params.add(new BasicNameValuePair("lat", mLat)); params.add(new BasicNameValuePair("long", mLong)); } try { httpPost.setEntity(new UrlEncodedFormEntity(params)); response = MyfeedleHttpClient.httpResponse(mHttpClient, myfeedleOAuth.getSignedRequest(httpPost)); } catch (UnsupportedEncodingException e) { Log.e(TAG, e.toString()); } publishProgress(serviceName, getString(response != null ? R.string.success : R.string.failure)); } break; case CHATTER: // need to get an updated access_token response = MyfeedleHttpClient.httpResponse(mHttpClient, new HttpPost( String.format(CHATTER_URL_ACCESS, CHATTER_KEY, mMyfeedleCrypto.Decrypt( account.getString(account.getColumnIndex(Accounts.TOKEN)))))); if (response != null) { try { JSONObject jobj = new JSONObject(response); if (jobj.has("instance_url") && jobj.has(Saccess_token)) { httpPost = new HttpPost(String.format(CHATTER_URL_POST, jobj.getString("instance_url"), Uri.encode(mMessage.getText().toString()))); httpPost.setHeader("Authorization", "OAuth " + jobj.getString(Saccess_token)); response = MyfeedleHttpClient.httpResponse(mHttpClient, httpPost); } } catch (JSONException e) { Log.e(TAG, serviceName + ":" + e.toString()); Log.e(TAG, response); } } publishProgress(serviceName, getString(response != null ? R.string.success : R.string.failure)); break; } } account.close(); } return null; } @Override protected void onProgressUpdate(String... params) { if (params.length == 1) { loadingDialog.setMessage(String.format(getString(R.string.sending), params[0])); } else { (Toast.makeText(MyfeedleCreatePost.this, params[0] + " " + params[1], Toast.LENGTH_LONG)).show(); } } @Override protected void onPostExecute(Void result) { if (loadingDialog.isShowing()) loadingDialog.dismiss(); finish(); } }; loadingDialog.setMessage(getString(R.string.loading)); loadingDialog.setCancelable(true); loadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { if (!asyncTask.isCancelled()) asyncTask.cancel(true); } }); loadingDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, getString(android.R.string.cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); loadingDialog.show(); asyncTask.execute(); } else (Toast.makeText(MyfeedleCreatePost.this, "no accounts selected", Toast.LENGTH_LONG)).show(); } }
From source file:org.linphone.ContactsManager.java
public Contact findContactWithAddress(ContentResolver contentResolver, LinphoneAddress address) { String sipUri = address.asStringUriOnly(); if (sipUri.startsWith("sip:")) sipUri = sipUri.substring(4);//from w w w .j a v a 2 s . co m LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); if (lc != null && lc.getFriendList() != null && LinphoneManager.getLcIfManagerNotDestroyedOrNull().getFriendList().length > 0) { for (LinphoneFriend friend : LinphoneManager.getLcIfManagerNotDestroyedOrNull().getFriendList()) { if (friend.getAddress().equals(address)) { return getContact(friend.getRefKey(), contentResolver); } } } //Find Sip address Contact contact; String[] projection = new String[] { ContactsContract.Data.CONTACT_ID, ContactsContract.Data.DISPLAY_NAME }; String selection = new StringBuilder().append(ContactsContract.CommonDataKinds.SipAddress.SIP_ADDRESS) .append(" = ?").toString(); Cursor cur = contentResolver.query(ContactsContract.Data.CONTENT_URI, projection, selection, new String[] { sipUri }, null); if (cur != null) { if (cur.moveToFirst()) { contact = Compatibility.getContact(contentResolver, cur, cur.getPosition()); cur.close(); if (contact != null) { return contact; } } cur.close(); } //Find number Uri lookupUri = Uri.withAppendedPath(android.provider.ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address.getUserName())); projection = new String[] { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.NUMBER, ContactsContract.PhoneLookup.DISPLAY_NAME }; Cursor c = contentResolver.query(lookupUri, projection, null, null, null); contact = checkPhoneQueryResult(contentResolver, c, ContactsContract.PhoneLookup.NUMBER, ContactsContract.PhoneLookup._ID, address.getUserName()); if (contact != null) { return contact; } return null; }
From source file:com.android.mms.ui.ComposeMessageActivity.java
private Uri getContactUriForEmail(String emailAddress) { Cursor cursor = SqliteWrapper.query(this, getContentResolver(), Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode(emailAddress)), new String[] { Email.CONTACT_ID, Contacts.DISPLAY_NAME }, null, null, null); if (cursor != null) { try {/*ww w .j av a 2 s .c o m*/ while (cursor.moveToNext()) { String name = cursor.getString(1); if (!TextUtils.isEmpty(name)) { return ContentUris.withAppendedId(Contacts.CONTENT_URI, cursor.getLong(0)); } } } finally { cursor.close(); } } return null; }
From source file:android.support.v7.widget.SearchView.java
/** * When a particular suggestion has been selected, perform the various lookups required * to use the suggestion. This includes checking the cursor for suggestion-specific data, * and/or falling back to the XML for defaults; It also creates REST style Uri data when * the suggestion includes a data id./*from www . ja v a 2s .c o m*/ * * @param c The suggestions cursor, moved to the row of the user's selection * @param actionKey The key code of the action key that was pressed, * or {@link KeyEvent#KEYCODE_UNKNOWN} if none. * @param actionMsg The message for the action key that was pressed, * or <code>null</code> if none. * @return An intent for the suggestion at the cursor's position. */ private Intent createIntentFromSuggestion(Cursor c, int actionKey, String actionMsg) { try { // use specific action if supplied, or default action if supplied, or fixed default String action = getColumnString(c, SearchManager.SUGGEST_COLUMN_INTENT_ACTION); if (action == null && Build.VERSION.SDK_INT >= 8) { action = mSearchable.getSuggestIntentAction(); } if (action == null) { action = Intent.ACTION_SEARCH; } // use specific data if supplied, or default data if supplied String data = getColumnString(c, SearchManager.SUGGEST_COLUMN_INTENT_DATA); if (IS_AT_LEAST_FROYO && data == null) { data = mSearchable.getSuggestIntentData(); } // then, if an ID was provided, append it. if (data != null) { String id = getColumnString(c, SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID); if (id != null) { data = data + "/" + Uri.encode(id); } } Uri dataUri = (data == null) ? null : Uri.parse(data); String query = getColumnString(c, SearchManager.SUGGEST_COLUMN_QUERY); String extraData = getColumnString(c, SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA); return createIntent(action, dataUri, extraData, query, actionKey, actionMsg); } catch (RuntimeException e) { int rowNum; try { // be really paranoid now rowNum = c.getPosition(); } catch (RuntimeException e2) { rowNum = -1; } Log.w(LOG_TAG, "Search suggestions cursor at row " + rowNum + " returned exception.", e); return null; } }
From source file:net.kidlogger.kidlogger.KLService.java
private String getNameFromContacts(String number, boolean record) { ContentResolver cr = getContentResolver(); // Get Name from Contacts Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); Cursor curId = cr.query(uri, new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null); if (curId == null) { if (record) return "unknown"; else// w w w . j a v a 2 s . c om return number + " unknown"; } if (curId.getCount() > 0) { while (curId.moveToNext()) { String name = curId.getString(curId.getColumnIndex(PhoneLookup.DISPLAY_NAME)); if (!name.equals("")) { if (record) return name; else return number + " " + name; } } } curId.close(); if (record) return "unknown"; else return number + " unknown"; }