OSDN Git Service

Fix camera facing by querying characteristics
[android-x86/packages-apps-Camera2.git] / src / com / android / camera / DisableCameraReceiver.java
1 /*
2  * Copyright (C) 2012 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.camera;
18
19 import android.content.BroadcastReceiver;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.PackageManager;
24 import android.hardware.Camera.CameraInfo;
25
26 import com.android.camera.debug.Log;
27
28 // We want to disable camera-related activities if there is no camera. This
29 // receiver runs when BOOT_COMPLETED intent is received. After running once
30 // this receiver will be disabled, so it will not run again.
31 public class DisableCameraReceiver extends BroadcastReceiver {
32     private static final Log.Tag TAG = new Log.Tag("DisableCamRcver");
33     private static final boolean CHECK_BACK_CAMERA_ONLY = false;
34     private static final String ACTIVITIES[] = {
35         "com.android.camera.CameraLauncher",
36     };
37
38     @Override
39     public void onReceive(Context context, Intent intent) {
40         // Disable camera-related activities if there is no camera.
41         boolean needCameraActivity = CHECK_BACK_CAMERA_ONLY
42             ? hasBackCamera()
43             : hasCamera();
44
45         if (!needCameraActivity) {
46             Log.i(TAG, "disable all camera activities");
47             for (int i = 0; i < ACTIVITIES.length; i++) {
48                 disableComponent(context, ACTIVITIES[i]);
49             }
50         }
51
52         // Disable this receiver so it won't run again.
53         disableComponent(context, "com.android.camera.DisableCameraReceiver");
54     }
55
56     private boolean hasCamera() {
57         int n = android.hardware.Camera.getNumberOfCameras();
58         Log.i(TAG, "number of camera: " + n);
59         return (n > 0);
60     }
61
62     private boolean hasBackCamera() {
63         int n = android.hardware.Camera.getNumberOfCameras();
64         CameraInfo info = new CameraInfo();
65         for (int i = 0; i < n; i++) {
66             android.hardware.Camera.getCameraInfo(i, info);
67             if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
68                 Log.i(TAG, "back camera found: " + i);
69                 return true;
70             }
71         }
72         Log.i(TAG, "no back camera");
73         return false;
74     }
75
76     private void disableComponent(Context context, String klass) {
77         ComponentName name = new ComponentName(context, klass);
78         PackageManager pm = context.getPackageManager();
79
80         // We need the DONT_KILL_APP flag, otherwise we will be killed
81         // immediately because we are in the same app.
82         pm.setComponentEnabledSetting(name,
83             PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
84             PackageManager.DONT_KILL_APP);
85     }
86 }