0307f990bb950d62d800c307c7515f429c92bfc0
[pub/Android/ownCloud.git] / src / com / owncloud / android / providers / UsersAndGroupsSearchProvider.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21
22 package com.owncloud.android.providers;
23
24 import android.accounts.Account;
25 import android.app.SearchManager;
26 import android.content.ContentProvider;
27 import android.content.ContentValues;
28 import android.content.UriMatcher;
29 import android.database.Cursor;
30 import android.database.MatrixCursor;
31 import android.net.Uri;
32 import android.provider.BaseColumns;
33 import android.support.annotation.Nullable;
34 import android.util.Pair;
35
36 import com.owncloud.android.R;
37 import com.owncloud.android.authentication.AccountUtils;
38 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
39 import com.owncloud.android.lib.common.utils.Log_OC;
40 import com.owncloud.android.lib.resources.shares.GetRemoteShareesOperation;
41 import com.owncloud.android.lib.resources.shares.ShareType;
42
43 import org.json.JSONException;
44 import org.json.JSONObject;
45
46 import java.util.ArrayList;
47 import java.util.Iterator;
48 import java.util.List;
49
50
51 /**
52 * Content provider for search suggestions, to search for users and groups existing in an ownCloud server.
53 */
54 public class UsersAndGroupsSearchProvider extends ContentProvider {
55
56 private static final String TAG = UsersAndGroupsSearchProvider.class.getSimpleName();
57
58 private static final String[] COLUMNS = {
59 BaseColumns._ID,
60 SearchManager.SUGGEST_COLUMN_TEXT_1,
61 SearchManager.SUGGEST_COLUMN_INTENT_DATA
62 };
63
64 private static final int SEARCH = 1;
65
66 private static final int RESULTS_PER_PAGE = 50;
67 private static final int REQUESTED_PAGE = 1;
68
69 public static final String AUTHORITY = UsersAndGroupsSearchProvider.class.getCanonicalName();
70 public static final String ACTION_SHARE_WITH = AUTHORITY + ".action.SHARE_WITH";
71 public static final String DATA_USER = AUTHORITY + ".data.user";
72 public static final String DATA_GROUP = AUTHORITY + ".data.group";
73
74 private UriMatcher mUriMatcher;
75
76 @Nullable
77 @Override
78 public String getType(Uri uri) {
79 // TODO implement
80 return null;
81 }
82
83 @Override
84 public boolean onCreate() {
85 mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
86 mUriMatcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH);
87 return true;
88 }
89
90 /**
91 * TODO description
92 *
93 * Reference: http://developer.android.com/guide/topics/search/adding-custom-suggestions.html#CustomContentProvider
94 *
95 * @param uri Content {@link Uri}, formattted as
96 * "content://com.owncloud.android.providers.UsersAndGroupsSearchProvider/" +
97 * {@link android.app.SearchManager#SUGGEST_URI_PATH_QUERY} + "/" + 'userQuery'
98 * @param projection Expected to be NULL.
99 * @param selection Expected to be NULL.
100 * @param selectionArgs Expected to be NULL.
101 * @param sortOrder Expected to be NULL.
102 * @return Cursor with users and groups in the ownCloud server that match 'userQuery'.
103 */
104 @Nullable
105 @Override
106 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
107 Log_OC.d(TAG, "query received in thread " + Thread.currentThread().getName());
108
109 int match = mUriMatcher.match(uri);
110 switch (match) {
111 case SEARCH:
112 return searchForUsersOrGroups(uri);
113
114 default:
115 return null;
116 }
117 }
118
119 private Cursor searchForUsersOrGroups(Uri uri) {
120 MatrixCursor response = null;
121
122
123 String userQuery = uri.getLastPathSegment().toLowerCase();
124
125
126 /// need to trust on the AccountUtils to get the current account since the query in the client side is not
127 /// directly started by our code, but from SearchView implementation
128 Account account = AccountUtils.getCurrentOwnCloudAccount(getContext());
129
130 /// request to the OC server about users and groups matching userQuery
131 GetRemoteShareesOperation searchRequest = new GetRemoteShareesOperation(
132 userQuery, REQUESTED_PAGE, RESULTS_PER_PAGE
133 );
134 RemoteOperationResult result = searchRequest.execute(account, getContext());
135 List<JSONObject> names = new ArrayList<JSONObject>();
136 if (result.isSuccess()) {
137 for (Object o : result.getData()) {
138 // Get JSonObjects from response
139 names.add((JSONObject) o);
140 }
141 }
142
143 /// convert the responses from the OC server to the expected format
144 if (names.size() > 0) {
145 response = new MatrixCursor(COLUMNS);
146 Iterator<JSONObject> namesIt = names.iterator();
147 int count = 0;
148 JSONObject item;
149 String displayName;
150 Uri dataUri;
151 Uri userBaseUri = new Uri.Builder().scheme("content").authority(DATA_USER).build();
152 Uri groupBaseUri = new Uri.Builder().scheme("content").authority(DATA_GROUP).build();
153 try {
154 while (namesIt.hasNext()) {
155 item = namesIt.next();
156 String userName = item.getString(GetRemoteShareesOperation.PROPERTY_LABEL);
157 JSONObject value = item.getJSONObject(GetRemoteShareesOperation.NODE_VALUE);
158 byte type = (byte) value.getInt(GetRemoteShareesOperation.PROPERTY_SHARE_TYPE);
159 String shareWith = value.getString(GetRemoteShareesOperation.PROPERTY_SHARE_WITH);
160 if (GetRemoteShareesOperation.GROUP_TYPE.equals(type)) {
161 displayName = getContext().getString(R.string.share_group_clarification, userName);
162 dataUri = Uri.withAppendedPath(groupBaseUri, shareWith);
163 } else {
164 displayName = userName;
165 dataUri = Uri.withAppendedPath(userBaseUri, shareWith);
166 }
167 response.newRow()
168 .add(count++) // BaseColumns._ID
169 .add(displayName) // SearchManager.SUGGEST_COLUMN_TEXT_1
170 .add(dataUri);
171 }
172 } catch (JSONException e) {
173 Log_OC.e(TAG, "Exception while parsing data of users/groups", e);
174 }
175 }
176
177 return response;
178 }
179
180 @Nullable
181 @Override
182 public Uri insert(Uri uri, ContentValues values) {
183 // TODO implementation
184 return null;
185 }
186
187 @Override
188 public int delete(Uri uri, String selection, String[] selectionArgs) {
189 // TODO implementation
190 return 0;
191 }
192
193 @Override
194 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
195 // TODO implementation
196 return 0;
197 }
198
199 }