Search in sources :

Example 1 with GetRemoteShareesOperation

use of com.owncloud.android.lib.resources.shares.GetRemoteShareesOperation in project android by owncloud.

the class UsersAndGroupsSearchProvider method searchForUsersOrGroups.

private Cursor searchForUsersOrGroups(Uri uri) {
    MatrixCursor response = null;
    String userQuery = uri.getLastPathSegment().toLowerCase();
    /// need to trust on the AccountUtils to get the current account since the query in the client side is not
    /// directly started by our code, but from SearchView implementation
    Account account = AccountUtils.getCurrentOwnCloudAccount(getContext());
    /// request to the OC server about users and groups matching userQuery
    GetRemoteShareesOperation searchRequest = new GetRemoteShareesOperation(userQuery, REQUESTED_PAGE, RESULTS_PER_PAGE);
    RemoteOperationResult result = searchRequest.execute(account, getContext());
    List<JSONObject> names = new ArrayList<JSONObject>();
    if (result.isSuccess()) {
        for (Object o : result.getData()) {
            // Get JSonObjects from response
            names.add((JSONObject) o);
        }
    } else {
        showErrorMessage(result);
    }
    /// convert the responses from the OC server to the expected format
    if (names.size() > 0) {
        response = new MatrixCursor(COLUMNS);
        Iterator<JSONObject> namesIt = names.iterator();
        JSONObject item;
        String displayName = null;
        int icon = 0;
        Uri dataUri = null;
        int count = 0;
        MainApp app = (MainApp) getContext().getApplicationContext();
        Uri userBaseUri = new Uri.Builder().scheme(CONTENT).authority(sSuggestAuthority + DATA_USER_SUFFIX).build();
        Uri groupBaseUri = new Uri.Builder().scheme(CONTENT).authority(sSuggestAuthority + DATA_GROUP_SUFFIX).build();
        Uri remoteBaseUri = new Uri.Builder().scheme(CONTENT).authority(sSuggestAuthority + DATA_REMOTE_SUFFIX).build();
        FileDataStorageManager manager = new FileDataStorageManager(account, getContext().getContentResolver());
        boolean federatedShareAllowed = manager.getCapability(account.name).getFilesSharingFederationOutgoing().isTrue();
        try {
            while (namesIt.hasNext()) {
                item = namesIt.next();
                String userName = item.getString(GetRemoteShareesOperation.PROPERTY_LABEL);
                JSONObject value = item.getJSONObject(GetRemoteShareesOperation.NODE_VALUE);
                int type = value.getInt(GetRemoteShareesOperation.PROPERTY_SHARE_TYPE);
                String shareWith = value.getString(GetRemoteShareesOperation.PROPERTY_SHARE_WITH);
                if (ShareType.GROUP.getValue() == type) {
                    displayName = getContext().getString(R.string.share_group_clarification, userName);
                    icon = R.drawable.ic_group;
                    dataUri = Uri.withAppendedPath(groupBaseUri, shareWith);
                } else if (ShareType.FEDERATED.getValue() == type && federatedShareAllowed) {
                    icon = R.drawable.ic_user;
                    if (userName.equals(shareWith)) {
                        displayName = getContext().getString(R.string.share_remote_clarification, userName);
                    } else {
                        String[] uriSplitted = shareWith.split("@");
                        displayName = getContext().getString(R.string.share_known_remote_clarification, userName, uriSplitted[uriSplitted.length - 1]);
                    }
                    dataUri = Uri.withAppendedPath(remoteBaseUri, shareWith);
                } else if (ShareType.USER.getValue() == type) {
                    displayName = userName;
                    icon = R.drawable.ic_user;
                    dataUri = Uri.withAppendedPath(userBaseUri, shareWith);
                }
                if (displayName != null && dataUri != null) {
                    response.newRow().add(// BaseColumns._ID
                    count++).add(// SearchManager.SUGGEST_COLUMN_TEXT_1
                    displayName).add(// SearchManager.SUGGEST_COLUMN_ICON_1
                    icon).add(dataUri);
                }
            }
        } catch (JSONException e) {
            Log_OC.e(TAG, "Exception while parsing data of users/groups", e);
        }
    }
    return response;
}
Also used : Account(android.accounts.Account) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) GetRemoteShareesOperation(com.owncloud.android.lib.resources.shares.GetRemoteShareesOperation) ArrayList(java.util.ArrayList) MainApp(com.owncloud.android.MainApp) JSONException(org.json.JSONException) Uri(android.net.Uri) MatrixCursor(android.database.MatrixCursor) JSONObject(org.json.JSONObject) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) JSONObject(org.json.JSONObject)

Aggregations

Account (android.accounts.Account)1 MatrixCursor (android.database.MatrixCursor)1 Uri (android.net.Uri)1 MainApp (com.owncloud.android.MainApp)1 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)1 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)1 GetRemoteShareesOperation (com.owncloud.android.lib.resources.shares.GetRemoteShareesOperation)1 ArrayList (java.util.ArrayList)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1