Skip to content

Commit

Permalink
Android: move permission asking from onCreate to onConnected
Browse files Browse the repository at this point in the history
Closes #229
  • Loading branch information
bk138 committed Oct 17, 2023
1 parent 060a1eb commit fa90488
Showing 1 changed file with 42 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ public void onSystemUiVisibilityChange(int visibility) {
* Setup canvas and conn.
*/
VNCConn conn = new VNCConn(vncCanvas, vncCanvas);
// the Android 13 permission launcher and callback handler
ActivityResultLauncher<String> requestPermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
// show UI _after_ all the permission handling
showHelpDialog();
VNCConnService.register(VncCanvasActivity.this, conn);
vncCanvas.showConnectionInfo();
});
// the actual connection init
// Startup the VNCConn with a nifty progress dialog
final ProgressDialog pd = new ProgressDialog(this);
Expand All @@ -257,16 +265,47 @@ public void onSystemUiVisibilityChange(int visibility) {
@Override
public void onConnected() {
runOnUiThread(() -> {
// register connection
VNCConnService.register(VncCanvasActivity.this, conn);

if (Build.VERSION.SDK_INT < 33) {
/*
* Show all the on-connect UI directly
*/
showHelpDialog();
VNCConnService.register(VncCanvasActivity.this, conn);
vncCanvas.showConnectionInfo();
} else {
/*
permission asking according to the book https://developer.android.com/training/permissions/requesting
*/
// the permission asking logic as per the book https://developer.android.com/training/permissions/requesting
if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
showHelpDialog();
VNCConnService.register(VncCanvasActivity.this, conn);
vncCanvas.showConnectionInfo();
} else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
new AlertDialog.Builder(VncCanvasActivity.this)
.setCancelable(false)
.setTitle(R.string.notification_title)
.setMessage(R.string.notification_msg)
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
})
.setCancelable(false)
.show();
} else {
// You can directly ask for the permission.
// The registered ActivityResultCallback gets the result of this request.
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
}
}

setTitle(conn.getDesktopName());
// actually set scale type with this, otherwise no scaling
setModes();
firstFrameWaitDialog.setMessage("Downloading first frame.\nPlease wait...");
// center pointer
vncCanvas.mouseX = conn.getFramebufferWidth() / 2;
vncCanvas.mouseY = conn.getFramebufferHeight() / 2;
vncCanvas.showConnectionInfo();
});
}

Expand Down Expand Up @@ -361,44 +400,6 @@ public void onClick(View v) {

if(! prefs.getBoolean(Constants.PREFS_KEY_POINTERHIGHLIGHT, true))
vncCanvas.setPointerHighlight(false);


if (Build.VERSION.SDK_INT < 33) {
/*
* ask whether to show help on first connection directly
*/
showHelpDialog();
} else {
/*
permission asking according to the book https://developer.android.com/training/permissions/requesting
*/
// the permission launcher and callback handler
ActivityResultLauncher<String> requestPermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
// show help _after_ all the permission handling
showHelpDialog();
});

// the permission asking logic as per the book https://developer.android.com/training/permissions/requesting
if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
showHelpDialog();
} else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
new AlertDialog.Builder(this)
.setCancelable(false)
.setTitle(R.string.notification_title)
.setMessage(R.string.notification_msg)
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
})
.setCancelable(false)
.show();
} else {
// You can directly ask for the permission.
// The registered ActivityResultCallback gets the result of this request.
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
}
}

}

/**
Expand Down

0 comments on commit fa90488

Please sign in to comment.