diff --git a/app/src/main/java/com/to3g/snipasteandroid/fragment/HomeFragment.java b/app/src/main/java/com/to3g/snipasteandroid/fragment/HomeFragment.java index 3d7d38a..9edcd31 100644 --- a/app/src/main/java/com/to3g/snipasteandroid/fragment/HomeFragment.java +++ b/app/src/main/java/com/to3g/snipasteandroid/fragment/HomeFragment.java @@ -144,11 +144,7 @@ private void setFloatViewOpacity () { } } - /** - * When click the camera button - */ - @OnClick(R.id.cameraButton) - protected void onCameraButtonClick () { + private void pasteCamera () { PictureSelector .create(getActivity()) .openCamera(PictureMimeType.ofImage()) @@ -169,10 +165,41 @@ protected void onCameraButtonClick () { } /** - * When click the album button + * When click the camera button */ - @OnClick(R.id.albumButton) - protected void onAlbumButtonClick () { + @OnClick(R.id.cameraButton) + protected void onCameraButtonClick () { + // check the permission + if (PermissionUtils.checkPermission(Objects.requireNonNull(getContext()))) { + pasteCamera(); + } else { + // prompt to request permission + new QMUIDialog.MessageDialogBuilder(getActivity()) + .setMessage(getText(R.string.floatingPermissionText)) + .addAction(getText(R.string.cancelText), new QMUIDialogAction.ActionListener() { + @Override + public void onClick(QMUIDialog dialog, int index) { + dialog.dismiss(); + } + }) + .addAction(0, getText(R.string.toOpen), QMUIDialogAction.ACTION_PROP_POSITIVE, new QMUIDialogAction.ActionListener() { + @Override + public void onClick(QMUIDialog dialog, int index) { + dialog.dismiss(); + PermissionUtils.requestPermission(getActivity(), result -> { + if(result) { + pasteCamera(); + } else { + Toast.makeText(getContext(), getText(R.string.needFloatingPermission), Toast.LENGTH_SHORT).show(); + } + }); + } + }) + .create(R.style.QMUI_Dialog).show(); + } + } + + private void pasteAlbum () { PictureSelector .create(getActivity()) .openGallery(PictureMimeType.ofImage()) @@ -194,6 +221,41 @@ protected void onAlbumButtonClick () { }); } + /** + * When click the album button + */ + @OnClick(R.id.albumButton) + protected void onAlbumButtonClick () { + // check the permission + if (PermissionUtils.checkPermission(Objects.requireNonNull(getContext()))) { + pasteAlbum(); + } else { + // prompt to request permission + new QMUIDialog.MessageDialogBuilder(getActivity()) + .setMessage(getText(R.string.floatingPermissionText)) + .addAction(getText(R.string.cancelText), new QMUIDialogAction.ActionListener() { + @Override + public void onClick(QMUIDialog dialog, int index) { + dialog.dismiss(); + } + }) + .addAction(0, getText(R.string.toOpen), QMUIDialogAction.ACTION_PROP_POSITIVE, new QMUIDialogAction.ActionListener() { + @Override + public void onClick(QMUIDialog dialog, int index) { + dialog.dismiss(); + PermissionUtils.requestPermission(getActivity(), result -> { + if(result) { + pasteAlbum(); + } else { + Toast.makeText(getContext(), getText(R.string.needFloatingPermission), Toast.LENGTH_SHORT).show(); + } + }); + } + }) + .create(R.style.QMUI_Dialog).show(); + } + } + /** * When click the screenButton */