防止AlertDialog显示在另一个DialogFragment

时间:2016-09-20 16:27:33

标签: android android-dialogfragment android-dialog bottom-sheet runtime-permissions

我正在创建一个BottomSheetDialogFragment,允许用户拍照或从库中选择一张照片。要访问这两项功能,需要WRITE_EXTERNAL_STORAGE权限。

因此,我想请求BottomSheetDialogFragment的许可,阻止用户在授予权限之前点击其他任何内容。如果我在onViewCreated中请求权限,则权限对话框显示正常:

Permission Request

但是,如果权限被拒绝且用户再次尝试,我会尝试在AlertDialog中显示基本原理,但对话框会被阻止;大概来自BottomSheetDialogFragment的暗淡:

Permission Rationale

我认为这是由BottomSheetDialogFragment的动画引起的,它在片段完成动画之前不显示背景暗淡。这恰巧发生在onViewCreated之后。有没有人知道在没有关闭或解雇AlertDialog的情况下是否有办法迫使BottomSheetDialogFragment到前线?或者,如果有办法监听BottomSheetDialogFragment动画完成?

我知道在添加BottomSheetDialogFragment之前我可以请求权限,但是我希望通过对话框来请求它,以便为用户提供一些上下文。

以下是Fragment

public class ImageChooserDialogFragment extends BottomSheetDialogFragment {

    public interface OnImageChosenListener {
        void onImageChosen(Uri data);
    }

    private static final String PREFIX_IMAGE_CAPTURE = "IMG_";

    private static final int REQUEST_PERMISSION_CAMERA  = 0;
    private static final int REQUEST_PERMISSION_STORAGE = 1;

    private static final int REQUEST_IMAGE_CAPTURE      = 2;
    private static final int REQUEST_IMAGE_SELECTION    = 3;

    private static final String[] PERMISSIONS_CAMERA = new String[] {
            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA
    };

    private static final String[] PERMISSIONS_STORAGE = new String[] {
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    private boolean hasFeatureCamera;

    private Uri mCurrentPhotoResource;

    private View mView;

    private OnImageChosenListener mOnImageChosenListener;

    public static ImageChooserDialogFragment newInstance() {
        return new ImageChooserDialogFragment();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Timber.d("onAttach");

        if(context instanceof OnImageChosenListener) {
            mOnImageChosenListener = (OnImageChosenListener) context;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fragment parent = getParentFragment();

        if(parent != null) {
            onAttachToFragment(parent);
        }

        PackageManager manager = getContext().getPackageManager();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            hasFeatureCamera = manager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
        } else {
            hasFeatureCamera = manager.hasSystemFeature(PackageManager.FEATURE_CAMERA)
                    || manager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
        }
    }

    public void onAttachToFragment(Fragment fragment) {
        if(fragment instanceof OnImageChosenListener) {
            mOnImageChosenListener = (OnImageChosenListener) fragment;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.dialog_image_chooser, container, false);
        ButterKnife.bind(this, mView);

        if(!hasFeatureCamera) {
            mView.setVisibility(View.GONE);
        }

        return mView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if(!PermissionUtil.checkPermissions(getContext(), PERMISSIONS_STORAGE)) {
            requestPermissionsWithRationale(REQUEST_PERMISSION_STORAGE, PERMISSIONS_STORAGE);
        } else if(!hasFeatureCamera) {
            dispatchImageSelectionIntent();
        } else {
            displayRequestPermissionsAlert(R.string.msg_snackbar_permissions_storage);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        switch(requestCode) {
            case REQUEST_PERMISSION_CAMERA:
                if(PermissionUtil.verifyPermissions(grantResults)) {
                    dispatchImageCaptureIntent();
                } else {
                    displayRequestPermissionsAlert(R.string.msg_snackbar_permissions_camera);
                } break;
            case REQUEST_PERMISSION_STORAGE:
                if(PermissionUtil.verifyPermissions(grantResults)) {
                    if(!hasFeatureCamera) {
                        dispatchImageSelectionIntent();
                    }
                } else {
                    displayRequestPermissionsAlert(R.string.msg_snackbar_permissions_storage);
                } break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_IMAGE_CAPTURE:
                if(resultCode == Activity.RESULT_OK) {
                    handleImageCaptureResult(data);
                } else {
                    destroyTemporaryFile();
                } break;
            case REQUEST_IMAGE_SELECTION:
                if(resultCode == Activity.RESULT_OK) {
                    handleImageSelectionResult(data);
                } break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @OnClick(R.id.photo_take)
    public void onClickCapture() {
        if(!PermissionUtil.checkPermissions(getContext(), PERMISSIONS_CAMERA)) {
            requestPermissionsWithRationale(REQUEST_PERMISSION_CAMERA, PERMISSIONS_CAMERA);
        } else {
            dispatchImageCaptureIntent();
        }
    }

    @OnClick(R.id.photo_choose)
    public void onClickChoose() {
        if(!PermissionUtil.checkPermissions(getContext(), PERMISSIONS_STORAGE)) {
            requestPermissionsWithRationale(REQUEST_PERMISSION_STORAGE, PERMISSIONS_STORAGE);
        } else {
            dispatchImageSelectionIntent();
        }
    }

    private void dispatchImageCaptureIntent() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if(intent.resolveActivity(getContext().getPackageManager()) != null) {
            try {
                File image = createTemporaryFile();
                Uri data = FileProvider.getUriForFile(getContext(), "com.example.app.fileprovider", image);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, data);
                startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
            } catch (IOException exception) {
                Timber.w(exception, "Error occurred while creating image file");
            }
        } else {
            // TODO: handle no application to handle intent
        }
    }

    private void dispatchImageSelectionIntent() {
        final Intent intent = new Intent(Intent.ACTION_PICK,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        if(intent.resolveActivity(getContext().getPackageManager()) != null) {
            startActivityForResult(intent, REQUEST_IMAGE_SELECTION);
        } else {
            // TODO: handle no application to handle intent
        } dismiss();
    }

    private void dispatchDetailSettingsIntent() {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                Uri.fromParts("package", getContext().getPackageName(), null));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        if(intent.resolveActivity(getContext().getPackageManager()) != null) {
            startActivity(intent);
        } else {
            // TODO: handle no application to handle intent
        } dismiss();
    }

    private void dispatchMediaScanIntent(Uri data) {
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data);
        getContext().sendBroadcast(intent);
    }

    private void displayPermissionsRationale(int requestCode) {
        switch (requestCode) {
            case REQUEST_PERMISSION_CAMERA:
                Timber.d("Request Image capture rationale");
                DialogFactory.createRationaleAlert(getContext(),
                        R.string.title_dialog_rationale_camera,
                        R.string.msg_dialog_rationale_camera).show();
                break;
            case REQUEST_PERMISSION_STORAGE:
                Timber.d("Request Image selection rationale");
                DialogFactory.createRationaleAlert(getContext(),
                        R.string.title_dialog_rationale_storage,
                        R.string.msg_dialog_rationale_storage).show();
                break;
            default:
                Timber.d("No rationale");
        }
    }

    private void displayRequestPermissionsAlert(@StringRes int message) {
        Snackbar.make(mView, message, Snackbar.LENGTH_LONG)
                .setAction(R.string.action_settings, view -> dispatchDetailSettingsIntent()).show();
        dismiss();
    }

    private void requestPermissionsWithRationale(int requestCode, @NonNull String[] permissions) {
        Timber.d("Request permissions with rationale");
        if(PermissionUtil.shouldShowRequestPermissionsRationale(this, permissions)) {
            Timber.d("Display rationale");
            displayPermissionsRationale(requestCode);
        } else {
            Timber.d("Request Permissions");
            requestPermissions(permissions, requestCode);
        }
    }

    private File createTemporaryFile() throws IOException {
        String fileName = PREFIX_IMAGE_CAPTURE /*+ TimeUtil.getTimeStamp()*/;

        File directory = getContext().getExternalFilesDir(Environment.DIRECTORY_DCIM);
        File file = File.createTempFile(fileName, ".jpeg", directory);

        mCurrentPhotoResource = Uri.fromFile(file);

        return file;
    }

    private void destroyTemporaryFile() {
        File file = new File(mCurrentPhotoResource.getPath());

        if(file.delete()) {
            Timber.i("Temporary file deleted");
        } else {
            Timber.w("Failed to delete temporary file: " + file);
        }
    }

    private void handleImageCaptureResult(Intent intent) {
        if(mCurrentPhotoResource != null) {
            dispatchMediaScanIntent(mCurrentPhotoResource);

            if (mOnImageChosenListener != null) {
                mOnImageChosenListener.onImageChosen(mCurrentPhotoResource);
            } else {
                Timber.w("Parent Activity or Fragment does not implement OnImageChosenListener; captured result cannot be used");
            }
        }
    }

    private void handleImageSelectionResult(Intent intent) {
        Timber.d("Selection: " + intent.getData());
        if(mOnImageChosenListener != null) {
            mOnImageChosenListener.onImageChosen(intent.getData());
        } else {
            Timber.w("Parent Activity or Fragment does not implement OnImageChosenListener; selected result cannot be used");
        }
    }

}

DialogFactory类:

public final class DialogFactory {

    public static AlertDialog createRationaleAlert(
            Context context, @StringRes int title, @StringRes int message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(title).setMessage(message);
        return createRationaleAlert(context, builder);
    }

    public static AlertDialog createRationaleAlert(
            Context context, CharSequence title, CharSequence message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(title).setMessage(message);
        return createRationaleAlert(context, builder);
    }

    private static AlertDialog createRationaleAlert(
            Context context, AlertDialog.Builder builder) {
        builder.setPositiveButton(R.string.btn_try, (dialog, which) -> {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            context.startActivity(intent);
        }).setNegativeButton(R.string.btn_cancel, (dialog, which) -> {
            dialog.cancel();
        });

        return builder.create();
    }

}

Fragment调用DialogFactory.createRationaleAlert().show()时会出现此问题。

4 个答案:

答案 0 :(得分:2)

经过深思熟虑,我终于找到了解决方案。诀窍是实现DialogInterface.OnShowListener,并在Dialog回调中创建新的onShow()

public class ImageChooserDialogFragment extends BottomSheetDialogFragment
        implements DialogInterface.OnShowListener {

    @Override @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        super.onCreateDialog(savedInstanceState);
        getDialog().setOnShowListener(this);
    }

    @Override
    public void onShow(DialogInterface dialog) {
        if(!PermissionUtil.checkPermissions(getContext(), PERMISSIONS_STORAGE)) {
            requestPermissionsWithRationale(REQUEST_PERMISSION_STORAGE, PERMISSIONS_STORAGE);
        } else {
            displayRequestPermissionsAlert(R.string.msg_snackbar_permissions_storage);
        }
    }

    // ...

}

答案 1 :(得分:1)

  

有没有办法监听BottomSheetDialogFragment动画完成?

mBottomSheetController = BottomSheetBehavior.from(mBottomSheet);
mBottomSheetController.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        switch (newState) {
            case BottomSheetBehavior.STATE_EXPANDED:
                //fully expanded, this is the event you want to listen for
            break;
        }
    }

    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        //do nothing
    }
});

答案 2 :(得分:0)

将对话框设置为TYPE_SYSTEM_ALERT。

使用此:

builder.create().getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

答案 3 :(得分:0)

禁用负责暗淡背景的标志

dialog.getWindow()clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);