d642bfa2e77352ba1eb5e8730bb0b913cc11cd5d
[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
42 import java.util.ArrayList;
43 import java.util.Iterator;
44 import java.util.List;
45
46
47 /**
48 * Content provider for search suggestions, to search for users and groups existing in an ownCloud server.
49 */
50 public class UsersAndGroupsSearchProvider extends ContentProvider {
51
52 private static final String TAG = UsersAndGroupsSearchProvider.class.getSimpleName();
53
54 private static final String[] COLUMNS = {
55 BaseColumns._ID,
56 SearchManager.SUGGEST_COLUMN_TEXT_1,
57 SearchManager.SUGGEST_COLUMN_FLAGS,
58 SearchManager.SUGGEST_COLUMN_INTENT_DATA
59 };
60
61 private static final int SEARCH = 1;
62
63 private static final int RESULTS_PER_PAGE = 50;
64 private static final int REQUESTED_PAGE = 1;
65
66 public static final String AUTHORITY = UsersAndGroupsSearchProvider.class.getCanonicalName();
67 public static final String ACTION_SHARE_WITH = AUTHORITY + ".action.SHARE_WITH";
68 public static final String DATA_USER = AUTHORITY + ".data.user";
69 public static final String DATA_GROUP = AUTHORITY + ".data.group";
70
71 private UriMatcher mUriMatcher;
72
73 @Nullable
74 @Override
75 public String getType(Uri uri) {
76 // TODO implement
77 return null;
78 }
79
80 @Override
81 public boolean onCreate() {
82 mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
83 mUriMatcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH);
84 return true;
85 }
86
87 /**
88 * TODO description
89 *
90 * Reference: http://developer.android.com/guide/topics/search/adding-custom-suggestions.html#CustomContentProvider
91 *
92 * @param uri Content {@link Uri}, formattted as
93 * "content://com.owncloud.android.providers.UsersAndGroupsSearchProvider/" +
94 * {@link android.app.SearchManager#SUGGEST_URI_PATH_QUERY} + "/" + 'userQuery'
95 * @param projection Expected to be NULL.
96 * @param selection Expected to be NULL.
97 * @param selectionArgs Expected to be NULL.
98 * @param sortOrder Expected to be NULL.
99 * @return Cursor with users and groups in the ownCloud server that match 'userQuery'.
100 */
101 @Nullable
102 @Override
103 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
104 Log_OC.d(TAG, "query received in thread " + Thread.currentThread().getName());
105
106 int match = mUriMatcher.match(uri);
107 switch (match) {
108 case SEARCH:
109 return searchForUsersOrGroups(uri);
110
111 default:
112 return null;
113 }
114 }
115
116 private Cursor searchForUsersOrGroups(Uri uri) {
117 MatrixCursor response = null;
118
119
120 String userQuery = uri.getLastPathSegment().toLowerCase();
121
122
123 /// need to trust on the AccountUtils to get the current account since the query in the client side is not
124 /// directly started by our code, but from SearchView implementation
125 Account account = AccountUtils.getCurrentOwnCloudAccount(getContext());
126
127 /// request to the OC server about users and groups matching userQuery
128 GetRemoteShareesOperation searchRequest = new GetRemoteShareesOperation(
129 userQuery, REQUESTED_PAGE, RESULTS_PER_PAGE
130 );
131 RemoteOperationResult result = searchRequest.execute(account, getContext());
132 List<Pair<String, Byte>> names = new ArrayList<Pair<String, Byte>>();
133 if (result.isSuccess()) {
134 for (Object o : result.getData()) {
135 names.add((Pair<String, Byte>) o);
136 }
137 }
138
139 /// convert the responses from the OC server to the expected format
140 if (names.size() > 0) {
141 response = new MatrixCursor(COLUMNS);
142 Iterator<Pair<String, Byte>> namesIt = names.iterator();
143 int count = 0;
144 Pair<String, Byte> item;
145 String displayName;
146 Uri dataUri;
147 Uri userBaseUri = new Uri.Builder().scheme("content").authority(DATA_USER).build();
148 Uri groupBaseUri = new Uri.Builder().scheme("content").authority(DATA_GROUP).build();
149
150 while (namesIt.hasNext()) {
151 item = namesIt.next();
152 if (GetRemoteShareesOperation.GROUP_TYPE.equals(item.second)) {
153 displayName = getContext().getString(R.string.share_group_clarification, item.first);
154 dataUri = Uri.withAppendedPath(groupBaseUri, item.first);
155 } else {
156 displayName = item.first;
157 dataUri = Uri.withAppendedPath(userBaseUri, item.first);
158 }
159 response.newRow()
160 .add(count++) // BaseColumns._ID
161 .add(displayName) // SearchManager.SUGGEST_COLUMN_TEXT_1
162 .add(SearchManager.FLAG_QUERY_REFINEMENT)
163 .add(dataUri);
164 }
165 }
166
167 return response;
168 }
169
170 @Nullable
171 @Override
172 public Uri insert(Uri uri, ContentValues values) {
173 // TODO implementation
174 return null;
175 }
176
177 @Override
178 public int delete(Uri uri, String selection, String[] selectionArgs) {
179 // TODO implementation
180 return 0;
181 }
182
183 @Override
184 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
185 // TODO implementation
186 return 0;
187 }
188
189 }