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