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