Replace deprecated AsyncTask in MainActivity
app/src/main/java/eu/lepiller/nani/MainActivity.java
3 | 3 | import android.annotation.SuppressLint; | |
4 | 4 | import android.content.Intent; | |
5 | 5 | import android.content.SharedPreferences; | |
6 | - | import android.os.AsyncTask; | |
7 | 6 | import android.os.Bundle; | |
8 | 7 | import com.google.android.material.snackbar.Snackbar; | |
9 | 8 | ||
10 | - | import androidx.annotation.NonNull; | |
11 | 9 | import androidx.appcompat.app.AppCompatActivity; | |
12 | 10 | import androidx.appcompat.widget.Toolbar; | |
13 | 11 | import androidx.preference.PreferenceManager; | |
… | |||
40 | 38 | ||
41 | 39 | import static java.lang.Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS; | |
42 | 40 | ||
43 | - | public class MainActivity extends AppCompatActivity implements OnTaskCompleted<SearchResult>, SharedPreferences.OnSharedPreferenceChangeListener { | |
41 | + | public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener { | |
44 | 42 | private LinearLayout result_layout; | |
45 | 43 | private TextView feedback_text; | |
46 | 44 | private SearchView search_form; | |
… | |||
50 | 48 | ViewPager2 viewPager2; | |
51 | 49 | ResultPagerAdapter pagerAdapter; | |
52 | 50 | ||
51 | + | private SearchThread searchThread; | |
52 | + | ||
53 | 53 | static final String TAG = "MAIN"; | |
54 | 54 | ||
55 | 55 | // At this point we really need to redraw pagerAdapter, so suppress the warning. | |
… | |||
152 | 152 | return sharedPreferences.getString(SettingsActivity.KEY_PREF_READING_STYLE, "furigana"); | |
153 | 153 | } | |
154 | 154 | ||
155 | - | private static class SearchTask extends AsyncTask<String, Integer, SearchResult> { | |
156 | - | OnTaskCompleted<SearchResult> callback; | |
157 | - | SearchTask(OnTaskCompleted<SearchResult> callback) { | |
158 | - | this.callback = callback; | |
155 | + | private class SearchThread extends Thread { | |
156 | + | String text; | |
157 | + | ||
158 | + | SearchThread(String text) { | |
159 | + | this.text = text; | |
159 | 160 | } | |
160 | 161 | ||
161 | 162 | @Override | |
162 | - | protected SearchResult doInBackground(String... sInput) { | |
163 | - | String text = sInput[0]; | |
163 | + | public void run() { | |
164 | + | SearchResult res = findResults(); | |
165 | + | runOnUiThread(() -> { | |
166 | + | stopSearchThread(); | |
167 | + | showResults(res); | |
168 | + | }); | |
169 | + | } | |
170 | + | ||
171 | + | private SearchResult findResults() { | |
164 | 172 | ArrayList<String> tried = new ArrayList<>(); | |
165 | 173 | tried.add(text); | |
166 | 174 | ||
… | |||
226 | 234 | ||
227 | 235 | return new SearchResult(searchResult, kanjiResults, text, true); | |
228 | 236 | } | |
237 | + | } | |
229 | 238 | ||
230 | - | @Override | |
231 | - | protected void onPostExecute(SearchResult r) { | |
232 | - | callback.onTaskCompleted(r); | |
233 | - | } | |
239 | + | private synchronized void stopSearchThread() { | |
240 | + | this.searchThread = null; | |
234 | 241 | } | |
235 | 242 | ||
236 | - | @Override | |
237 | - | public void onTaskCompleted(SearchResult r) { | |
243 | + | public void showResults(SearchResult r) { | |
244 | + | stopSearchThread(); | |
238 | 245 | search_form.setEnabled(true); | |
246 | + | ||
239 | 247 | if(r.isException()) { | |
240 | 248 | DictionaryException e = r.getException(); | |
241 | 249 | if (e instanceof NoDictionaryException) { | |
… | |||
329 | 337 | feedback_text.setText(R.string.feedback_progress); | |
330 | 338 | ||
331 | 339 | DictionaryFactory.prepare(getApplicationContext()); | |
332 | - | new SearchTask(MainActivity.this).execute(text); | |
340 | + | MainActivity.this.search(text); | |
333 | 341 | return true; | |
334 | 342 | } | |
335 | 343 | ||
… | |||
342 | 350 | return super.onCreateOptionsMenu(menu); | |
343 | 351 | } | |
344 | 352 | ||
353 | + | private synchronized void search(String text) { | |
354 | + | if(searchThread == null) { | |
355 | + | searchThread = new SearchThread(text); | |
356 | + | searchThread.start(); | |
357 | + | } | |
358 | + | } | |
359 | + | ||
345 | 360 | @Override | |
346 | 361 | public boolean onPrepareOptionsMenu(Menu menu) { | |
347 | 362 | if(intentSearch != null) { |