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.example.android.searchabledict; 18 19 import android.app.Activity; 20 import android.app.ActionBar; 21 import android.app.SearchManager; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.database.Cursor; 25 import android.net.Uri; 26 import android.os.Bundle; 27 import android.view.Menu; 28 import android.view.MenuInflater; 29 import android.view.MenuItem; 30 import android.view.View; 31 import android.widget.AdapterView; 32 import android.widget.ListView; 33 import android.widget.SearchView; 34 import android.widget.SimpleCursorAdapter; 35 import android.widget.TextView; 36 import android.widget.AdapterView.OnItemClickListener; 37 38 /** 39 * The main activity for the dictionary. 40 * Displays search results triggered by the search dialog and handles 41 * actions from search suggestions. 42 */ 43 public class SearchableDictionary extends Activity { 44 45 private TextView mTextView; 46 private ListView mListView; 47 48 @Override onCreate(Bundle savedInstanceState)49 public void onCreate(Bundle savedInstanceState) { 50 super.onCreate(savedInstanceState); 51 setContentView(R.layout.main); 52 53 mTextView = (TextView) findViewById(R.id.text); 54 mListView = (ListView) findViewById(R.id.list); 55 56 handleIntent(getIntent()); 57 } 58 59 @Override onNewIntent(Intent intent)60 protected void onNewIntent(Intent intent) { 61 // Because this activity has set launchMode="singleTop", the system calls this method 62 // to deliver the intent if this actvity is currently the foreground activity when 63 // invoked again (when the user executes a search from this activity, we don't create 64 // a new instance of this activity, so the system delivers the search intent here) 65 handleIntent(intent); 66 } 67 handleIntent(Intent intent)68 private void handleIntent(Intent intent) { 69 if (Intent.ACTION_VIEW.equals(intent.getAction())) { 70 // handles a click on a search suggestion; launches activity to show word 71 Intent wordIntent = new Intent(this, WordActivity.class); 72 wordIntent.setData(intent.getData()); 73 startActivity(wordIntent); 74 finish(); 75 } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 76 // handles a search query 77 String query = intent.getStringExtra(SearchManager.QUERY); 78 showResults(query); 79 } 80 } 81 82 /** 83 * Searches the dictionary and displays results for the given query. 84 * @param query The search query 85 */ showResults(String query)86 private void showResults(String query) { 87 88 Cursor cursor = managedQuery(DictionaryProvider.CONTENT_URI, null, null, 89 new String[] {query}, null); 90 91 if (cursor == null) { 92 // There are no results 93 mTextView.setText(getString(R.string.no_results, new Object[] {query})); 94 } else { 95 // Display the number of results 96 int count = cursor.getCount(); 97 String countString = getResources().getQuantityString(R.plurals.search_results, 98 count, new Object[] {count, query}); 99 mTextView.setText(countString); 100 101 // Specify the columns we want to display in the result 102 String[] from = new String[] { DictionaryDatabase.KEY_WORD, 103 DictionaryDatabase.KEY_DEFINITION }; 104 105 // Specify the corresponding layout elements where we want the columns to go 106 int[] to = new int[] { R.id.word, 107 R.id.definition }; 108 109 // Create a simple cursor adapter for the definitions and apply them to the ListView 110 SimpleCursorAdapter words = new SimpleCursorAdapter(this, 111 R.layout.result, cursor, from, to); 112 mListView.setAdapter(words); 113 114 // Define the on-click listener for the list items 115 mListView.setOnItemClickListener(new OnItemClickListener() { 116 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 117 // Build the Intent used to open WordActivity with a specific word Uri 118 Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class); 119 Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI, 120 String.valueOf(id)); 121 wordIntent.setData(data); 122 startActivity(wordIntent); 123 } 124 }); 125 } 126 } 127 128 @Override onCreateOptionsMenu(Menu menu)129 public boolean onCreateOptionsMenu(Menu menu) { 130 MenuInflater inflater = getMenuInflater(); 131 inflater.inflate(R.menu.options_menu, menu); 132 133 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 134 SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView(); 135 searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 136 searchView.setIconifiedByDefault(false); 137 138 return true; 139 } 140 141 @Override onOptionsItemSelected(MenuItem item)142 public boolean onOptionsItemSelected(MenuItem item) { 143 switch (item.getItemId()) { 144 case R.id.search: 145 onSearchRequested(); 146 return true; 147 default: 148 return false; 149 } 150 } 151 } 152