-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#8: Implemented Android-6 Runtime permission request.
- Loading branch information
Showing
20 changed files
with
155 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
app/src/main/java/de/k3b/android/ToGoZip/PermissionHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright (C) 2017-2018 k3b | ||
* | ||
* This file is part of de.k3b.android.toGoZip (https://github.com/k3b/ToGoZip/) . | ||
* | ||
* This program is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <http://www.gnu.org/licenses/> | ||
*/ | ||
package de.k3b.android.toGoZip; | ||
|
||
import android.Manifest; | ||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.content.pm.PackageManager; | ||
import android.os.Build; | ||
import android.support.annotation.NonNull; | ||
import android.support.v4.app.ActivityCompat; | ||
import android.util.Log; | ||
import android.widget.Toast; | ||
|
||
/** | ||
* Support for android-6.0 (M) ff runtime permissons. | ||
* Created by k3b on 23.12.2017. | ||
* | ||
* implements ActivityCompat.OnRequestPermissionsResultCallback | ||
*/ | ||
|
||
public class PermissionHelper { | ||
/** | ||
* Id to identify a Storage permission request. | ||
*/ | ||
private static final int REQUEST_CODE_STORAGE = 215; | ||
|
||
/** | ||
* Permissions required to read and write Storage. | ||
*/ | ||
private static String[] PERMISSIONS_STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE, | ||
Manifest.permission.WRITE_EXTERNAL_STORAGE}; | ||
|
||
/** | ||
* called before permission is required. | ||
* | ||
* @return true if has-permissions. else false and request permissions | ||
* */ | ||
public static boolean hasPermissionOrRequest(Activity context) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
boolean needsRead = ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) | ||
!= PackageManager.PERMISSION_GRANTED; | ||
|
||
boolean needsWrite = ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) | ||
!= PackageManager.PERMISSION_GRANTED; | ||
|
||
if (needsRead || needsWrite) { | ||
// Storage permission has not been requeste yet. Request for first time. | ||
ActivityCompat.requestPermissions(context, PERMISSIONS_STORAGE, REQUEST_CODE_STORAGE); | ||
// no permission yet | ||
return false; | ||
} | ||
} // if android-m | ||
|
||
// already has permission. | ||
return true; | ||
} | ||
|
||
/** | ||
* called in onRequestPermissionsResult(). | ||
* | ||
* @return true if just received permissions. else false and calling finish | ||
*/ | ||
public static boolean receivedPermissionsOrFinish(Activity activity, int requestCode, | ||
@NonNull String[] permissions, @NonNull int[] grantResults) { | ||
if (requestCode == REQUEST_CODE_STORAGE) { | ||
if ( (grantResults.length == 2) | ||
&& (grantResults[0] == PackageManager.PERMISSION_GRANTED) | ||
&& (grantResults[1] == PackageManager.PERMISSION_GRANTED)) { | ||
return true; | ||
} else { | ||
String format = activity.getString(R.string.ERR_NO_WRITE_PERMISSIONS); | ||
|
||
String msg = String.format( | ||
format, | ||
"", | ||
""); | ||
|
||
activity.finish(); | ||
Toast.makeText(activity, msg, Toast.LENGTH_LONG).show(); | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters