anthonycr / grant Goto Github PK
View Code? Open in Web Editor NEWSimplifying Android Permissions
License: Apache License 2.0
Simplifying Android Permissions
License: Apache License 2.0
Can you check this? https://commonsware.com/blog/2015/11/09/you-cannot-hold-nonexistent-permissions.html Can this be part of the library or should I check it myself?
Shouldn't PermissionsManager.hasPermission method condsider the fix you have made about old versions of Android ? Change log was : Fixed bug where new permissions requested on old Android versions were denied (optional method can change this).
I noticed that you used the new HashSet<>(1)
instead of new HashSet<>()
to initialize a HashSet
,is there any profits by doing that?
I read the source code and find that new HashSet<>(1)
or new HashSet<>()
neither really initialize a HashMapEntry
array,it's initialized when method V put(K,V)
was first called by void inflateTable(int size)
.
AM I WRONG OR MISSED?
I think ,this way perhaps better:
public static PermissionsManager getInstance() {
return PermissionsManagerHolder.mInstance;
}
private static class PermissionsManagerHolder{
private static final PermissionsManager mInstance = new PermissionsManager();
}
Jcenter will shutdown
Hello, can you add a Log output switch?
Can ActivityCompat.shouldShowRequestPermissionRationale() be implemented for cases where user denies permission in first attempt?
See http://developer.android.com/reference/android/support/v4/app/ActivityCompat.html#shouldShowRequestPermissionRationale(android.app.Activity, java.lang.String)
I am trying to do:
PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(getActivity(),
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new PermissionsResultAction() {
@Override
public void onGranted() {
Intent taskPicturesStarter = new Intent(getActivity(), TaskPicturesActivity.class);
taskPicturesStarter.putExtra(ActivityHelper.KEY_TASK_MODEL, mTaskModel);
startActivityForResult(taskPicturesStarter, DATE_TIME_PICKER_ACTIVITY_CODE);
}
@Override
public void onDenied(String permission) {
Toast.makeText(getActivity(),
"Sorry, we need the Storage Permission to do that",
Toast.LENGTH_SHORT).show();
}
});
But onGranted or onDenied method are not executing.
In the fragment i have also included:
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
PermissionsManager.getInstance().notifyPermissionsChange(permissions, grantResults);
}
I have also tried reinstalling the app but still same problem.
I was thinking on a variation of PermissionsManager.hasAllPermissions
that only takes a context and retrieves all the app permissions out of the package info, just like PermissionsManager.requestAllManifestPermissionsIfNecessary
Just by looking at the code, it could be something like the following
public static boolean hasAllPermissions( @Nullable Context context ) {
if (context == null) {
return false;
}
PackageInfo packageInfo = null;
try {
Log.d(TAG, context.getPackageName());
packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "A problem occurred when retrieving permissions", e);
}
boolean hasAllPermissions = false;
if (packageInfo != null) {
hasAllPermissions = true;
String[] permissions = packageInfo.requestedPermissions;
if (permissions != null) {
for (String perm : permissions) {
hasAllPermissions &= ActivityCompat.checkSelfPermission(context, perm)
== PackageManager.PERMISSION_GRANTED;
}
}
}
return hasAllPermissions;
}
Disclaimer: I have not tested the code nor even compiled it, this is just an idea. I will be happy to contribute properly if the idea is good enough.
Thanks
According to java doc it should be called internally. It will lead to memory leaks if you create annonymous PermissionsResultAction in Activity class.
final
. In my point of view, I think no class will inherit the Manager class. If they inherit it, the developer have the duty to make sure his/her inherited class can work in right way.Reference:
#7
PermissionsManager.java
private final List mPendingActions = new ArrayList<>(1);
PermissionsResultAction.java
private final Set mPermissions = new HashSet<>(1);
我不会说中文,我不说了
i use LeakCanary checked it case memory leak , i I suggest add a method to remove the callback or use WeakReference
Right now, as it is written, your current PermissionsResultAction list is creating strong references to said elements. This creates issues in that when creating an anonymous callback from an Activity, it implicitly creates a reference to that Activity (according to your example). As a result, it prevents it from garbage collection, until it is eventually removed from that list.
This problem is further compounded if one were to change the device's orientation multiple times, creating multiple adds from previous Activity instances, thus keeping strong refs to them, and thus leaking memory heavily.
My suggestion would be to wrap each PermissionsResultAction element in your List as a WeakReference:
private final List<WeakReference<PermissionsResultAction>> mPendingActions = new ArrayList<>(1);
Then, when adding new actions, you would simply do the following:
mPendingActions.add(new WeakReference<PermissionsResultAction>(action));
And finally, when calling back to them, you would simply check if there's currently a valid reference to them:
while (iterator.hasNext()) {
final WeakReference<PermissionsResultAction> callbackRef = iterator.next();
final PermissionsResultAction callback= callbackRef.get();
if (callback != null && callback.onResult(permissions[n], results[n])) {
iterator.remove();
}
mPendingRequests.remove(permissions[n]);
}
As a result, you create a clean break from your observers that may otherwise not exist when it comes time to call them, and enable their references to get GC'd quickly.
Note that iterator block was my best guess in handling when to remove that Iterator's element given this new model. You may have to polish it slightly to determine when it gets removed given the valid state of a callback reference.
I only get one onDenied result if I pass permission requests from different groups.
In my use of grant, I met a problem that, when user spend more than 10 seconds or more time to decide whether to give the permission, references in the action list will be GCed, actions get from iterator will return null and will be removed from the list. In that situation, no followed code will be executed, callbacks will NEVER be executed.
For the case, I changed the weakreference list to strong reference objects list as an alternative way, thus suffered from memory leaked risk, but it just worked fine for me and infact you do not have that much dangerous permissions to gain permission.
Hope for a more memory safe way to handle this problem.
I am trying to implement following code in my app -
PermissionsManager.getInstance().requestAllManifestPermissionsIfNecessary(this,
new PermissionsResultAction() {
@OverRide
public void onGranted() {
// Proceed with initialization
proceed();
}
@Override
public void onDenied(String permission) {
// Notify the user that you need all of the permissions
}
});
But what is happening is proceed() method is not calling after granting all the permissions. onGrant method itself not getting called when I tried to debug the app. Please help to resolve my issue.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.