Add notification on dictionary download
CHANGELOG.md
| 1 | 1 | Changelog | |
| 2 | 2 | ========= | |
| 3 | 3 | ||
| 4 | + | Changes Since 0.2 | |
| 5 | + | ----------------- | |
| 6 | + | ||
| 7 | + | ### Bug Fixes | |
| 8 | + | ||
| 9 | + | * Show dictionary sizes in appropriate unit, to prevent showing 0MB on | |
| 10 | + | small files | |
| 11 | + | * Show a notification when downloading, which helps keeping the app alive | |
| 12 | + | while downloading. | |
| 13 | + | ||
| 14 | + | ### Translations | |
| 15 | + | ||
| 16 | + | * Updat efr translation | |
| 17 | + | ||
| 4 | 18 | Changes Since 0.1 | |
| 5 | 19 | ----------------- | |
| 6 | 20 |
app/src/main/java/eu/lepiller/nani/DictionaryDownloadActivity.java
| 1 | 1 | package eu.lepiller.nani; | |
| 2 | 2 | ||
| 3 | + | import android.app.NotificationManager; | |
| 3 | 4 | import android.content.Context; | |
| 4 | 5 | import android.graphics.drawable.Drawable; | |
| 5 | 6 | import android.os.AsyncTask; | |
| 6 | 7 | import android.os.Build; | |
| 8 | + | ||
| 9 | + | import androidx.core.app.NotificationCompat; | |
| 7 | 10 | import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat; | |
| 8 | 11 | import androidx.appcompat.app.AppCompatActivity; | |
| 9 | 12 | import android.os.Bundle; | |
… | |||
| 28 | 31 | ||
| 29 | 32 | public class DictionaryDownloadActivity extends AppCompatActivity { | |
| 30 | 33 | final static String TAG = "DWN"; | |
| 34 | + | final static int notificationID = 1111; | |
| 31 | 35 | ||
| 32 | 36 | ProgressBar download_bar; | |
| 33 | 37 | ||
| 38 | + | NotificationManager manager; | |
| 39 | + | NotificationCompat.Builder builder; | |
| 40 | + | ||
| 34 | 41 | @Override | |
| 35 | 42 | protected void onCreate(Bundle savedInstanceState) { | |
| 36 | 43 | super.onCreate(savedInstanceState); | |
… | |||
| 39 | 46 | int position = getIntent().getExtras().getInt("dico"); | |
| 40 | 47 | ||
| 41 | 48 | final Dictionary d = DictionaryFactory.get(position); | |
| 49 | + | ||
| 50 | + | manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); | |
| 51 | + | builder = new NotificationCompat.Builder(DictionaryDownloadActivity.this, "dico_dll") | |
| 52 | + | .setSmallIcon(R.drawable.ic_launcher_foreground) | |
| 53 | + | .setContentTitle(d.getName()) | |
| 54 | + | .setContentText(getString(R.string.downloading)); | |
| 55 | + | ||
| 42 | 56 | setResult(DictionaryActivity.DICO_REQUEST); | |
| 43 | 57 | updateLayout(d); | |
| 44 | 58 | } | |
… | |||
| 97 | 111 | @Override | |
| 98 | 112 | public void onClick(View v) { | |
| 99 | 113 | download_button.setEnabled(false); | |
| 100 | - | final DownloadTask downloadTask = new DownloadTask(DictionaryDownloadActivity.this); | |
| 114 | + | final DownloadTask downloadTask = new DownloadTask(); | |
| 115 | + | builder.setProgress(0,0,true); | |
| 116 | + | builder.setOnlyAlertOnce(true); | |
| 117 | + | manager.notify(notificationID, builder.build()); | |
| 101 | 118 | downloadTask.execute(d); | |
| 102 | 119 | } | |
| 103 | 120 | }); | |
… | |||
| 111 | 128 | }); | |
| 112 | 129 | } | |
| 113 | 130 | ||
| 131 | + | private void notifyProgress(int progress, int max) { | |
| 132 | + | builder.setProgress(max, progress, false); | |
| 133 | + | manager.notify(notificationID, builder.build()); | |
| 134 | + | } | |
| 135 | + | ||
| 136 | + | private void removeProgress() { | |
| 137 | + | manager.cancel(notificationID); | |
| 138 | + | } | |
| 139 | + | ||
| 114 | 140 | private class DownloadTask extends AsyncTask<Dictionary, Integer, String> { | |
| 115 | 141 | private Dictionary d; | |
| 116 | - | private Context context; | |
| 117 | - | ||
| 118 | - | public DownloadTask(Context context) { | |
| 119 | - | this.context = context; | |
| 120 | - | } | |
| 121 | 142 | ||
| 122 | 143 | @Override | |
| 123 | 144 | protected String doInBackground(Dictionary... sDicos) { | |
… | |||
| 201 | 222 | download_bar.setIndeterminate(false); | |
| 202 | 223 | download_bar.setMax(100); | |
| 203 | 224 | download_bar.setProgress(progress[0]); | |
| 225 | + | notifyProgress(progress[0], 100); | |
| 204 | 226 | } | |
| 205 | 227 | ||
| 206 | 228 | ||
| 207 | 229 | @Override | |
| 208 | 230 | protected void onPostExecute(String result) { | |
| 209 | 231 | download_bar.setProgress(100); | |
| 232 | + | removeProgress(); | |
| 210 | 233 | updateLayout(d); | |
| 211 | 234 | } | |
| 212 | 235 | } | |
app/src/main/java/eu/lepiller/nani/MainActivity.java
| 1 | 1 | package eu.lepiller.nani; | |
| 2 | 2 | ||
| 3 | + | import android.app.NotificationChannel; | |
| 4 | + | import android.app.NotificationManager; | |
| 3 | 5 | import android.content.Intent; | |
| 4 | 6 | import android.os.AsyncTask; | |
| 5 | 7 | import android.os.Bundle; | |
… | |||
| 50 | 52 | ||
| 51 | 53 | Button radical_button = findViewById(R.id.radical_button); | |
| 52 | 54 | ||
| 55 | + | if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { | |
| 56 | + | NotificationChannel channel = new NotificationChannel("dico_dll", | |
| 57 | + | "Nani's dictionary download notification", NotificationManager.IMPORTANCE_DEFAULT); | |
| 58 | + | NotificationManager manager = | |
| 59 | + | (NotificationManager) getSystemService(NOTIFICATION_SERVICE); | |
| 60 | + | manager.createNotificationChannel(channel); | |
| 61 | + | } | |
| 62 | + | ||
| 53 | 63 | try { | |
| 54 | 64 | radical_selector.setDictionary(DictionaryFactory.getRadicalDictionary(getApplicationContext())); | |
| 55 | 65 | } catch (NoDictionaryException e) { | |
app/src/main/res/values-fr/strings.xml
| 25 | 25 | <string name="dictionary_size_mb">Taille r??elle : %d???Mo</string> | |
| 26 | 26 | <string name="dictionary_size_kb">Taille r??elle : %d???Ko</string> | |
| 27 | 27 | <string name="dictionary_size_b">Taille r??elle : %d???o</string> | |
| 28 | + | <string name="downloading">T??l??chargement d\'un dictionnaire???</string> | |
| 28 | 29 | <string name="feedback_no_result">Pas de r??sultat</string> | |
| 29 | 30 | <string name="feedback_progress">Recherche???</string> | |
| 30 | 31 | <string name="feedback_didyoumean">Vouliez-vous dire ?? %s ?? ?</string> |
app/src/main/res/values/strings.xml
| 21 | 21 | <string name="dictionary_size_mb">Actual size: %dMB</string> | |
| 22 | 22 | <string name="dictionary_size_kb">Actual size: %dKB</string> | |
| 23 | 23 | <string name="dictionary_size_b">Actual size: %dB</string> | |
| 24 | + | <string name="downloading">Downloading a dictionary???</string> | |
| 24 | 25 | <string name="feedback_no_result">No result</string> | |
| 25 | 26 | <string name="feedback_progress">Searching???</string> | |
| 26 | 27 | <string name="feedback_didyoumean">Did you mean "%s"?</string> |