2
2
3
3
import android .annotation .SuppressLint ;
4
4
import android .app .DownloadManager ;
5
- import android .content .BroadcastReceiver ;
6
5
import android .content .Context ;
7
6
import android .content .Intent ;
8
- import android .content .IntentFilter ;
9
- import android .database .Cursor ;
10
7
import android .database .DataSetObserver ;
11
8
import android .net .Uri ;
12
- import android .os .Build ;
13
9
import android .os .Bundle ;
14
10
import android .os .Environment ;
15
11
import android .support .v4 .app .Fragment ;
16
12
import android .support .v4 .app .FragmentManager ;
17
13
import android .support .v4 .app .FragmentStatePagerAdapter ;
14
+ import android .support .v4 .view .MenuItemCompat ;
18
15
import android .support .v4 .view .ViewPager ;
19
- import android .util . Log ;
16
+ import android .support . v7 . widget . ShareActionProvider ;
20
17
import android .view .LayoutInflater ;
21
18
import android .view .Menu ;
22
19
import android .view .MenuInflater ;
@@ -84,7 +81,7 @@ public int getCount() {
84
81
public View onCreateView (LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
85
82
View view = inflater .inflate (R .layout .fragment_media_detail_pager , container , false );
86
83
pager = (ViewPager ) view .findViewById (R .id .mediaDetailsPager );
87
- pager .setOnPageChangeListener (this );
84
+ pager .addOnPageChangeListener (this );
88
85
89
86
final MediaDetailAdapter adapter = new MediaDetailAdapter (getChildFragmentManager ());
90
87
@@ -130,27 +127,25 @@ public boolean onOptionsItemSelected(MenuItem item) {
130
127
Media m = provider .getMediaAtPosition (pager .getCurrentItem ());
131
128
switch (item .getItemId ()) {
132
129
case R .id .menu_share_current_image :
130
+ // Share - this is just logs it, intent set in onCreateOptionsMenu, around line 252
133
131
EventLog .schema (CommonsApplication .EVENT_SHARE_ATTEMPT )
134
132
.param ("username" , app .getCurrentAccount ().name )
135
133
.param ("filename" , m .getFilename ())
136
134
.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
135
return true ;
143
136
case R .id .menu_browser_current_image :
137
+ // View in browser
144
138
Intent viewIntent = new Intent ();
145
139
viewIntent .setAction (Intent .ACTION_VIEW );
146
140
viewIntent .setData (Uri .parse (m .getDescriptionUrl ()));
147
141
startActivity (viewIntent );
148
142
return true ;
149
143
case R .id .menu_download_current_image :
144
+ // Download
150
145
downloadMedia (m );
151
146
return true ;
152
147
case R .id .menu_retry_current_image :
153
- // Is this... sane? :)
148
+ // Retry
154
149
((ContributionsActivity )getActivity ()).retryUpload (pager .getCurrentItem ());
155
150
getActivity ().getSupportFragmentManager ().popBackStack ();
156
151
return true ;
@@ -168,69 +163,28 @@ public boolean onOptionsItemSelected(MenuItem item) {
168
163
* Start the media file downloading to the local SD card/storage.
169
164
* The file can then be opened in Gallery or other apps.
170
165
*
171
- * @param m
166
+ * @param m Media file to download
172
167
*/
173
168
private void downloadMedia (Media m ) {
174
169
String imageUrl = m .getImageUrl (),
175
170
fileName = m .getFilename ();
176
171
// Strip 'File:' from beginning of filename, we really shouldn't store it
177
172
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
173
Uri imageUri = Uri .parse (imageUrl );
185
174
186
175
DownloadManager .Request req = new DownloadManager .Request (imageUri );
187
176
//These are not the image title and description fields, they are download descs for notifications
188
177
req .setDescription (getString (R .string .app_name ));
189
178
req .setTitle (m .getDisplayTitle ());
190
179
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
180
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
- }
181
+ // Modern Android updates the gallery automatically. Yay!
182
+ req .allowScanningByMediaScanner ();
183
+ req .setNotificationVisibility (DownloadManager .Request .VISIBILITY_VISIBLE_NOTIFY_COMPLETED );
202
184
185
+ // TODO: Check we have android.permission.WRITE_EXTERNAL_STORAGE
203
186
final DownloadManager manager = (DownloadManager )getActivity ().getSystemService (Context .DOWNLOAD_SERVICE );
204
187
final long downloadId = manager .enqueue (req );
205
-
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 ));
223
-
224
- // todo: show a persistent notification?
225
- }
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 ));
233
- }
234
188
}
235
189
236
190
@ Override
@@ -249,6 +203,13 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
249
203
menu .findItem (R .id .menu_share_current_image ).setEnabled (true ).setVisible (true );
250
204
menu .findItem (R .id .menu_download_current_image ).setEnabled (true ).setVisible (true );
251
205
206
+ // Set ShareActionProvider Intent
207
+ ShareActionProvider mShareActionProvider = (ShareActionProvider ) MenuItemCompat .getActionProvider (menu .findItem (R .id .menu_share_current_image ));
208
+ Intent shareIntent = new Intent (Intent .ACTION_SEND );
209
+ shareIntent .setType ("text/plain" );
210
+ shareIntent .putExtra (Intent .EXTRA_TEXT , m .getDisplayTitle () + " \n " + m .getDescriptionUrl ());
211
+ mShareActionProvider .setShareIntent (shareIntent );
212
+
252
213
if (m instanceof Contribution ) {
253
214
Contribution c = (Contribution )m ;
254
215
switch (c .getState ()) {
@@ -272,7 +233,6 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
272
233
break ;
273
234
}
274
235
}
275
- return ;
276
236
}
277
237
}
278
238
}
0 commit comments