PermissionsDispatcher

Ce37cf75fa85b89a33916545978c64de?s=47 @hotchemi
September 07, 2015

 PermissionsDispatcher

kyobashi.dex #1

Ce37cf75fa85b89a33916545978c64de?s=128

@hotchemi

September 07, 2015
Tweet

Transcript

  1. Shintaro  Katafuchi PermissionsDispatcher

  2. I  am… • Shintaro  Katafuchi   • GitHub:  hotchemi  

    • Recruit  Technologies  Co.,Ltd.   • Scrum  Engineering  Group   • kyobashi.dex  ←  Now!!!
  3. Runtime  Permissions

  4. Before

  5. None
  6. After

  7. None
  8. Runtime  Permissions • From  Marshmallow   • User  can  manage

     permissions  at  runtime   • User  can  revoke  permissions  from  setting   • Showing  dialog  →  targeted  M  app   • From  setting  →  even  not  targeted  M  app
  9. Other  stuff… • Permission  groups   • ex)  android.permission-­‐group.CALENDAR  

    • android.permission.READ_CALENDAR   • android.permission.WRITE_CALENDAR   • shouldShowRequestPermissionRationale…?   • A  ton  of  support  library  methods…   • Activity,  ActivityCompat,  PermissionChcker,  ContextCompat,  Fragment,  FragmentCompat…
  10. Code

  11. public  class  MainActivity  extends  AppCompatActivity  {   
 @Override
 protected

     void  onCreate(Bundle  savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);   
        showCamera();                  
    
     }   void  showCamera()  {
        getSupportFragmentManager().beginTransaction()
                        .replace(R.id.sample_content_fragment,  CameraPreviewFragment.newInstance())
                        .addToBackStack("camera")
                        .commitAllowingStateLoss();
 }   }
  12. public  class  MainActivity  extends  AppCompatActivity  {   private  static  final

     int  REQUEST_SHOWCAMERA  =  0;   
 @Override
 protected  void  onCreate(Bundle  savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);          showCamera();   
 
    
     }   void  showCamera()  {
        getSupportFragmentManager().beginTransaction()
                        .replace(R.id.sample_content_fragment,  CameraPreviewFragment.newInstance())
                        .addToBackStack("camera")
                        .commitAllowingStateLoss();
 }   }
  13. public  class  MainActivity  extends  AppCompatActivity  {   private  static  final

     int  REQUEST_SHOWCAMERA  =  0;   private  static  final  String[]  PERMISSION_SHOWCAMERA  =  new  String[]{"android.permission.CAMERA"};   
 @Override
 protected  void  onCreate(Bundle  savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);          showCamera();   
 
    
     }   void  showCamera()  {
        getSupportFragmentManager().beginTransaction()
                        .replace(R.id.sample_content_fragment,  CameraPreviewFragment.newInstance())
                        .addToBackStack("camera")
                        .commitAllowingStateLoss();
 }   }
  14. public  class  MainActivity  extends  AppCompatActivity  {   private  static  final

     int  REQUEST_SHOWCAMERA  =  0;   private  static  final  String[]  PERMISSION_SHOWCAMERA  =  new  String[]{"android.permission.CAMERA"};   
 @Override
 protected  void  onCreate(Bundle  savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);          if  (PermissionUtils.hasSelfPermissions(target,  PERMISSION_SHOWCAMERA))  {
            showCamera();          }  else  {
            if  (PermissionUtils.shouldShowRequestPermissionRationale(target,  PERMISSION_SHOWCAMERA))  {
                Toast.makeText(this,  R.string.permission_camera_rationale,  Toast.LENGTH_SHORT).show();
            }
            ActivityCompat.requestPermissions(target,  PERMISSION_SHOWCAMERA,  REQUEST_SHOWCAMERA);          }     }   void  showCamera()  {
        getSupportFragmentManager().beginTransaction()
                        .replace(R.id.sample_content_fragment,  CameraPreviewFragment.newInstance())
                        .addToBackStack("camera")
                        .commitAllowingStateLoss();
 }   }
  15. public  class  MainActivity  extends  AppCompatActivity  {   private  static  final

     int  REQUEST_SHOWCAMERA  =  0;   private  static  final  String[]  PERMISSION_SHOWCAMERA  =  new  String[]{"android.permission.CAMERA"};   
 @Override
 protected  void  onCreate(Bundle  savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);          if  (PermissionUtils.hasSelfPermissions(target,  PERMISSION_SHOWCAMERA))  {
            showCamera();          }  else  {
            if  (PermissionUtils.shouldShowRequestPermissionRationale(target,  PERMISSION_SHOWCAMERA))  {
                Toast.makeText(this,  R.string.permission_camera_rationale,  Toast.LENGTH_SHORT).show();
            }
            ActivityCompat.requestPermissions(target,  PERMISSION_SHOWCAMERA,  REQUEST_SHOWCAMERA);          }     }   void  showCamera()  {
        getSupportFragmentManager().beginTransaction()
                        .replace(R.id.sample_content_fragment,  CameraPreviewFragment.newInstance())
                        .addToBackStack("camera")
                        .commitAllowingStateLoss();
 }   } @Override
 public  void  onRequestPermissionsResult(int  requestCode,  String[]  permissions,  int[]  grantResults)  {
 switch  (requestCode)  {
        case  REQUEST_SHOWCAMERA:
        if  (PermissionUtils.verifyPermissions(grantResults))  {
            target.showCamera();
        }  else  {              Toast.makeText(this,  R.string.permission_camera_denied,  Toast.LENGTH_SHORT).show();          }
        break;
 }
  16. PermissionsDispatcher

  17. PermissionsDispatcher • Generate  codes  with  annotation  processing   • Support

     over  api  level  4   • Simple  annotation  based  API   • @RuntimePermissions   • @NeedsPermission(s)   • @ShowRationale(s)  ※Option   • @DeniedPermission(s)  ※Option https://github.com/hotchemi/PermissionsDispatcher
  18. public  class  MainActivity  extends  AppCompatActivity  {   
 @Override
 protected

     void  onCreate(Bundle  savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showCamera();   }   void  showCamera()  {
        getSupportFragmentManager().beginTransaction()
                        .replace(R.id.sample_content_fragment,  CameraPreviewFragment.newInstance())
                        .addToBackStack("camera")
                        .commitAllowingStateLoss();
 }   @Override
 public  void  onRequestPermissionsResult(int  requestCode,  String[]  permissions,  int[]  grantResults)  {
  
 }   }
  19. @RuntimePermissions   public  class  MainActivity  extends  AppCompatActivity  {   


    @Override
 protected  void  onCreate(Bundle  savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showCamera();   }   void  showCamera()  {
        getSupportFragmentManager().beginTransaction()
                        .replace(R.id.sample_content_fragment,  CameraPreviewFragment.newInstance())
                        .addToBackStack("camera")
                        .commitAllowingStateLoss();
 }   @Override
 public  void  onRequestPermissionsResult(int  requestCode,  String[]  permissions,  int[]  grantResults)  {
  
 }   }
  20. @RuntimePermissions   public  class  MainActivity  extends  AppCompatActivity  {   


    @Override
 protected  void  onCreate(Bundle  savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showCamera();   }          @NeedsPermission(Manifest.permission.CAMERA)   void  showCamera()  {
        getSupportFragmentManager().beginTransaction()
                        .replace(R.id.sample_content_fragment,  CameraPreviewFragment.newInstance())
                        .addToBackStack("camera")
                        .commitAllowingStateLoss();
 }   @Override
 public  void  onRequestPermissionsResult(int  requestCode,  String[]  permissions,  int[]  grantResults)  {
  
 }   }
  21. @RuntimePermissions   public  class  MainActivity  extends  AppCompatActivity  {   


    @Override
 protected  void  onCreate(Bundle  savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showCamera();   }          @NeedsPermission(Manifest.permission.CAMERA)   void  showCamera()  {
        getSupportFragmentManager().beginTransaction()
                        .replace(R.id.sample_content_fragment,  CameraPreviewFragment.newInstance())
                        .addToBackStack("camera")
                        .commitAllowingStateLoss();
 }   @ShowsRationale(Manifest.permission.CAMERA)
 void  showRationaleForCamera()  {
        Toast.makeText(this,  R.string.permission_camera_rationale,  Toast.LENGTH_SHORT).show();
 }   @Override
 public  void  onRequestPermissionsResult(int  requestCode,  String[]  permissions,  int[]  grantResults)  {
  
 }   }
  22. @RuntimePermissions   public  class  MainActivity  extends  AppCompatActivity  {   


    @Override
 protected  void  onCreate(Bundle  savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showCamera();   }          @NeedsPermission(Manifest.permission.CAMERA)   void  showCamera()  {
        getSupportFragmentManager().beginTransaction()
                        .replace(R.id.sample_content_fragment,  CameraPreviewFragment.newInstance())
                        .addToBackStack("camera")
                        .commitAllowingStateLoss();
 }   @ShowsRationale(Manifest.permission.CAMERA)
 void  showRationaleForCamera()  {
        Toast.makeText(this,  R.string.permission_camera_rationale,  Toast.LENGTH_SHORT).show();
 }   @DeniedPermission(Manifest.permission.CAMERA)
 void  deniedPermissionForCamera()  {
        Toast.makeText(this,  R.string.permission_camera_denied,  Toast.LENGTH_SHORT).show();
 }   @Override
 public  void  onRequestPermissionsResult(int  requestCode,  String[]  permissions,  int[]  grantResults)  {
  
 }   }
  23. @RuntimePermissions   public  class  MainActivity  extends  AppCompatActivity  {   


    @Override
 protected  void  onCreate(Bundle  savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showCamera();   }          @NeedsPermission(Manifest.permission.CAMERA)   void  showCamera()  {
        getSupportFragmentManager().beginTransaction()
                        .replace(R.id.sample_content_fragment,  CameraPreviewFragment.newInstance())
                        .addToBackStack("camera")
                        .commitAllowingStateLoss();
 }   @ShowsRationale(Manifest.permission.CAMERA)
 void  showRationaleForCamera()  {
        Toast.makeText(this,  R.string.permission_camera_rationale,  Toast.LENGTH_SHORT).show();
 }   @DeniedPermission(Manifest.permission.CAMERA)
 void  deniedPermissionForCamera()  {
        Toast.makeText(this,  R.string.permission_camera_denied,  Toast.LENGTH_SHORT).show();
 }   @Override
 public  void  onRequestPermissionsResult(int  requestCode,  String[]  permissions,  int[]  grantResults)  {
    MainActivityPermissionsDispatcher.onRequestPermissionsResult(this,  requestCode,  grantResults);
 }   }
  24. @RuntimePermissions   public  class  MainActivity  extends  AppCompatActivity  {   


    @Override
 protected  void  onCreate(Bundle  savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
           }          @NeedsPermission(Manifest.permission.CAMERA)   void  showCamera()  {
        getSupportFragmentManager().beginTransaction()
                        .replace(R.id.sample_content_fragment,  CameraPreviewFragment.newInstance())
                        .addToBackStack("camera")
                        .commitAllowingStateLoss();
 }   @ShowsRationale(Manifest.permission.CAMERA)
 void  showRationaleForCamera()  {
        Toast.makeText(this,  R.string.permission_camera_rationale,  Toast.LENGTH_SHORT).show();
 }   @DeniedPermission(Manifest.permission.CAMERA)
 void  deniedPermissionForCamera()  {
        Toast.makeText(this,  R.string.permission_camera_denied,  Toast.LENGTH_SHORT).show();
 }   @Override
 public  void  onRequestPermissionsResult(int  requestCode,  String[]  permissions,  int[]  grantResults)  {
    MainActivityPermissionsDispatcher.onRequestPermissionsResult(this,  requestCode,  grantResults);
 }   } MainActivityPermissionsDispatcher.showCameraWithCheck(this);
  25. package  permissions.dispatcher.sample;
 
 import  android.support.v4.app.ActivityCompat;
 import  java.lang.String;
 import  permissions.dispatcher.PermissionUtils;
 


    public  final  class  MainActivityPermissionsDispatcher  {
    private  static  final  int  REQUEST_SHOWCAMERA  =  0;
 
    private  static  final  String[]  PERMISSION_SHOWCAMERA  =  new  String[]{"android.permission.CAMERA"};
 
    private  MainActivityPermissionsDispatcher()  {
    }
 
    public  static  void  showCameraWithCheck(MainActivity  target)  {
        if  (PermissionUtils.hasSelfPermissions(target,  PERMISSION_SHOWCAMERA))  {
            target.showCamera();
        }  else  {
            if  (PermissionUtils.shouldShowRequestPermissionRationale(target,  PERMISSION_SHOWCAMERA))  {
                target.showRationaleForCamera();
            }
            ActivityCompat.requestPermissions(target,  PERMISSION_SHOWCAMERA,  REQUEST_SHOWCAMERA);
        }
    }
 
    public  static  void  onRequestPermissionsResult(MainActivity  target,  int  requestCode,  int[]  grantResults)  {
        switch  (requestCode)  {
            case  REQUEST_SHOWCAMERA:
            if  (PermissionUtils.verifyPermissions(grantResults))  {
                target.showCamera();
            }
            break;
            case  REQUEST_SHOWCONTACTS:
            if  (PermissionUtils.verifyPermissions(grantResults))  {
                target.showContacts();
            }
            break;
            default:
            break;
        }
    }
 } Generated  code
  26. Milestone • 1.2.1…  existing  bug  fix   • 2.0.0…  Rewriting

     processor  in  Kotlin   • Thanks  @aurae!
  27. Practice • 1.Finding   • https://github.com/hotchemi/m-­‐permissions-­‐checker   • 2.Handling  

    • https://github.com/hotchemi/permissionsdispatcher   • That’s  it!
  28. Thanks.   Any  questions?