1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.quicksearchbox.tests.partial;
18 
19 import android.app.SearchManager;
20 import android.content.ContentProvider;
21 import android.content.ContentValues;
22 import android.content.Intent;
23 import android.database.Cursor;
24 import android.database.MatrixCursor;
25 import android.net.Uri;
26 import android.os.Bundle;
27 import android.os.Handler;
28 import android.os.Message;
29 import android.util.Log;
30 
31 public class PartialSuggestionProvider extends ContentProvider {
32 
33     private static final String TAG = PartialSuggestionProvider.class.getSimpleName();
34 
35     private static final int MSG_COMPLETE = 1;
36 
37     private MutableMatrixCursor mCursor;
38     private int mType = -1;
39 
40     private static final String[] COLUMNS = {
41         "_id",
42         SearchManager.SUGGEST_COLUMN_TEXT_1,
43         SearchManager.SUGGEST_COLUMN_TEXT_2,
44         SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
45         SearchManager.SUGGEST_COLUMN_INTENT_DATA,
46     };
47 
48     Handler mHandler = new Handler() {
49         @Override
50         public void handleMessage(Message msg) {
51             switch (msg.what) {
52             case MSG_COMPLETE:
53                 mCursor.getExtras().putBoolean(SearchManager.CURSOR_EXTRA_KEY_IN_PROGRESS, false);
54                 addRows(mCursor, false);
55                 mCursor.notifyChange();
56             }
57 
58         }
59     };
60 
61     private static class MutableMatrixCursor extends MatrixCursor {
62 
63         Bundle mBundle;
64 
MutableMatrixCursor(String[] columns)65         MutableMatrixCursor(String[] columns) {
66             super(columns);
67         }
68 
69         @Override
getExtras()70         public Bundle getExtras() {
71             if (mBundle == null) mBundle = new Bundle();
72             return mBundle;
73         }
74 
notifyChange()75         public void notifyChange() {
76             onChange(false);
77         }
78     }
79 
80     @Override
onCreate()81     public boolean onCreate() {
82         return true;
83     }
84 
addRows(MatrixCursor cursor, boolean partial)85     private void addRows(MatrixCursor cursor, boolean partial) {
86         for (int i = 0; i < 3; i++) {
87             cursor.addRow(new Object[]{
88                 i,
89                 (partial? "Partial" : "Final ") + " suggestion " + i,
90                 "This is a suggestion",
91                 Intent.ACTION_VIEW,
92                 "content://com.android.quicksearchbox.partial/partial/" + i
93             });
94         }
95     }
96 
97     @Override
query(Uri uri, String[] projectionIn, String selection, String[] selectionArgs, String sortOrder)98     public Cursor query(Uri uri, String[] projectionIn, String selection,
99             String[] selectionArgs, String sortOrder) {
100         Log.d(TAG, "query(" + uri + ")");
101         mType = (mType + 1) % 3;
102 
103         if (mType == 0) {
104             Log.d(TAG, "returning null cursor");
105             return null;
106         }
107 
108         MutableMatrixCursor cursor = new MutableMatrixCursor(COLUMNS);
109         if (mType == 1) {
110             addRows(cursor, true);
111         } else {
112             Log.d(TAG, "returning empty cursor");
113         }
114         cursor.getExtras().putBoolean(SearchManager.CURSOR_EXTRA_KEY_IN_PROGRESS, true);
115         mCursor = cursor;
116         // Update the cursor in 2 seconds
117         mHandler.removeMessages(MSG_COMPLETE);
118         mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_COMPLETE), 2000);
119         return cursor;
120     }
121 
122     @Override
getType(Uri uri)123     public String getType(Uri uri) {
124         return SearchManager.SUGGEST_MIME_TYPE;
125     }
126 
127     @Override
insert(Uri uri, ContentValues values)128     public Uri insert(Uri uri, ContentValues values) {
129         throw new UnsupportedOperationException();
130     }
131 
132     @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)133     public int update(Uri uri, ContentValues values, String selection,
134             String[] selectionArgs) {
135         throw new UnsupportedOperationException();
136     }
137 
138     @Override
delete(Uri uri, String selection, String[] selectionArgs)139     public int delete(Uri uri, String selection, String[] selectionArgs) {
140         throw new UnsupportedOperationException();
141     }
142 
143 }
144