blob: c204c02df083c94f5764cb60dc8f5f1ffba8fca1 [file] [log] [blame]
Eduardo Pinar Menoyoa82eb5d2020-04-23 19:49:46 +02001package com.fairphone.moduledetect;
2
Luca Weissb7fbbf62021-11-12 08:45:20 +01003import android.content.ComponentName;
Eduardo Pinar Menoyoa82eb5d2020-04-23 19:49:46 +02004import android.content.Context;
5import android.content.Intent;
6import android.util.Log;
7
8import androidx.annotation.NonNull;
9
10public class ModuleDetectUtils {
11
12 private static final String TAG = "ModuleDetect";
13
14 /**
15 * Action to handle a change of camera(s).
16 */
17 private static final String ACTION_HANDLE_CAMERA_CHANGED =
18 "com.fairphone.moduledetect.ACTION_HANDLE_CAMERA_CHANGED";
19
20 private static final String ACTION_HANDLE_RECEIVER_MODULE_CHANGED =
21 "com.fairphone.psensor.receivers.ACTION_HANDLE_RECEIVER_MODULE_CHANGED";
22
23 private static final String EXTRA_FRONT_CAMERA_CHANGED =
24 "com.fairphone.moduledetect.EXTRA_FRONT_CAMERA_CHANGED";
25
26 private static final String EXTRA_REAR_CAMERA_CHANGED =
27 "com.fairphone.moduledetect.EXTRA_REAR_CAMERA_CHANGED";
28
29 private static final String PERMISSION_CALIBRATE_PROXIMITY_SENSOR =
30 "com.fairphone.psensor.permission.CALIBRATE_PROXIMITY_SENSOR";
31
32 static void handleActionModuleChanged(
33 @NonNull Context context,
34 boolean hasReceiverModuleChanged,
35 boolean hasCameraModuleChanged
36 ) {
37 if (!hasReceiverModuleChanged && !hasCameraModuleChanged) {
38 Log.w(TAG, "handleActionComponentSwapped: No camera changed; Doing nothing...");
39 } else {
40 Log.w(TAG, "handleActionComponentSwapped: Camera change detected; Launching CameraSwapInfo!");
41 sendCameraSwapBroadcast(context, hasReceiverModuleChanged, hasCameraModuleChanged);
42
43 if (hasReceiverModuleChanged) {
44 Log.w(TAG, "handleActionCameraSwapped: Receiver module change detected; Launching ProximitySensor!");
45 sendProximitySensorBroadcast(context);
46 }
47 }
48 }
49
50 private static void sendCameraSwapBroadcast(
51 @NonNull Context context,
52 boolean hasReceiverModuleChanged,
53 boolean hasCameraModuleChanged
54 ) {
55 Intent intent = new Intent(ACTION_HANDLE_CAMERA_CHANGED);
56 intent.putExtra(EXTRA_FRONT_CAMERA_CHANGED, hasReceiverModuleChanged);
57 intent.putExtra(EXTRA_REAR_CAMERA_CHANGED, hasCameraModuleChanged);
58 // Anyone can receive this broadcast; CameraSwapInfo is the most important receiver, but any
59 // (third party) camera app can listen to it as well.
60 context.sendBroadcast(intent);
Luca Weissb7fbbf62021-11-12 08:45:20 +010061
62 // Also send intent explicitly to Camera app, to avoid implicit broadcast restrictions
63 ComponentName componentName = new ComponentName("com.android.camera2", "com.android.camera.CameraSwapBroadcastReceiver");
64 intent.setComponent(componentName);
65 context.sendBroadcast(intent);
Eduardo Pinar Menoyoa82eb5d2020-04-23 19:49:46 +020066 }
67
68 private static void sendProximitySensorBroadcast(@NonNull Context context) {
69 Intent intent = new Intent(ACTION_HANDLE_RECEIVER_MODULE_CHANGED);
70 context.sendBroadcast(intent, PERMISSION_CALIBRATE_PROXIMITY_SENSOR);
71 }
72}