mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Merge pull request #18 from brion/download-photo
"Download" menu button on image detail page.
This commit is contained in:
commit
8ee8c2796f
8 changed files with 76 additions and 0 deletions
BIN
commons/res/drawable-hdpi/ic_menu_download.png
Normal file
BIN
commons/res/drawable-hdpi/ic_menu_download.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.4 KiB |
BIN
commons/res/drawable-hdpi/ic_menu_upload.png
Normal file
BIN
commons/res/drawable-hdpi/ic_menu_upload.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.4 KiB |
BIN
commons/res/drawable-mdpi/ic_menu_download.png
Normal file
BIN
commons/res/drawable-mdpi/ic_menu_download.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2 KiB |
BIN
commons/res/drawable-mdpi/ic_menu_upload.png
Executable file
BIN
commons/res/drawable-mdpi/ic_menu_upload.png
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 2 KiB |
BIN
commons/res/drawable-xhdpi/ic_menu_download.png
Normal file
BIN
commons/res/drawable-xhdpi/ic_menu_download.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.9 KiB |
BIN
commons/res/drawable-xhdpi/ic_menu_upload.png
Normal file
BIN
commons/res/drawable-xhdpi/ic_menu_upload.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.9 KiB |
|
|
@ -11,6 +11,11 @@
|
|||
android:icon="@android:drawable/ic_menu_view"
|
||||
android:title="@string/menu_open_in_browser"
|
||||
/>
|
||||
<item android:id="@+id/menu_download_current_image"
|
||||
android:showAsAction="always"
|
||||
android:icon="@drawable/ic_menu_download"
|
||||
android:title="Download"
|
||||
/>
|
||||
<item android:id="@+id/menu_retry_current_image"
|
||||
android:showAsAction="always|withText"
|
||||
android:icon="@android:drawable/ic_menu_revert"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
package org.wikimedia.commons.media;
|
||||
|
||||
import android.app.DownloadManager;
|
||||
import android.content.*;
|
||||
import android.database.Cursor;
|
||||
import android.net.*;
|
||||
import android.os.*;
|
||||
import android.support.v4.app.Fragment;
|
||||
|
|
@ -132,6 +134,9 @@ public class MediaDetailPagerFragment extends SherlockFragment implements ViewPa
|
|||
viewIntent.setData(Uri.parse(m.getDescriptionUrl()));
|
||||
startActivity(viewIntent);
|
||||
return true;
|
||||
case R.id.menu_download_current_image:
|
||||
downloadMedia(m);
|
||||
return true;
|
||||
case R.id.menu_retry_current_image:
|
||||
// Is this... sane? :)
|
||||
((ContributionsActivity)getSherlockActivity()).retryUpload(pager.getCurrentItem());
|
||||
|
|
@ -147,6 +152,71 @@ public class MediaDetailPagerFragment extends SherlockFragment implements ViewPa
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the media file downloading to the local SD card/storage.
|
||||
* The file can then be opened in Gallery or other apps.
|
||||
*
|
||||
* @param m
|
||||
*/
|
||||
private void downloadMedia(Media m) {
|
||||
String imageUrl = m.getImageUrl(),
|
||||
fileName = m.getFilename();
|
||||
// Strip 'File:' from beginning of filename, we really shouldn't store it
|
||||
fileName = fileName.replaceFirst("^File:", "");
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
|
||||
// Gingerbread DownloadManager has no HTTPS support...
|
||||
// Download file over HTTP, there'll be no credentials
|
||||
// sent so it should be safe-ish.
|
||||
imageUrl = imageUrl.replaceFirst("^https://", "http://");
|
||||
}
|
||||
Uri imageUri = Uri.parse(imageUrl);
|
||||
|
||||
DownloadManager.Request req = new DownloadManager.Request(imageUri);
|
||||
req.setDescription(m.getDisplayTitle());
|
||||
req.setTitle("Commons");
|
||||
req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
||||
// Modern Android updates the gallery automatically. Yay!
|
||||
req.allowScanningByMediaScanner();
|
||||
|
||||
// On HC/ICS/JB we can leave the download notification up when complete.
|
||||
// This allows folks to open the file directly in gallery viewer.
|
||||
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
|
||||
}
|
||||
|
||||
final DownloadManager manager = (DownloadManager)getSherlockActivity().getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
final long downloadId = manager.enqueue(req);
|
||||
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
|
||||
// For Gingerbread compatibility...
|
||||
BroadcastReceiver onComplete = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
// Check if the download has completed...
|
||||
Cursor c = manager.query(new DownloadManager.Query()
|
||||
.setFilterById(downloadId)
|
||||
.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL | DownloadManager.STATUS_FAILED)
|
||||
);
|
||||
if (c.moveToFirst()) {
|
||||
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
|
||||
Log.d("Commons", "Download completed with status " + status);
|
||||
if (status == DownloadManager.STATUS_SUCCESSFUL) {
|
||||
// Force Gallery to index the new file
|
||||
Uri mediaUri = Uri.parse("file://" + Environment.getExternalStorageDirectory());
|
||||
getSherlockActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, mediaUri));
|
||||
|
||||
// todo: show a persistent notification?
|
||||
}
|
||||
} else {
|
||||
Log.d("Commons", "Couldn't get download status for some reason");
|
||||
}
|
||||
getSherlockActivity().unregisterReceiver(this);
|
||||
}
|
||||
};
|
||||
getSherlockActivity().registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
if(!editable) { // Disable menu options for editable views
|
||||
|
|
@ -159,6 +229,7 @@ public class MediaDetailPagerFragment extends SherlockFragment implements ViewPa
|
|||
// Crude way of checking if the file has been successfully saved!
|
||||
menu.findItem(R.id.menu_browser_current_image).setEnabled(false).setVisible(false);
|
||||
menu.findItem(R.id.menu_share_current_image).setEnabled(false).setVisible(false);
|
||||
menu.findItem(R.id.menu_download_current_image).setEnabled(false).setVisible(false);
|
||||
menu.findItem(R.id.menu_retry_current_image).setEnabled(true).setVisible(true);
|
||||
menu.findItem(R.id.menu_abort_current_image).setEnabled(true).setVisible(true);
|
||||
return;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue