-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug WRITE_EXTERNAL_STORAGE permission missing
- Loading branch information
1 parent
e36861c
commit f7d24ca
Showing
5 changed files
with
121 additions
and
4 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
69 changes: 69 additions & 0 deletions
69
app/src/main/java/com/xfrocks/api/androiddemo/common/AndroidPermissions.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,69 @@ | ||
package com.xfrocks.api.androiddemo.common; | ||
|
||
import android.content.Context; | ||
import android.content.pm.PackageManager; | ||
import android.os.Build; | ||
import android.support.v4.app.Fragment; | ||
import android.support.v4.content.ContextCompat; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
public class AndroidPermissions { | ||
// https://gist.github.com/gotev/67c300c563bdf68a502c | ||
|
||
private final Context mContext; | ||
private final String[] mRequiredPermissions; | ||
private final ArrayList<String> mPermissionsToRequest = new ArrayList<>(); | ||
|
||
public AndroidPermissions(Context context, String... requiredPermissions) { | ||
mContext = context; | ||
mRequiredPermissions = requiredPermissions; | ||
} | ||
|
||
public boolean needRequesting() { | ||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) { | ||
return false; | ||
} | ||
|
||
mPermissionsToRequest.clear(); | ||
|
||
for (String permission : mRequiredPermissions) { | ||
if (ContextCompat.checkSelfPermission(mContext, permission) != PackageManager.PERMISSION_GRANTED) { | ||
mPermissionsToRequest.add(permission); | ||
} | ||
} | ||
|
||
return !mPermissionsToRequest.isEmpty(); | ||
|
||
} | ||
|
||
public void requestPermissions(Fragment fragment, int requestCode) { | ||
String[] permissions = mPermissionsToRequest.toArray(new String[mPermissionsToRequest.size()]); | ||
fragment.requestPermissions(permissions, requestCode); | ||
} | ||
|
||
public boolean areAllRequiredPermissionsGranted(String[] permissions, int[] grantResults) { | ||
if (permissions == null || permissions.length == 0 | ||
|| grantResults == null || grantResults.length == 0) { | ||
return false; | ||
} | ||
|
||
LinkedHashMap<String, Integer> perms = new LinkedHashMap<>(); | ||
|
||
for (int i = 0; i < permissions.length; i++) { | ||
if (!perms.containsKey(permissions[i]) | ||
|| (perms.containsKey(permissions[i]) && perms.get(permissions[i]) == PackageManager.PERMISSION_DENIED)) | ||
perms.put(permissions[i], grantResults[i]); | ||
} | ||
|
||
for (Map.Entry<String, Integer> entry : perms.entrySet()) { | ||
if (entry.getValue() != PackageManager.PERMISSION_GRANTED) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
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