• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import 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.
30 public class DisableCameraReceiver extends BroadcastReceiver {
31     private static final String TAG = "G: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
onReceive(Context context, Intent intent)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 
hasCamera()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 
hasBackCamera()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 
disableComponent(Context context, String klass)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 }
86