blob: c204c02df083c94f5764cb60dc8f5f1ffba8fca1 [file] [log] [blame]
package com.fairphone.moduledetect;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import androidx.annotation.NonNull;
public class ModuleDetectUtils {
private static final String TAG = "ModuleDetect";
/**
* Action to handle a change of camera(s).
*/
private static final String ACTION_HANDLE_CAMERA_CHANGED =
"com.fairphone.moduledetect.ACTION_HANDLE_CAMERA_CHANGED";
private static final String ACTION_HANDLE_RECEIVER_MODULE_CHANGED =
"com.fairphone.psensor.receivers.ACTION_HANDLE_RECEIVER_MODULE_CHANGED";
private static final String EXTRA_FRONT_CAMERA_CHANGED =
"com.fairphone.moduledetect.EXTRA_FRONT_CAMERA_CHANGED";
private static final String EXTRA_REAR_CAMERA_CHANGED =
"com.fairphone.moduledetect.EXTRA_REAR_CAMERA_CHANGED";
private static final String PERMISSION_CALIBRATE_PROXIMITY_SENSOR =
"com.fairphone.psensor.permission.CALIBRATE_PROXIMITY_SENSOR";
static void handleActionModuleChanged(
@NonNull Context context,
boolean hasReceiverModuleChanged,
boolean hasCameraModuleChanged
) {
if (!hasReceiverModuleChanged && !hasCameraModuleChanged) {
Log.w(TAG, "handleActionComponentSwapped: No camera changed; Doing nothing...");
} else {
Log.w(TAG, "handleActionComponentSwapped: Camera change detected; Launching CameraSwapInfo!");
sendCameraSwapBroadcast(context, hasReceiverModuleChanged, hasCameraModuleChanged);
if (hasReceiverModuleChanged) {
Log.w(TAG, "handleActionCameraSwapped: Receiver module change detected; Launching ProximitySensor!");
sendProximitySensorBroadcast(context);
}
}
}
private static void sendCameraSwapBroadcast(
@NonNull Context context,
boolean hasReceiverModuleChanged,
boolean hasCameraModuleChanged
) {
Intent intent = new Intent(ACTION_HANDLE_CAMERA_CHANGED);
intent.putExtra(EXTRA_FRONT_CAMERA_CHANGED, hasReceiverModuleChanged);
intent.putExtra(EXTRA_REAR_CAMERA_CHANGED, hasCameraModuleChanged);
// Anyone can receive this broadcast; CameraSwapInfo is the most important receiver, but any
// (third party) camera app can listen to it as well.
context.sendBroadcast(intent);
// Also send intent explicitly to Camera app, to avoid implicit broadcast restrictions
ComponentName componentName = new ComponentName("com.android.camera2", "com.android.camera.CameraSwapBroadcastReceiver");
intent.setComponent(componentName);
context.sendBroadcast(intent);
}
private static void sendProximitySensorBroadcast(@NonNull Context context) {
Intent intent = new Intent(ACTION_HANDLE_RECEIVER_MODULE_CHANGED);
context.sendBroadcast(intent, PERMISSION_CALIBRATE_PROXIMITY_SENSOR);
}
}