Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-20320] Android: Provide generic requestPermission(s) method

GitHub Issuen/a
TypeImprovement
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2016-04-19T02:39:33.000+0000
Affected Version/sn/a
Fix Version/sRelease 5.4.0, Release 5.3.2
ComponentsAndroid
Labelspermissions
ReporterFokke Zandbergen
AssigneeHieu Pham
Created2016-02-03T17:22:12.000+0000
Updated2016-07-04T08:33:29.000+0000

Description

We need a generic method to request permissions that our own APIs do not use. I know we've discussed this and thought this was best to leave this to the modules that require them, but there are multiple reasons for having a generic one instead: * So that module doesn't come up with different ways (method name, method arguments, callback/event response and payload) * So that developers don't need to rely on maintainers of 3rd party modules to update their modules to support Android 6.0 runtime permissions Also see the discussion in TIMOB-20144

Comments

  1. Fokke Zandbergen 2016-02-03

    /cc [~titanium@webmasterei-hamburg.de]
  2. Rainer Schleevoigt 2016-02-03

    +1 (for i.e. titutorial.audiorecorder)
  3. Rainer Schleevoigt 2016-02-10

    I think the best way is to make a PR for TiBaseActivity for extending perfmissions.
  4. Fokke Zandbergen 2016-02-26

    We could integrate this module: https://github.com/stgrosshh/tipermissions
  5. Rainer Schleevoigt 2016-02-27

    @fokke Currently we (Stefan and me) test and improve the module. Though the coder needs a special titanium.jar in his SDK5.2.0.GA. This jar contains the PR and can find here: https://github.com/AppWerft/Tierstimmenarchiv/blob/master/modules/android/titanium.jar screenshot: !http://i.imgur.com/LUPmlPN.jpg!
  6. Fokke Zandbergen 2016-02-27

    So it requires a change in the Titanium SDK. Could you open up the source for that change as well?
  7. Rainer Schleevoigt 2016-02-27

    https://github.com/appcelerator/titanium_mobile/pull/7778
  8. Jörgen Buder 2016-03-01

    Hi We are working to support permissions in our Android 6.0 support for our client as we speak, do you know when this will be available in the nightly builds? It seems you are working actively on it right now? And it is so much easier for us to use your nightly build than to patch the SDK our selves.. Thanks
  9. Hans Knöchel 2016-03-01

    [~buder], the PR is not merged,but the Android team is currently reviewing it. As there are many other features and issues to take care about, we cannot provide a concrete timeline for now, but will keep this ticket updated.
  10. Rainer Schleevoigt 2016-03-01

    @jörgen for quick solution I can send you the new android.jar for copying into android folder of your 5.2.0.GA folder.
  11. Jörgen Buder 2016-03-01

    or maybe you need better transfer ? Drop box?
  12. Rainer Schleevoigt 2016-03-01

    You find it here: https://github.com/AppWerft/Tierstimmenarchiv/tree/master/modules/android You email doesn't work.
  13. Jörgen Buder 2016-03-01

    Thank you so much..
  14. Fokke Zandbergen 2016-03-14

    [~morahman] just came up with another important use case for such a generic method. On Android, it would allow developers to ask for all permissions the app needs at once.
  15. Rainer Schleevoigt 2016-04-06

    With the new module you can requests all permissions in one dialog. First parameter is array of permissions. Android6.0 has a bug: although permissions are confirmed the dialog appears. In Android 6.0.1 it is fixed. As workaround we suggest a JS-module ti.permissions: https://github.com/AppWerft/Tierstimmenarchiv/blob/master/Resources/vendor/permissions.js
  16. Hieu Pham 2016-04-14

    refactored PR: https://github.com/appcelerator/titanium_mobile/pull/7948
  17. Fokke Zandbergen 2016-04-15

    [~hpham] could you add a sample of how this would work for the developer?
  18. Rainer Schleevoigt 2016-04-15

    Yes, in most cases (compatibility with iOS and bug in A6.0.0) you need this JS-wrapper:
        var TiPermissions = require('ti.permissions');
        exports.requestPermissions = function(_permissions, _callback) {
        	if (Ti.Platform.osname != 'android') {
        		_callback(true);
        		return;
        	}
        var permissions = (Array.isArray(_permissions) ? _permissions : [_permissions]).map(function(perm) {
        		return (perm.match(/^android\.permission\./)) ? perm : 'android.permission.' + perm;
        	});
        	var grantedpermissions = 0;
        	permissions.forEach(function(perm) {
        		if (TiPermissions.hasPermission(perm)) 
        			grantedpermissions++;
        		
        		if (grantedpermissions == permissions.length)
        			_callback(true);
        	});
        	if (grantedpermissions < permissions.length) {
        		TiPermissions.requestPermissions(permissions, function(_e) {
        			_callback(_e.success);
        		});
        	}
        };
        
    Now you can use in app:
        require('lib/tipermissions').requestPermissions(['WRITE_EXTERNAL_STORAGE','RECORD_AUDIO'],function(){
          if (arguments[0]==true) {
           // do critical things
        } else alert('you must confirm to enjoy the fancy shit')
        });
        
  19. Rainer Schleevoigt 2016-04-15

    The formatter makes trouble or I don't know how.
  20. Fokke Zandbergen 2016-04-15

    [~titanium@webmasterei-hamburg.de] thanks but I meant how the builtin support [~hpham] is adding will be used.
  21. Hieu Pham 2016-04-18

    Link to PR again: https://github.com/appcelerator/titanium_mobile/pull/7948 Implemented 2 new methods hasPermission() and requestPermissions(). (please read documentation in the PR for usage). Sample usage code:
        var cameraPermission = "android.permission.CAMERA";
        var storagePermission = "android.permission.READ_EXTERNAL_STORAGE";
        var hasCameraPerm = Ti.Android.hasPermission(cameraPermission);
        var hasStoragePerm = Ti.Android.hasPermission(storagePermission);
        var permissionsToRequest = [];
        if (!hasCameraPerm) {
        	permissionsToRequest.push(cameraPermission);
        }
        if (!hasStoragePerm) {
        	permissionsToRequest.push(storagePermission);
        }
        if (permissionsToRequest.length > 0) {
        	Ti.Android.requestPermissions(permissionsToRequest, function(e) {
            	if (e.success) {
            		Ti.API.info("SUCCESS");
            	} else {
            		Ti.API.info("ERROR: " + e.error);
            	}
        	});
        }
        
  22. Fokke Zandbergen 2016-04-20

    We could also use this library: https://github.com/00ec454/Ask
  23. Ashraf Abu 2016-04-20

    I'm generally not in favour of adding in an external library if possible to reduce the complexity of dependencies if this can be done without an external library.
  24. Fokke Zandbergen 2016-04-20

    Agree
  25. Lokesh Choudhary 2016-06-14

    Verified the implementation of the 2 methods hasPermission() & requestPermissions(). According to docs in the PR, permissions should be included in the android manifest for the successful granting of the required permissions. Closing. Environment: Appc Studio : 4.6.0.201605201934 Ti SDK : 5.4.0.v20160608165242 Ti CLI : 5.0.8 Alloy : 1.8.7 MAC El Capitan : 10.11.4 Appc NPM : 4.2.7-2 Appc CLI : 5.4.0-15 Node: 4.4.4 Nexus 6 - Android 6.0.0
  26. Quang Pham 2016-07-04

  27. Quang Pham 2016-07-04

    Environment: Titanium SDK 5.4.0.v20160703211246 OSX Nexus 7 2013 - Android 6.0.1
  28. Ashraf Abu 2016-07-04

    [~ptquang86] What do you mean by being unable to click the Allow Button? Do you have a screenshot?
  29. Quang Pham 2016-07-04

    I mean the button Allow is freeze, not click able https://goo.gl/photos/vpsDg7HagD4KGNZKA It works on Genymotion
  30. Ashraf Abu 2016-07-04

    Thanks for the photo. Based on the information, I do not think there is any bug or fault in the Titanium SDK. What's being done is simply calling the native Android method to bring up that requestPermission dialog. Nothing special is being done. Could it be that the device has some issues? Do other apps that require permission also have that button frozen? This seems very odd.
  31. Rainer Schleevoigt 2016-07-04

    @ashraf You are right. This is not in relation to Titanium. The module only opens a "native" system dialog. Maybe a problem with Genymotion. I'm only work on devices and never seen this issue.
  32. Quang Pham 2016-07-04

    yes, this is an issue of my device. I tested with facebook and this happened. thank you :)
  33. Ashraf Abu 2016-07-04

    [~ptquang86] (y) Thanks [~titanium@webmasterei-hamburg.de]

JSON Source