Eduardo Pinar Menoyo | a82eb5d | 2020-04-23 19:49:46 +0200 | [diff] [blame] | 1 | package com.fairphone.moduledetect; |
| 2 | |
Luca Weiss | b7fbbf6 | 2021-11-12 08:45:20 +0100 | [diff] [blame] | 3 | import android.content.ComponentName; |
Eduardo Pinar Menoyo | a82eb5d | 2020-04-23 19:49:46 +0200 | [diff] [blame] | 4 | import android.content.Context; |
| 5 | import android.content.Intent; |
| 6 | import android.util.Log; |
| 7 | |
| 8 | import androidx.annotation.NonNull; |
| 9 | |
| 10 | public 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 Weiss | b7fbbf6 | 2021-11-12 08:45:20 +0100 | [diff] [blame] | 61 | |
| 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 Menoyo | a82eb5d | 2020-04-23 19:49:46 +0200 | [diff] [blame] | 66 | } |
| 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 | } |