2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.camera;
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;
26 import com.android.camera.debug.Log;
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",
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
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]);
52 // Disable this receiver so it won't run again.
53 disableComponent(context, "com.android.camera.DisableCameraReceiver");
56 private boolean hasCamera() {
57 int n = android.hardware.Camera.getNumberOfCameras();
58 Log.i(TAG, "number of camera: " + n);
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);
72 Log.i(TAG, "no back camera");
76 private void disableComponent(Context context, String klass) {
77 ComponentName name = new ComponentName(context, klass);
78 PackageManager pm = context.getPackageManager();
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);