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