Replace deprecated AsyncTask
app/src/main/java/eu/lepiller/nani/DictionaryActivity.java
| 2 | 2 | ||
| 3 | 3 | import android.app.Activity; | |
| 4 | 4 | import android.content.Intent; | |
| 5 | - | import android.os.AsyncTask; | |
| 6 | 5 | import android.os.Bundle; | |
| 7 | 6 | ||
| 8 | 7 | import androidx.activity.result.contract.ActivityResultContracts; | |
… | |||
| 43 | 42 | List<String> langs; | |
| 44 | 43 | String selectedLanguage = ""; | |
| 45 | 44 | private SwipeRefreshLayout refresher; | |
| 45 | + | private DownloadThread downloadThread; | |
| 46 | 46 | ||
| 47 | 47 | @Override | |
| 48 | 48 | protected void onCreate(Bundle savedInstanceState) { | |
… | |||
| 119 | 119 | } | |
| 120 | 120 | ||
| 121 | 121 | private void refresh() { | |
| 122 | - | new DownloadTask().execute(); | |
| 122 | + | if(downloadThread == null) | |
| 123 | + | downloadThread = new DownloadThread(); | |
| 124 | + | downloadThread.start(); | |
| 123 | 125 | } | |
| 124 | 126 | ||
| 125 | 127 | private void setObserver() { | |
| 126 | - | LiveData<Boolean> data = DownloadTask.getRefreshingData(); | |
| 128 | + | if(downloadThread == null) | |
| 129 | + | return; | |
| 130 | + | ||
| 131 | + | LiveData<Boolean> data = downloadThread.getRefreshingData(); | |
| 127 | 132 | data.observe(this, refreshing -> { | |
| 128 | 133 | refresher.setRefreshing(refreshing); | |
| 129 | 134 | if(!refreshing) | |
… | |||
| 132 | 137 | } | |
| 133 | 138 | ||
| 134 | 139 | private void unsetObserver() { | |
| 135 | - | LiveData<Boolean> data = DownloadTask.getRefreshingData(); | |
| 140 | + | if(downloadThread == null) | |
| 141 | + | return; | |
| 142 | + | LiveData<Boolean> data = downloadThread.getRefreshingData(); | |
| 136 | 143 | data.removeObservers(this); | |
| 137 | 144 | } | |
| 138 | 145 | ||
| 139 | - | private static class DownloadTask extends AsyncTask<String, Integer, Integer> { | |
| 140 | - | final private static MutableLiveData<Boolean> refreshing = new MutableLiveData<>(); | |
| 146 | + | private class DownloadThread extends Thread { | |
| 147 | + | final private MutableLiveData<Boolean> refreshing = new MutableLiveData<>(); | |
| 141 | 148 | ||
| 142 | - | static LiveData<Boolean> getRefreshingData() { | |
| 149 | + | LiveData<Boolean> getRefreshingData() { | |
| 143 | 150 | return refreshing; | |
| 144 | 151 | } | |
| 145 | 152 | ||
| 146 | 153 | @Override | |
| 147 | - | protected Integer doInBackground(String... strings) { | |
| 154 | + | public void run() { | |
| 148 | 155 | refreshing.postValue(true); | |
| 156 | + | update(); | |
| 157 | + | refreshing.setValue(false); | |
| 158 | + | } | |
| 159 | + | ||
| 160 | + | private void update() { | |
| 149 | 161 | try { | |
| 150 | 162 | URL url = DictionaryFactory.getListUrl(); | |
| 151 | 163 | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
… | |||
| 161 | 173 | byte[] data = new byte[4096]; | |
| 162 | 174 | FileOutputStream output = new FileOutputStream(file); | |
| 163 | 175 | while((count = input.read(data)) != -1) { | |
| 164 | - | if (isCancelled()) { | |
| 165 | - | input.close(); | |
| 166 | - | return null; | |
| 167 | - | } | |
| 168 | 176 | output.write(data, 0, count); | |
| 169 | 177 | } | |
| 170 | 178 | } else { | |
| 171 | - | return R.string.error_dico_not_found; | |
| 179 | + | postResult(R.string.error_dico_not_found); | |
| 172 | 180 | } | |
| 173 | 181 | } catch (MalformedURLException e) { | |
| 174 | 182 | e.printStackTrace(); | |
| 175 | - | return R.string.error_dico_url; | |
| 183 | + | postResult(R.string.error_dico_url); | |
| 176 | 184 | } catch (IOException e) { | |
| 177 | 185 | e.printStackTrace(); | |
| 178 | - | return R.string.error_dico_io; | |
| 186 | + | postResult(R.string.error_dico_io); | |
| 179 | 187 | } | |
| 180 | - | return null; | |
| 181 | 188 | } | |
| 182 | 189 | ||
| 183 | - | @Override | |
| 184 | - | protected void onPostExecute(Integer i) { | |
| 185 | - | super.onPostExecute(i); | |
| 186 | - | refreshing.setValue(false); | |
| 190 | + | protected void postResult(Integer i) { | |
| 191 | + | runOnUiThread(() -> postMessage(i)); | |
| 187 | 192 | } | |
| 188 | 193 | } | |
| 189 | 194 | ||
| 195 | + | private void postMessage(Integer i) { | |
| 196 | + | Snackbar.make(findViewById(R.id.dictionary_view), getString(i), Snackbar.LENGTH_LONG).show(); | |
| 197 | + | refresh(); | |
| 198 | + | } | |
| 199 | + | ||
| 190 | 200 | private void updateDataSet() { | |
| 191 | 201 | DictionaryFactory.updatePackageList(); | |
| 192 | 202 | dictionaries.clear(); | |