Karl Rosaen | 7a3e95a | 2009-08-07 15:45:17 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2009 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.SearchManager; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.os.Bundle; |
| 24 | import android.text.TextUtils; |
| 25 | import android.util.Log; |
| 26 | import android.view.LayoutInflater; |
| 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
| 29 | import android.view.Menu; |
| 30 | import android.view.MenuItem; |
| 31 | import android.widget.AdapterView; |
| 32 | import android.widget.BaseAdapter; |
| 33 | import android.widget.ListView; |
| 34 | import android.widget.TextView; |
| 35 | import android.widget.TwoLineListItem; |
| 36 | |
| 37 | import java.util.List; |
| 38 | |
| 39 | /** |
| 40 | * The main activity for the dictionary. Also displays search results triggered by the search |
| 41 | * dialog. |
| 42 | */ |
| 43 | public class SearchableDictionary extends Activity { |
| 44 | |
| 45 | private static final int MENU_SEARCH = 1; |
| 46 | |
| 47 | private TextView mTextView; |
| 48 | private ListView mList; |
| 49 | |
| 50 | @Override |
| 51 | public void onCreate(Bundle savedInstanceState) { |
| 52 | super.onCreate(savedInstanceState); |
| 53 | |
| 54 | Intent intent = getIntent(); |
| 55 | |
| 56 | setContentView(R.layout.main); |
| 57 | mTextView = (TextView) findViewById(R.id.textField); |
| 58 | mList = (ListView) findViewById(R.id.list); |
| 59 | |
| 60 | if (Intent.ACTION_VIEW.equals(intent.getAction())) { |
| 61 | // from click on search results |
| 62 | Dictionary.getInstance().ensureLoaded(getResources()); |
| 63 | String word = intent.getDataString(); |
| 64 | Dictionary.Word theWord = Dictionary.getInstance().getMatches(word).get(0); |
| 65 | launchWord(theWord); |
| 66 | finish(); |
| 67 | } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) { |
| 68 | String query = intent.getStringExtra(SearchManager.QUERY); |
| 69 | mTextView.setText(getString(R.string.search_results, query)); |
| 70 | WordAdapter wordAdapter = new WordAdapter(Dictionary.getInstance().getMatches(query)); |
| 71 | mList.setAdapter(wordAdapter); |
| 72 | mList.setOnItemClickListener(wordAdapter); |
| 73 | } |
| 74 | |
| 75 | Log.d("dict", intent.toString()); |
| 76 | if (intent.getExtras() != null) { |
| 77 | Log.d("dict", intent.getExtras().keySet().toString()); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public boolean onCreateOptionsMenu(Menu menu) { |
| 83 | menu.add(0, MENU_SEARCH, 0, R.string.menu_search) |
| 84 | .setIcon(android.R.drawable.ic_search_category_default) |
| 85 | .setAlphabeticShortcut(SearchManager.MENU_KEY); |
| 86 | |
| 87 | return super.onCreateOptionsMenu(menu); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public boolean onOptionsItemSelected(MenuItem item) { |
| 92 | switch (item.getItemId()) { |
| 93 | case MENU_SEARCH: |
| 94 | onSearchRequested(); |
| 95 | return true; |
| 96 | } |
| 97 | return super.onOptionsItemSelected(item); |
| 98 | } |
| 99 | |
| 100 | private void launchWord(Dictionary.Word theWord) { |
| 101 | Intent next = new Intent(); |
| 102 | next.setClass(this, WordActivity.class); |
| 103 | next.putExtra("word", theWord.word); |
| 104 | next.putExtra("definition", theWord.definition); |
| 105 | startActivity(next); |
| 106 | } |
| 107 | |
| 108 | class WordAdapter extends BaseAdapter implements AdapterView.OnItemClickListener { |
| 109 | |
| 110 | private final List<Dictionary.Word> mWords; |
| 111 | private final LayoutInflater mInflater; |
| 112 | |
| 113 | public WordAdapter(List<Dictionary.Word> words) { |
| 114 | mWords = words; |
| 115 | mInflater = (LayoutInflater) SearchableDictionary.this.getSystemService( |
| 116 | Context.LAYOUT_INFLATER_SERVICE); |
| 117 | } |
| 118 | |
| 119 | public int getCount() { |
| 120 | return mWords.size(); |
| 121 | } |
| 122 | |
| 123 | public Object getItem(int position) { |
| 124 | return position; |
| 125 | } |
| 126 | |
| 127 | public long getItemId(int position) { |
| 128 | return position; |
| 129 | } |
| 130 | |
| 131 | public View getView(int position, View convertView, ViewGroup parent) { |
| 132 | TwoLineListItem view = (convertView != null) ? (TwoLineListItem) convertView : |
| 133 | createView(parent); |
| 134 | bindView(view, mWords.get(position)); |
| 135 | return view; |
| 136 | } |
| 137 | |
| 138 | private TwoLineListItem createView(ViewGroup parent) { |
| 139 | TwoLineListItem item = (TwoLineListItem) mInflater.inflate( |
| 140 | android.R.layout.simple_list_item_2, parent, false); |
| 141 | item.getText2().setSingleLine(); |
| 142 | item.getText2().setEllipsize(TextUtils.TruncateAt.END); |
| 143 | return item; |
| 144 | } |
| 145 | |
| 146 | private void bindView(TwoLineListItem view, Dictionary.Word word) { |
| 147 | view.getText1().setText(word.word); |
| 148 | view.getText2().setText(word.definition); |
| 149 | } |
| 150 | |
| 151 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 152 | launchWord(mWords.get(position)); |
| 153 | } |
| 154 | } |
| 155 | } |