FreeRDP
FreeRDPSuggestionProvider.java
1 /*
2  Suggestion Provider for RDP bookmarks
3 
4  Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
5 
6  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7  If a copy of the MPL was not distributed with this file, You can obtain one at
8  http://mozilla.org/MPL/2.0/.
9 */
10 
11 package com.freerdp.freerdpcore.services;
12 
13 import android.app.SearchManager;
14 import android.content.ContentProvider;
15 import android.content.ContentValues;
16 import android.database.Cursor;
17 import android.database.MatrixCursor;
18 import android.net.Uri;
19 
20 import com.freerdp.freerdpcore.R;
21 import com.freerdp.freerdpcore.application.GlobalApp;
22 import com.freerdp.freerdpcore.domain.BookmarkBase;
23 import com.freerdp.freerdpcore.domain.ConnectionReference;
24 import com.freerdp.freerdpcore.domain.ManualBookmark;
25 
26 import java.util.ArrayList;
27 
28 public class FreeRDPSuggestionProvider extends ContentProvider
29 {
30 
31  public static final Uri CONTENT_URI =
32  Uri.parse("content://com.freerdp.afreerdp.services.freerdpsuggestionprovider");
33 
34  @Override public int delete(Uri uri, String selection, String[] selectionArgs)
35  {
36  // TODO Auto-generated method stub
37  return 0;
38  }
39 
40  @Override public String getType(Uri uri)
41  {
42  return "vnd.android.cursor.item/vnd.freerdp.remote";
43  }
44 
45  @Override public Uri insert(Uri uri, ContentValues values)
46  {
47  // TODO Auto-generated method stub
48  return null;
49  }
50 
51  @Override public boolean onCreate()
52  {
53  return true;
54  }
55 
56  @Override
57  public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
58  String sortOrder)
59  {
60 
61  String query = (selectionArgs != null && selectionArgs.length > 0) ? selectionArgs[0] : "";
62 
63  // search history
64  ArrayList<BookmarkBase> history =
65  GlobalApp.getQuickConnectHistoryGateway().findHistory(query);
66 
67  // search bookmarks
68  ArrayList<BookmarkBase> manualBookmarks;
69  if (query.length() > 0)
70  manualBookmarks = GlobalApp.getManualBookmarkGateway().findByLabelOrHostnameLike(query);
71  else
72  manualBookmarks = GlobalApp.getManualBookmarkGateway().findAll();
73 
74  return createResultCursor(history, manualBookmarks);
75  }
76 
77  @Override
78  public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
79  {
80  // TODO Auto-generated method stub
81  return 0;
82  }
83 
84  private void addBookmarksToCursor(ArrayList<BookmarkBase> bookmarks, MatrixCursor resultCursor)
85  {
86  Object[] row = new Object[5];
87  for (BookmarkBase bookmark : bookmarks)
88  {
89  row[0] = bookmark.getId();
90  row[1] = bookmark.getLabel();
91  row[2] = bookmark.<ManualBookmark>get().getHostname();
92  row[3] = ConnectionReference.getManualBookmarkReference(bookmark.getId());
93  row[4] = "android.resource://" + getContext().getPackageName() + "/" +
94  R.drawable.icon_star_on;
95  resultCursor.addRow(row);
96  }
97  }
98 
99  private void addHistoryToCursor(ArrayList<BookmarkBase> history, MatrixCursor resultCursor)
100  {
101  Object[] row = new Object[5];
102  for (BookmarkBase bookmark : history)
103  {
104  row[0] = 1;
105  row[1] = bookmark.getLabel();
106  row[2] = bookmark.getLabel();
107  row[3] = ConnectionReference.getHostnameReference(bookmark.getLabel());
108  row[4] = "android.resource://" + getContext().getPackageName() + "/" +
109  R.drawable.icon_star_off;
110  resultCursor.addRow(row);
111  }
112  }
113 
114  private Cursor createResultCursor(ArrayList<BookmarkBase> history,
115  ArrayList<BookmarkBase> manualBookmarks)
116  {
117 
118  // create result matrix cursor
119  int totalCount = history.size() + manualBookmarks.size();
120  String[] columns = { android.provider.BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1,
121  SearchManager.SUGGEST_COLUMN_TEXT_2,
122  SearchManager.SUGGEST_COLUMN_INTENT_DATA,
123  SearchManager.SUGGEST_COLUMN_ICON_2 };
124  MatrixCursor matrixCursor = new MatrixCursor(columns, totalCount);
125 
126  // populate result matrix
127  if (totalCount > 0)
128  {
129  addHistoryToCursor(history, matrixCursor);
130  addBookmarksToCursor(manualBookmarks, matrixCursor);
131  }
132  return matrixCursor;
133  }
134 }