1 /*
2  * Copyright (C) 2016 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.google.android.car.kitchensink.camera;
18 
19 import android.car.CarNotConnectedException;
20 import android.car.hardware.camera.CarCamera;
21 import android.car.hardware.camera.CarCameraManager;
22 import android.car.hardware.camera.CarCameraState;
23 import android.graphics.Rect;
24 import android.os.Bundle;
25 import android.support.v4.app.Fragment;
26 import android.util.Log;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.Button;
31 import android.widget.TextView;
32 import android.widget.ToggleButton;
33 
34 import com.google.android.car.kitchensink.R;
35 
36 import java.lang.Override;
37 
38 public class CameraTestFragment extends Fragment {
39     private final boolean DBG = true;
40     private final String TAG = "CameraTestFragment";
41     private TextView mTvCap;
42     private TextView mTvRvcCrop;
43     private TextView mTvRvcPos;
44     private TextView mTvCameraState;
45     private CarCameraManager mCarCameraManager;
46     private CarCamera mRvcCamera;
47 
48     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)49     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) {
50         View v = inflater.inflate(R.layout.camera_test, container, false);
51 
52         int[] cameraList = mCarCameraManager.getCameraList();
53         for (int camera : cameraList) {
54             if (camera == CarCameraManager.CAR_CAMERA_TYPE_RVC) {
55                 mRvcCamera = mCarCameraManager.openCamera(1);
56                 break;
57             }
58         }
59 
60         mTvCap = (TextView)v.findViewById(R.id.tvCap);
61         mTvRvcCrop = (TextView)v.findViewById(R.id.tvRvcCrop);
62         mTvRvcPos = (TextView)v.findViewById(R.id.tvRvcPos);
63         mTvCameraState = (TextView)v.findViewById(R.id.tvCameraState);
64 
65         Button btn = (Button) v.findViewById(R.id.btnGetCap);
66         btn.setOnClickListener(new View.OnClickListener() {
67             public void onClick(View v) {
68                 try {
69                     int cap = mRvcCamera.getCapabilities();
70                     mTvCap.setText(String.valueOf(cap));
71                 } catch (CarNotConnectedException e) {
72                     Log.e(TAG, "Failed to get camera capabilities", e);
73                 }
74             }
75         });
76 
77         btn = (Button) v.findViewById(R.id.btnGetRvcCrop);
78         btn.setOnClickListener(new View.OnClickListener() {
79             public void onClick(View v) {
80                 try {
81                     Rect rect = mRvcCamera.getCameraCrop();
82                     if(rect != null) {
83                         mTvRvcCrop.setText(rect.toString());
84                     } else {
85                         mTvRvcCrop.setText("null");
86                     }
87                 } catch (CarNotConnectedException e) {
88                     Log.e(TAG, "Failed to get camera crop", e);
89                 }
90             }
91         });
92 
93         btn = (Button) v.findViewById(R.id.btnGetRvcPos);
94         btn.setOnClickListener(new View.OnClickListener() {
95             public void onClick(View v) {
96                 try {
97                     Rect rect = mRvcCamera.getCameraPosition();
98                     if(rect != null) {
99                         mTvRvcPos.setText(String.valueOf(rect));
100                     } else {
101                         mTvRvcPos.setText("null");
102                     }
103                 } catch (CarNotConnectedException e) {
104                     Log.e(TAG, "Failed to get camere position", e);
105                 }
106             }
107         });
108 
109         btn = (Button) v.findViewById(R.id.btnGetCameraState);
110         btn.setOnClickListener(new View.OnClickListener() {
111             public void onClick(View v) {
112                 try {
113                     CarCameraState state = mRvcCamera.getCameraState();
114                     if(state != null) {
115                         mTvCameraState.setText(state.toString());
116                     } else {
117                         mTvCameraState.setText("null");
118                     }
119                 } catch (CarNotConnectedException e) {
120                     Log.e(TAG, "Failed to get camere state", e);
121                 }
122             }
123         });
124 
125         btn = (Button) v.findViewById(R.id.btnSetRvcCrop);
126         btn.setOnClickListener(new View.OnClickListener() {
127             public void onClick(View v) {
128                 Rect rect = new Rect(160, 240, 560, 480);
129                 try {
130                     mRvcCamera.setCameraCrop(rect);
131                 } catch (CarNotConnectedException e) {
132                     Log.e(TAG, "Failed to set camera crop", e);
133                 }
134             }
135         });
136 
137         btn = (Button) v.findViewById(R.id.btnSetRvcCrop2);
138         btn.setOnClickListener(new View.OnClickListener() {
139             public void onClick(View v) {
140                 Rect rect = new Rect(0, 0, 720, 480);
141                 try {
142                     mRvcCamera.setCameraCrop(rect);
143                 } catch (CarNotConnectedException e) {
144                     Log.e(TAG, "Failed to set camera crop", e);
145                 }
146             }
147         });
148 
149         btn = (Button) v.findViewById(R.id.btnSetRvcPos);
150         btn.setOnClickListener(new View.OnClickListener() {
151             public void onClick(View v) {
152                 Rect rect = new Rect(300, 0, 800, 480);
153                 try {
154                     mRvcCamera.setCameraPosition(rect);
155                 } catch (CarNotConnectedException e) {
156                     Log.e(TAG, "Failed to set camera position", e);
157                 }
158             }
159         });
160 
161         btn = (Button) v.findViewById(R.id.btnSetRvcPos2);
162         btn.setOnClickListener(new View.OnClickListener() {
163             public void onClick(View v) {
164                 Rect rect = new Rect(500, 0, 800, 480);
165                 try {
166                     mRvcCamera.setCameraPosition(rect);
167                 } catch (CarNotConnectedException e) {
168                     Log.e(TAG, "Failed to set camera position", e);
169                 }
170             }
171         });
172 
173         btn = (Button) v.findViewById(R.id.btnSetRvcPos3);
174         btn.setOnClickListener(new View.OnClickListener() {
175             public void onClick(View v) {
176                 Rect rect = new Rect(300, 0, 500, 300);
177                 try {
178                     mRvcCamera.setCameraPosition(rect);
179                 } catch (CarNotConnectedException e) {
180                     Log.e(TAG, "Failed to set camera position", e);
181                 }
182             }
183         });
184 
185         final ToggleButton toggleBtn = (ToggleButton) v.findViewById(R.id.btnRvcState);
186         toggleBtn.setOnClickListener(new View.OnClickListener() {
187             public void onClick(View v) {
188                 CarCameraState state = new CarCameraState(false, toggleBtn.isChecked());
189                 try {
190                     mRvcCamera.setCameraState(state);
191                 } catch (CarNotConnectedException e) {
192                     Log.e(TAG, "Failed to set camera state", e);
193                 }
194             }
195         });
196 
197         if(DBG) {
198             Log.d(TAG, "Starting CameraTestFragment");
199         }
200         return v;
201     }
202 
setCameraManager(CarCameraManager cameraManager)203     public void setCameraManager(CarCameraManager cameraManager) {
204         Log.d(TAG, "setCameraManager()");
205         mCarCameraManager = cameraManager;
206     }
207 }
208