Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PermissionsDispatcher

@hotchemi
September 07, 2015

 PermissionsDispatcher

kyobashi.dex #1

@hotchemi

September 07, 2015
Tweet

More Decks by @hotchemi

Other Decks in Programming

Transcript

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

    • Recruit  Technologies  Co.,Ltd.   • Scrum  Engineering  Group   • kyobashi.dex  ←  Now!!!
  2. 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
  3. 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…
  4. 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();
 }   }
  5. 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();
 }   }
  6. 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();
 }   }
  7. 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();
 }   }
  8. 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;
 }
  9. 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
  10. 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)  {
  
 }   }
  11. @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)  {
  
 }   }
  12. @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)  {
  
 }   }
  13. @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)  {
  
 }   }
  14. @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)  {
  
 }   }
  15. @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);
 }   }
  16. @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);
  17. 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
  18. Milestone • 1.2.1…  existing  bug  fix   • 2.0.0…  Rewriting

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

    • https://github.com/hotchemi/permissionsdispatcher   • That’s  it!