List of usage examples for android.net Uri buildUpon
public abstract Builder buildUpon();
From source file:com.he5ed.lib.cloudprovider.apis.BoxApi.java
@Override public synchronized List<Object> exploreFolder(@NonNull CFolder folder, int offset) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); }//from w w w . j a v a 2 s .c o m List<Object> list = new ArrayList<>(); String folderId = folder.getId(); Uri uri = Uri.parse(API_BASE_URL); String url = uri.buildUpon().appendEncodedPath("folders/" + folderId + "/items") .appendQueryParameter("limit", "500").appendQueryParameter("offset", String.valueOf(offset)).build() .toString(); Request request = new Request.Builder().url(url) .header("Authorization", String.format("Bearer %s", mAccessToken)).get().build(); try { Response response = mHttpClient.newCall(request).execute(); if (response.isSuccessful()) { JSONObject jsonObject = new JSONObject(response.body().string()); int total = jsonObject.getInt("total_count"); // return null if no item found if (total == 0) return null; JSONArray entries = jsonObject.getJSONArray("entries"); list.addAll(createFilteredItemsList(entries, folder)); // suspect search result over 500 items if (total > 500 && total - list.size() > 0) { list.addAll(exploreFolder(folder, 500)); } return list; } else { throw new RequestFailException(response.message(), response.code()); } } catch (JSONException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } catch (IOException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } }
From source file:com.he5ed.lib.cloudprovider.apis.OneDriveApi.java
@Override public List<Object> search(@NonNull String keyword, CFolder folder) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); }// www. ja v a 2 s . c o m List<Object> list = new ArrayList<>(); Uri uri = Uri.parse(API_BASE_URL); String url = uri.buildUpon().appendEncodedPath("drive/items/" + folder.getId() + "/view.search") .appendQueryParameter("q", keyword).build().toString(); Request request = new Request.Builder().url(url) .header("Authorization", String.format("Bearer %s", mAccessToken)).get().build(); try { Response response = mHttpClient.newCall(request).execute(); if (response.isSuccessful()) { JSONObject jsonObject = new JSONObject(response.body().string()); JSONArray entries = jsonObject.getJSONArray("value"); if (entries.length() > 0) { list.addAll(createFilteredItemsList(entries, folder)); } else { // return null if no item found return null; } // pagination available if (jsonObject.has("@odata.nextLink")) { list.addAll(searchContinue(jsonObject.getString("@odata.nextLink"), folder)); } return list; } else { throw new RequestFailException(response.message(), response.code()); } } catch (JSONException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } catch (IOException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } }
From source file:com.he5ed.lib.cloudprovider.apis.OneDriveApi.java
@Override public CFile updateFile(@NonNull CFile file, final File content) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); }/*w ww.ja va2 s . c o m*/ Uri uri = Uri.parse(API_BASE_URL); String url = uri.buildUpon().appendEncodedPath("drive/items/" + file.getId() + "/content") .appendQueryParameter("@name.conflictBehavior", "replace").build().toString(); RequestBody fileBody = new RequestBody() { @Override public MediaType contentType() { return null; } @Override public void writeTo(BufferedSink sink) throws IOException { // copy file into RequestBody FilesUtils.copyFile(new FileInputStream(content), sink.outputStream()); } }; Request request = new Request.Builder().url(url) .header("Authorization", String.format("Bearer %s", mAccessToken)).put(fileBody).build(); try { Response response = mHttpClient.newCall(request).execute(); if (response.isSuccessful()) { // new file created JSONObject jsonObject = new JSONObject(response.body().string()); return buildFile(jsonObject); } else { throw new RequestFailException(response.message(), response.code()); } } catch (JSONException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } catch (IOException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } }
From source file:com.he5ed.lib.cloudprovider.apis.OneDriveApi.java
@Override public CFile uploadFile(@NonNull final File file, @Nullable CFolder parent) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); }//from w w w .j a v a 2 s. c o m Uri uri = Uri.parse(API_BASE_URL); String url = uri.buildUpon() .appendEncodedPath("drive/items/" + (parent != null ? parent.getId() : getRoot().getId()) + "/children/" + file.getName() + "/content") .appendQueryParameter("@name.conflictBehavior", "fail").build().toString(); RequestBody fileBody = new RequestBody() { @Override public MediaType contentType() { return null; } @Override public void writeTo(BufferedSink sink) throws IOException { // copy file into RequestBody FilesUtils.copyFile(new FileInputStream(file), sink.outputStream()); } }; Request request = new Request.Builder().url(url) .header("Authorization", String.format("Bearer %s", mAccessToken)).put(fileBody).build(); try { Response response = mHttpClient.newCall(request).execute(); if (response.isSuccessful()) { // new file created JSONObject jsonObject = new JSONObject(response.body().string()); return buildFile(jsonObject); } else { throw new RequestFailException(response.message(), response.code()); } } catch (JSONException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } catch (IOException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } }
From source file:com.he5ed.lib.cloudprovider.apis.BoxApi.java
/** * Search the cloud for all contents// w w w.j av a2s . c o m * * @param params for search query * @param parent folder wto search for * @return list of files and folders that match search criteria * @throws RequestFailException */ public synchronized List<Object> search(@NonNull Map<String, Object> params, CFolder parent) throws RequestFailException { List<Object> list = new ArrayList<>(); Uri uri = Uri.parse(API_BASE_URL); Uri.Builder urlBuilder = uri.buildUpon().appendEncodedPath("search"); // pre-defined parameters urlBuilder.appendQueryParameter("limit", "100"); urlBuilder.appendQueryParameter("scope", "user_content"); // add the rest of the user defined parameters params.put("ancestor_folder_ids", parent.getId()); for (Map.Entry<String, Object> param : params.entrySet()) { urlBuilder.appendQueryParameter(param.getKey(), (String) param.getValue()); } Request request = new Request.Builder().url(urlBuilder.toString()) .header("Authorization", String.format("Bearer %s", mAccessToken)).get().build(); try { Response response = mHttpClient.newCall(request).execute(); if (response.isSuccessful()) { JSONObject jsonObject = new JSONObject(response.body().string()); int total = jsonObject.getInt("total_count"); // return null if no item found if (total == 0) return null; JSONArray entries = jsonObject.getJSONArray("entries"); list.addAll(createFilteredItemsList(entries, parent)); // suspect search result over 100 items if (total > 100 && total - list.size() > 0) { params.put("offset", "100"); list.addAll(search(params, parent)); } return list; } else { throw new RequestFailException(response.message(), response.code()); } } catch (JSONException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } catch (IOException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } }
From source file:com.android.gallery3d.app.Gallery.java
private void startViewAction(Intent intent) { Boolean slideshow = intent.getBooleanExtra(EXTRA_SLIDESHOW, false); getStateManager().setLaunchGalleryOnTop(true); if (slideshow) { getActionBar().hide();// w w w .j av a2s . c om DataManager manager = getDataManager(); Path path = manager.findPathByUri(intent.getData()); if (path == null || manager.getMediaObject(path) instanceof MediaItem) { path = Path.fromString(manager.getTopSetPath(DataManager.INCLUDE_IMAGE)); } Bundle data = new Bundle(); data.putString(SlideshowPage.KEY_SET_PATH, path.toString()); data.putBoolean(SlideshowPage.KEY_RANDOM_ORDER, true); data.putBoolean(SlideshowPage.KEY_REPEAT, true); getStateManager().startState(SlideshowPage.class, data); } else { Bundle data = new Bundle(); DataManager dm = getDataManager(); Uri uri = intent.getData(); String contentType = getContentType(intent); if (contentType == null) { Toast.makeText(this, R.string.no_such_item, Toast.LENGTH_LONG).show(); finish(); return; } if (uri == null) { int typeBits = GalleryUtils.determineTypeBits(this, intent); data.putInt(KEY_TYPE_BITS, typeBits); data.putString(AlbumSetPage.KEY_MEDIA_PATH, getDataManager().getTopSetPath(typeBits)); getStateManager().setLaunchGalleryOnTop(true); getStateManager().startState(AlbumSetPage.class, data); } else if (contentType.startsWith(ContentResolver.CURSOR_DIR_BASE_TYPE)) { int mediaType = intent.getIntExtra(KEY_MEDIA_TYPES, 0); if (mediaType != 0) { uri = uri.buildUpon().appendQueryParameter(KEY_MEDIA_TYPES, String.valueOf(mediaType)).build(); } Path setPath = dm.findPathByUri(uri); MediaSet mediaSet = null; if (setPath != null) { mediaSet = (MediaSet) dm.getMediaObject(setPath); } if (mediaSet != null) { if (mediaSet.isLeafAlbum()) { data.putString(AlbumPage.KEY_MEDIA_PATH, setPath.toString()); getStateManager().startState(AlbumPage.class, data); } else { data.putString(AlbumSetPage.KEY_MEDIA_PATH, setPath.toString()); getStateManager().startState(AlbumSetPage.class, data); } } else { startDefaultPage(); } } else { Path itemPath = dm.findPathByUri(uri); Path albumPath = dm.getDefaultSetOf(itemPath); // TODO: Make this parameter public so other activities can reference it. boolean singleItemOnly = intent.getBooleanExtra("SingleItemOnly", false); if (!singleItemOnly && albumPath != null) { data.putString(PhotoPage.KEY_MEDIA_SET_PATH, albumPath.toString()); } data.putString(PhotoPage.KEY_MEDIA_ITEM_PATH, itemPath.toString()); getStateManager().startState(PhotoPage.class, data); } } }
From source file:net.sf.xfd.provider.PublicProvider.java
@Nullable @Override//from ww w. j a va 2s . c o m public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { String path = uri.getPath(); if (TextUtils.isEmpty(uri.getPath())) { path = "/"; } try { assertAbsolute(path); } catch (FileNotFoundException e) { return null; } path = canonString(path); if (!path.equals(uri.getPath())) { uri = uri.buildUpon().path(path).build(); } if (!checkAccess(uri, "r")) { return null; } if (projection == null) { projection = COMMON_PROJECTION; } final OS os = base.getOS(); if (os == null) { return null; } try { final MatrixCursor cursor = new MatrixCursor(projection, 1); final Object[] row = new Object[projection.length]; final Stat stat = new Stat(); final String name = extractName(path); final String mime = base.getTypeFast(path, name, stat); for (int i = 0; i < projection.length; ++i) { String col = projection[i]; switch (col) { case BaseColumns._ID: row[i] = stat.st_ino; break; case COLUMN_DISPLAY_NAME: row[i] = name; break; case COLUMN_SIZE: row[i] = stat.st_size; break; case COLUMN_MIME_TYPE: row[i] = mime; break; default: row[i] = null; } } cursor.addRow(row); final Context context = getContext(); assert context != null; final String packageName = context.getPackageName(); cursor.setNotificationUri(context.getContentResolver(), DocumentsContract.buildDocumentUri(packageName + FileProvider.AUTHORITY_SUFFIX, path)); return cursor; } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:com.mobicage.rogerthat.AddFriendsActivity.java
private void postOnWall() { FacebookUtils.ensureOpenSession(this, Arrays.asList("email", "user_friends"), PermissionType.READ, new Session.StatusCallback() { @Override/*from w w w .j a v a 2 s .c o m*/ public void call(Session session, SessionState state, Exception exception) { if (session != Session.getActiveSession()) { session.removeCallback(this); return; } session.removeCallback(this); if (exception == null && session.isOpened()) { String myEmailHash = new String(getMyIdentity().getEmailHash()); String picture = CloudConstants.HTTPS_BASE_URL + "/invite?code=" + myEmailHash; Uri identityUri = Uri.parse(getMyIdentity().getShortLink()); Builder b = identityUri.buildUpon(); b.appendQueryParameter("target", "fbwall"); b.appendQueryParameter("from", "phone"); b.build(); String link = b.toString(); String caption = getString(R.string.fb_wall_post_caption, getString(R.string.app_name)); String description = getString(R.string.fb_wall_post_description, getString(R.string.app_name)); L.d("Posting to facebook:\n- FACEBOOK_APP_ID: " + CloudConstants.FACEBOOK_APP_ID + "\n- Picture: " + picture + "\n- Catption: " + caption + "\n- Description: " + description + "\n- Link :" + link); if (FacebookDialog.canPresentShareDialog(getApplicationContext(), FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) { L.d("Share via FacebookDialog"); FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder( AddFriendsActivity.this).setLink(link).setPicture(picture) .setCaption(caption).setDescription(description).build(); mUiHelper.trackPendingDialogCall(shareDialog.present()); } else { L.d("Share via WebDialog"); Bundle params = new Bundle(); params.putString("app_id", CloudConstants.FACEBOOK_APP_ID); params.putString("picture", picture); params.putString("link", link); params.putString("caption", caption); params.putString("description", description); new WebDialog.FeedDialogBuilder(AddFriendsActivity.this, session, params) .setOnCompleteListener(new WebDialog.OnCompleteListener() { @Override public void onComplete(Bundle values, FacebookException error) { findRogerthatUsersViaFacebook(); } }).build().show(); } } } }, true); }
From source file:com.chen.emailsync.SyncManager.java
public static Uri asSyncAdapter(Uri uri, String account, String accountType) { return uri.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true") .appendQueryParameter(Calendars.ACCOUNT_NAME, account) .appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType).build(); }
From source file:com.he5ed.lib.cloudprovider.apis.CloudDriveApi.java
@Override public File downloadFile(@NonNull CFile file, String filename) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); }// www.j a v a 2 s. co m Uri uri = Uri.parse(mContentUrl); String url = uri.buildUpon().appendEncodedPath("nodes/" + file.getId() + "/content").build().toString(); Request request = new Request.Builder().url(url) .header("Authorization", String.format("Bearer %s", mAccessToken)).get().build(); try { File localFile = new File(mContext.getFilesDir(), TextUtils.isEmpty(filename) ? file.getName() : filename); Response response = mHttpClient.newCall(request).execute(); if (response.isSuccessful()) { FilesUtils.copyFile(response.body().byteStream(), new FileOutputStream(localFile)); } else { throw new RequestFailException(response.message(), response.code()); } return localFile; } catch (IOException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } }