Add pause button

Julien LepillerThu May 28 17:01:55+0200 2020

02629a6

Add pause button

CHANGELOG.md

1010
  not always require an app update.
1111
* Show a message when a file is corrupted and delete it.
1212
* Show partial download status when a file failed partway.
13+
* New pause button to stop downloads partway and retry later.
1314
1415
### Bug Fixes
1516

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

1212
import android.os.Bundle;
1313
import android.util.Log;
1414
import android.view.View;
15+
import android.widget.ImageButton;
1516
import android.widget.ImageView;
1617
import android.widget.LinearLayout;
1718
import android.widget.ProgressBar;

3637
    final static String TAG = "DWN";
3738
    final static int notificationID = 1111;
3839
40+
    Dictionary d;
41+
    DownloadTask currentDownloadTask = null;
42+
    ImageView download_button = null;
43+
3944
    ProgressBar download_bar;
4045
4146
    NotificationManager manager;
4247
    NotificationCompat.Builder builder;
4348
49+
    View.OnClickListener download_click_listener = new View.OnClickListener() {
50+
        @Override
51+
        public void onClick(View v) {
52+
            if(currentDownloadTask != null)
53+
                return;
54+
55+
            currentDownloadTask = new DownloadTask();
56+
            builder.setProgress(0,0,true);
57+
            builder.setOnlyAlertOnce(true);
58+
            manager.notify(notificationID, builder.build());
59+
            currentDownloadTask.execute(d);
60+
            setIcon(download_button, R.drawable.ic_pause);
61+
62+
            download_button.setOnClickListener(pause_click_listener);
63+
        }
64+
    };
65+
66+
    View.OnClickListener pause_click_listener = new View.OnClickListener() {
67+
        @Override
68+
        public void onClick(View v) {
69+
            currentDownloadTask.cancel(true);
70+
            setIcon(download_button, R.drawable.ic_nani_download);
71+
72+
            download_button.setOnClickListener(download_click_listener);
73+
            currentDownloadTask = null;
74+
        }
75+
    };
76+
4477
    @Override
4578
    protected void onCreate(Bundle savedInstanceState) {
4679
        super.onCreate(savedInstanceState);

4881
4982
        int position = getIntent().getExtras().getInt("dico");
5083
51-
        final Dictionary d = DictionaryFactory.get(position);
84+
        d = DictionaryFactory.get(position);
5285
5386
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
5487
        builder =  new NotificationCompat.Builder(DictionaryDownloadActivity.this, "dico_dll")

82115
            icon_view.setImageDrawable(icon);
83116
        }
84117
85-
        Drawable drawable;
86118
        int drawableResId = d.isDownloaded() ? R.drawable.ic_nani_refresh : R.drawable.ic_nani_download;
87-
        if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
88-
            drawable = getResources().getDrawable(drawableResId, getTheme());
89-
        } else {
90-
            drawable = VectorDrawableCompat.create(getResources(), drawableResId, getTheme());
91-
        }
92-
        final ImageView download_button = findViewById(R.id.download_button);
93-
        download_button.setImageDrawable(drawable);
119+
        download_button = findViewById(R.id.download_button);
120+
        setIcon(download_button, drawableResId);
94121
        download_button.setEnabled(true);
95122
96123
        drawableResId = R.drawable.ic_nani_trash;
97-
        if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
98-
            drawable = getResources().getDrawable(drawableResId, getTheme());
99-
        } else {
100-
            drawable = VectorDrawableCompat.create(getResources(), drawableResId, getTheme());
101-
        }
102124
        ImageView trash_button = findViewById(R.id.remove_button);
103-
        trash_button.setImageDrawable(drawable);
125+
        setIcon(trash_button, drawableResId);
104126
105127
        LinearLayout remove_layout = findViewById(R.id.remove_layout);
106-
        remove_layout.setVisibility(d.isDownloaded()? View.VISIBLE: View.INVISIBLE);
128+
        remove_layout.setVisibility(d.getSize() > 0? View.VISIBLE: View.INVISIBLE);
107129
108130
        TextView size_view = findViewById(R.id.size_view);
109131
        int size = d.getSize();

114136
        else
115137
            size_view.setText(String.format(getResources().getString(R.string.dictionary_size_mb), size/1000000));
116138
117-
        download_button.setOnClickListener(new View.OnClickListener() {
118-
            @Override
119-
            public void onClick(View v) {
120-
                download_button.setEnabled(false);
121-
                final DownloadTask downloadTask = new DownloadTask();
122-
                builder.setProgress(0,0,true);
123-
                builder.setOnlyAlertOnce(true);
124-
                manager.notify(notificationID, builder.build());
125-
                downloadTask.execute(d);
126-
            }
127-
        });
139+
        download_button.setOnClickListener(download_click_listener);
128140
129141
        trash_button.setOnClickListener(new View.OnClickListener() {
130142
            @Override

135147
        });
136148
    }
137149
150+
    private void setIcon(ImageView download_button, int drawableResId) {
151+
        Drawable drawable;
152+
        if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
153+
            drawable = getResources().getDrawable(drawableResId, getTheme());
154+
        } else {
155+
            drawable = VectorDrawableCompat.create(getResources(), drawableResId, getTheme());
156+
        }
157+
        download_button.setImageDrawable(drawable);
158+
    }
159+
138160
    private void notifyProgress(int progress, int max) {
139161
        builder.setProgress(max, progress, false);
140162
        manager.notify(notificationID, builder.build());

app/src/main/res/drawable/ic_pause.xml unknown status 1

1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
    android:width="32dp"
3+
    android:height="32dp"
4+
    android:viewportWidth="32"
5+
    android:viewportHeight="32">
6+
  <path
7+
      android:pathData="M6,2h8v28h-8z"
8+
      android:strokeWidth="0.22660473"
9+
      android:fillColor="#333333"/>
10+
  <path
11+
      android:pathData="M18,2h8v28h-8z"
12+
      android:strokeWidth="0.22660473"
13+
      android:fillColor="#333333"/>
14+
</vector>