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 
26 import com.android.camera.debug.Log;
27 
28 // Enable or disable camera-related activities based on "camera hal" in every boot up.
29 // This receiver runs when BOOT_COMPLETED intent is received.
30 public class SetActivitiesCameraReceiver extends BroadcastReceiver {
31     private static final Log.Tag TAG = new Log.Tag("SetActivitiesCameraReceiver");
32     private static final boolean CHECK_BACK_CAMERA_ONLY = false;
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         int component_state = (CHECK_BACK_CAMERA_ONLY
41             ? hasBackCamera(context) : hasCamera(context))
42             ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
43             : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
44 
45         Log.i(TAG, "component state is " + component_state);
46         for (int i = 0; i < ACTIVITIES.length; i++) {
47             setComponent(context, ACTIVITIES[i],
48                 component_state);
49         }
50     }
51 
hasCamera(Context context)52     private boolean hasCamera(Context context) {
53         PackageManager pm = context.getPackageManager();
54         return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)
55             || pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)
56             || pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_EXTERNAL);
57     }
58 
hasBackCamera(Context context)59     private boolean hasBackCamera(Context context) {
60         PackageManager pm = context.getPackageManager();
61         return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
62     }
63 
setComponent(Context context, String klass, final int enabledState)64     private void setComponent(Context context, String klass, final int enabledState) {
65         ComponentName name = new ComponentName(context, klass);
66         PackageManager pm = context.getPackageManager();
67 
68         // We need the DONT_KILL_APP flag, otherwise we will be killed
69         // immediately because we are in the same app.
70         pm.setComponentEnabledSetting(name,
71             enabledState,
72             PackageManager.DONT_KILL_APP);
73     }
74 }
75