use of com.owncloud.android.MainApp 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;
}
Aggregations