• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Build;
27 import android.os.Bundle;
28 import android.view.Menu;
29 import android.view.MenuInflater;
30 import android.view.MenuItem;
31 import android.view.View;
32 import android.widget.AdapterView;
33 import android.widget.ListView;
34 import android.widget.SearchView;
35 import android.widget.SimpleCursorAdapter;
36 import android.widget.TextView;
37 import android.widget.AdapterView.OnItemClickListener;
38 
39 /**
40  * The main activity for the dictionary.
41  * Displays search results triggered by the search dialog and handles
42  * actions from search suggestions.
43  */
44 public class SearchableDictionary extends Activity {
45 
46     private TextView mTextView;
47     private ListView mListView;
48 
49     @Override
onCreate(Bundle savedInstanceState)50     public void onCreate(Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52         setContentView(R.layout.main);
53 
54         mTextView = (TextView) findViewById(R.id.text);
55         mListView = (ListView) findViewById(R.id.list);
56 
57         handleIntent(getIntent());
58     }
59 
60     @Override
onNewIntent(Intent intent)61     protected void onNewIntent(Intent intent) {
62         // Because this activity has set launchMode="singleTop", the system calls this method
63         // to deliver the intent if this activity is currently the foreground activity when
64         // invoked again (when the user executes a search from this activity, we don't create
65         // a new instance of this activity, so the system delivers the search intent here)
66         handleIntent(intent);
67     }
68 
handleIntent(Intent intent)69     private void handleIntent(Intent intent) {
70         if (Intent.ACTION_VIEW.equals(intent.getAction())) {
71             // handles a click on a search suggestion; launches activity to show word
72             Intent wordIntent = new Intent(this, WordActivity.class);
73             wordIntent.setData(intent.getData());
74             startActivity(wordIntent);
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 
117                 @Override
118                 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
119                     // Build the Intent used to open WordActivity with a specific word Uri
120                     Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class);
121                     Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI,
122                                                     String.valueOf(id));
123                     wordIntent.setData(data);
124                     startActivity(wordIntent);
125                 }
126             });
127         }
128     }
129 
130     @Override
onCreateOptionsMenu(Menu menu)131     public boolean onCreateOptionsMenu(Menu menu) {
132         MenuInflater inflater = getMenuInflater();
133         inflater.inflate(R.menu.options_menu, menu);
134 
135         if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
136             SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
137             SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
138             searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
139             searchView.setIconifiedByDefault(false);
140         }
141 
142         return true;
143     }
144 
145     @Override
onOptionsItemSelected(MenuItem item)146     public boolean onOptionsItemSelected(MenuItem item) {
147         switch (item.getItemId()) {
148             case R.id.search:
149                 onSearchRequested();
150                 return true;
151             default:
152                 return false;
153         }
154     }
155 }
156