|
1 | 1 | /*
|
2 |
| - * Copyright (C) 2017 Peter Gregus for GravityBox Project (C3C076@xda) |
| 2 | + * Copyright (C) 2018 Peter Gregus for GravityBox Project (C3C076@xda) |
3 | 3 | * Licensed under the Apache License, Version 2.0 (the "License");
|
4 | 4 | * you may not use this file except in compliance with the License.
|
5 | 5 | * You may obtain a copy of the License at
|
|
29 | 29 | import android.content.pm.PackageManager;
|
30 | 30 | import android.os.AsyncTask;
|
31 | 31 | import android.os.Bundle;
|
32 |
| -import android.text.Editable; |
33 |
| -import android.text.TextWatcher; |
34 | 32 | import android.view.Menu;
|
35 | 33 | import android.view.MenuItem;
|
36 | 34 | import android.view.View;
|
37 | 35 | import android.widget.AdapterView;
|
38 |
| -import android.widget.EditText; |
39 | 36 | import android.widget.AdapterView.OnItemClickListener;
|
| 37 | +import android.widget.SearchView.OnQueryTextListener; |
40 | 38 | import android.widget.ListView;
|
| 39 | +import android.widget.SearchView; |
41 | 40 |
|
42 | 41 | public class LedControlActivity extends GravityBoxListActivity implements ListItemActionHandler, OnItemClickListener {
|
43 | 42 |
|
44 | 43 | private static final int REQ_SETTINGS = 1;
|
| 44 | + private static final String KEY_ACTIVE_ONLY = "showActiveOnly"; |
| 45 | + private static final String KEY_SEARCH_QUERY = "searchQuery"; |
45 | 46 |
|
46 | 47 | private ListView mList;
|
47 | 48 | private AsyncTask<Void, Void, ArrayList<LedListItem>> mAsyncTask;
|
48 | 49 | private ProgressDialog mProgressDialog;
|
49 | 50 | private LedListItem mCurrentItem;
|
50 |
| - private EditText mSearchEditText; |
51 | 51 | private boolean mShowActiveOnly;
|
| 52 | + private String mSearchQuery; |
| 53 | + private SearchView mSearchView; |
52 | 54 |
|
53 | 55 | @Override
|
54 | 56 | public void onCreate(Bundle savedInstanceState) {
|
55 | 57 | super.onCreate(savedInstanceState);
|
56 | 58 |
|
57 | 59 | if (savedInstanceState != null) {
|
58 |
| - mShowActiveOnly = savedInstanceState.getBoolean("showActiveOnly", false); |
| 60 | + mShowActiveOnly = savedInstanceState.getBoolean(KEY_ACTIVE_ONLY, false); |
| 61 | + mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY, null); |
59 | 62 | }
|
60 | 63 |
|
61 | 64 | setContentView(R.layout.led_control_activity);
|
62 | 65 |
|
63 | 66 | mList = getListView();
|
64 | 67 | mList.setOnItemClickListener(this);
|
65 | 68 |
|
66 |
| - mSearchEditText = (EditText) findViewById(R.id.input_search); |
67 |
| - mSearchEditText.addTextChangedListener(new TextWatcher() { |
68 |
| - @Override |
69 |
| - public void beforeTextChanged(CharSequence s, int start, int count, int after) { } |
70 |
| - |
71 |
| - @Override |
72 |
| - public void onTextChanged(CharSequence s, int start, int before, int count) { |
73 |
| - if (mList.getAdapter() != null) { |
74 |
| - ((LedListAdapter)mList.getAdapter()).getFilter().filter(s); |
75 |
| - } |
76 |
| - } |
77 |
| - |
78 |
| - @Override |
79 |
| - public void afterTextChanged(Editable s) { } |
80 |
| - }); |
81 |
| - |
82 | 69 | setData();
|
83 | 70 | }
|
84 | 71 |
|
85 | 72 | @Override
|
86 | 73 | public void onStop() {
|
87 | 74 | cancelSetData();
|
| 75 | + mSearchView.clearFocus(); |
88 | 76 | super.onStop();
|
89 | 77 | }
|
90 | 78 |
|
91 | 79 | @Override
|
92 | 80 | public boolean onCreateOptionsMenu(Menu menu) {
|
93 | 81 | getMenuInflater().inflate(R.menu.led_control_activity_menu, menu);
|
| 82 | + |
| 83 | + final MenuItem search = menu.findItem(R.id.search); |
| 84 | + mSearchView = (SearchView) search.getActionView(); |
| 85 | + |
| 86 | + if (mSearchQuery != null) { |
| 87 | + mSearchView.setQuery(mSearchQuery, false); |
| 88 | + } |
| 89 | + |
| 90 | + mSearchView.setOnQueryTextListener(new OnQueryTextListener() { |
| 91 | + @Override |
| 92 | + public boolean onQueryTextChange(String text) { |
| 93 | + mSearchQuery = text; |
| 94 | + if (mList.getAdapter() != null) { |
| 95 | + ((LedListAdapter)mList.getAdapter()).getFilter().filter(mSearchQuery); |
| 96 | + } |
| 97 | + return true; |
| 98 | + } |
| 99 | + @Override |
| 100 | + public boolean onQueryTextSubmit(String text) { |
| 101 | + mSearchView.clearFocus(); |
| 102 | + return true; |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + int closeBtnResId = getResources().getIdentifier( |
| 107 | + "android:id/search_close_btn", null, null); |
| 108 | + if (closeBtnResId != 0) { |
| 109 | + View closeBtn = mSearchView.findViewById(closeBtnResId); |
| 110 | + if (closeBtn != null) { |
| 111 | + closeBtn.setOnClickListener(new View.OnClickListener() { |
| 112 | + @Override |
| 113 | + public void onClick(View v) { |
| 114 | + search.collapseActionView(); |
| 115 | + } |
| 116 | + }); |
| 117 | + } |
| 118 | + } |
| 119 | + |
94 | 120 | return true;
|
95 | 121 | }
|
96 | 122 |
|
@@ -139,11 +165,12 @@ protected ArrayList<LedListItem> doInBackground(Void... arg0) {
|
139 | 165 | @Override
|
140 | 166 | protected void onPostExecute(ArrayList<LedListItem> result) {
|
141 | 167 | dismissProgressDialog();
|
142 |
| - mSearchEditText.setText(""); |
143 |
| - mList.setAdapter(new LedListAdapter(LedControlActivity.this, result, |
144 |
| - LedControlActivity.this)); |
145 |
| - ((LedListAdapter)mList.getAdapter()).notifyDataSetChanged(); |
146 |
| - mSearchEditText.setVisibility(mShowActiveOnly ? View.GONE : View.VISIBLE); |
| 168 | + LedListAdapter adapter = new LedListAdapter(LedControlActivity.this, result, |
| 169 | + LedControlActivity.this); |
| 170 | + if (mSearchQuery != null) { |
| 171 | + adapter.getFilter().filter(mSearchQuery); |
| 172 | + } |
| 173 | + mList.setAdapter(adapter); |
147 | 174 | }
|
148 | 175 | }.execute();
|
149 | 176 | }
|
@@ -213,6 +240,10 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
213 | 240 |
|
214 | 241 | @Override
|
215 | 242 | public void onSaveInstanceState(Bundle bundle) {
|
216 |
| - bundle.putBoolean("showActiveOnly", mShowActiveOnly); |
| 243 | + bundle.putBoolean(KEY_ACTIVE_ONLY, mShowActiveOnly); |
| 244 | + if (mSearchQuery != null) { |
| 245 | + bundle.putString(KEY_SEARCH_QUERY, mSearchQuery); |
| 246 | + } |
| 247 | + super.onSaveInstanceState(bundle); |
217 | 248 | }
|
218 | 249 | }
|
0 commit comments