From 57f829e58e62b3171a9ed73e1fa505b5303b075a Mon Sep 17 00:00:00 2001 From: ntngrtltr Date: Fri, 7 Apr 2017 12:12:26 +0200 Subject: [PATCH 1/3] download images by tap an hold --- app/src/main/AndroidManifest.xml | 1 + .../tusky/ViewMediaFragment.java | 41 ++++++++++++++++++- app/src/main/res/values-de/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 62955811..5ff4b955 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ + Ein Fehler ist Aufgetreten. Kein Webbrowser gefunden. Es konnte kein Login-Token abgerufen werden. + Herunterladen diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 24fd9ca9..78b16188 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -126,4 +126,5 @@ %1$s and %2$s %d new interactions + Download From 2c94ee9c2ec0f1dc90e45840a3508f4bbb6aa640 Mon Sep 17 00:00:00 2001 From: ntngrtltr Date: Fri, 7 Apr 2017 13:11:31 +0200 Subject: [PATCH 2/3] add support for runtime-permissions (fix needed) --- .../tusky/ViewMediaFragment.java | 95 ++++++++++++++++--- app/src/main/res/values-de/strings.xml | 1 + app/src/main/res/values/strings.xml | 2 + 3 files changed, 85 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/ViewMediaFragment.java b/app/src/main/java/com/keylesspalace/tusky/ViewMediaFragment.java index d3a7aed1..93e4d576 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ViewMediaFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/ViewMediaFragment.java @@ -15,15 +15,23 @@ package com.keylesspalace.tusky; +import android.*; import android.app.AlertDialog; import android.app.DownloadManager; import android.content.Context; import android.content.DialogInterface; +import android.content.pm.PackageManager; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.os.Environment; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.annotation.StringRes; +import android.support.design.widget.Snackbar; +import android.support.v13.app.ActivityCompat; import android.support.v4.app.DialogFragment; +import android.support.v4.content.ContextCompat; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; @@ -34,6 +42,7 @@ import com.squareup.picasso.Callback; import com.squareup.picasso.Picasso; import java.io.File; +import java.security.Permission; import butterknife.BindView; import butterknife.ButterKnife; @@ -44,6 +53,9 @@ public class ViewMediaFragment extends DialogFragment { private PhotoViewAttacher attacher; private DownloadManager downloadManager; + + private static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1; + @BindView(R.id.view_media_image) PhotoView photoView; public static ViewMediaFragment newInstance(String url) { @@ -118,19 +130,7 @@ public class ViewMediaFragment extends DialogFragment { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); - String url = getArguments().getString("url"); - Uri uri = Uri.parse(url); - - String filename = new File(url).getName(); - - downloadManager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE); - - DownloadManager.Request request = new DownloadManager.Request(uri); - request.allowScanningByMediaScanner(); - request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, getString(R.string.app_name) + "/" + filename); - - downloadManager.enqueue(request); - System.out.println(url); + downloadImage(); } }); downloadDialog.show(); @@ -160,4 +160,73 @@ public class ViewMediaFragment extends DialogFragment { attacher.cleanup(); super.onDestroyView(); } + + /** + * Check permissions and download the thing at getArguments().getString("url") as image, listed in the systems gallery. + * This works in general, but when the permission is granted at runtime, the download button ha to be pressed again (see comment further down) + */ + private void downloadImage(){ + + //Permission stuff + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && + ContextCompat.checkSelfPermission(this.getContext(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE) + != PackageManager.PERMISSION_GRANTED) { + android.support.v4.app.ActivityCompat.requestPermissions(getActivity(), + new String[] { android.Manifest.permission.WRITE_EXTERNAL_STORAGE }, + PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE); + } else { + + + //download stuff + String url = getArguments().getString("url"); + Uri uri = Uri.parse(url); + + String filename = new File(url).getName(); + + downloadManager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE); + + DownloadManager.Request request = new DownloadManager.Request(uri); + request.allowScanningByMediaScanner(); + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, getString(R.string.app_name) + "/" + filename); + + downloadManager.enqueue(request); + } + } + + /* + * took this from ComposeActivity.java (Media upload) to handle permission requests. + * However, onRequestPermissionResult seems not to be called. + */ + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], + @NonNull int[] grantResults) { + System.out.println("Requestcode: " + requestCode); + switch (requestCode) { + case PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: { + if (grantResults.length > 0 + && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + downloadImage(); + } else { + System.out.println("not granted\n"); + doErrorDialog(R.string.error_media_download_permission, R.string.action_retry, + new View.OnClickListener() { + @Override + public void onClick(View v) { + downloadImage(); + } + }); + } + break; + } + } + } + + private void doErrorDialog(@StringRes int descriptionId, @StringRes int actionId, + View.OnClickListener listener) { + Snackbar bar = Snackbar.make(getActivity().findViewById(R.id.view_media_image), getString(descriptionId), + Snackbar.LENGTH_SHORT); + bar.setAction(actionId, listener); + bar.show(); + } } diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index e9c178f3..b097a1de 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -122,5 +122,6 @@ Kein Webbrowser gefunden. Es konnte kein Login-Token abgerufen werden. Herunterladen + Eine Berechtigung wird zum Speichern des Mediums benötigt. diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 78b16188..c0179be7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -12,6 +12,8 @@ That type of file cannot be uploaded. That file could not be opened. Permission to read media is required. + Permission to store media is required. + Images and videos cannot both be attached to the same status. The upload failed. At least one status must be reported. From e02eb76a9130587e81e848469945e5a1ec2f9e86 Mon Sep 17 00:00:00 2001 From: ntngrtltr Date: Tue, 11 Apr 2017 19:39:30 +0200 Subject: [PATCH 3/3] fix permission requests --- .../com/keylesspalace/tusky/MainActivity.java | 13 +++++++++++++ .../keylesspalace/tusky/ViewMediaFragment.java | 15 +-------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java index dd651af5..ca4130eb 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java @@ -24,8 +24,10 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.PersistableBundle; +import android.support.annotation.NonNull; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.TabLayout; +import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.text.SpannableStringBuilder; import android.text.Spanned; @@ -437,4 +439,15 @@ public class MainActivity extends BaseActivity { viewPager.setCurrentItem(pageHistory.peek()); } } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + List fragments = getSupportFragmentManager().getFragments(); + if (fragments != null) { + for (Fragment fragment : fragments) { + fragment.onRequestPermissionsResult(requestCode, permissions, grantResults); + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/keylesspalace/tusky/ViewMediaFragment.java b/app/src/main/java/com/keylesspalace/tusky/ViewMediaFragment.java index 93e4d576..9b80a1cb 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ViewMediaFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/ViewMediaFragment.java @@ -15,7 +15,6 @@ package com.keylesspalace.tusky; -import android.*; import android.app.AlertDialog; import android.app.DownloadManager; import android.content.Context; @@ -29,7 +28,6 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.StringRes; import android.support.design.widget.Snackbar; -import android.support.v13.app.ActivityCompat; import android.support.v4.app.DialogFragment; import android.support.v4.content.ContextCompat; import android.view.LayoutInflater; @@ -42,7 +40,6 @@ import com.squareup.picasso.Callback; import com.squareup.picasso.Picasso; import java.io.File; -import java.security.Permission; import butterknife.BindView; import butterknife.ButterKnife; @@ -161,10 +158,6 @@ public class ViewMediaFragment extends DialogFragment { super.onDestroyView(); } - /** - * Check permissions and download the thing at getArguments().getString("url") as image, listed in the systems gallery. - * This works in general, but when the permission is granted at runtime, the download button ha to be pressed again (see comment further down) - */ private void downloadImage(){ //Permission stuff @@ -194,21 +187,15 @@ public class ViewMediaFragment extends DialogFragment { } } - /* - * took this from ComposeActivity.java (Media upload) to handle permission requests. - * However, onRequestPermissionResult seems not to be called. - */ @Override public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { - System.out.println("Requestcode: " + requestCode); switch (requestCode) { case PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { downloadImage(); } else { - System.out.println("not granted\n"); doErrorDialog(R.string.error_media_download_permission, R.string.action_retry, new View.OnClickListener() { @Override @@ -224,7 +211,7 @@ public class ViewMediaFragment extends DialogFragment { private void doErrorDialog(@StringRes int descriptionId, @StringRes int actionId, View.OnClickListener listener) { - Snackbar bar = Snackbar.make(getActivity().findViewById(R.id.view_media_image), getString(descriptionId), + Snackbar bar = Snackbar.make(getView(), getString(descriptionId), Snackbar.LENGTH_SHORT); bar.setAction(actionId, listener); bar.show();