1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-27 20:36:51 +01:00

Add camera implementation

This is to support watches with remote shutter applets that do not
act as remote triggers for other apps automatically.
This commit is contained in:
Martin.JM 2024-05-05 21:14:42 +02:00 committed by José Rebelo
parent d9863786de
commit 4c4ba623c4
14 changed files with 376 additions and 0 deletions

View File

@ -212,6 +212,10 @@ dependencies {
// testImplementation "ch.qos.logback:logback-classic:1.1.3"
// testImplementation "ch.qos.logback:logback-core:1.1.3"
implementation 'com.android.support.constraint:constraint-layout:2.0.4'
implementation "androidx.camera:camera-core:1.2.3"
implementation "androidx.camera:camera-camera2:1.2.3"
implementation 'androidx.camera:camera-view:1.2.3'
implementation 'androidx.camera:camera-lifecycle:1.2.3'
testImplementation "junit:junit:4.13.2"
testImplementation "org.mockito:mockito-core:2.28.2"
testImplementation "org.robolectric:robolectric:4.12"

View File

@ -74,6 +74,8 @@
<!-- Used for starting activities from the background with intents -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.CAMERA"/>
<!--
SDK 30 & Android 11 - Used for getting app name from notifications, and for starting
services from other packages via intents, when targeting Android API level 30 or later
@ -99,6 +101,9 @@
<uses-feature
android:name="android.software.companion_device_setup"
android:required="false" />
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<application
android:name=".GBApplication"
@ -875,6 +880,11 @@
android:name=".activities.dashboard.DashboardCalendarActivity"
android:label="@string/menuitem_calendar"
android:parentActivityName=".activities.ControlCenterv2" />
<activity
android:name=".activities.CameraActivity"
android:launchMode="singleInstance"
android:exported="false" />
</application>
</manifest>

View File

@ -0,0 +1,218 @@
/* Copyright (C) 2024 Martin.JM
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities;
import android.Manifest;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Toast;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.ImageCapture;
import androidx.camera.core.ImageCaptureException;
import androidx.camera.core.Preview;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.view.PreviewView;
import androidx.core.content.ContextCompat;
import com.google.common.util.concurrent.ListenableFuture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ExecutionException;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCameraRemote;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
public class CameraActivity extends AppCompatActivity {
private static final Logger LOG = LoggerFactory.getLogger(CameraActivity.class);
public static final String intentExtraEvent = "EVENT";
private ListenableFuture<ProcessCameraProvider> cameraProviderListenableFuture;
private ImageCapture imageCapture;
private boolean reportClosing = true;
public static boolean supportsCamera() {
return GBApplication.getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
public static boolean hasCameraPermission() {
return GBApplication.getContext().checkCallingOrSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
if (!supportsCamera()) {
LOG.error("No camera support");
GB.toast(getString(R.string.toast_camera_support_required), Toast.LENGTH_SHORT, GB.ERROR);
GBApplication.deviceService().onCameraStatusChange(GBDeviceEventCameraRemote.Event.EXCEPTION, null);
reportClosing = false;
finish();
return;
}
if (!hasCameraPermission()) {
LOG.info("Requesting camera permission");
ActivityResultLauncher<String> requestPermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), new ActivityResultCallback<Boolean>() {
@Override
public void onActivityResult(Boolean isGranted) {
if (isGranted) {
initCamera();
} else {
LOG.error("Did not receive camera permission");
GB.toast(getString(R.string.toast_camera_permission_required), Toast.LENGTH_SHORT, GB.ERROR);
GBApplication.deviceService().onCameraStatusChange(GBDeviceEventCameraRemote.Event.EXCEPTION, null);
reportClosing = false;
finish();
}
}
});
requestPermissionLauncher.launch(Manifest.permission.CAMERA);
return;
}
initCamera();
}
private void initCamera() {
cameraProviderListenableFuture = ProcessCameraProvider.getInstance(this);
cameraProviderListenableFuture.addListener(new Runnable() {
@Override
public void run() {
try {
ProcessCameraProvider cameraProvider = cameraProviderListenableFuture.get();
PreviewView previewView = findViewById(R.id.preview);
Preview preview = new Preview.Builder().build();
CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK) // TODO: make setting
.build();
preview.setSurfaceProvider(previewView.getSurfaceProvider());
imageCapture = new ImageCapture.Builder()
.setTargetRotation(preview.getTargetRotation())
.build();
cameraProvider.bindToLifecycle(
CameraActivity.this,
cameraSelector,
imageCapture,
preview
);
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}, ContextCompat.getMainExecutor(this));
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (!intent.hasExtra(intentExtraEvent)) {
this.finish();
return;
}
GBDeviceEventCameraRemote.Event event = GBDeviceEventCameraRemote.intToEvent(intent.getIntExtra(intentExtraEvent, 0));
LOG.info("Camera received event: " + event.name());
// Nothing to do for unknown events
if (event == GBDeviceEventCameraRemote.Event.CLOSE_CAMERA) {
finish();
} else if (event == GBDeviceEventCameraRemote.Event.OPEN_CAMERA) {
GBApplication.deviceService().onCameraStatusChange(GBDeviceEventCameraRemote.Event.OPEN_CAMERA, null);
} else if (event == GBDeviceEventCameraRemote.Event.TAKE_PICTURE) {
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Images.Media.TITLE, "Gadgetbridge photo");
contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
ImageCapture.OutputFileOptions outputFileOptions = new ImageCapture.OutputFileOptions.Builder(
getContentResolver(),
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
contentValues
).build();
imageCapture.takePicture(outputFileOptions, ContextCompat.getMainExecutor(this), new ImageCapture.OnImageSavedCallback() {
@Override
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
if (outputFileResults.getSavedUri() == null) {
// Shouldn't ever happen
GBApplication.deviceService().onCameraStatusChange(GBDeviceEventCameraRemote.Event.EXCEPTION, null);
return;
}
// TODO: improve feedback that the photo has been taken
GB.toast(
String.format(getString(R.string.toast_camera_photo_taken),
outputFileResults.getSavedUri().getPath()),
Toast.LENGTH_LONG,
GB.INFO
);
GBApplication.deviceService().onCameraStatusChange(
GBDeviceEventCameraRemote.Event.TAKE_PICTURE,
outputFileResults.getSavedUri().getPath()
);
}
@Override
public void onError(@NonNull ImageCaptureException exception) {
LOG.error("Failed to save image", exception);
GBApplication.deviceService().onCameraStatusChange(GBDeviceEventCameraRemote.Event.EXCEPTION, null);
}
});
}
}
@Override
protected void onStop() {
super.onStop();
if (reportClosing)
GBApplication.deviceService().onCameraStatusChange(GBDeviceEventCameraRemote.Event.CLOSE_CAMERA, null);
}
}

View File

@ -102,6 +102,7 @@ import nodomain.freeyourgadget.gadgetbridge.adapter.SpinnerWithIconAdapter;
import nodomain.freeyourgadget.gadgetbridge.adapter.SpinnerWithIconItem;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCameraRemote;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
@ -752,6 +753,18 @@ public class DebugActivity extends AbstractGBActivity {
handler.postDelayed(runnable, delay);
}
});
Button cameraOpenButton = findViewById(R.id.cameraOpen);
cameraOpenButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent cameraIntent = new Intent(getApplicationContext(), CameraActivity.class);
cameraIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
cameraIntent.putExtra(CameraActivity.intentExtraEvent, GBDeviceEventCameraRemote.eventToInt(GBDeviceEventCameraRemote.Event.OPEN_CAMERA));
getApplicationContext().startActivity(cameraIntent);
}
});
}
@RequiresApi(Build.VERSION_CODES.O)

View File

@ -0,0 +1,57 @@
/* Copyright (C) 2024 Martin.JM
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.deviceevents;
public class GBDeviceEventCameraRemote extends GBDeviceEvent {
public Event event = Event.UNKNOWN;
public enum Event {
UNKNOWN,
OPEN_CAMERA,
TAKE_PICTURE,
CLOSE_CAMERA,
EXCEPTION
}
static public int eventToInt(Event event) {
switch (event) {
case UNKNOWN:
return 0;
case OPEN_CAMERA:
return 1;
case TAKE_PICTURE:
return 2;
case CLOSE_CAMERA:
return 3;
}
return -1;
}
static public Event intToEvent(int event) {
switch (event) {
case 0:
return Event.UNKNOWN;
case 1:
return Event.OPEN_CAMERA;
case 2:
return Event.TAKE_PICTURE;
case 3:
return Event.CLOSE_CAMERA;
}
return Event.EXCEPTION;
}
}

View File

@ -26,6 +26,7 @@ import java.util.ArrayList;
import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.capabilities.loyaltycards.LoyaltyCard;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCameraRemote;
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec;
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
@ -151,4 +152,6 @@ public interface EventHandler {
void onSetGpsLocation(Location location);
void onSleepAsAndroidAction(String action, Bundle extras);
void onCameraStatusChange(GBDeviceEventCameraRemote.Event event, String filename);
}

View File

@ -35,6 +35,7 @@ import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.capabilities.loyaltycards.LoyaltyCard;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCameraRemote;
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec;
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
@ -558,4 +559,13 @@ public class GBDeviceService implements DeviceService {
}
invokeService(intent);
}
@Override
public void onCameraStatusChange(GBDeviceEventCameraRemote.Event event, String filename) {
Intent intent = createIntent().setAction(ACTION_CAMERA_STATUS_CHANGE);
intent.putExtra(EXTRA_CAMERA_EVENT, GBDeviceEventCameraRemote.eventToInt(event));
if (event == GBDeviceEventCameraRemote.Event.TAKE_PICTURE)
intent.putExtra(EXTRA_CAMERA_FILENAME, filename);
invokeService(intent);
}
}

View File

@ -79,6 +79,7 @@ public interface DeviceService extends EventHandler {
String ACTION_SET_GPS_LOCATION = PREFIX + ".action.set_gps_location";
String ACTION_SET_LED_COLOR = PREFIX + ".action.set_led_color";
String ACTION_POWER_OFF = PREFIX + ".action.power_off";
String ACTION_CAMERA_STATUS_CHANGE = PREFIX + ".action.camera_status_change";
String ACTION_SLEEP_AS_ANDROID = ".action.sleep_as_android";
String EXTRA_SLEEP_AS_ANDROID_ACTION = "sleepasandroid_action";
@ -144,6 +145,8 @@ public interface DeviceService extends EventHandler {
String EXTRA_LED_COLOR = "led_color";
String EXTRA_GPS_LOCATION = "gps_location";
String EXTRA_RESET_FLAGS = "reset_flags";
String EXTRA_CAMERA_EVENT = "event";
String EXTRA_CAMERA_FILENAME = "filename";
/**
* Use EXTRA_REALTIME_SAMPLE instead

View File

@ -58,6 +58,7 @@ import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.CameraActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.FindPhoneActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.appmanager.AbstractAppManagerFragment;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
@ -69,6 +70,7 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCameraRemote;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventDisplayMessage;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventFindPhone;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventFmFrequency;
@ -207,6 +209,8 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
handleGBDeviceEvent((GBDeviceEventMusicControl) deviceEvent);
} else if (deviceEvent instanceof GBDeviceEventCallControl) {
handleGBDeviceEvent((GBDeviceEventCallControl) deviceEvent);
} else if (deviceEvent instanceof GBDeviceEventCameraRemote) {
handleGBDeviceEvent((GBDeviceEventCameraRemote) deviceEvent);
} else if (deviceEvent instanceof GBDeviceEventVersionInfo) {
handleGBDeviceEvent((GBDeviceEventVersionInfo) deviceEvent);
} else if (deviceEvent instanceof GBDeviceEventAppInfo) {
@ -338,6 +342,13 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
context.sendBroadcast(callIntent);
}
protected void handleGBDeviceEvent(GBDeviceEventCameraRemote cameraRemoteEvent) {
Intent cameraIntent = new Intent(getContext(), CameraActivity.class);
cameraIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
cameraIntent.putExtra(CameraActivity.intentExtraEvent, GBDeviceEventCameraRemote.eventToInt(cameraRemoteEvent.event));
getContext().startActivity(cameraIntent);
}
protected void handleGBDeviceEvent(GBDeviceEventVersionInfo infoEvent) {
Context context = getContext();
LOG.info("Got event for VERSION_INFO: " + infoEvent);
@ -1187,4 +1198,7 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
public void onSleepAsAndroidAction(String action, Bundle extras) {
}
@Override
public void onCameraStatusChange(GBDeviceEventCameraRemote.Event event, String filename) {}
}

View File

@ -62,7 +62,9 @@ import nodomain.freeyourgadget.gadgetbridge.GBException;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.HeartRateUtils;
import nodomain.freeyourgadget.gadgetbridge.capabilities.loyaltycards.LoyaltyCard;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCameraRemote;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.CameraRemote;
import nodomain.freeyourgadget.gadgetbridge.externalevents.AlarmClockReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.AlarmReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothConnectReceiver;
@ -1093,6 +1095,14 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
deviceSupport.onSleepAsAndroidAction(sleepAsAndroidAction, intent.getExtras());
}
break;
case ACTION_CAMERA_STATUS_CHANGE:
final GBDeviceEventCameraRemote.Event event = GBDeviceEventCameraRemote.intToEvent(intent.getIntExtra(EXTRA_CAMERA_EVENT, -1));
String filename = null;
if (event == GBDeviceEventCameraRemote.Event.TAKE_PICTURE) {
filename = intent.getStringExtra(EXTRA_CAMERA_FILENAME);
}
deviceSupport.onCameraStatusChange(event, filename);
break;
}
}

View File

@ -32,6 +32,7 @@ import java.util.EnumSet;
import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.capabilities.loyaltycards.LoyaltyCard;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCameraRemote;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec;
@ -520,4 +521,12 @@ public class ServiceDeviceSupport implements DeviceSupport {
}
delegate.onSleepAsAndroidAction(action, extras);
}
@Override
public void onCameraStatusChange(GBDeviceEventCameraRemote.Event event, String filename) {
if (checkBusy("camera status")) {
return;
}
delegate.onCameraStatusChange(event, filename);
}
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.camera.view.PreviewView
android:id="@+id/preview"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -301,6 +301,14 @@
grid:layout_columnSpan="2"
grid:layout_gravity="fill_horizontal"
android:text="@string/debug_companion_pair_current" />
<Button
android:id="@+id/cameraOpen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
grid:layout_columnSpan="2"
grid:layout_gravity="fill_horizontal"
android:text="@string/open_camera" />
</androidx.gridlayout.widget.GridLayout>
</ScrollView>

View File

@ -2842,4 +2842,10 @@
<string name="pref_title_cycling_persistence_interval">Persistence interval</string>
<string name="chart_cycling_point_label_distance">Today: %.1f km\nTotal: %.1f km</string>
<string name="chart_cycling_point_label_speed">%.1f km/h</string>
<!-- Camera strings -->
<string name="open_camera">Open Camera</string>
<string name="toast_camera_permission_required">Camera permission is required for this function.</string>
<string name="toast_camera_support_required">Camera support is required for this function.</string>
<string name="toast_camera_photo_taken">Photo has been taken and saved at: %s</string>
</resources>