use of com.owncloud.android.lib.resources.shares.GetShareesRemoteOperation in project android by nextcloud.
the class UsersAndGroupsSearchProvider method searchForUsersOrGroups.
private Cursor searchForUsersOrGroups(Uri uri) {
String lastPathSegment = uri.getLastPathSegment();
if (lastPathSegment == null) {
throw new IllegalArgumentException("Wrong URI passed!");
}
// 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
User user = accountManager.getUser();
String userQuery = lastPathSegment.toLowerCase(Locale.ROOT);
// request to the OC server about users and groups matching userQuery
GetShareesRemoteOperation searchRequest = new GetShareesRemoteOperation(userQuery, REQUESTED_PAGE, RESULTS_PER_PAGE);
RemoteOperationResult result = searchRequest.execute(user.toPlatformAccount(), getContext());
List<JSONObject> names = new ArrayList<>();
if (result.isSuccess()) {
for (Object o : result.getData()) {
names.add((JSONObject) o);
}
} else {
showErrorMessage(result);
}
MatrixCursor response = null;
// convert the responses from the OC server to the expected format
if (names.size() > 0) {
if (getContext() == null) {
throw new IllegalArgumentException("Context may not be null!");
}
response = new MatrixCursor(COLUMNS);
Uri userBaseUri = new Uri.Builder().scheme(CONTENT).authority(DATA_USER).build();
Uri groupBaseUri = new Uri.Builder().scheme(CONTENT).authority(DATA_GROUP).build();
Uri roomBaseUri = new Uri.Builder().scheme(CONTENT).authority(DATA_ROOM).build();
Uri remoteBaseUri = new Uri.Builder().scheme(CONTENT).authority(DATA_REMOTE).build();
Uri emailBaseUri = new Uri.Builder().scheme(CONTENT).authority(DATA_EMAIL).build();
Uri circleBaseUri = new Uri.Builder().scheme(CONTENT).authority(DATA_CIRCLE).build();
FileDataStorageManager manager = new FileDataStorageManager(user, getContext().getContentResolver());
boolean federatedShareAllowed = manager.getCapability(user.getAccountName()).getFilesSharingFederationOutgoing().isTrue();
try {
Iterator<JSONObject> namesIt = names.iterator();
JSONObject item;
String displayName;
String subline = null;
Object icon = 0;
Uri dataUri;
int count = 0;
while (namesIt.hasNext()) {
item = namesIt.next();
dataUri = null;
displayName = null;
String userName = item.getString(GetShareesRemoteOperation.PROPERTY_LABEL);
String name = item.isNull("name") ? "" : item.getString("name");
JSONObject value = item.getJSONObject(GetShareesRemoteOperation.NODE_VALUE);
ShareType type = ShareType.fromValue(value.getInt(GetShareesRemoteOperation.PROPERTY_SHARE_TYPE));
String shareWith = value.getString(GetShareesRemoteOperation.PROPERTY_SHARE_WITH);
Status status;
JSONObject statusObject = item.optJSONObject(PROPERTY_STATUS);
if (statusObject != null) {
status = new Status(StatusType.valueOf(statusObject.getString(PROPERTY_STATUS).toUpperCase(Locale.US)), statusObject.isNull(PROPERTY_MESSAGE) ? "" : statusObject.getString(PROPERTY_MESSAGE), statusObject.isNull(PROPERTY_ICON) ? "" : statusObject.getString(PROPERTY_ICON), statusObject.isNull(PROPERTY_CLEAR_AT) ? -1 : statusObject.getLong(PROPERTY_CLEAR_AT));
} else {
status = new Status(StatusType.OFFLINE, "", "", -1);
}
switch(type) {
case GROUP:
displayName = userName;
icon = R.drawable.ic_group;
dataUri = Uri.withAppendedPath(groupBaseUri, shareWith);
break;
case FEDERATED:
if (federatedShareAllowed) {
icon = R.drawable.ic_user;
dataUri = Uri.withAppendedPath(remoteBaseUri, shareWith);
if (userName.equals(shareWith)) {
displayName = name;
subline = getContext().getString(R.string.remote);
} else {
String[] uriSplitted = shareWith.split("@");
displayName = name;
subline = getContext().getString(R.string.share_known_remote_on_clarification, uriSplitted[uriSplitted.length - 1]);
}
}
break;
case USER:
displayName = userName;
subline = (status.getMessage() == null || status.getMessage().isEmpty()) ? null : status.getMessage();
Uri.Builder builder = Uri.parse("content://com.nextcloud.android.providers.UsersAndGroupsSearchProvider/icon").buildUpon();
builder.appendQueryParameter("shareWith", shareWith);
builder.appendQueryParameter("displayName", displayName);
builder.appendQueryParameter("status", status.getStatus().toString());
if (!TextUtils.isEmpty(status.getIcon()) && !"null".equals(status.getIcon())) {
builder.appendQueryParameter("icon", status.getIcon());
}
icon = builder.build();
dataUri = Uri.withAppendedPath(userBaseUri, shareWith);
break;
case EMAIL:
icon = R.drawable.ic_email;
displayName = name;
subline = shareWith;
dataUri = Uri.withAppendedPath(emailBaseUri, shareWith);
break;
case ROOM:
icon = R.drawable.ic_talk;
displayName = userName;
dataUri = Uri.withAppendedPath(roomBaseUri, shareWith);
break;
case CIRCLE:
icon = R.drawable.ic_circles;
displayName = userName;
dataUri = Uri.withAppendedPath(circleBaseUri, shareWith);
break;
default:
break;
}
if (displayName != null && dataUri != null) {
response.newRow().add(// BaseColumns._ID
count++).add(// SearchManager.SUGGEST_COLUMN_TEXT_1
displayName).add(// SearchManager.SUGGEST_COLUMN_TEXT_2
subline).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