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 com.example.android.common.logger.Log;
20 import com.example.android.system.runtimepermissions.R;
21 
22 import android.hardware.Camera;
23 import android.os.Bundle;
24 import android.support.v4.app.Fragment;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.FrameLayout;
29 import android.widget.Toast;
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 
newInstance()54     public static CameraPreviewFragment newInstance() {
55         return new CameraPreviewFragment();
56     }
57 
58     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)59     public View onCreateView(LayoutInflater inflater, ViewGroup container,
60             Bundle savedInstanceState) {
61 
62         // Open an instance of the first camera and retrieve its info.
63         mCamera = getCameraInstance(CAMERA_ID);
64         Camera.CameraInfo cameraInfo = null;
65 
66         if (mCamera != null) {
67             // Get camera info only if the camera is available
68             cameraInfo = new Camera.CameraInfo();
69             Camera.getCameraInfo(CAMERA_ID, cameraInfo);
70         }
71 
72         if (mCamera == null || cameraInfo == null) {
73             // Camera is not available, display error message
74             Toast.makeText(getActivity(), "Camera is not available.", Toast.LENGTH_SHORT).show();
75             return inflater.inflate(R.layout.fragment_camera_unavailable, null);
76         }
77 
78         View root = inflater.inflate(R.layout.fragment_camera, null);
79 
80         // Get the rotation of the screen to adjust the preview image accordingly.
81         final int displayRotation = getActivity().getWindowManager().getDefaultDisplay()
82                 .getRotation();
83 
84         // Create the Preview view and set it as the content of this Activity.
85         mPreview = new CameraPreview(getActivity(), mCamera, cameraInfo, displayRotation);
86         FrameLayout preview = (FrameLayout) root.findViewById(R.id.camera_preview);
87         preview.addView(mPreview);
88 
89         return root;
90     }
91 
92     @Override
onPause()93     public void onPause() {
94         super.onPause();
95         // Stop camera access
96         releaseCamera();
97     }
98 
99     /** A safe way to get an instance of the Camera object. */
getCameraInstance(int cameraId)100     public static Camera getCameraInstance(int cameraId) {
101         Camera c = null;
102         try {
103             c = Camera.open(cameraId); // attempt to get a Camera instance
104         } catch (Exception e) {
105             // Camera is not available (in use or does not exist)
106             Log.d(TAG, "Camera " + cameraId + " is not available: " + e.getMessage());
107         }
108         return c; // returns null if camera is unavailable
109     }
110 
releaseCamera()111     private void releaseCamera() {
112         if (mCamera != null) {
113             mCamera.release();        // release the camera for other applications
114             mCamera = null;
115         }
116     }
117 }
118