Home | History | Annotate | Download | only in camera
      1 /*
      2 * Copyright 2017 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.design.widget.Snackbar
     22 import android.support.v4.app.Fragment
     23 import android.view.LayoutInflater
     24 import android.view.View
     25 import android.view.ViewGroup
     26 import com.example.android.system.runtimepermissions.R
     27 import kotlinx.android.synthetic.main.fragment_camera.*
     28 
     29 /**
     30  * Displays a [CameraPreview] of the first [Camera].
     31  * An error message is displayed if the Camera is not available.
     32  *
     33  *
     34  * This Fragment is only used to illustrate that access to the Camera API has been granted (or
     35  * denied) as part of the runtime permissions model. It is not relevant for the use of the
     36  * permissions API.
     37  *
     38  *
     39  * Implementation is based directly on the documentation at
     40  * http://developer.android.com/guide/topics/media/camera.html
     41  */
     42 class CameraPreviewFragment : Fragment() {
     43 
     44     private lateinit var preview: CameraPreview
     45     private var camera: Camera? = null
     46 
     47     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
     48             savedInstanceState: Bundle?): View {
     49 
     50         // Open an instance of the first camera and retrieve its info.
     51         camera = Camera.open(CAMERA_ID)
     52         val cameraInfo: Camera.CameraInfo = Camera.CameraInfo()
     53 
     54         if (camera != null) {
     55             // Get camera info only if the camera is available
     56             Camera.getCameraInfo(CAMERA_ID, cameraInfo)
     57         }
     58 
     59         val root: View
     60 
     61         if (camera == null) {
     62             // Camera is not available, display error message
     63             root = inflater.inflate(R.layout.fragment_camera_unavailable, container, false)
     64             Snackbar.make(root, "Camera is not available.", Snackbar.LENGTH_SHORT).show()
     65         } else {
     66             root = inflater.inflate(R.layout.fragment_camera, container, false)
     67 
     68             // Get the rotation of the screen to adjust the preview image accordingly.
     69             val displayRotation = activity.windowManager.defaultDisplay.rotation
     70 
     71             // Create the Preview view and set it as the content of this Activity.
     72             preview = CameraPreview(activity, camera, cameraInfo, displayRotation)
     73             cameraPreview.addView(preview)
     74         }
     75 
     76         return root
     77     }
     78 
     79     override fun onPause() {
     80         super.onPause()
     81         // Stop camera access
     82         releaseCamera()
     83     }
     84 
     85     private fun releaseCamera() {
     86         camera?.release() // release the camera for other applications.
     87         camera = null
     88     }
     89 
     90     companion object {
     91 
     92         /**
     93          * Id of the camera to access. 0 is the first camera.
     94          */
     95         private const val CAMERA_ID = 0
     96 
     97         fun newInstance() = CameraPreviewFragment()
     98     }
     99 }
    100