blob: aea6ec5b990f79fdef31136eac5c730c456154f4 [file] [log] [blame]
Michael Kolb8872c232013-01-29 10:33:22 -08001/*
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
17package com.android.camera;
18
19import android.content.BroadcastReceiver;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
24import android.hardware.Camera.CameraInfo;
Angus Kong2bca2102014-03-11 16:27:30 -070025
26import com.android.camera.debug.Log;
Michael Kolb8872c232013-01-29 10:33:22 -080027
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.
31public class DisableCameraReceiver extends BroadcastReceiver {
Angus Kong2bca2102014-03-11 16:27:30 -070032 private static final Log.Tag TAG = new Log.Tag("DisableCamRcver");
Michael Kolb8872c232013-01-29 10:33:22 -080033 private static final boolean CHECK_BACK_CAMERA_ONLY = true;
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}