Add pause button
CHANGELOG.md
10 | 10 | not always require an app update. | |
11 | 11 | * Show a message when a file is corrupted and delete it. | |
12 | 12 | * Show partial download status when a file failed partway. | |
13 | + | * New pause button to stop downloads partway and retry later. | |
13 | 14 | ||
14 | 15 | ### Bug Fixes | |
15 | 16 |
app/src/main/java/eu/lepiller/nani/DictionaryDownloadActivity.java
12 | 12 | import android.os.Bundle; | |
13 | 13 | import android.util.Log; | |
14 | 14 | import android.view.View; | |
15 | + | import android.widget.ImageButton; | |
15 | 16 | import android.widget.ImageView; | |
16 | 17 | import android.widget.LinearLayout; | |
17 | 18 | import android.widget.ProgressBar; | |
… | |||
36 | 37 | final static String TAG = "DWN"; | |
37 | 38 | final static int notificationID = 1111; | |
38 | 39 | ||
40 | + | Dictionary d; | |
41 | + | DownloadTask currentDownloadTask = null; | |
42 | + | ImageView download_button = null; | |
43 | + | ||
39 | 44 | ProgressBar download_bar; | |
40 | 45 | ||
41 | 46 | NotificationManager manager; | |
42 | 47 | NotificationCompat.Builder builder; | |
43 | 48 | ||
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 | + | ||
44 | 77 | @Override | |
45 | 78 | protected void onCreate(Bundle savedInstanceState) { | |
46 | 79 | super.onCreate(savedInstanceState); | |
… | |||
48 | 81 | ||
49 | 82 | int position = getIntent().getExtras().getInt("dico"); | |
50 | 83 | ||
51 | - | final Dictionary d = DictionaryFactory.get(position); | |
84 | + | d = DictionaryFactory.get(position); | |
52 | 85 | ||
53 | 86 | manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); | |
54 | 87 | builder = new NotificationCompat.Builder(DictionaryDownloadActivity.this, "dico_dll") | |
… | |||
82 | 115 | icon_view.setImageDrawable(icon); | |
83 | 116 | } | |
84 | 117 | ||
85 | - | Drawable drawable; | |
86 | 118 | 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); | |
94 | 121 | download_button.setEnabled(true); | |
95 | 122 | ||
96 | 123 | 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 | - | } | |
102 | 124 | ImageView trash_button = findViewById(R.id.remove_button); | |
103 | - | trash_button.setImageDrawable(drawable); | |
125 | + | setIcon(trash_button, drawableResId); | |
104 | 126 | ||
105 | 127 | 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); | |
107 | 129 | ||
108 | 130 | TextView size_view = findViewById(R.id.size_view); | |
109 | 131 | int size = d.getSize(); | |
… | |||
114 | 136 | else | |
115 | 137 | size_view.setText(String.format(getResources().getString(R.string.dictionary_size_mb), size/1000000)); | |
116 | 138 | ||
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); | |
128 | 140 | ||
129 | 141 | trash_button.setOnClickListener(new View.OnClickListener() { | |
130 | 142 | @Override | |
… | |||
135 | 147 | }); | |
136 | 148 | } | |
137 | 149 | ||
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 | + | ||
138 | 160 | private void notifyProgress(int progress, int max) { | |
139 | 161 | builder.setProgress(max, progress, false); | |
140 | 162 | 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> |