add support for runtime-permissions (fix needed)

This commit is contained in:
ntngrtltr 2017-04-07 13:11:31 +02:00
parent 57f829e58e
commit 2c94ee9c2e
3 changed files with 85 additions and 13 deletions

View File

@ -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();
}
}

View File

@ -122,5 +122,6 @@
<string name="error_no_web_browser_found">Kein Webbrowser gefunden.</string>
<string name="error_retrieving_oauth_token">Es konnte kein Login-Token abgerufen werden.</string>
<string name="dialog_download_image">Herunterladen</string>
<string name="error_media_download_permission">Eine Berechtigung wird zum Speichern des Mediums benötigt.</string>
</resources>

View File

@ -12,6 +12,8 @@
<string name="error_media_upload_type">That type of file cannot be uploaded.</string>
<string name="error_media_upload_opening">That file could not be opened.</string>
<string name="error_media_upload_permission">Permission to read media is required.</string>
<string name="error_media_download_permission">Permission to store media is required.</string>
<string name="error_media_upload_image_or_video">Images and videos cannot both be attached to the same status.</string>
<string name="error_media_upload_sending">The upload failed.</string>
<string name="error_report_too_few_statuses">At least one status must be reported.</string>