1
1
package com .zulip .android .activities ;
2
2
3
+ import android .Manifest ;
4
+ import android .app .NotificationManager ;
5
+ import android .app .PendingIntent ;
3
6
import android .content .ClipData ;
4
7
import android .content .ClipboardManager ;
5
8
import android .content .Context ;
9
+ import android .content .DialogInterface ;
6
10
import android .content .Intent ;
11
+ import android .content .SharedPreferences ;
12
+ import android .content .pm .PackageManager ;
7
13
import android .net .ConnectivityManager ;
8
14
import android .net .NetworkInfo ;
9
15
import android .net .Uri ;
16
+ import android .os .AsyncTask ;
10
17
import android .os .Bundle ;
18
+ import android .os .Environment ;
19
+ import android .provider .Settings ;
20
+ import android .support .annotation .NonNull ;
21
+ import android .support .v4 .app .ActivityCompat ;
22
+ import android .support .v7 .app .AlertDialog ;
11
23
import android .support .v7 .app .AppCompatActivity ;
24
+ import android .support .v7 .app .NotificationCompat ;
12
25
import android .support .v7 .widget .Toolbar ;
13
26
import android .view .Menu ;
14
27
import android .view .MenuItem ;
23
36
import com .bumptech .glide .request .target .GlideDrawableImageViewTarget ;
24
37
import com .bumptech .glide .request .target .Target ;
25
38
import com .zulip .android .R ;
39
+ import com .zulip .android .util .ZLog ;
26
40
27
- public class PhotoViewActivity extends AppCompatActivity {
41
+ import java .io .BufferedInputStream ;
42
+ import java .io .File ;
43
+ import java .io .FileOutputStream ;
44
+ import java .io .IOException ;
45
+ import java .io .InputStream ;
46
+ import java .io .OutputStream ;
47
+ import java .net .MalformedURLException ;
48
+ import java .net .URL ;
49
+ import java .net .URLConnection ;
28
50
29
51
52
+ public class PhotoViewActivity extends AppCompatActivity implements ActivityCompat .OnRequestPermissionsResultCallback {
53
+
54
+
55
+ private static final int EXTERNAL_STORAGE_PERMISSION_CONSTANT = 100 ;
56
+ private static final int REQUEST_PERMISSION_SETTING = 101 ;
57
+ int id = 1 ;
30
58
private ImageView linkImage ;
31
59
private ProgressBar progressBar ;
32
-
60
+ private NotificationManager mNotifyManager ;
61
+ private NotificationCompat .Builder mBuilder ;
62
+ private boolean openSettings = false ;
63
+ private SharedPreferences permissionStatus ;
33
64
34
65
@ Override
35
66
protected void onCreate (Bundle savedInstanceState ) {
@@ -39,6 +70,7 @@ protected void onCreate(Bundle savedInstanceState) {
39
70
setSupportActionBar (toolbar );
40
71
getSupportActionBar ().setDisplayHomeAsUpEnabled (true );
41
72
getSupportActionBar ().setHomeButtonEnabled (true );
73
+ permissionStatus = getSharedPreferences ("permissionStatus" , MODE_PRIVATE );
42
74
final Intent intent = getIntent ();
43
75
String url = intent .getStringExtra (Intent .EXTRA_TEXT );
44
76
getSupportActionBar ().setTitle (url );
@@ -95,20 +127,193 @@ public boolean onOptionsItemSelected(MenuItem item) {
95
127
case R .id .copy_link :
96
128
copyLink (url );
97
129
break ;
98
-
130
+ case R .id .download :
131
+ if (ActivityCompat .checkSelfPermission (PhotoViewActivity .this , Manifest .permission .WRITE_EXTERNAL_STORAGE ) != PackageManager .PERMISSION_GRANTED ) {
132
+ allowPermission ();
133
+ } else {
134
+ downloadImage (url );
135
+ }
136
+ break ;
99
137
default :
100
138
return super .onOptionsItemSelected (item );
101
139
}
102
140
103
141
return true ;
104
142
}
105
143
144
+ private void allowPermission () {
145
+ if (ActivityCompat .shouldShowRequestPermissionRationale (PhotoViewActivity .this , Manifest .permission .WRITE_EXTERNAL_STORAGE )) {
146
+ //Information about the permission
147
+ AlertDialog .Builder builder = new AlertDialog .Builder (PhotoViewActivity .this );
148
+ builder .setTitle (R .string .permission_title );
149
+ builder .setMessage (R .string .permission_message );
150
+ builder .setPositiveButton ("Grant" , new DialogInterface .OnClickListener () {
151
+ @ Override
152
+ public void onClick (DialogInterface dialog , int which ) {
153
+ dialog .cancel ();
154
+ ActivityCompat .requestPermissions (PhotoViewActivity .this , new String []{Manifest .permission .WRITE_EXTERNAL_STORAGE }, EXTERNAL_STORAGE_PERMISSION_CONSTANT );
155
+ }
156
+ });
157
+
158
+ builder .setNegativeButton ("Cancel" , new DialogInterface .OnClickListener () {
159
+ @ Override
160
+ public void onClick (DialogInterface dialog , int which ) {
161
+ dialog .cancel ();
162
+ }
163
+ });
164
+ builder .show ();
165
+
166
+ } else if (permissionStatus .getBoolean (Manifest .permission .WRITE_EXTERNAL_STORAGE , false )) {
167
+ //Previously Permission Request was cancelled with 'Dont Ask Again',
168
+ // Now Redirect to Settings after showing Information about permission
169
+ AlertDialog .Builder builder = new AlertDialog .Builder (PhotoViewActivity .this );
170
+ builder .setTitle (R .string .permission_title );
171
+ builder .setMessage (R .string .permission_message );
172
+ builder .setPositiveButton ("Grant" , new DialogInterface .OnClickListener () {
173
+ @ Override
174
+ public void onClick (DialogInterface dialog , int which ) {
175
+ dialog .cancel ();
176
+ openSettings = true ;
177
+ Intent intent = new Intent (Settings .ACTION_APPLICATION_DETAILS_SETTINGS );
178
+ Uri uri = Uri .fromParts ("package" , getPackageName (), null );
179
+ intent .setData (uri );
180
+ startActivityForResult (intent , REQUEST_PERMISSION_SETTING );
181
+ }
182
+ });
183
+ builder .setNegativeButton ("Cancel" , new DialogInterface .OnClickListener () {
184
+ @ Override
185
+ public void onClick (DialogInterface dialog , int which ) {
186
+ dialog .cancel ();
187
+ }
188
+ });
189
+ builder .show ();
190
+
191
+ } else {
192
+
193
+ ActivityCompat .requestPermissions (PhotoViewActivity .this , new String []{Manifest .permission .WRITE_EXTERNAL_STORAGE }, EXTERNAL_STORAGE_PERMISSION_CONSTANT );
194
+ }
195
+
196
+ SharedPreferences .Editor editor = permissionStatus .edit ();
197
+ editor .putBoolean (Manifest .permission .WRITE_EXTERNAL_STORAGE , true );
198
+ editor .apply ();
199
+
200
+ }
201
+
202
+ @ Override
203
+ public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
204
+
205
+ if (requestCode == EXTERNAL_STORAGE_PERMISSION_CONSTANT ) {
206
+
207
+ if (grantResults .length == 1 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
208
+
209
+ final Intent intent = getIntent ();
210
+ final String url = intent .getStringExtra (Intent .EXTRA_TEXT );
211
+ downloadImage (url );
212
+
213
+ }
214
+
215
+ } else {
216
+ super .onRequestPermissionsResult (requestCode , permissions , grantResults );
217
+ }
218
+
219
+ }
220
+
221
+ private void downloadImage (String url ) {
222
+ Toast .makeText (PhotoViewActivity .this , R .string .downloading , Toast .LENGTH_SHORT ).show ();
223
+ mNotifyManager = (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
224
+ mBuilder = new NotificationCompat .Builder (PhotoViewActivity .this );
225
+ mBuilder .setContentTitle (url )
226
+ .setSmallIcon (android .R .drawable .stat_sys_download );
227
+ DownloadImage download = new DownloadImage ();
228
+ download .execute (url );
229
+
230
+ }
231
+
106
232
private void copyLink (String url ) {
107
233
ClipboardManager clipboard
108
234
= (ClipboardManager ) getSystemService (Context .CLIPBOARD_SERVICE );
109
235
ClipData clip = ClipData .newPlainText ("link" , url );
110
236
clipboard .setPrimaryClip (clip );
111
- Toast .makeText (PhotoViewActivity .this , R .string .link_copied , Toast .LENGTH_SHORT ).show ();
237
+ Toast .makeText (PhotoViewActivity .this , R .string .link_copied , Toast .LENGTH_SHORT ).show ();
112
238
}
113
239
240
+ class DownloadImage extends AsyncTask <String , Integer , String > {
241
+
242
+ int calculatedProgress = 0 ;
243
+ int imageLength = 0 ;
244
+
245
+ @ Override
246
+ protected void onPreExecute () {
247
+
248
+ mBuilder .setProgress (100 , 0 , false );
249
+ mNotifyManager .notify (id , mBuilder .build ());
250
+
251
+ }
252
+
253
+ @ Override
254
+ protected String doInBackground (String ... params ) {
255
+
256
+ String path = params [0 ];
257
+ File downloadedImages = new File (Environment .getExternalStoragePublicDirectory (Environment .DIRECTORY_DOWNLOADS ).getAbsolutePath () + "/" + Uri .parse (path ).getLastPathSegment ());
258
+ int total = 0 ;
259
+ int count = 0 ;
260
+
261
+ try {
262
+ URL url = new URL (path );
263
+ URLConnection urlConnection = url .openConnection ();
264
+ urlConnection .connect ();
265
+ imageLength = urlConnection .getContentLength ();
266
+ boolean isFileCreated = downloadedImages .exists ();
267
+ InputStream inputStream = new BufferedInputStream (url .openStream (), 8192 );
268
+ byte [] data = new byte [1024 ];
269
+ OutputStream outputStream = new FileOutputStream (downloadedImages );
270
+
271
+ if (!isFileCreated ) {
272
+ isFileCreated = downloadedImages .createNewFile ();
273
+ }
274
+ while ((count = inputStream .read (data )) != -1 ) {
275
+ total = total + count ;
276
+ outputStream .write (data , 0 , count );
277
+ calculatedProgress = (int ) total * 100 / imageLength ;
278
+ publishProgress (total );
279
+ }
280
+ inputStream .close ();
281
+ outputStream .close ();
282
+
283
+ } catch (MalformedURLException e ) {
284
+ ZLog .logException (e );
285
+ } catch (IOException e ) {
286
+ ZLog .logException (e );
287
+ }
288
+
289
+ return String .valueOf (downloadedImages );
290
+ }
291
+
292
+ @ Override
293
+ protected void onProgressUpdate (Integer ... values ) {
294
+
295
+ mBuilder .setProgress (100 , values [0 ], true );
296
+ mNotifyManager .notify (id , mBuilder .build ());
297
+ super .onProgressUpdate (values );
298
+
299
+ }
300
+
301
+ @ Override
302
+ protected void onPostExecute (String result ) {
303
+
304
+ mBuilder .setContentText ("Download complete" );
305
+ mBuilder .setSmallIcon (android .R .drawable .stat_sys_download_done );
306
+ mBuilder .setProgress (0 , 0 , false );
307
+ Toast .makeText (PhotoViewActivity .this , "Saved at " + result , Toast .LENGTH_LONG ).show ();
308
+ File file = new File (result );
309
+ Uri uri = Uri .fromFile (file );
310
+ Intent intent = new Intent ();
311
+ intent .setAction (android .content .Intent .ACTION_VIEW );
312
+ intent .setDataAndType (uri , "image/*" );
313
+ PendingIntent pIntent = PendingIntent .getActivity (PhotoViewActivity .this , 0 , intent , 0 );
314
+ mBuilder .setContentIntent (pIntent ).build ();
315
+ mNotifyManager .notify (id , mBuilder .build ());
316
+
317
+ }
318
+ }
114
319
}
0 commit comments