1 /* 2 * Copyright 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.system.runtimepermissions.camera; 18 19 import android.hardware.Camera; 20 import android.os.Bundle; 21 import android.support.v4.app.Fragment; 22 import android.util.Log; 23 import android.view.LayoutInflater; 24 import android.view.View; 25 import android.view.ViewGroup; 26 import android.widget.FrameLayout; 27 import android.widget.Toast; 28 29 import com.example.android.system.runtimepermissions.R; 30 31 /** 32 * Displays a {@link CameraPreview} of the first {@link Camera}. 33 * An error message is displayed if the Camera is not available. 34 * <p> 35 * This Fragment is only used to illustrate that access to the Camera API has been granted (or 36 * denied) as part of the runtime permissions model. It is not relevant for the use of the 37 * permissions API. 38 * <p> 39 * Implementation is based directly on the documentation at 40 * http://developer.android.com/guide/topics/media/camera.html 41 */ 42 public class CameraPreviewFragment extends Fragment { 43 44 private static final String TAG = "CameraPreview"; 45 46 /** 47 * Id of the camera to access. 0 is the first camera. 48 */ 49 private static final int CAMERA_ID = 0; 50 51 private CameraPreview mPreview; 52 private Camera mCamera; 53 54 public static CameraPreviewFragment newInstance() { 55 return new CameraPreviewFragment(); 56 } 57 58 /** 59 * A safe way to get an instance of the Camera object. 60 */ 61 public static Camera getCameraInstance(int cameraId) { 62 Camera c = null; 63 try { 64 c = Camera.open(cameraId); // attempt to get a Camera instance 65 } catch (Exception e) { 66 // Camera is not available (in use or does not exist) 67 Log.d(TAG, "Camera " + cameraId + " is not available: " + e.getMessage()); 68 } 69 return c; // returns null if camera is unavailable 70 } 71 72 @Override 73 public View onCreateView(LayoutInflater inflater, ViewGroup container, 74 Bundle savedInstanceState) { 75 76 // Open an instance of the first camera and retrieve its info. 77 mCamera = getCameraInstance(CAMERA_ID); 78 Camera.CameraInfo cameraInfo = null; 79 80 if (mCamera != null) { 81 // Get camera info only if the camera is available 82 cameraInfo = new Camera.CameraInfo(); 83 Camera.getCameraInfo(CAMERA_ID, cameraInfo); 84 } 85 86 if (mCamera == null || cameraInfo == null) { 87 // Camera is not available, display error message 88 Toast.makeText(getActivity(), "Camera is not available.", Toast.LENGTH_SHORT).show(); 89 return inflater.inflate(R.layout.fragment_camera_unavailable, null); 90 } 91 92 View root = inflater.inflate(R.layout.fragment_camera, null); 93 94 // Get the rotation of the screen to adjust the preview image accordingly. 95 final int displayRotation = getActivity().getWindowManager().getDefaultDisplay() 96 .getRotation(); 97 98 // Create the Preview view and set it as the content of this Activity. 99 mPreview = new CameraPreview(getActivity(), mCamera, cameraInfo, displayRotation); 100 FrameLayout preview = (FrameLayout) root.findViewById(R.id.camera_preview); 101 preview.addView(mPreview); 102 103 return root; 104 } 105 106 @Override 107 public void onPause() { 108 super.onPause(); 109 // Stop camera access 110 releaseCamera(); 111 } 112 113 private void releaseCamera() { 114 if (mCamera != null) { 115 mCamera.release(); // release the camera for other applications 116 mCamera = null; 117 } 118 } 119 } 120