mirror of
https://github.com/nextcloud/android.git
synced 2024-11-28 02:17:43 +03:00
refactored permission handling to separate class
This commit is contained in:
parent
55b4952687
commit
999ad9f355
3 changed files with 77 additions and 24 deletions
|
@ -100,6 +100,7 @@ import com.owncloud.android.ui.preview.PreviewVideoActivity;
|
||||||
import com.owncloud.android.utils.DisplayUtils;
|
import com.owncloud.android.utils.DisplayUtils;
|
||||||
import com.owncloud.android.utils.ErrorMessageAdapter;
|
import com.owncloud.android.utils.ErrorMessageAdapter;
|
||||||
import com.owncloud.android.utils.FileStorageUtils;
|
import com.owncloud.android.utils.FileStorageUtils;
|
||||||
|
import com.owncloud.android.utils.PermissionUtil;
|
||||||
import com.owncloud.android.utils.UriUtils;
|
import com.owncloud.android.utils.UriUtils;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -112,7 +113,6 @@ public class FileDisplayActivity extends HookActivity
|
||||||
implements FileFragment.ContainerActivity,
|
implements FileFragment.ContainerActivity,
|
||||||
OnSslUntrustedCertListener, OnEnforceableRefreshListener {
|
OnSslUntrustedCertListener, OnEnforceableRefreshListener {
|
||||||
|
|
||||||
private static final int PERMISSIONS_WRITE_EXTERNAL_STORAGE = 1;
|
|
||||||
private SyncBroadcastReceiver mSyncBroadcastReceiver;
|
private SyncBroadcastReceiver mSyncBroadcastReceiver;
|
||||||
private UploadFinishReceiver mUploadFinishReceiver;
|
private UploadFinishReceiver mUploadFinishReceiver;
|
||||||
private DownloadFinishReceiver mDownloadFinishReceiver;
|
private DownloadFinishReceiver mDownloadFinishReceiver;
|
||||||
|
@ -210,33 +210,26 @@ public class FileDisplayActivity extends HookActivity
|
||||||
protected void onPostCreate(Bundle savedInstanceState) {
|
protected void onPostCreate(Bundle savedInstanceState) {
|
||||||
super.onPostCreate(savedInstanceState);
|
super.onPostCreate(savedInstanceState);
|
||||||
|
|
||||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
if (PermissionUtil.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
||||||
!= android.content.pm.PackageManager.PERMISSION_GRANTED) {
|
|
||||||
// Check if we should show an explanation
|
// Check if we should show an explanation
|
||||||
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
|
if (PermissionUtil.shouldShowRequestPermissionRationale(this,
|
||||||
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
||||||
|
|
||||||
// write access isn't considered a rationale enforced permission in 23
|
|
||||||
// code is still in place in case this changes in the future
|
|
||||||
|
|
||||||
// Show explanation to the user and then request permission
|
// Show explanation to the user and then request permission
|
||||||
Snackbar snackbar = Snackbar.make(findViewById(R.id.ListLayout), R.string.permission_storage_access,
|
Snackbar snackbar = Snackbar.make(findViewById(R.id.ListLayout), R.string.permission_storage_access,
|
||||||
Snackbar.LENGTH_INDEFINITE)
|
Snackbar.LENGTH_INDEFINITE)
|
||||||
.setAction(R.string.common_ok, new View.OnClickListener() {
|
.setAction(R.string.common_ok, new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
requestWriteExternalStoreagePermission();
|
PermissionUtil.requestWriteExternalStoreagePermission(FileDisplayActivity.this);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Changing action button text color
|
DisplayUtils.colorSnackbar(this, snackbar);
|
||||||
View sbView = snackbar.getView();
|
|
||||||
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_action);
|
|
||||||
textView.setTextColor(ContextCompat.getColor(this, R.color.white));
|
|
||||||
snackbar.show();
|
snackbar.show();
|
||||||
} else {
|
} else {
|
||||||
// No explanation needed, request the permission.
|
// No explanation needed, request the permission.
|
||||||
requestWriteExternalStoreagePermission();
|
PermissionUtil.requestWriteExternalStoreagePermission(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -250,20 +243,11 @@ public class FileDisplayActivity extends HookActivity
|
||||||
setBackgroundText();
|
setBackgroundText();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* request the write permission for external storage.
|
|
||||||
*/
|
|
||||||
private void requestWriteExternalStoreagePermission() {
|
|
||||||
ActivityCompat.requestPermissions(this,
|
|
||||||
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
|
|
||||||
PERMISSIONS_WRITE_EXTERNAL_STORAGE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRequestPermissionsResult(int requestCode,
|
public void onRequestPermissionsResult(int requestCode,
|
||||||
String permissions[], int[] grantResults) {
|
String permissions[], int[] grantResults) {
|
||||||
switch (requestCode) {
|
switch (requestCode) {
|
||||||
case PERMISSIONS_WRITE_EXTERNAL_STORAGE: {
|
case PermissionUtil.PERMISSIONS_WRITE_EXTERNAL_STORAGE: {
|
||||||
// If request is cancelled, result arrays are empty.
|
// If request is cancelled, result arrays are empty.
|
||||||
if (grantResults.length > 0
|
if (grantResults.length > 0
|
||||||
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
|
|
@ -28,10 +28,14 @@ import android.content.Context;
|
||||||
import android.graphics.Point;
|
import android.graphics.Point;
|
||||||
import android.graphics.PorterDuff;
|
import android.graphics.PorterDuff;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
import android.support.design.widget.Snackbar;
|
||||||
|
import android.support.v4.content.ContextCompat;
|
||||||
import android.text.format.DateUtils;
|
import android.text.format.DateUtils;
|
||||||
import android.view.Display;
|
import android.view.Display;
|
||||||
|
import android.view.View;
|
||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
import android.widget.SeekBar;
|
import android.widget.SeekBar;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.owncloud.android.MainApp;
|
import com.owncloud.android.MainApp;
|
||||||
import com.owncloud.android.R;
|
import com.owncloud.android.R;
|
||||||
|
@ -278,4 +282,16 @@ public class DisplayUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set the owncloud standard colors for the snackbar.
|
||||||
|
*
|
||||||
|
* @param context the context relevant for setting the color according to the context's theme
|
||||||
|
* @param snackbar the snackbar to be colored
|
||||||
|
*/
|
||||||
|
public static void colorSnackbar(Context context, Snackbar snackbar) {
|
||||||
|
// Changing action button text color
|
||||||
|
View sbView = snackbar.getView();
|
||||||
|
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_action);
|
||||||
|
textView.setTextColor(ContextCompat.getColor(context, R.color.white));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
53
src/com/owncloud/android/utils/PermissionUtil.java
Normal file
53
src/com/owncloud/android/utils/PermissionUtil.java
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
package com.owncloud.android.utils;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.support.v4.app.ActivityCompat;
|
||||||
|
import android.support.v4.content.ContextCompat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by scherzia on 29.12.2015.
|
||||||
|
*/
|
||||||
|
public class PermissionUtil {
|
||||||
|
public static final int PERMISSIONS_WRITE_EXTERNAL_STORAGE = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper method for ContextCompat.checkSelfPermission().
|
||||||
|
* Determine whether <em>the app</em> has been granted a particular permission.
|
||||||
|
*
|
||||||
|
* @param permission The name of the permission being checked.
|
||||||
|
*
|
||||||
|
* @return <code>true</code> if app has the permission, or <code>false</code> if not.
|
||||||
|
*/
|
||||||
|
public static boolean checkSelfPermission(Context context, String permission) {
|
||||||
|
return ContextCompat.checkSelfPermission(context, permission)
|
||||||
|
!= android.content.pm.PackageManager.PERMISSION_GRANTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper method for ActivityCompat.shouldShowRequestPermissionRationale().
|
||||||
|
* Gets whether you should show UI with rationale for requesting a permission.
|
||||||
|
* You should do this only if you do not have the permission and the context in
|
||||||
|
* which the permission is requested does not clearly communicate to the user
|
||||||
|
* what would be the benefit from granting this permission.
|
||||||
|
*
|
||||||
|
* @param activity The target activity.
|
||||||
|
* @param permission A permission to be requested.
|
||||||
|
* @return Whether to show permission rationale UI.
|
||||||
|
*/
|
||||||
|
public static boolean shouldShowRequestPermissionRationale(Activity activity, String permission) {
|
||||||
|
return ActivityCompat.shouldShowRequestPermissionRationale(activity, permission);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* request the write permission for external storage.
|
||||||
|
*
|
||||||
|
* @param activity The target activity.
|
||||||
|
*/
|
||||||
|
public static void requestWriteExternalStoreagePermission(Activity activity) {
|
||||||
|
ActivityCompat.requestPermissions(activity,
|
||||||
|
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
|
||||||
|
PERMISSIONS_WRITE_EXTERNAL_STORAGE);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue