Slide 15
Slide 15 text
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;
}