1 /*
2  * Copyright (C) 2024 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.android.deviceaswebcam
18 
19 import android.content.Context
20 import android.content.Intent
21 import android.os.Binder
22 import android.os.IBinder
23 
24 /**
25  * Concrete implementation of [DeviceAsWebcamFgService] which uses [WebcamControllerImpl] for camera
26  * controls, and [DeviceAsWebcamPreview] for the user facing activity.
27  */
28 class DeviceAsWebcamFgServiceImpl : DeviceAsWebcamFgService() {
29     private val mBinder: IBinder = LocalBinder()
30     lateinit var mWebcamController: WebcamControllerImpl
31 
getWebcamControllernull32     override fun getWebcamController(context: Context): WebcamController {
33         if (!::mWebcamController.isInitialized) {
34             mWebcamController = WebcamControllerImpl(context)
35         }
36         return mWebcamController
37     }
38 
getPreviewActivityIntentnull39     override fun getPreviewActivityIntent(context: Context): Intent {
40         return Intent(context, DeviceAsWebcamPreview::class.java)
41     }
42 
onBindnull43     override fun onBind(intent: Intent?): IBinder {
44         return mBinder
45     }
46 
47     /**
48      * Returns the backing [WebcamControllerImpl] to allow [DeviceAsWebcamPreview] to interact with
49      * the camera controls.
50      */
getWebcamControllerImplnull51     fun getWebcamControllerImpl(): WebcamControllerImpl {
52         return mWebcamController
53     }
54 
55     /**
56      * Simple class to hold a reference to [DeviceAsWebcamFgService] instance and have it be
57      * accessible from [android.content.ServiceConnection.onServiceConnected] callback.
58      */
59     inner class LocalBinder : Binder() {
60         val service: DeviceAsWebcamFgServiceImpl
61             get() = this@DeviceAsWebcamFgServiceImpl
62     }
63 }
64