Suggest Url Provider
package app.test;
import android.app.Activity;
import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Intent;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
class SuggestUrlProvider extends ContentProvider
{
public static String AUTHORITY = "app.test.suggesturlprovider";
final int SEARCH_SUGGEST = 0;
private static final int SHORTCUT_REFRESH = 1;
private static final UriMatcher sURIMatcher = buildUriMatcher();
private static final String[] COLUMNS = {
"_id", // must include this column
SearchManager.SUGGEST_COLUMN_TEXT_1,
SearchManager.SUGGEST_COLUMN_TEXT_2,
SearchManager.SUGGEST_COLUMN_INTENT_DATA,
SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
SearchManager.SUGGEST_COLUMN_SHORTCUT_ID
};
private static UriMatcher buildUriMatcher() {
UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT, SHORTCUT_REFRESH);
matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT + "/*", SHORTCUT_REFRESH);
return matcher;
}
@Override
public boolean onCreate() {
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
String query = selectionArgs[0];
switch (sURIMatcher.match(uri)) {
case SEARCH_SUGGEST:
return getSuggestions(query);
case SHORTCUT_REFRESH:
return null;
default:
throw new IllegalArgumentException("Unknown URL " + uri);
}
}
private Cursor getSuggestions(String query)
{
if (query == null) return null;
String word = getWord(query);
if (word == null)
return null;
MatrixCursor cursor = new MatrixCursor(COLUMNS);
cursor.addRow(createRow1(word));
cursor.addRow(createRow2(word));
return cursor;
}
private Object[] createRow1(String query)
{
return columnValuesOfQuery(query,
"android.intent.action.VIEW",
"http://www.thefreedictionary.com/" + query,
"Look up in freedictionary.com for",
query);
}
private Object[] createRow2(String query)
{
return columnValuesOfQuery(query,
"android.intent.action.VIEW",
"http://www.google.com/search?hl=en&source=hp&q=define%3A" + query,
"Look up in google.com for",
query);
}
private Object[] columnValuesOfQuery(String query,
String intentAction,
String url,
String text1,
String text2)
{
return new String[] {
query, // _id
text1, // text1
text2, // text2
url, // intent_data (included when clicking on item)
intentAction, //action
SearchManager.SUGGEST_NEVER_MAKE_SHORTCUT
};
}
private Cursor refreshShortcut(String shortcutId, String[] projection) {
return null;
}
public String getType(Uri uri) {
switch (sURIMatcher.match(uri)) {
case SEARCH_SUGGEST:
return SearchManager.SUGGEST_MIME_TYPE;
case SHORTCUT_REFRESH:
return SearchManager.SHORTCUT_MIME_TYPE;
default:
throw new IllegalArgumentException("Unknown URL " + uri);
}
}
public Uri insert(Uri uri, ContentValues values) {
throw new UnsupportedOperationException();
}
public int delete(Uri uri, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException();
}
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException();
}
private String getWord(String query)
{
int dotIndex = query.indexOf('.');
if (dotIndex < 0)
return null;
return query.substring(0,dotIndex);
}
}
public class Test extends Activity
{
private final static String tag ="SearchActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Intent queryIntent = getIntent();
final String queryAction = queryIntent.getAction();
final String queryString = queryIntent.getStringExtra(SearchManager.QUERY);
if (Intent.ACTION_SEARCH.equals(queryAction))
{
this.doSearchQuery(queryIntent);
}
else if (Intent.ACTION_VIEW.equals(queryAction))
{
this.doView(queryIntent);
}
return;
}
@Override
public void onNewIntent(final Intent newIntent)
{
super.onNewIntent(newIntent);
final Intent queryIntent = newIntent;
final String queryAction = queryIntent.getAction();
final String queryString = queryIntent.getStringExtra(SearchManager.QUERY);
if (Intent.ACTION_SEARCH.equals(queryAction)) {
this.doSearchQuery(queryIntent);
}else if (Intent.ACTION_VIEW.equals(queryAction)){
this.doView(queryIntent);
}
return;
}
private void doSearchQuery(final Intent queryIntent)
{
final String queryString = queryIntent.getStringExtra(SearchManager.QUERY);
appendText("You are searching for:" + queryString);
}
private void appendText(String msg)
{
TextView tv = (TextView)this.findViewById(R.id.text1);
tv.setText(tv.getText() + "\n" + msg);
}
private void doView(final Intent queryIntent)
{
Uri uri = queryIntent.getData();
String action = queryIntent.getAction();
Intent i = new Intent(action);
i.setData(uri);
i.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
startActivity(i);
this.finish();
}
}
Related examples in the same category