Advertisement
riyanwenas

CameraSelectorDialogFragment

Feb 15th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. public class CameraSelectorDialogFragment extends DialogFragment {
  2. public interface CameraSelectorDialogListener {
  3. public void onCameraSelected(int cameraId);
  4. }
  5.  
  6. private int mCameraId;
  7. private CameraSelectorDialogListener mListener;
  8.  
  9. public void onCreate(Bundle state) {
  10. super.onCreate(state);
  11. setRetainInstance(true);
  12. }
  13.  
  14. public static CameraSelectorDialogFragment newInstance(CameraSelectorDialogListener listener, int cameraId) {
  15. CameraSelectorDialogFragment fragment = new CameraSelectorDialogFragment();
  16. fragment.mCameraId = cameraId;
  17. fragment.mListener = listener;
  18. return fragment;
  19. }
  20.  
  21. @Override
  22. public Dialog onCreateDialog(Bundle savedInstanceState) {
  23. if (mListener == null) {
  24. dismiss();
  25. return null;
  26. }
  27.  
  28. int numberOfCameras = Camera.getNumberOfCameras();
  29. String[] cameraNames = new String[numberOfCameras];
  30. int checkedIndex = 0;
  31.  
  32. for (int i = 0; i < numberOfCameras; i++) {
  33. Camera.CameraInfo info = new Camera.CameraInfo();
  34. Camera.getCameraInfo(i, info);
  35. if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
  36. cameraNames[i] = "Front Facing";
  37. } else if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
  38. cameraNames[i] = "Rear Facing";
  39. } else {
  40. cameraNames[i] = "Camera ID: " + i;
  41. }
  42. if (i == mCameraId) {
  43. checkedIndex = i;
  44. }
  45. }
  46.  
  47. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  48. // Set the dialog title
  49. builder.setTitle(R.string.select_camera)
  50. // Specify the list array, the items to be selected by default (null for none),
  51. // and the listener through which to receive callbacks when items are selected
  52. .setSingleChoiceItems(cameraNames, checkedIndex,
  53. new DialogInterface.OnClickListener() {
  54. @Override
  55. public void onClick(DialogInterface dialog, int which) {
  56. mCameraId = which;
  57. }
  58. })
  59. // Set the action buttons
  60. .setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
  61. @Override
  62. public void onClick(DialogInterface dialog, int id) {
  63. // User clicked OK, so save the mSelectedIndices results somewhere
  64. // or return them to the component that opened the dialog
  65. if (mListener != null) {
  66. mListener.onCameraSelected(mCameraId);
  67. }
  68. }
  69. })
  70. .setNegativeButton(R.string.cancel_button, new DialogInterface.OnClickListener() {
  71. @Override
  72. public void onClick(DialogInterface dialog, int id) {
  73. }
  74. });
  75.  
  76. return builder.create();
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement