1
1
package fr .free .nrw .commons .media ;
2
2
3
+ import android .Manifest ;
3
4
import android .annotation .SuppressLint ;
4
5
import android .app .DownloadManager ;
5
- import android .content .BroadcastReceiver ;
6
6
import android .content .Context ;
7
7
import android .content .Intent ;
8
- import android .content .IntentFilter ;
9
- import android .database .Cursor ;
8
+ import android .content .pm .PackageManager ;
10
9
import android .database .DataSetObserver ;
11
10
import android .net .Uri ;
12
11
import android .os .Build ;
13
12
import android .os .Bundle ;
14
13
import android .os .Environment ;
14
+ import android .support .design .widget .Snackbar ;
15
+ import android .support .v4 .app .ActivityCompat ;
15
16
import android .support .v4 .app .Fragment ;
16
17
import android .support .v4 .app .FragmentManager ;
17
18
import android .support .v4 .app .FragmentStatePagerAdapter ;
19
+ import android .support .v4 .content .ContextCompat ;
20
+ import android .support .v4 .view .MenuItemCompat ;
18
21
import android .support .v4 .view .ViewPager ;
19
- import android .util . Log ;
22
+ import android .support . v7 . widget . ShareActionProvider ;
20
23
import android .view .LayoutInflater ;
21
24
import android .view .Menu ;
22
25
import android .view .MenuInflater ;
@@ -84,7 +87,7 @@ public int getCount() {
84
87
public View onCreateView (LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
85
88
View view = inflater .inflate (R .layout .fragment_media_detail_pager , container , false );
86
89
pager = (ViewPager ) view .findViewById (R .id .mediaDetailsPager );
87
- pager .setOnPageChangeListener (this );
90
+ pager .addOnPageChangeListener (this );
88
91
89
92
final MediaDetailAdapter adapter = new MediaDetailAdapter (getChildFragmentManager ());
90
93
@@ -130,27 +133,25 @@ public boolean onOptionsItemSelected(MenuItem item) {
130
133
Media m = provider .getMediaAtPosition (pager .getCurrentItem ());
131
134
switch (item .getItemId ()) {
132
135
case R .id .menu_share_current_image :
136
+ // Share - this is just logs it, intent set in onCreateOptionsMenu, around line 252
133
137
EventLog .schema (CommonsApplication .EVENT_SHARE_ATTEMPT )
134
138
.param ("username" , app .getCurrentAccount ().name )
135
139
.param ("filename" , m .getFilename ())
136
140
.log ();
137
- Intent shareIntent = new Intent ();
138
- shareIntent .setAction (Intent .ACTION_SEND );
139
- shareIntent .setType ("text/plain" );
140
- shareIntent .putExtra (Intent .EXTRA_TEXT , m .getDisplayTitle () + " " + m .getDescriptionUrl ());
141
- startActivity (shareIntent );
142
141
return true ;
143
142
case R .id .menu_browser_current_image :
143
+ // View in browser
144
144
Intent viewIntent = new Intent ();
145
145
viewIntent .setAction (Intent .ACTION_VIEW );
146
146
viewIntent .setData (Uri .parse (m .getDescriptionUrl ()));
147
147
startActivity (viewIntent );
148
148
return true ;
149
149
case R .id .menu_download_current_image :
150
+ // Download
150
151
downloadMedia (m );
151
152
return true ;
152
153
case R .id .menu_retry_current_image :
153
- // Is this... sane? :)
154
+ // Retry
154
155
((ContributionsActivity )getActivity ()).retryUpload (pager .getCurrentItem ());
155
156
getActivity ().getSupportFragmentManager ().popBackStack ();
156
157
return true ;
@@ -168,68 +169,38 @@ public boolean onOptionsItemSelected(MenuItem item) {
168
169
* Start the media file downloading to the local SD card/storage.
169
170
* The file can then be opened in Gallery or other apps.
170
171
*
171
- * @param m
172
+ * @param m Media file to download
172
173
*/
173
174
private void downloadMedia (Media m ) {
174
175
String imageUrl = m .getImageUrl (),
175
176
fileName = m .getFilename ();
176
177
// Strip 'File:' from beginning of filename, we really shouldn't store it
177
178
fileName = fileName .replaceFirst ("^File:" , "" );
178
- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .ICE_CREAM_SANDWICH ) {
179
- // Gingerbread DownloadManager has no HTTPS support...
180
- // Download file over HTTP, there'll be no credentials
181
- // sent so it should be safe-ish.
182
- imageUrl = imageUrl .replaceFirst ("^https://" , "http://" );
183
- }
184
179
Uri imageUri = Uri .parse (imageUrl );
185
180
186
181
DownloadManager .Request req = new DownloadManager .Request (imageUri );
187
182
//These are not the image title and description fields, they are download descs for notifications
188
183
req .setDescription (getString (R .string .app_name ));
189
184
req .setTitle (m .getDisplayTitle ());
190
185
req .setDestinationInExternalPublicDir (Environment .DIRECTORY_DOWNLOADS , fileName );
191
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .HONEYCOMB ) {
192
- // Modern Android updates the gallery automatically. Yay!
193
- req .allowScanningByMediaScanner ();
194
-
195
- // On HC/ICS/JB we can leave the download notification up when complete.
196
- // This allows folks to open the file directly in gallery viewer.
197
- // But for some reason it fails on Honeycomb (Google TV). Sigh.
198
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .ICE_CREAM_SANDWICH ) {
199
- req .setNotificationVisibility (DownloadManager .Request .VISIBILITY_VISIBLE_NOTIFY_COMPLETED );
200
- }
201
- }
202
-
203
- final DownloadManager manager = (DownloadManager )getActivity ().getSystemService (Context .DOWNLOAD_SERVICE );
204
- final long downloadId = manager .enqueue (req );
205
186
206
- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .HONEYCOMB ) {
207
- // For Gingerbread compatibility...
208
- BroadcastReceiver onComplete = new BroadcastReceiver () {
209
- @ Override
210
- public void onReceive (Context context , Intent intent ) {
211
- // Check if the download has completed...
212
- Cursor c = manager .query (new DownloadManager .Query ()
213
- .setFilterById (downloadId )
214
- .setFilterByStatus (DownloadManager .STATUS_SUCCESSFUL | DownloadManager .STATUS_FAILED )
215
- );
216
- if (c .moveToFirst ()) {
217
- int status = c .getInt (c .getColumnIndex (DownloadManager .COLUMN_STATUS ));
218
- Log .d ("Commons" , "Download completed with status " + status );
219
- if (status == DownloadManager .STATUS_SUCCESSFUL ) {
220
- // Force Gallery to index the new file
221
- Uri mediaUri = Uri .parse ("file://" + Environment .getExternalStorageDirectory ());
222
- getActivity ().sendBroadcast (new Intent (Intent .ACTION_MEDIA_MOUNTED , mediaUri ));
187
+ // Modern Android updates the gallery automatically. Yay!
188
+ req .allowScanningByMediaScanner ();
189
+ req .setNotificationVisibility (DownloadManager .Request .VISIBILITY_VISIBLE_NOTIFY_COMPLETED );
223
190
224
- // todo: show a persistent notification?
191
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M && !(ContextCompat .checkSelfPermission (getContext (), Manifest .permission .READ_EXTERNAL_STORAGE ) == PackageManager .PERMISSION_GRANTED )) {
192
+ Snackbar .make (getView (), R .string .storage_permission_rationale ,
193
+ Snackbar .LENGTH_INDEFINITE )
194
+ .setAction (R .string .ok , new View .OnClickListener () {
195
+ @ Override
196
+ public void onClick (View view ) {
197
+ ActivityCompat .requestPermissions (getActivity (),
198
+ new String []{Manifest .permission .READ_EXTERNAL_STORAGE }, 1 );
225
199
}
226
- } else {
227
- Log .d ("Commons" , "Couldn't get download status for some reason" );
228
- }
229
- getActivity ().unregisterReceiver (this );
230
- }
231
- };
232
- getActivity ().registerReceiver (onComplete , new IntentFilter (DownloadManager .ACTION_DOWNLOAD_COMPLETE ));
200
+ }).show ();
201
+ } else {
202
+ final DownloadManager manager = (DownloadManager )getActivity ().getSystemService (Context .DOWNLOAD_SERVICE );
203
+ manager .enqueue (req );
233
204
}
234
205
}
235
206
@@ -249,6 +220,13 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
249
220
menu .findItem (R .id .menu_share_current_image ).setEnabled (true ).setVisible (true );
250
221
menu .findItem (R .id .menu_download_current_image ).setEnabled (true ).setVisible (true );
251
222
223
+ // Set ShareActionProvider Intent
224
+ ShareActionProvider mShareActionProvider = (ShareActionProvider ) MenuItemCompat .getActionProvider (menu .findItem (R .id .menu_share_current_image ));
225
+ Intent shareIntent = new Intent (Intent .ACTION_SEND );
226
+ shareIntent .setType ("text/plain" );
227
+ shareIntent .putExtra (Intent .EXTRA_TEXT , m .getDisplayTitle () + " \n " + m .getDescriptionUrl ());
228
+ mShareActionProvider .setShareIntent (shareIntent );
229
+
252
230
if (m instanceof Contribution ) {
253
231
Contribution c = (Contribution )m ;
254
232
switch (c .getState ()) {
@@ -272,7 +250,6 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
272
250
break ;
273
251
}
274
252
}
275
- return ;
276
253
}
277
254
}
278
255
}
0 commit comments