Replace deprecated AsyncTask in MainActivity

Julien LepillerSun Jun 19 22:42:39+0200 2022

5428e72

Replace deprecated AsyncTask in MainActivity

app/src/main/java/eu/lepiller/nani/MainActivity.java

33
import android.annotation.SuppressLint;
44
import android.content.Intent;
55
import android.content.SharedPreferences;
6-
import android.os.AsyncTask;
76
import android.os.Bundle;
87
import com.google.android.material.snackbar.Snackbar;
98
10-
import androidx.annotation.NonNull;
119
import androidx.appcompat.app.AppCompatActivity;
1210
import androidx.appcompat.widget.Toolbar;
1311
import androidx.preference.PreferenceManager;

4038
4139
import static java.lang.Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS;
4240
43-
public class MainActivity extends AppCompatActivity implements OnTaskCompleted<SearchResult>, SharedPreferences.OnSharedPreferenceChangeListener {
41+
public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
4442
    private LinearLayout result_layout;
4543
    private TextView feedback_text;
4644
    private SearchView search_form;

5048
    ViewPager2 viewPager2;
5149
    ResultPagerAdapter pagerAdapter;
5250
51+
    private SearchThread searchThread;
52+
5353
    static final String TAG = "MAIN";
5454
5555
    // At this point we really need to redraw pagerAdapter, so suppress the warning.

152152
        return sharedPreferences.getString(SettingsActivity.KEY_PREF_READING_STYLE, "furigana");
153153
    }
154154
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;
159160
        }
160161
161162
        @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() {
164172
            ArrayList<String> tried = new ArrayList<>();
165173
            tried.add(text);
166174

226234
227235
            return new SearchResult(searchResult, kanjiResults, text, true);
228236
        }
237+
    }
229238
230-
        @Override
231-
        protected void onPostExecute(SearchResult r) {
232-
            callback.onTaskCompleted(r);
233-
        }
239+
    private synchronized void stopSearchThread() {
240+
        this.searchThread = null;
234241
    }
235242
236-
    @Override
237-
    public void onTaskCompleted(SearchResult r) {
243+
    public void showResults(SearchResult r) {
244+
        stopSearchThread();
238245
        search_form.setEnabled(true);
246+
239247
        if(r.isException()) {
240248
            DictionaryException e = r.getException();
241249
            if (e instanceof NoDictionaryException) {

329337
                feedback_text.setText(R.string.feedback_progress);
330338
331339
                DictionaryFactory.prepare(getApplicationContext());
332-
                new SearchTask(MainActivity.this).execute(text);
340+
                MainActivity.this.search(text);
333341
                return true;
334342
            }
335343

342350
        return super.onCreateOptionsMenu(menu);
343351
    }
344352
353+
    private synchronized void search(String text) {
354+
        if(searchThread == null) {
355+
            searchThread = new SearchThread(text);
356+
            searchThread.start();
357+
        }
358+
    }
359+
345360
    @Override
346361
    public boolean onPrepareOptionsMenu(Menu menu) {
347362
        if(intentSearch != null) {