1 /* 2 * Copyright (C) 2008 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.example.android.apis.app; 18 19 import com.example.android.apis.R; 20 21 import android.app.Activity; 22 import android.app.SearchManager; 23 import android.content.Intent; 24 import android.os.Bundle; 25 import android.provider.SearchRecentSuggestions; 26 import android.widget.TextView; 27 28 public class SearchQueryResults extends Activity 29 { 30 // UI elements 31 TextView mQueryText; 32 TextView mAppDataText; 33 TextView mDeliveredByText; 34 35 /** Called with the activity is first created. 36 * 37 * After the typical activity setup code, we check to see if we were launched 38 * with the ACTION_SEARCH intent, and if so, we handle it. 39 */ 40 @Override onCreate(Bundle savedInstanceState)41 public void onCreate(Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 44 // Inflate our UI from its XML layout description. 45 setContentView(R.layout.search_query_results); 46 47 // Get active display items for later updates 48 mQueryText = (TextView) findViewById(R.id.txt_query); 49 mAppDataText = (TextView) findViewById(R.id.txt_appdata); 50 mDeliveredByText = (TextView) findViewById(R.id.txt_deliveredby); 51 52 // get and process search query here 53 final Intent queryIntent = getIntent(); 54 final String queryAction = queryIntent.getAction(); 55 if (Intent.ACTION_SEARCH.equals(queryAction)) { 56 doSearchQuery(queryIntent, "onCreate()"); 57 } 58 else { 59 mDeliveredByText.setText("onCreate(), but no ACTION_SEARCH intent"); 60 } 61 } 62 63 /** 64 * Called when new intent is delivered. 65 * 66 * This is where we check the incoming intent for a query string. 67 * 68 * @param newIntent The intent used to restart this activity 69 */ 70 @Override onNewIntent(final Intent newIntent)71 public void onNewIntent(final Intent newIntent) { 72 super.onNewIntent(newIntent); 73 74 // get and process search query here 75 final Intent queryIntent = getIntent(); 76 final String queryAction = queryIntent.getAction(); 77 if (Intent.ACTION_SEARCH.equals(queryAction)) { 78 doSearchQuery(queryIntent, "onNewIntent()"); 79 } 80 else { 81 mDeliveredByText.setText("onNewIntent(), but no ACTION_SEARCH intent"); 82 } 83 } 84 85 /** 86 * Generic search handler. 87 * 88 * In a "real" application, you would use the query string to select results from 89 * your data source, and present a list of those results to the user. 90 */ doSearchQuery(final Intent queryIntent, final String entryPoint)91 private void doSearchQuery(final Intent queryIntent, final String entryPoint) { 92 93 // The search query is provided as an "extra" string in the query intent 94 final String queryString = queryIntent.getStringExtra(SearchManager.QUERY); 95 mQueryText.setText(queryString); 96 97 // Record the query string in the recent queries suggestions provider. 98 SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this, 99 SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE); 100 suggestions.saveRecentQuery(queryString, null); 101 102 // If your application provides context data for its searches, 103 // you will receive it as an "extra" bundle in the query intent. 104 // The bundle can contain any number of elements, using any number of keys; 105 // For this Api Demo we're just using a single string, stored using "demo key". 106 final Bundle appData = queryIntent.getBundleExtra(SearchManager.APP_DATA); 107 if (appData == null) { 108 mAppDataText.setText("<no app data bundle>"); 109 } 110 if (appData != null) { 111 String testStr = appData.getString("demo_key"); 112 mAppDataText.setText((testStr == null) ? "<no app data>" : testStr); 113 } 114 115 // Report the method by which we were called. 116 mDeliveredByText.setText(entryPoint); 117 } 118 } 119