From d48c4c13ee76a4e40db2c2da3c7c4e04311da87d Mon Sep 17 00:00:00 2001 From: Adam Jones Date: Sun, 5 Mar 2017 17:40:51 +0000 Subject: [PATCH 1/3] Convert share button to ShareActionProvider --- .../media/MediaDetailPagerFragment.java | 19 ++++-- .../res/layout/activity_contributions.xml | 3 +- .../main/res/menu/fragment_image_detail.xml | 58 +++++++++---------- 3 files changed, 44 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/media/MediaDetailPagerFragment.java b/app/src/main/java/fr/free/nrw/commons/media/MediaDetailPagerFragment.java index be755a6def..404364eff2 100644 --- a/app/src/main/java/fr/free/nrw/commons/media/MediaDetailPagerFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/media/MediaDetailPagerFragment.java @@ -15,7 +15,9 @@ import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; +import android.support.v4.view.MenuItemCompat; import android.support.v4.view.ViewPager; +import android.support.v7.widget.ShareActionProvider; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; @@ -130,27 +132,25 @@ public boolean onOptionsItemSelected(MenuItem item) { Media m = provider.getMediaAtPosition(pager.getCurrentItem()); switch(item.getItemId()) { case R.id.menu_share_current_image: + // Share - this is just logs it, intent set in onCreateOptionsMenu, around line 252 EventLog.schema(CommonsApplication.EVENT_SHARE_ATTEMPT) .param("username", app.getCurrentAccount().name) .param("filename", m.getFilename()) .log(); - Intent shareIntent = new Intent(); - shareIntent.setAction(Intent.ACTION_SEND); - shareIntent.setType("text/plain"); - shareIntent.putExtra(Intent.EXTRA_TEXT, m.getDisplayTitle() + " " + m.getDescriptionUrl()); - startActivity(shareIntent); return true; case R.id.menu_browser_current_image: + // View in browser Intent viewIntent = new Intent(); viewIntent.setAction(Intent.ACTION_VIEW); viewIntent.setData(Uri.parse(m.getDescriptionUrl())); startActivity(viewIntent); return true; case R.id.menu_download_current_image: + // Download downloadMedia(m); return true; case R.id.menu_retry_current_image: - // Is this... sane? :) + // Retry ((ContributionsActivity)getActivity()).retryUpload(pager.getCurrentItem()); getActivity().getSupportFragmentManager().popBackStack(); return true; @@ -249,6 +249,13 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { menu.findItem(R.id.menu_share_current_image).setEnabled(true).setVisible(true); menu.findItem(R.id.menu_download_current_image).setEnabled(true).setVisible(true); + // Set ShareActionProvider Intent + ShareActionProvider mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menu.findItem(R.id.menu_share_current_image)); + Intent shareIntent = new Intent(Intent.ACTION_SEND); + shareIntent.setType("text/plain"); + shareIntent.putExtra(Intent.EXTRA_TEXT, m.getDisplayTitle() + " " + m.getDescriptionUrl()); + mShareActionProvider.setShareIntent(shareIntent); + if(m instanceof Contribution) { Contribution c = (Contribution)m; switch(c.getState()) { diff --git a/app/src/main/res/layout/activity_contributions.xml b/app/src/main/res/layout/activity_contributions.xml index 8a1576f1b9..4abbeb3c37 100644 --- a/app/src/main/res/layout/activity_contributions.xml +++ b/app/src/main/res/layout/activity_contributions.xml @@ -1,5 +1,6 @@ + tools:layout="@layout/fragment_contributions" /> \ No newline at end of file diff --git a/app/src/main/res/menu/fragment_image_detail.xml b/app/src/main/res/menu/fragment_image_detail.xml index 670bdf689d..3e0fa4f139 100644 --- a/app/src/main/res/menu/fragment_image_detail.xml +++ b/app/src/main/res/menu/fragment_image_detail.xml @@ -2,34 +2,34 @@ - - - - - + + + + + \ No newline at end of file From bd97b9b91a49380934c57c780cf820de0a04e8df Mon Sep 17 00:00:00 2001 From: Adam Jones Date: Sun, 5 Mar 2017 17:54:51 +0000 Subject: [PATCH 2/3] Tidy up MediaDetailPagerFragment --- .../media/MediaDetailPagerFragment.java | 59 ++----------------- 1 file changed, 6 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/media/MediaDetailPagerFragment.java b/app/src/main/java/fr/free/nrw/commons/media/MediaDetailPagerFragment.java index 404364eff2..c8ec424aa7 100644 --- a/app/src/main/java/fr/free/nrw/commons/media/MediaDetailPagerFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/media/MediaDetailPagerFragment.java @@ -2,14 +2,10 @@ import android.annotation.SuppressLint; import android.app.DownloadManager; -import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.content.IntentFilter; -import android.database.Cursor; import android.database.DataSetObserver; import android.net.Uri; -import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.support.v4.app.Fragment; @@ -18,7 +14,6 @@ import android.support.v4.view.MenuItemCompat; import android.support.v4.view.ViewPager; import android.support.v7.widget.ShareActionProvider; -import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -86,7 +81,7 @@ public int getCount() { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_media_detail_pager, container, false); pager = (ViewPager) view.findViewById(R.id.mediaDetailsPager); - pager.setOnPageChangeListener(this); + pager.addOnPageChangeListener(this); final MediaDetailAdapter adapter = new MediaDetailAdapter(getChildFragmentManager()); @@ -168,19 +163,13 @@ public boolean onOptionsItemSelected(MenuItem item) { * Start the media file downloading to the local SD card/storage. * The file can then be opened in Gallery or other apps. * - * @param m + * @param m Media file to download */ 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); @@ -188,49 +177,14 @@ private void downloadMedia(Media m) { req.setDescription(getString(R.string.app_name)); req.setTitle(m.getDisplayTitle()); 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. - // But for some reason it fails on Honeycomb (Google TV). Sigh. - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { - req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); - } - } + // Modern Android updates the gallery automatically. Yay! + req.allowScanningByMediaScanner(); + req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); + // TODO: Check we have android.permission.WRITE_EXTERNAL_STORAGE final DownloadManager manager = (DownloadManager)getActivity().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()); - getActivity().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"); - } - getActivity().unregisterReceiver(this); - } - }; - getActivity().registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); - } } @Override @@ -279,7 +233,6 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { break; } } - return; } } } From 3203932fd31c7095245200912b36602e63773fb2 Mon Sep 17 00:00:00 2001 From: Adam Jones Date: Sun, 5 Mar 2017 18:18:13 +0000 Subject: [PATCH 3/3] Add newline in extra_text shareIntent --- .../fr/free/nrw/commons/media/MediaDetailPagerFragment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/fr/free/nrw/commons/media/MediaDetailPagerFragment.java b/app/src/main/java/fr/free/nrw/commons/media/MediaDetailPagerFragment.java index c8ec424aa7..7359f82fdc 100644 --- a/app/src/main/java/fr/free/nrw/commons/media/MediaDetailPagerFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/media/MediaDetailPagerFragment.java @@ -207,7 +207,7 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { ShareActionProvider mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menu.findItem(R.id.menu_share_current_image)); Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("text/plain"); - shareIntent.putExtra(Intent.EXTRA_TEXT, m.getDisplayTitle() + " " + m.getDescriptionUrl()); + shareIntent.putExtra(Intent.EXTRA_TEXT, m.getDisplayTitle() + " \n" + m.getDescriptionUrl()); mShareActionProvider.setShareIntent(shareIntent); if(m instanceof Contribution) {