lmi: parts: Initial popup camera impl

Change-Id: Ib3f1840401c5fb6b77656da4ee0f8e5f387aaaec
This commit is contained in:
TheScarastic
2019-06-14 14:24:30 +05:30
committed by Sebastiano Barezzi
parent fb90f43101
commit 9e3bf24941
7 changed files with 290 additions and 0 deletions

View File

@@ -15,5 +15,10 @@ android_app {
static_libs: [
"org.lineageos.settings.resources",
"//hardware/xiaomi:vendor.xiaomi.hardware.motor-V1.0-java",
],
optimize: {
proguard_flags_files: ["proguard.flags"],
},
}

View File

@@ -40,5 +40,9 @@
</intent-filter>
</receiver>
<service
android:name=".popupcamera.PopupCameraService"
android:permission="PopupCameraService">
</service>
</application>
</manifest>

4
parts/proguard.flags Normal file
View File

@@ -0,0 +1,4 @@
-keep class org.lineageos.devicesettings.popupcamera.* {
*;
}

View File

@@ -22,6 +22,8 @@ import android.content.Context;
import android.content.Intent;
import android.util.Log;
import org.lineageos.devicesettings.popupcamera.PopupCameraUtils;
public class BootCompletedReceiver extends BroadcastReceiver {
private static final boolean DEBUG = false;
@@ -30,5 +32,6 @@ public class BootCompletedReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
if (DEBUG) Log.d(TAG, "Received boot completed intent");
PopupCameraUtils.startService(context);
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright (C) 2020 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lineageos.devicesettings.popupcamera;
public class Constants {
public static final int FREE_FALL_SENSOR_ID = 33171042;
public static final int CAMERA_EVENT_DELAY_TIME = 100; // ms
public static final int MSG_CAMERA_CLOSED = 1001;
public static final int MSG_CAMERA_OPEN = 1002;
public static final int MOTOR_STATUS_CALIB_ERROR = 18;
public static final int MOTOR_STATUS_CALIB_OK = 17;
public static final int MOTOR_STATUS_CANCELED = 20;
public static final int MOTOR_STATUS_DROPPED = 16;
public static final int MOTOR_STATUS_INVALID = 10;
public static final int MOTOR_STATUS_MAX = 20;
public static final int MOTOR_STATUS_MIN = 10;
public static final int MOTOR_STATUS_NOT_INIT = -1;
public static final int MOTOR_STATUS_POPUP_JAMMED = 12;
public static final int MOTOR_STATUS_POPUP_OK = 11;
public static final int MOTOR_STATUS_PRESSED = 15;
public static final int MOTOR_STATUS_REQUEST_CALIB = 19;
public static final int MOTOR_STATUS_TAKEBACK_JAMMED = 14;
public static final int MOTOR_STATUS_TAKEBACK_OK = 13;
public static final String CLOSE_CAMERA_STATE = "0";
public static final String OPEN_CAMERA_STATE = "1";
public static final String FRONT_CAMERA_ID = "1";
}

View File

@@ -0,0 +1,197 @@
/*
* Copyright (C) 2019 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lineageos.devicesettings.popupcamera;
import android.annotation.NonNull;
import android.app.Service;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.hardware.camera2.CameraManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.UserHandle;
import android.util.Log;
import org.lineageos.devicesettings.R;
import vendor.xiaomi.hardware.motor.V1_0.IMotor;
public class PopupCameraService extends Service implements Handler.Callback {
private static final String TAG = "PopupCameraService";
private static final boolean DEBUG = false;
private boolean mMotorBusy = false;
private long mClosedEvent;
private long mOpenEvent;
private IMotor mMotor = null;
private SensorManager mSensorManager;
private Sensor mFreeFallSensor;
private CameraManager.AvailabilityCallback availabilityCallback =
new CameraManager.AvailabilityCallback() {
@Override
public void onCameraAvailable(@NonNull String cameraId) {
super.onCameraAvailable(cameraId);
if (cameraId.equals(Constants.FRONT_CAMERA_ID)) {
mClosedEvent = SystemClock.elapsedRealtime();
if (SystemClock.elapsedRealtime() - mOpenEvent <
Constants.CAMERA_EVENT_DELAY_TIME &&
mHandler.hasMessages(Constants.MSG_CAMERA_OPEN)) {
mHandler.removeMessages(Constants.MSG_CAMERA_OPEN);
}
mHandler.sendEmptyMessageDelayed(Constants.MSG_CAMERA_CLOSED,
Constants.CAMERA_EVENT_DELAY_TIME);
}
}
@Override
public void onCameraUnavailable(@NonNull String cameraId) {
super.onCameraAvailable(cameraId);
if (cameraId.equals(Constants.FRONT_CAMERA_ID)) {
mOpenEvent = SystemClock.elapsedRealtime();
if (SystemClock.elapsedRealtime() - mClosedEvent <
Constants.CAMERA_EVENT_DELAY_TIME &&
mHandler.hasMessages(Constants.MSG_CAMERA_CLOSED)) {
mHandler.removeMessages(Constants.MSG_CAMERA_CLOSED);
}
mHandler.sendEmptyMessageDelayed(Constants.MSG_CAMERA_OPEN,
Constants.CAMERA_EVENT_DELAY_TIME);
}
}
};
private SensorEventListener mFreeFallListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
if (event.values[0] == 2.0f) {
updateMotor(Constants.CLOSE_CAMERA_STATE);
goBackHome();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
@Override
public void onCreate() {
CameraManager cameraManager = getSystemService(CameraManager.class);
cameraManager.registerAvailabilityCallback(availabilityCallback, null);
mSensorManager = getSystemService(SensorManager.class);
mFreeFallSensor =
mSensorManager.getDefaultSensor(Constants.FREE_FALL_SENSOR_ID);
try {
mMotor = IMotor.getService();
int status = mMotor.getMotorStatus();
if (status == Constants.MOTOR_STATUS_POPUP_OK ||
status == Constants.MOTOR_STATUS_POPUP_JAMMED ||
status == Constants.MOTOR_STATUS_TAKEBACK_JAMMED) {
mHandler.sendEmptyMessage(Constants.MSG_CAMERA_CLOSED);
}
} catch (RemoteException e) {
// Do nothing
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (DEBUG)
Log.d(TAG, "Starting service");
return START_STICKY;
}
@Override
public void onDestroy() {
if (DEBUG)
Log.d(TAG, "Destroying service");
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void updateMotor(String cameraState) {
if (mMotor == null) {
return;
}
final Runnable r = () -> {
mMotorBusy = true;
try {
if (cameraState.equals(Constants.OPEN_CAMERA_STATE) &&
mMotor.getMotorStatus() == Constants.MOTOR_STATUS_TAKEBACK_OK) {
mMotor.popupMotor(1);
mSensorManager.registerListener(mFreeFallListener, mFreeFallSensor,
SensorManager.SENSOR_DELAY_NORMAL);
} else if (cameraState.equals(Constants.CLOSE_CAMERA_STATE) &&
mMotor.getMotorStatus() == Constants.MOTOR_STATUS_POPUP_OK) {
mMotor.takebackMotor(1);
mSensorManager.unregisterListener(mFreeFallListener, mFreeFallSensor);
} else {
mMotorBusy = false;
}
} catch (RemoteException e) {
// Do nothing
}
mHandler.postDelayed(() -> mMotorBusy = false, 1200);
};
if (mMotorBusy) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (mMotorBusy) {
mHandler.postDelayed(this, 100);
} else {
mHandler.post(r);
}
}
}, 100);
} else {
mHandler.post(r);
}
}
}
public void goBackHome() {
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityAsUser(homeIntent, null, UserHandle.CURRENT);
}
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case Constants.MSG_CAMERA_CLOSED: {
updateMotor(Constants.CLOSE_CAMERA_STATE);
} break;
case Constants.MSG_CAMERA_OPEN: {
updateMotor(Constants.OPEN_CAMERA_STATE);
} break;
}
return true;
}
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright (C) 2019 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lineageos.devicesettings.popupcamera;
import android.content.Context;
import android.content.Intent;
import android.os.UserHandle;
public class PopupCameraUtils {
private static final String TAG = "PopupCameraUtils";
private static final boolean DEBUG = false;
public static void startService(Context context) {
context.startServiceAsUser(new Intent(context, PopupCameraService.class),
UserHandle.CURRENT);
}
}