Add notification on dictionary download

Julien LepillerTue May 19 01:12:19+0200 2020

3bce0cb

Add notification on dictionary download

CHANGELOG.md

11
Changelog
22
=========
33
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+
418
Changes Since 0.1
519
-----------------
620

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

11
package eu.lepiller.nani;
22
3+
import android.app.NotificationManager;
34
import android.content.Context;
45
import android.graphics.drawable.Drawable;
56
import android.os.AsyncTask;
67
import android.os.Build;
8+
9+
import androidx.core.app.NotificationCompat;
710
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;
811
import androidx.appcompat.app.AppCompatActivity;
912
import android.os.Bundle;

2831
2932
public class DictionaryDownloadActivity extends AppCompatActivity {
3033
    final static String TAG = "DWN";
34+
    final static int notificationID = 1111;
3135
3236
    ProgressBar download_bar;
3337
38+
    NotificationManager manager;
39+
    NotificationCompat.Builder builder;
40+
3441
    @Override
3542
    protected void onCreate(Bundle savedInstanceState) {
3643
        super.onCreate(savedInstanceState);

3946
        int position = getIntent().getExtras().getInt("dico");
4047
4148
        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+
4256
        setResult(DictionaryActivity.DICO_REQUEST);
4357
        updateLayout(d);
4458
    }

97111
            @Override
98112
            public void onClick(View v) {
99113
                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());
101118
                downloadTask.execute(d);
102119
            }
103120
        });

111128
        });
112129
    }
113130
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+
114140
    private class DownloadTask extends AsyncTask<Dictionary, Integer, String> {
115141
        private Dictionary d;
116-
        private Context context;
117-
118-
        public DownloadTask(Context context) {
119-
            this.context = context;
120-
        }
121142
122143
        @Override
123144
        protected String doInBackground(Dictionary... sDicos) {

201222
            download_bar.setIndeterminate(false);
202223
            download_bar.setMax(100);
203224
            download_bar.setProgress(progress[0]);
225+
            notifyProgress(progress[0], 100);
204226
        }
205227
206228
207229
        @Override
208230
        protected void onPostExecute(String result) {
209231
            download_bar.setProgress(100);
232+
            removeProgress();
210233
            updateLayout(d);
211234
        }
212235
    }

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

11
package eu.lepiller.nani;
22
3+
import android.app.NotificationChannel;
4+
import android.app.NotificationManager;
35
import android.content.Intent;
46
import android.os.AsyncTask;
57
import android.os.Bundle;

5052
5153
        Button radical_button = findViewById(R.id.radical_button);
5254
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+
5363
        try {
5464
            radical_selector.setDictionary(DictionaryFactory.getRadicalDictionary(getApplicationContext()));
5565
        } catch (NoDictionaryException e) {

app/src/main/res/values-fr/strings.xml

2525
    <string name="dictionary_size_mb">Taille r??elle : %d???Mo</string>
2626
    <string name="dictionary_size_kb">Taille r??elle : %d???Ko</string>
2727
    <string name="dictionary_size_b">Taille r??elle : %d???o</string>
28+
    <string name="downloading">T??l??chargement d\'un dictionnaire???</string>
2829
    <string name="feedback_no_result">Pas de r??sultat</string>
2930
    <string name="feedback_progress">Recherche???</string>
3031
    <string name="feedback_didyoumean">Vouliez-vous dire ?? %s ?? ?</string>

app/src/main/res/values/strings.xml

2121
    <string name="dictionary_size_mb">Actual size: %dMB</string>
2222
    <string name="dictionary_size_kb">Actual size: %dKB</string>
2323
    <string name="dictionary_size_b">Actual size: %dB</string>
24+
    <string name="downloading">Downloading a dictionary???</string>
2425
    <string name="feedback_no_result">No result</string>
2526
    <string name="feedback_progress">Searching???</string>
2627
    <string name="feedback_didyoumean">Did you mean "%s"?</string>