Android Samples
修訂 | 93e47b0f72244c8ef93e84ffb30a546bf1ebb777 (tree) |
---|---|
時間 | 2013-05-10 18:03:44 |
作者 | Masahiko, SAWAI <say@user...> |
Commiter | Masahiko, SAWAI |
searchview-recent-query-suggestion を追加
@@ -34,7 +34,7 @@ | ||
34 | 34 | <module>actionmode-hello</module> |
35 | 35 | <module>actionmode-listview</module> |
36 | 36 | <module>actionmode-listview-dynamic-layout</module> |
37 | - <module>actionmode-listview-fragment</module> | |
37 | + <module>actionmode-listview-in-fragment</module> | |
38 | 38 | <module>android-app-hello</module> |
39 | 39 | <module>app-uuid-hello</module> |
40 | 40 | <module>appwidget-gridview</module> |
@@ -63,6 +63,9 @@ | ||
63 | 63 | <module>preference-custom-dialog</module> |
64 | 64 | <module>preference-fragment-hello</module> |
65 | 65 | <module>searchview-hello</module> |
66 | + <module>searchview-in-actionbar</module> | |
67 | + <module>searchview-in-actionbar-collapse</module> | |
68 | + <module>searchview-recent-query-suggestion</module> | |
66 | 69 | <module>view-composite-hello</module> |
67 | 70 | <module>wifi-state-get</module> |
68 | 71 | <module>wifi-state-receiver</module> |
@@ -0,0 +1,56 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<manifest | |
3 | + xmlns:android="http://schemas.android.com/apk/res/android" | |
4 | + package="com.example.hello.android.searchview_recent_query_suggestion" | |
5 | + android:versionCode="1" | |
6 | + android:versionName="1.0" | |
7 | +> | |
8 | + <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="11" /> | |
9 | + | |
10 | + <application android:label="@string/app_name"> | |
11 | + | |
12 | + <activity | |
13 | + android:name=".UserListActivity" | |
14 | + android:label="@string/app_name" | |
15 | + android:launchMode="singleTop" | |
16 | + > | |
17 | + <intent-filter> | |
18 | + <action android:name="android.intent.action.MAIN" /> | |
19 | + <category android:name="android.intent.category.LAUNCHER" /> | |
20 | + </intent-filter> | |
21 | + <intent-filter> | |
22 | + <action android:name="android.intent.action.SEARCH" /> | |
23 | + <!-- | |
24 | + <category android:name="android.intent.category.DEFAULT" /> | |
25 | + --> | |
26 | + </intent-filter> | |
27 | + | |
28 | + <!-- 検索可能なアクティビティであることを示すメタデータ --> | |
29 | + <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/> | |
30 | + </activity> | |
31 | + | |
32 | + <provider | |
33 | + android:name=".db.UserSearchRecentSuggestionProvider" | |
34 | + android:authorities="com.example.hello.android.searchview_recent_query_suggestion_provider" | |
35 | + android:syncable="false" | |
36 | + /> | |
37 | + | |
38 | + <!-- | |
39 | + 検索機能を利用した際に検索に使うアクティビティを示すメタデータ。 | |
40 | + application 要素の直下に記述するとすべてのアクティビティで | |
41 | + 検索機能を要求したとき(検索キーを押したときなど)のデフォルトの | |
42 | + 検索アクティビティとなる。 | |
43 | + activity 要素以下に記述してそのアクティビティ用の検索アクティビティ | |
44 | + を示すこともできる。 | |
45 | + --> | |
46 | + <meta-data | |
47 | + android:name="android.app.default_searchable" | |
48 | + android:value=".UserListActivity" | |
49 | + /> | |
50 | + | |
51 | + </application> | |
52 | + | |
53 | + <!-- | |
54 | + <uses-permission android:name="android.permission.INTERNET" /> | |
55 | + --> | |
56 | +</manifest> |
@@ -0,0 +1,47 @@ | ||
1 | +RELEASE_PROFILE=android-hello | |
2 | + | |
3 | +all : package | |
4 | + | |
5 | +#################### build | |
6 | + | |
7 | +compile : | |
8 | + mvn $@ | |
9 | + | |
10 | +gen : | |
11 | + mvn android:generate-sources | |
12 | + | |
13 | +package : | |
14 | + mvn $@ | |
15 | + | |
16 | +release : | |
17 | + mvn clean && mvn -P$(RELEASE_PROFILE) package | |
18 | + | |
19 | +install : | |
20 | + mvn $@ | |
21 | + | |
22 | +deploy : | |
23 | + mvn package android:deploy | |
24 | + | |
25 | +undeploy : | |
26 | + mvn android:undeploy | |
27 | + | |
28 | +rebuild : | |
29 | + mvn clean package | |
30 | + | |
31 | +redeploy : | |
32 | + mvn clean package android:deploy | |
33 | + | |
34 | + | |
35 | +#################### emulator | |
36 | +start : | |
37 | + mvn android:emulator-start | |
38 | + | |
39 | +stop : | |
40 | + mvn android:emulator-stop | |
41 | + | |
42 | +#################### project | |
43 | +dist : | |
44 | + mvn assembly:assembly | |
45 | + | |
46 | +clean : | |
47 | + mvn $@ |
@@ -0,0 +1,43 @@ | ||
1 | +! searchview-recent-suggestion (API Level 11) | |
2 | + | |
3 | +検索機能を実装してるサンプル。 | |
4 | + | |
5 | +Android 3.0 からアクションバー上にアクションビューとして表示できる | |
6 | +SerachView というクラスが用意されている。これを用いて検索機能を実現 | |
7 | +するサンプル。 | |
8 | + | |
9 | +以下の機能を実装している。 | |
10 | +* 入力テキストで検索する | |
11 | +* 最近検索した検索語を候補として表示 | |
12 | + | |
13 | +最近検索したテキストを候補に表示するには、 SearchRecentSuggestionsProvider を | |
14 | +継承した Content Provider を作成して AndroidManifest.xml に登録。 | |
15 | + | |
16 | +さらに searchable 要素をXMLファイルに定義する。 | |
17 | +このサンプルでは searchable.xml に定義している。 | |
18 | + | |
19 | +作成した searchable.xml は AndroidManifest.xml のアクティビティ要素から | |
20 | +参照する。 | |
21 | + | |
22 | +searchable 要素に以下のような属性を指定する。 | |
23 | +* android:searchSuggestAuthority="com.example.hello.android.search_box_hello.user_search_suggestion_provider" | |
24 | +* android:searchSuggestSelection=" ? " | |
25 | +* android:searchSuggestIntentAction="android.intent.action.VIEW" | |
26 | + | |
27 | + | |
28 | +!! ビルドターゲット | |
29 | + | |
30 | +http://maven-android-plugin-m2site.googlecode.com/svn/plugin-info.html | |
31 | + | |
32 | +* mvn compile - アプリケーションのコンパイル | |
33 | +* mvn package - apk の作成 | |
34 | +* mvn clean - ビルドファイルの削除 | |
35 | +* mvn install - maven の local リポジトリにインストール | |
36 | + | |
37 | +* mvn android:deploy - エミュレータやデバイスにインストール | |
38 | +* mvn android:generate-sources | |
39 | + | |
40 | +* mvn install - アプリケーションのインストール | |
41 | +* mvn uninstall - アプリケーションのアンインストール | |
42 | + | |
43 | + |
@@ -0,0 +1,145 @@ | ||
1 | +<?xml version="1.0" encoding="UTF-8" ?> | |
2 | +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
3 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" | |
4 | +> | |
5 | + <modelVersion>4.0.0</modelVersion> | |
6 | + <groupId>com.example.hello</groupId> | |
7 | + <artifactId>searchview-recent-query-suggestion</artifactId> | |
8 | + <packaging>apk</packaging> | |
9 | + <version>1.0.0</version> | |
10 | + <name>SerachView : Recent Query Suggestion</name> | |
11 | + | |
12 | + <parent> | |
13 | + <groupId>com.example.hello</groupId> | |
14 | + <artifactId>android-samples</artifactId> | |
15 | + <version>1.0.0</version> | |
16 | + </parent> | |
17 | + | |
18 | + <dependencies> | |
19 | + | |
20 | + <dependency> | |
21 | + <groupId>android</groupId> | |
22 | + <artifactId>android</artifactId> | |
23 | + <version>3.0_r2</version> | |
24 | + <!-- | |
25 | + <version>1.5_r3</version> | |
26 | + <version>1.5_r4</version> | |
27 | + <version>1.6_r2</version> | |
28 | + <version>2.1.2</version> | |
29 | + <version>2.1_r1</version> | |
30 | + <version>2.2.1</version> | |
31 | + <version>2.3.1</version> | |
32 | + <version>2.3.3</version> | |
33 | + <version>4.0.1.2</version> | |
34 | + --> | |
35 | + <scope>provided</scope> | |
36 | + </dependency> | |
37 | + | |
38 | + <dependency> | |
39 | + <groupId>junit</groupId> | |
40 | + <artifactId>junit</artifactId> | |
41 | + <scope>test</scope> | |
42 | + </dependency> | |
43 | + </dependencies> | |
44 | + | |
45 | + <build> | |
46 | + <sourceDirectory>src</sourceDirectory> | |
47 | + | |
48 | + <plugins> | |
49 | + <plugin> | |
50 | + <groupId>com.jayway.maven.plugins.android.generation2</groupId> | |
51 | + <artifactId>android-maven-plugin</artifactId> | |
52 | + <configuration> | |
53 | + <sdk> | |
54 | + <path>${env.ANDROID_HOME}</path> | |
55 | + <!-- <path>C:/Java/android-sdk</path> --> | |
56 | + <platform>11</platform> | |
57 | + </sdk> | |
58 | + <proguard> | |
59 | + <skip>true</skip> | |
60 | + <config>proguard.conf</config> | |
61 | + </proguard> | |
62 | + <extractDuplicates>true</extractDuplicates> | |
63 | + </configuration> | |
64 | + <extensions>true</extensions> | |
65 | + </plugin> | |
66 | + | |
67 | + <!-- mvn compile --> | |
68 | + <plugin> | |
69 | + <groupId>org.apache.maven.plugins</groupId> | |
70 | + <artifactId>maven-compiler-plugin</artifactId> | |
71 | + <configuration> | |
72 | + <source>1.5</source> | |
73 | + <target>1.5</target> | |
74 | + <encoding>UTF-8</encoding> | |
75 | + </configuration> | |
76 | + </plugin> | |
77 | + | |
78 | + <!-- mvn assembly:assembly --> | |
79 | + <plugin> | |
80 | + <groupId>org.apache.maven.plugins</groupId> | |
81 | + <artifactId>maven-assembly-plugin</artifactId> | |
82 | + <configuration> | |
83 | + <descriptorRefs> | |
84 | + <!-- | |
85 | + <descriptorRef>jar-with-dependencies</descriptorRef> | |
86 | + <descriptorRef>bin</descriptorRef> | |
87 | + <descriptorRef>src</descriptorRef> | |
88 | + --> | |
89 | + <descriptorRef>project</descriptorRef> | |
90 | + </descriptorRefs> | |
91 | + </configuration> | |
92 | + </plugin> | |
93 | + | |
94 | + <!-- mvn resources:resources --> | |
95 | + <plugin> | |
96 | + <groupId>org.apache.maven.plugins</groupId> | |
97 | + <artifactId>maven-resources-plugin</artifactId> | |
98 | + <configuration> | |
99 | + <encoding>UTF-8</encoding> | |
100 | + </configuration> | |
101 | + </plugin> | |
102 | + | |
103 | + <!-- mvn site --> | |
104 | + <plugin> | |
105 | + <groupId>org.apache.maven.plugins</groupId> | |
106 | + <artifactId>maven-site-plugin</artifactId> | |
107 | + <configuration> | |
108 | + <locales>en</locales> | |
109 | + <inputEncoding>UTF-8</inputEncoding> | |
110 | + <outputEncoding>UTF-8</outputEncoding> | |
111 | + | |
112 | + <reportPlugins> | |
113 | + <plugin> | |
114 | + <groupId>org.apache.maven.plugins</groupId> | |
115 | + <artifactId>maven-project-info-reports-plugin</artifactId> | |
116 | + </plugin> | |
117 | + | |
118 | + <!-- mvn javadoc:javadoc --> | |
119 | + <plugin> | |
120 | + <groupId>org.apache.maven.plugins</groupId> | |
121 | + <artifactId>maven-javadoc-plugin</artifactId> | |
122 | + <configuration> | |
123 | + <charset>utf-8</charset> | |
124 | + </configuration> | |
125 | + </plugin> | |
126 | + </reportPlugins> | |
127 | + </configuration> | |
128 | + </plugin> | |
129 | + | |
130 | + </plugins> | |
131 | + | |
132 | + <!-- mvn resources:resources --> | |
133 | + <resources> | |
134 | + <resource> | |
135 | + <directory>res</directory> | |
136 | + <filtering>false</filtering> | |
137 | + <includes> | |
138 | + <include>**/*.properties</include> | |
139 | + </includes> | |
140 | + </resource> | |
141 | + </resources> | |
142 | + </build> | |
143 | + | |
144 | + | |
145 | +</project> |
@@ -0,0 +1,19 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
3 | + android:layout_width="fill_parent" | |
4 | + android:layout_height="fill_parent" | |
5 | +> | |
6 | + <ListView | |
7 | + android:id="@android:id/list" | |
8 | + android:layout_width="fill_parent" | |
9 | + android:layout_height="fill_parent" | |
10 | + android:fastScrollEnabled="true" | |
11 | + /> | |
12 | + <TextView | |
13 | + android:id="@android:id/empty" | |
14 | + android:layout_width="wrap_content" | |
15 | + android:layout_height="wrap_content" | |
16 | + android:layout_gravity="center" | |
17 | + android:text="@string/no_data" | |
18 | + /> | |
19 | +</FrameLayout> | |
\ No newline at end of file |
@@ -0,0 +1,11 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<LinearLayout | |
3 | + xmlns:android="http://schemas.android.com/apk/res/android" | |
4 | + android:orientation="vertical" | |
5 | + android:layout_width="fill_parent" | |
6 | + android:layout_height="fill_parent" | |
7 | + android:padding="4dip" | |
8 | +> | |
9 | + <include layout="@layout/simple_list" /> | |
10 | +</LinearLayout> | |
11 | + |
@@ -0,0 +1,28 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<menu xmlns:android="http://schemas.android.com/apk/res/android"> | |
3 | + <!-- | |
4 | + showAsAction | |
5 | + - ifRoom | |
6 | + - always | |
7 | + - never | |
8 | + - withText | |
9 | + - collapseActionView (API Level 14) | |
10 | + --> | |
11 | + <item | |
12 | + android:id="@+id/search_menuitem" | |
13 | + android:title="@string/search" | |
14 | + android:icon="@android:drawable/ic_menu_search" | |
15 | + android:actionViewClass="android.widget.SearchView" | |
16 | + android:showAsAction="ifRoom" | |
17 | + android:numericShortcut="1" | |
18 | + android:alphabeticShortcut="s" | |
19 | + /> | |
20 | + <item | |
21 | + android:id="@+id/quit_menuitem" | |
22 | + android:title="@string/quit" | |
23 | + android:icon="@android:drawable/ic_menu_close_clear_cancel" | |
24 | + android:numericShortcut="9" | |
25 | + android:alphabeticShortcut="q" | |
26 | + android:showAsAction="never" | |
27 | + /> | |
28 | +</menu> | |
\ No newline at end of file |
@@ -0,0 +1,31 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<resources> | |
3 | + <array name="names"> | |
4 | + <item>Alice</item> | |
5 | + <item>Bob</item> | |
6 | + <item>Charlie</item> | |
7 | + <item>David</item> | |
8 | + <item>Eve</item> | |
9 | + <item>Frank</item> | |
10 | + <item>George</item> | |
11 | + <item>Harvey</item> | |
12 | + <item>Ivan</item> | |
13 | + <item>Justin</item> | |
14 | + <item>Kyle</item> | |
15 | + <item>Linda</item> | |
16 | + <item>Matilda</item> | |
17 | + <item>Natalia</item> | |
18 | + <item>Oscar</item> | |
19 | + <item>Pat</item> | |
20 | + <item>Quincy</item> | |
21 | + <item>Rachel</item> | |
22 | + <item>Steave</item> | |
23 | + <item>Trent</item> | |
24 | + <item>Urien</item> | |
25 | + <item>Victor</item> | |
26 | + <item>Walter</item> | |
27 | + <item>Xenia</item> | |
28 | + <item>Yuriya</item> | |
29 | + <item>Zoe</item> | |
30 | + </array> | |
31 | +</resources> |
@@ -0,0 +1,14 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<resources> | |
3 | + <string name="app_name">SearchView Suggestion</string> | |
4 | + <string name="hello">Hello Android World!!</string> | |
5 | + <string name="no_data">NO DATA</string> | |
6 | + | |
7 | + <string name="search">Search</string> | |
8 | + <string name="user_search_description">User Description</string> | |
9 | + <string name="user_search_label">Search Box Hello</string> | |
10 | + <string name="user_search_hint">Search users</string> | |
11 | + | |
12 | + <string name="quit">Quit</string> | |
13 | + | |
14 | +</resources> |
@@ -0,0 +1,13 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<searchable | |
3 | + xmlns:android="http://schemas.android.com/apk/res/android" | |
4 | + android:label="@string/user_search_label" | |
5 | + android:hint="@string/user_search_hint" | |
6 | + android:searchSuggestAuthority="com.example.hello.android.searchview_recent_query_suggestion_provider" | |
7 | + android:searchSuggestSelection=" ? " | |
8 | + android:searchSuggestIntentAction="android.intent.action.VIEW" | |
9 | +/> | |
10 | +<!-- | |
11 | + android:includeInGlobalSearch="true" | |
12 | + android:searchSettingsDescription="@string/user_search_description" | |
13 | +--> | |
\ No newline at end of file |
@@ -0,0 +1,224 @@ | ||
1 | +package com.example.hello.android.searchview_recent_query_suggestion; | |
2 | + | |
3 | +import android.app.ListActivity; | |
4 | +import android.app.SearchManager; | |
5 | +import android.app.SearchableInfo; | |
6 | +import android.content.Intent; | |
7 | +import android.database.Cursor; | |
8 | +import android.database.sqlite.SQLiteDatabase; | |
9 | +import android.os.Bundle; | |
10 | +import android.provider.SearchRecentSuggestions; | |
11 | +import android.view.Menu; | |
12 | +import android.view.MenuInflater; | |
13 | +import android.view.MenuItem; | |
14 | +import android.widget.SearchView; | |
15 | +import android.widget.SimpleCursorAdapter; | |
16 | +import com.example.hello.android.searchview_recent_query_suggestion.db.UserListOpenHelper; | |
17 | +import com.example.hello.android.searchview_recent_query_suggestion.db.UserSearchRecentSuggestionProvider; | |
18 | +import com.example.hello.android.searchview_recent_query_suggestion.util.Log; | |
19 | + | |
20 | +public class UserListActivity extends ListActivity | |
21 | + implements SearchView.OnCloseListener | |
22 | +{ | |
23 | + | |
24 | + private static final String LOG_TAG = "XXX"; | |
25 | + private static final String[] MAPPING_FROM = new String[] | |
26 | + { | |
27 | + "name", "email" | |
28 | + }; | |
29 | + private static final int[] MAPPING_TO = new int[] | |
30 | + { | |
31 | + android.R.id.text1, android.R.id.text2, | |
32 | + }; | |
33 | + private UserListOpenHelper dbHelper; | |
34 | + private SQLiteDatabase db; | |
35 | + private Cursor cursor; | |
36 | + private SimpleCursorAdapter adapter; | |
37 | + private SearchView searchView; | |
38 | + | |
39 | + @Override | |
40 | + public boolean onCreateOptionsMenu(Menu menu) | |
41 | + { | |
42 | + MenuInflater menuInflater = getMenuInflater(); | |
43 | + menuInflater.inflate(R.menu.user_list_activity_option_menu, menu); | |
44 | + | |
45 | + // SearchView の取得 | |
46 | + MenuItem searchMenuItem = menu.findItem(R.id.search_menuitem); | |
47 | + searchView = (SearchView) searchMenuItem.getActionView(); | |
48 | + searchView.setOnCloseListener(this); | |
49 | + | |
50 | + // このアクティビティの SearchableInfo を取得 | |
51 | + // (AndroidManifest.xml で設定してある SearchableInfo が取得される) | |
52 | + SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE); | |
53 | + SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName()); | |
54 | + | |
55 | + // SearchView に SearchableInfo を設定 | |
56 | + searchView.setSearchableInfo(searchableInfo); | |
57 | + | |
58 | + // SearchView の表示の設定 | |
59 | + searchView.setIconifiedByDefault(true); | |
60 | + searchView.setSubmitButtonEnabled(false); | |
61 | + | |
62 | + return true; | |
63 | + } | |
64 | + | |
65 | + @Override | |
66 | + public boolean onOptionsItemSelected(MenuItem item) | |
67 | + { | |
68 | + boolean result = true; | |
69 | + | |
70 | + switch (item.getItemId()) | |
71 | + { | |
72 | + case R.id.quit_menuitem: | |
73 | + finish(); | |
74 | + break; | |
75 | + default: | |
76 | + result = super.onOptionsItemSelected(item); | |
77 | + } | |
78 | + | |
79 | + return result; | |
80 | + } | |
81 | + | |
82 | + public boolean onClose() | |
83 | + { | |
84 | + Log.v(LOG_TAG, "Hello"); | |
85 | + updateListData(null); | |
86 | + Log.v(LOG_TAG, "Bye"); | |
87 | + | |
88 | + return false; | |
89 | + } | |
90 | + | |
91 | + @Override | |
92 | + protected void onCreate(Bundle savedInstanceState) | |
93 | + { | |
94 | + Log.v(LOG_TAG, "Hello"); | |
95 | + super.onCreate(savedInstanceState); | |
96 | + setContentView(R.layout.user_list_activity); | |
97 | + | |
98 | + initializeListData(); | |
99 | + | |
100 | + Intent queryIntent = getIntent(); | |
101 | + String queryAction = queryIntent.getAction(); | |
102 | + Log.v(LOG_TAG, "queryAction => " + queryAction); | |
103 | + | |
104 | + if (Intent.ACTION_SEARCH.equals(queryAction)) | |
105 | + { | |
106 | + Log.v(LOG_TAG, "action => ACTION_SEARCH"); | |
107 | + doSearchWithIntent(queryIntent); | |
108 | + } | |
109 | + else if (Intent.ACTION_VIEW.equals(queryAction)) | |
110 | + { | |
111 | + Log.v(LOG_TAG, "intent.action => ACTION_VIEW"); | |
112 | + Log.v(LOG_TAG, "intent.data => " + queryIntent.getData()); | |
113 | + Log.v(LOG_TAG, "intent.type => " + queryIntent.getType()); | |
114 | + Log.v(LOG_TAG, "intent.scheme => " + queryIntent.getScheme()); | |
115 | + | |
116 | + int flags = queryIntent.getFlags(); | |
117 | + Log.v(LOG_TAG, "flags => 0x" + Integer.toHexString(flags)); | |
118 | + | |
119 | + if ((queryIntent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) | |
120 | + { | |
121 | + Log.v(LOG_TAG, "flags includes FLAG_ACTIVITY_NEW_TASK"); | |
122 | + doSearchWithIntent(queryIntent); | |
123 | + } | |
124 | + } | |
125 | + | |
126 | + Log.v(LOG_TAG, "Bye"); | |
127 | + } | |
128 | + | |
129 | + @Override | |
130 | + protected void onNewIntent(Intent intent) | |
131 | + { | |
132 | + Log.v(LOG_TAG, "Hello"); | |
133 | + | |
134 | + Log.d(LOG_TAG, "intent => " + intent.getAction()); | |
135 | + doSearchWithIntent(intent); | |
136 | + | |
137 | + Log.v(LOG_TAG, "Bye"); | |
138 | + } | |
139 | + | |
140 | + @Override | |
141 | + protected void onDestroy() | |
142 | + { | |
143 | + Log.v(LOG_TAG, "Hello"); | |
144 | + | |
145 | + finalizeListData(); | |
146 | + | |
147 | + super.onDestroy(); | |
148 | + Log.v(LOG_TAG, "Bye"); | |
149 | + } | |
150 | + | |
151 | + private void doSearchWithIntent(Intent queryIntent) | |
152 | + { | |
153 | + Log.v(LOG_TAG, "Hello"); | |
154 | + | |
155 | + String queryString = queryIntent.getStringExtra(SearchManager.QUERY); | |
156 | + doSearchWithQuery(queryString); | |
157 | + | |
158 | + Log.v(LOG_TAG, "Bye"); | |
159 | + } | |
160 | + | |
161 | + private void doSearchWithQuery(String queryString) | |
162 | + { | |
163 | + Log.d(LOG_TAG, "queryString => " + queryString); | |
164 | + | |
165 | + // update | |
166 | + updateListData(queryString); | |
167 | + | |
168 | + // save query | |
169 | + SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this, | |
170 | + UserSearchRecentSuggestionProvider.AUTHORITY, | |
171 | + UserSearchRecentSuggestionProvider.DATABASE_MODE_QUERIES); | |
172 | + suggestions.saveRecentQuery(queryString, null); | |
173 | + } | |
174 | + | |
175 | + private void initializeListData() | |
176 | + { | |
177 | + Log.v(LOG_TAG, "Hello"); | |
178 | + if (dbHelper == null) | |
179 | + { | |
180 | + dbHelper = new UserListOpenHelper(this); | |
181 | + } | |
182 | + | |
183 | + db = dbHelper.getReadableDatabase(); | |
184 | + | |
185 | + cursor = dbHelper.selectByName(db, null); | |
186 | + | |
187 | + adapter = new SimpleCursorAdapter(this, | |
188 | + android.R.layout.simple_list_item_2, cursor, MAPPING_FROM, MAPPING_TO); | |
189 | + setListAdapter(adapter); | |
190 | + Log.v(LOG_TAG, "Bye"); | |
191 | + } | |
192 | + | |
193 | + private void finalizeListData() | |
194 | + { | |
195 | + Log.v(LOG_TAG, "Hello"); | |
196 | + if (cursor != null) | |
197 | + { | |
198 | + cursor.close(); | |
199 | + cursor = null; | |
200 | + } | |
201 | + if (db != null) | |
202 | + { | |
203 | + db.close(); | |
204 | + db = null; | |
205 | + } | |
206 | + Log.v(LOG_TAG, "Bye"); | |
207 | + } | |
208 | + | |
209 | + private void updateListData(String queryString) | |
210 | + { | |
211 | + Log.v(LOG_TAG, "Hello"); | |
212 | + if (db != null) | |
213 | + { | |
214 | + Cursor newCursor = dbHelper.selectByName(db, queryString); | |
215 | + adapter.changeCursor(newCursor); | |
216 | + if (cursor != null) | |
217 | + { | |
218 | + cursor.close(); | |
219 | + } | |
220 | + cursor = newCursor; | |
221 | + } | |
222 | + Log.v(LOG_TAG, "Bye"); | |
223 | + } | |
224 | +} |
@@ -0,0 +1,93 @@ | ||
1 | +package com.example.hello.android.searchview_recent_query_suggestion.db; | |
2 | + | |
3 | +import android.content.ContentValues; | |
4 | +import android.content.Context; | |
5 | +import android.database.Cursor; | |
6 | +import android.database.sqlite.SQLiteDatabase; | |
7 | +import android.database.sqlite.SQLiteOpenHelper; | |
8 | +import com.example.hello.android.searchview_recent_query_suggestion.R; | |
9 | +import com.example.hello.android.searchview_recent_query_suggestion.util.Log; | |
10 | +import java.util.Random; | |
11 | + | |
12 | +/** | |
13 | + * | |
14 | + * @author sawai | |
15 | + */ | |
16 | +public class UserListOpenHelper extends SQLiteOpenHelper | |
17 | +{ | |
18 | + | |
19 | + private static final String LOG_TAG = "XXX"; | |
20 | + static final String DB_NAME = "userlist.sqlite"; | |
21 | + static final int DB_VERSION = 1; | |
22 | + static final String TABLE_NAME = "Users"; | |
23 | + static final String CREATE_TABLE_SQL = "CREATE TABLE " + TABLE_NAME + "(" | |
24 | + + " _id INTEGER PRIMARY KEY AUTOINCREMENT" | |
25 | + + ", name TEXT" | |
26 | + + ", age INTEGER" | |
27 | + + ", email TEXT" | |
28 | + + ");"; | |
29 | + static final String DROP_TABLE_SQL = "DROP TABLE " + TABLE_NAME + ";"; | |
30 | + private Context context; | |
31 | + | |
32 | + public UserListOpenHelper(Context context) | |
33 | + { | |
34 | + super(context, DB_NAME, null, DB_VERSION); | |
35 | + this.context = context; | |
36 | + } | |
37 | + | |
38 | + @Override | |
39 | + public void onCreate(SQLiteDatabase db) | |
40 | + { | |
41 | + db.execSQL(CREATE_TABLE_SQL); | |
42 | + | |
43 | + insertUsers(db); | |
44 | + } | |
45 | + | |
46 | + @Override | |
47 | + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) | |
48 | + { | |
49 | + db.execSQL(DROP_TABLE_SQL); | |
50 | + onCreate(db); | |
51 | + } | |
52 | + | |
53 | + private void insertUsers(SQLiteDatabase db) | |
54 | + { | |
55 | + String[] names = context.getResources().getStringArray(R.array.names); | |
56 | + ContentValues values = new ContentValues(); | |
57 | + | |
58 | + Random rand = new Random(0); | |
59 | + for (String name : names) | |
60 | + { | |
61 | + int age = rand.nextInt(10) - 5 + 20; | |
62 | + values.clear(); | |
63 | + values.put("name", name); | |
64 | + values.put("age", age); | |
65 | + values.put("email", name + "@example.com"); | |
66 | + db.insert(TABLE_NAME, null, values); | |
67 | + } | |
68 | + } | |
69 | + | |
70 | + public Cursor selectByName(SQLiteDatabase db, String nameQuery) | |
71 | + { | |
72 | + Cursor cursor; | |
73 | + | |
74 | + String name = (nameQuery == null) ? "" : nameQuery.trim(); | |
75 | + if (name.length() > 0) | |
76 | + { | |
77 | + cursor = db.query(TABLE_NAME, null, | |
78 | + "name like ?", new String[] | |
79 | + { | |
80 | + "%" + name + "%" | |
81 | + }, | |
82 | + null, null, "name ASC"); | |
83 | + Log.d(LOG_TAG, "cursor => " + cursor); | |
84 | + } | |
85 | + else | |
86 | + { | |
87 | + cursor = db.query(TABLE_NAME, null, | |
88 | + null, null, null, null, "name ASC"); | |
89 | + Log.d(LOG_TAG, "cursor => " + cursor); | |
90 | + } | |
91 | + return cursor; | |
92 | + } | |
93 | +} |
@@ -0,0 +1,129 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright 2012 Masahiko, SAWAI <masahiko.sawai@gmail.com>. | |
5 | + * | |
6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | + * of this software and associated documentation files (the "Software"), to deal | |
8 | + * in the Software without restriction, including without limitation the rights | |
9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | + * copies of the Software, and to permit persons to whom the Software is | |
11 | + * furnished to do so, subject to the following conditions: | |
12 | + * | |
13 | + * The above copyright notice and this permission notice shall be included in | |
14 | + * all copies or substantial portions of the Software. | |
15 | + * | |
16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | + * THE SOFTWARE. | |
23 | + */ | |
24 | +package com.example.hello.android.searchview_recent_query_suggestion.db; | |
25 | + | |
26 | +import android.content.ContentValues; | |
27 | +import android.content.SearchRecentSuggestionsProvider; | |
28 | +import android.database.Cursor; | |
29 | +import android.net.Uri; | |
30 | +import com.example.hello.android.searchview_recent_query_suggestion.util.Log; | |
31 | +import java.util.Map.Entry; | |
32 | +import java.util.Set; | |
33 | + | |
34 | +/** | |
35 | + * 最近使った検索ワードをサジェスチョンする。 | |
36 | + * | |
37 | + * 最近使った検索ワードを保存しておいて、それを提案する検索サジェスチョン用のコンテントプロバイダ。 実際使う場合はコンストラクタの | |
38 | + * setupSuggestions(AUTHORITY,DATABASE_MODE_QUERIES); | |
39 | + * の呼び出しだけでよい。オーバーライドしている他のメソッドはすべて動作確認用のログ。 | |
40 | + * | |
41 | + * @author Masahiko, SAWAI <masahiko.sawai@gmail.com> | |
42 | + */ | |
43 | +public class UserSearchRecentSuggestionProvider extends SearchRecentSuggestionsProvider | |
44 | +{ | |
45 | + | |
46 | + private static final String LOG_TAG = "XXX"; | |
47 | + public static final String AUTHORITY = "com.example.hello.android.searchview_recent_query_suggestion_provider"; | |
48 | + | |
49 | + public UserSearchRecentSuggestionProvider() | |
50 | + { | |
51 | + Log.v(LOG_TAG, "Hello"); | |
52 | + setupSuggestions(AUTHORITY, DATABASE_MODE_QUERIES); | |
53 | + Log.v(LOG_TAG, "Bye"); | |
54 | + } | |
55 | + | |
56 | + @Override | |
57 | + public String getType(Uri uri) | |
58 | + { | |
59 | + Log.v(LOG_TAG, "Hello"); | |
60 | + | |
61 | + Log.v(LOG_TAG, "uri => " + uri); | |
62 | + String type = super.getType(uri); | |
63 | + Log.v(LOG_TAG, "type => " + type); | |
64 | + | |
65 | + Log.v(LOG_TAG, "Bye"); | |
66 | + return type; | |
67 | + } | |
68 | + | |
69 | + @Override | |
70 | + public Uri insert(Uri uri, ContentValues values) | |
71 | + { | |
72 | + Log.v(LOG_TAG, "Hello"); | |
73 | + | |
74 | + Log.v(LOG_TAG, "uri => " + uri); | |
75 | + Set<Entry<String, Object>> valueSet = values.valueSet(); | |
76 | + Log.v(LOG_TAG, "valueSet.size => " + valueSet.size()); | |
77 | + | |
78 | + for (Entry<String, Object> entry : valueSet) | |
79 | + { | |
80 | + Log.v(LOG_TAG, "valueSet key => " + entry.getKey()); | |
81 | + Log.v(LOG_TAG, "valueSet value => " + entry.getValue()); | |
82 | + } | |
83 | + | |
84 | + Uri result = super.insert(uri, values); | |
85 | + | |
86 | + Log.v(LOG_TAG, "Bye"); | |
87 | + return result; | |
88 | + } | |
89 | + | |
90 | + @Override | |
91 | + public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) | |
92 | + { | |
93 | + Log.v(LOG_TAG, "Hello"); | |
94 | + | |
95 | + Log.v(LOG_TAG, "uri => " + uri); | |
96 | + Log.v(LOG_TAG, "projection => " + projection); | |
97 | + if (projection != null) | |
98 | + { | |
99 | + for (String proj : projection) | |
100 | + { | |
101 | + Log.v(LOG_TAG, "proj => " + proj); | |
102 | + } | |
103 | + } | |
104 | + Log.v(LOG_TAG, "selection => " + selection); | |
105 | + Log.v(LOG_TAG, "selectionArgs => " + selectionArgs); | |
106 | + if (selectionArgs != null) | |
107 | + { | |
108 | + for (String arg : selectionArgs) | |
109 | + { | |
110 | + Log.v(LOG_TAG, "arg => " + arg); | |
111 | + } | |
112 | + } | |
113 | + Log.v(LOG_TAG, "sortOrder => " + sortOrder); | |
114 | + Cursor cursor = super.query(uri, projection, selection, selectionArgs, sortOrder); | |
115 | + | |
116 | + String[] columnNames = cursor.getColumnNames(); | |
117 | + Log.v(LOG_TAG, "columnNames => " + columnNames); | |
118 | + if (columnNames != null) | |
119 | + { | |
120 | + for (String columnName : columnNames) | |
121 | + { | |
122 | + Log.v(LOG_TAG, "columnName => " + columnName); | |
123 | + } | |
124 | + } | |
125 | + | |
126 | + Log.v(LOG_TAG, "Bye"); | |
127 | + return cursor; | |
128 | + } | |
129 | +} |
@@ -0,0 +1,185 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright 2011-2012 Masahiko, SAWAI <masahiko.sawai@gmail.com>. | |
5 | + * | |
6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | + * of this software and associated documentation files (the "Software"), to deal | |
8 | + * in the Software without restriction, including without limitation the rights | |
9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | + * copies of the Software, and to permit persons to whom the Software is | |
11 | + * furnished to do so, subject to the following conditions: | |
12 | + * | |
13 | + * The above copyright notice and this permission notice shall be included in | |
14 | + * all copies or substantial portions of the Software. | |
15 | + * | |
16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | + * THE SOFTWARE. | |
23 | + */ | |
24 | +package com.example.hello.android.searchview_recent_query_suggestion.util; | |
25 | + | |
26 | +/** | |
27 | + * | |
28 | + * @author sawai | |
29 | + */ | |
30 | +public class Log | |
31 | +{ | |
32 | + public static final int VERBOSE = android.util.Log.VERBOSE; | |
33 | + public static final int DEBUG = android.util.Log.DEBUG; | |
34 | + public static final int INFO = android.util.Log.INFO; | |
35 | + public static final int WARN = android.util.Log.WARN; | |
36 | + public static final int ERROR = android.util.Log.ERROR; | |
37 | + private static final int DEFAULT_OUTPUT_LEVEL = VERBOSE; | |
38 | + private static final boolean DEFAULT_TRACE_MODE = true; | |
39 | + static int outputLevel = DEFAULT_OUTPUT_LEVEL; | |
40 | + static boolean traceMode = DEFAULT_TRACE_MODE; | |
41 | + | |
42 | + public static int getOutputLevel() | |
43 | + { | |
44 | + return outputLevel; | |
45 | + } | |
46 | + | |
47 | + public static void setOutputLevel(int outputLevel) | |
48 | + { | |
49 | + Log.outputLevel = outputLevel; | |
50 | + } | |
51 | + | |
52 | + public static boolean isTraceMode() | |
53 | + { | |
54 | + return traceMode; | |
55 | + } | |
56 | + | |
57 | + public static void setTraceMode(boolean traceMode) | |
58 | + { | |
59 | + Log.traceMode = traceMode; | |
60 | + } | |
61 | + | |
62 | + public static void v(String tag, String message) | |
63 | + { | |
64 | + print(VERBOSE, tag, message, null); | |
65 | + } | |
66 | + | |
67 | + public static void v(String tag, String message, Throwable ex) | |
68 | + { | |
69 | + print(VERBOSE, tag, message, ex); | |
70 | + } | |
71 | + | |
72 | + public static void d(String tag, String message) | |
73 | + { | |
74 | + print(DEBUG, tag, message, null); | |
75 | + } | |
76 | + | |
77 | + public static void d(String tag, String message, Throwable ex) | |
78 | + { | |
79 | + print(DEBUG, tag, message, ex); | |
80 | + } | |
81 | + | |
82 | + public static void i(String tag, String message) | |
83 | + { | |
84 | + print(INFO, tag, message, null); | |
85 | + } | |
86 | + | |
87 | + public static void i(String tag, String message, Throwable ex) | |
88 | + { | |
89 | + print(INFO, tag, message, ex); | |
90 | + } | |
91 | + | |
92 | + public static void w(String tag, String message) | |
93 | + { | |
94 | + print(WARN, tag, message, null); | |
95 | + } | |
96 | + | |
97 | + public static void w(String tag, String message, Throwable ex) | |
98 | + { | |
99 | + print(WARN, tag, message, ex); | |
100 | + } | |
101 | + | |
102 | + public static void e(String tag, String message) | |
103 | + { | |
104 | + print(ERROR, tag, message, null); | |
105 | + } | |
106 | + | |
107 | + public static void e(String tag, String message, Throwable ex) | |
108 | + { | |
109 | + print(ERROR, tag, message, ex); | |
110 | + } | |
111 | + | |
112 | + public static void log(int priority, String tag, String message) | |
113 | + { | |
114 | + print(priority, tag, message, null); | |
115 | + } | |
116 | + | |
117 | + public static void log(int priority, String tag, String message, Throwable ex) | |
118 | + { | |
119 | + print(priority, tag, message, ex); | |
120 | + } | |
121 | + | |
122 | + private static void print(int priority, String tag, String message, Throwable ex) | |
123 | + { | |
124 | + if (priority < outputLevel) | |
125 | + { | |
126 | + return; | |
127 | + } | |
128 | + | |
129 | + String log = message; | |
130 | + if (traceMode) | |
131 | + { | |
132 | + StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); | |
133 | + if (stackTraceElements.length >= 5) | |
134 | + { | |
135 | + StackTraceElement e = stackTraceElements[4]; | |
136 | + log = "[" + e.getClassName() + "#" + e.getMethodName() + "():" + e.getLineNumber() + "]" | |
137 | + + " : " + message; | |
138 | + } | |
139 | + } | |
140 | + | |
141 | + | |
142 | + if (ex == null) | |
143 | + { | |
144 | + switch (priority) | |
145 | + { | |
146 | + case VERBOSE: | |
147 | + android.util.Log.v(tag, log); | |
148 | + break; | |
149 | + case DEBUG: | |
150 | + android.util.Log.d(tag, log); | |
151 | + break; | |
152 | + case INFO: | |
153 | + android.util.Log.i(tag, log); | |
154 | + break; | |
155 | + case WARN: | |
156 | + android.util.Log.w(tag, log); | |
157 | + break; | |
158 | + case ERROR: | |
159 | + android.util.Log.e(tag, log); | |
160 | + break; | |
161 | + } | |
162 | + } | |
163 | + else | |
164 | + { | |
165 | + switch (priority) | |
166 | + { | |
167 | + case VERBOSE: | |
168 | + android.util.Log.v(tag, log, ex); | |
169 | + break; | |
170 | + case DEBUG: | |
171 | + android.util.Log.d(tag, log, ex); | |
172 | + break; | |
173 | + case INFO: | |
174 | + android.util.Log.i(tag, log, ex); | |
175 | + break; | |
176 | + case WARN: | |
177 | + android.util.Log.w(tag, log, ex); | |
178 | + break; | |
179 | + case ERROR: | |
180 | + android.util.Log.e(tag, log, ex); | |
181 | + break; | |
182 | + } | |
183 | + } | |
184 | + } | |
185 | +} |