1 /*
2  * Copyright (C) 2018 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 package com.android.dumpviewer.pickers;
17 
18 import android.app.Activity;
19 import android.content.Intent;
20 import android.os.AsyncTask;
21 import android.os.Bundle;
22 import android.text.Editable;
23 import android.text.TextUtils;
24 import android.text.TextWatcher;
25 import android.view.View;
26 import android.widget.AdapterView;
27 import android.widget.ArrayAdapter;
28 import android.widget.AutoCompleteTextView;
29 import android.widget.ListView;
30 
31 import com.android.dumpviewer.R;
32 
33 import java.util.ArrayList;
34 
35 import androidx.annotation.Nullable;
36 import androidx.appcompat.app.AppCompatActivity;
37 
38 public abstract class PickerActivity extends AppCompatActivity {
getList()39     protected abstract String[] getList();
40 
41     private static final String KEY_SELECTION = "KEY_SELECTION";
42 
43     private AutoCompleteTextView mSearch; // TODO Save history? Would that be really useful?
44 
45     private ArrayAdapter<String> mAdapter;
46     private ListView mList;
47 
48     private String[] mAllItems;
49     private String mFilter = "";
50 
51     @Override
onCreate(@ullable Bundle savedInstanceState)52     protected void onCreate(@Nullable Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54 
55         setContentView(R.layout.picker);
56 
57         mSearch = findViewById(R.id.search);
58 
59         mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line);
60 
61         mList = findViewById(R.id.list);
62         mList.setAdapter(mAdapter);
63 
64         mSearch.addTextChangedListener(new TextWatcher() {
65             @Override
66             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
67             }
68 
69             @Override
70             public void onTextChanged(CharSequence s, int start, int before, int count) {
71             }
72 
73             @Override
74             public void afterTextChanged(Editable s) {
75                 mFilter = s.toString();
76                 refreshList();
77             }
78         });
79         mList.setOnItemClickListener(this::onListItemClicked);
80 
81         mLoader.execute();
82     }
83 
84     @Override
onDestroy()85     protected void onDestroy() {
86         mLoader.cancel(false);
87         super.onDestroy();
88     }
89 
getSelectedString(Intent i)90     public static String getSelectedString(Intent i) {
91         return i.getStringExtra(KEY_SELECTION);
92     }
93 
getSelectedItem(int position)94     private String getSelectedItem(int position) {
95         final String val = mAdapter.getItem(position).toString();
96         final int sepIndex = val.indexOf(' ');
97         if (sepIndex >= 0) {
98             return val.substring(0, sepIndex);
99         }
100         return val;
101     }
102 
onListItemClicked(AdapterView<?> parent, View view, int position, long id)103     private void onListItemClicked(AdapterView<?> parent, View view, int position, long id) {
104         final Intent result = new Intent().putExtra(KEY_SELECTION, getSelectedItem(position));
105         setResult(Activity.RESULT_OK, result);
106         finish();
107     }
108 
109     private final AsyncTask<Void, Void, String[]> mLoader = new AsyncTask<Void, Void, String[]>() {
110         @Override
111         protected String[] doInBackground(Void... voids) {
112             return getList();
113         }
114 
115         @Override
116         protected void onPostExecute(String[] list) {
117             if (list != null) {
118                 mAllItems = list;
119                 refreshList();
120             }
121         }
122     };
123 
refreshList()124     private void refreshList() {
125         if (isDestroyed() || mAllItems == null) {
126             return;
127         }
128         final ArrayList<String> list = new ArrayList<>(mAllItems.length);
129 
130         final String filter = mFilter.toLowerCase();
131         for (String s : mAllItems) {
132             if (filter.length() == 0 || s.toLowerCase().contains(filter)) {
133                 list.add(s);
134             }
135         }
136         mAdapter.clear();
137         mAdapter.addAll(list);
138     }
139 }
140