1 | /* |
2 | * Tomdroid |
3 | * Tomboy on Android |
4 | * http://www.launchpad.net/tomdroid |
5 | * |
6 | * Copyright 2011 Olivier Bilodeau <olivier@bottomlesspit.org> |
7 | * Copyright 2011 Stefan Hammer <j.4@gmx.at> |
8 | * |
9 | * This file is part of Tomdroid. |
10 | * |
11 | * Tomdroid is free software: you can redistribute it and/or modify |
12 | * it under the terms of the GNU General Public License as published by |
13 | * the Free Software Foundation, either version 3 of the License, or |
14 | * (at your option) any later version. |
15 | * |
16 | * Tomdroid is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU General Public License for more details. |
20 | * |
21 | * You should have received a copy of the GNU General Public License |
22 | * along with Tomdroid. If not, see <http://www.gnu.org/licenses/>. |
23 | */ |
24 | package org.tomdroid.ui; |
25 | |
26 | import org.tomdroid.Note; |
27 | import org.tomdroid.NoteManager; |
28 | import org.tomdroid.R; |
29 | import org.tomdroid.sync.SyncManager; |
30 | import org.tomdroid.util.SearchSuggestionProvider; |
31 | |
32 | import android.app.ListActivity; |
33 | import android.app.SearchManager; |
34 | import android.content.Intent; |
35 | import android.database.Cursor; |
36 | import android.graphics.Color; |
37 | import android.net.Uri; |
38 | import android.os.Bundle; |
39 | import android.os.Handler; |
40 | import android.provider.SearchRecentSuggestions; |
41 | import android.util.Log; |
42 | import android.view.View; |
43 | import android.widget.ListAdapter; |
44 | import android.widget.ListView; |
45 | import android.widget.TextView; |
46 | |
47 | public class Search extends ListActivity { |
48 | |
49 | // Logging info |
50 | private static final String TAG = "Tomdroid Search"; |
51 | // UI elements |
52 | private TextView title; |
53 | // UI to data model glue |
54 | private TextView listEmptyView; |
55 | // UI feedback handler |
56 | private Handler syncMessageHandler = new SyncMessageHandler(this); |
57 | |
58 | private ListAdapter adapter; |
59 | |
60 | |
61 | @Override |
62 | public void onCreate(Bundle savedInstanceState) { |
63 | super.onCreate(savedInstanceState); |
64 | setContentView(R.layout.search); |
65 | |
66 | title = (TextView) findViewById(R.id.title); |
67 | title.setTextColor(Color.DKGRAY); |
68 | title.setTextSize(18.0f); |
69 | title.setText((CharSequence) getString(R.string.SearchResultTitle)); |
70 | |
71 | handleIntent(getIntent()); |
72 | } |
73 | |
74 | @Override |
75 | protected void onNewIntent(Intent intent) { |
76 | setIntent(intent); |
77 | handleIntent(intent); |
78 | } |
79 | |
80 | private void handleIntent(Intent intent) { |
81 | //handles a search query |
82 | if (Intent.ACTION_SEARCH.equals(intent.getAction())) { |
83 | String query = intent.getStringExtra(SearchManager.QUERY); |
84 | showResults(query); |
85 | } |
86 | |
87 | //adds query to search history suggestions |
88 | if (Intent.ACTION_SEARCH.equals(intent.getAction())) { |
89 | String query = intent.getStringExtra(SearchManager.QUERY); |
90 | SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this, |
91 | SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE); |
92 | suggestions.saveRecentQuery(query, null); |
93 | } |
94 | } |
95 | |
96 | public void showResults(String query) { |
97 | if (Tomdroid.LOGGING_ENABLED) Log.d(TAG,"Start searching for: "+query); |
98 | |
99 | |
100 | // adapter that binds the ListView UI to the notes in the note manager |
101 | adapter = NoteManager.getListAdapter(this, query); |
102 | setListAdapter(adapter); |
103 | |
104 | // set the view shown when query not found |
105 | listEmptyView = (TextView) findViewById(R.id.no_results); |
106 | listEmptyView.setText(getString(R.string.strNoResults, new Object[] {query})); |
107 | getListView().setEmptyView(listEmptyView); |
108 | } |
109 | |
110 | @Override |
111 | protected void onListItemClick(ListView l, View v, int position, long id) { |
112 | |
113 | Cursor item = (Cursor) adapter.getItem(position); |
114 | int noteId = item.getInt(item.getColumnIndexOrThrow(Note.ID)); |
115 | |
116 | Uri intentUri = Uri.parse(Tomdroid.CONTENT_URI + "/" + noteId); |
117 | Intent i = new Intent(Intent.ACTION_VIEW, intentUri, this, ViewNote.class); |
118 | startActivity(i); |
119 | } |
120 | |
121 | @Override |
122 | public void onResume(){ |
123 | super.onResume(); |
124 | SyncManager.setActivity(this); |
125 | SyncManager.setHandler(this.syncMessageHandler); |
126 | } |
127 | } |