SHARE
TWEET

Untitled

a guest Nov 15th, 2017 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.content.pm.PackageManager;
  2. import android.support.annotation.NonNull;
  3. import android.support.v4.app.ActivityCompat;
  4. import android.support.v4.content.ContextCompat;
  5. import android.support.v7.app.AppCompatActivity;
  6.  
  7. import java.util.Arrays;
  8. import java.util.Iterator;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11.  
  12. /**
  13.  * Created by Abhishek3.patil on 19-04-2017.
  14.  */
  15.  
  16. public class PermissionsHelperActivity extends AppCompatActivity
  17. {
  18.     private int                                 PERMISSION_REQUEST_CODE;
  19.     private IPermissionRequest                  mIPermissionRequest;
  20.  
  21.     public interface IPermissionRequest {
  22.         void onPermissionGranted(boolean allowed, int requestCode);
  23.     }
  24.    
  25.     public void askForPermission(String[] permissions, int requestCode)
  26.     {
  27.         PERMISSION_REQUEST_CODE = requestCode;
  28.         List<String> permissionList = new LinkedList<>(Arrays.asList(permissions));
  29.  
  30.         Iterator<String> iterator = permissionList.iterator();
  31.         while(iterator.hasNext()) {
  32.             String permission = iterator.next();
  33.             //first, check the permission is already granted or not. If yes, remove it from permission list.
  34.             boolean check = ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED;
  35.             if(check) {
  36.                 iterator.remove();
  37.             }
  38.         }
  39.  
  40.         if (permissionList.size() > 0) {
  41.             ActivityCompat.requestPermissions(this, permissionList.toArray(new String[permissionList.size()]), requestCode);
  42.         } else {
  43.             notifyListener(true, requestCode);
  44.         }
  45.     }
  46.  
  47.     public void setPermissionRequestListener(IPermissionRequest listener) {
  48.         mIPermissionRequest = listener;
  49.     }
  50.  
  51.     @Override
  52.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  53.        
  54.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  55.        
  56.         if (requestCode == PERMISSION_REQUEST_CODE && grantResults.length > 0) {
  57.             if (isPermissionDenied(grantResults, PackageManager.PERMISSION_DENIED)) {
  58.                 notifyListener(false, PERMISSION_REQUEST_CODE);
  59.             } else {
  60.                 notifyListener(true, PERMISSION_REQUEST_CODE);
  61.             }
  62.         }
  63.     }
  64.  
  65.     private boolean isPermissionDenied(final int[] grantResults, final int granted) {
  66.         boolean result = false;
  67.         for(int i : grantResults) {
  68.             if(i == granted) {
  69.                 result = true;
  70.                 break;
  71.             }
  72.         }
  73.         return result;
  74.     }
  75.  
  76.     private void notifyListener(boolean allowed, int requestCode) {
  77.         if(mIPermissionRequest != null) {
  78.             mIPermissionRequest.onPermissionGranted(allowed, requestCode);
  79.         }
  80.     }
  81. }
RAW Paste Data
Top