lmi: parts: Add motor calibration

Change-Id: I6233685b3aa7567ad5919858699eeaa636c9b08b
This commit is contained in:
jhenrique09
2019-11-27 05:52:23 +00:00
committed by Sebastiano Barezzi
parent c7644d9a21
commit 3279195dc9
3 changed files with 121 additions and 0 deletions

View File

@@ -22,6 +22,7 @@
android:sharedUserId="android.uid.system">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-sdk
android:minSdkVersion="24"

View File

@@ -25,4 +25,16 @@
<string name="popup_title_jijia">Mecha</string>
<string name="popup_title_chilun">Gearwheel</string>
<string name="popup_title_cangmen">Cabin door</string>
<!-- Popup camera strings -->
<string name="popup_camera_tip">Warning</string>
<string name="popup_camera_takeback_failed_times_calibrate">Couldn\'t close front camera multiple times. Try calibrating the camera.</string>
<string name="popup_camera_popup_failed_times_calibrate">Couldn\'t open front camera multiple times. Try calibrating the camera.</string>
<string name="popup_camera_calibrate_running">Front camera cannot be used during calibration.</string>
<string name="popup_camera_calibrate_now">Calibrate</string>
<string name="popup_camera_calibrate_failed">Couldn\'t calibrate</string>
<string name="popup_camera_calibrate_success">Calibrated successfully. You can open the front camera now.</string>
<string name="stop_operate_camera_frequently">You\'re opening the front camera too frequently.</string>
<string name="takeback_camera_front_failed">Couldn\'t close front camera. Try again.</string>
<string name="popup_camera_front_failed">Couldn\'t open front camera. Try again.</string>
</resources>

View File

@@ -17,8 +17,11 @@
package org.lineageos.devicesettings.popupcamera;
import android.annotation.NonNull;
import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
@@ -33,9 +36,13 @@ import android.os.RemoteException;
import android.os.SystemClock;
import android.os.UserHandle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;
import org.lineageos.devicesettings.R;
import org.lineageos.devicesettings.utils.FileUtils;
import vendor.xiaomi.hardware.motor.V1_0.IMotor;
import vendor.xiaomi.hardware.motor.V1_0.IMotorCallback;
import vendor.xiaomi.hardware.motor.V1_0.MotorEvent;
public class PopupCameraService extends Service implements Handler.Callback {
@@ -49,6 +56,10 @@ public class PopupCameraService extends Service implements Handler.Callback {
private Handler mHandler = new Handler(this);
private IMotor mMotor = null;
private IMotorCallback mMotorStatusCallback;
private boolean mMotorCalibrating = false;
private boolean mErrorDialogShowing;
private final Object mLock = new Object();
private PopupCameraPreferences mPopupCameraPreferences;
private SensorManager mSensorManager;
private Sensor mFreeFallSensor;
@@ -128,6 +139,8 @@ public class PopupCameraService extends Service implements Handler.Callback {
try {
mMotor = IMotor.getService();
mMotorStatusCallback = new MotorStatusCallback();
mMotor.setMotorCallback(mMotorStatusCallback);
int status = mMotor.getMotorStatus();
if (status == Constants.MOTOR_STATUS_POPUP_OK ||
status == Constants.MOTOR_STATUS_POPUP_JAMMED ||
@@ -139,6 +152,44 @@ public class PopupCameraService extends Service implements Handler.Callback {
}
}
private final class MotorStatusCallback extends IMotorCallback.Stub {
public MotorStatusCallback() {}
@Override
public void onNotify(MotorEvent event) {
int status = event.vaalue;
int cookie = event.cookie;
if (DEBUG)
Log.d(TAG, "onNotify: cookie=" + cookie + ",status=" + status);
synchronized (mLock) {
if (status == Constants.MOTOR_STATUS_CALIB_OK ||
status == Constants.MOTOR_STATUS_CALIB_ERROR) {
mMotorCalibrating = false;
showCalibrationResult(status);
} else if (status == Constants.MOTOR_STATUS_PRESSED) {
updateMotor(Constants.CLOSE_CAMERA_STATE);
goBackHome();
} else if (status == Constants.MOTOR_STATUS_POPUP_JAMMED ||
status == Constants.MOTOR_STATUS_TAKEBACK_JAMMED) {
showErrorDialog();
}
}
}
}
private void calibrateMotor() {
synchronized (mLock) {
if (mMotorCalibrating || mMotor == null)
return;
try {
mMotorCalibrating = true;
mMotor.calibration();
} catch (RemoteException e) {
// Do nothing
}
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (DEBUG)
@@ -165,6 +216,9 @@ public class PopupCameraService extends Service implements Handler.Callback {
final Runnable r = () -> {
mMotorBusy = true;
try {
int status = mMotor.getMotorStatus();
if (DEBUG)
Log.d(TAG, "updateMotor: status=" + status);
if (cameraState.equals(Constants.OPEN_CAMERA_STATE) &&
mMotor.getMotorStatus() == Constants.MOTOR_STATUS_TAKEBACK_OK) {
lightUp();
@@ -180,6 +234,13 @@ public class PopupCameraService extends Service implements Handler.Callback {
mSensorManager.unregisterListener(mFreeFallListener, mFreeFallSensor);
} else {
mMotorBusy = false;
if (status == Constants.MOTOR_STATUS_REQUEST_CALIB ||
status == Constants.MOTOR_STATUS_POPUP_JAMMED ||
status == Constants.MOTOR_STATUS_TAKEBACK_JAMMED ||
status == Constants.MOTOR_STATUS_CALIB_ERROR) {
showErrorDialog();
}
return;
}
} catch (RemoteException e) {
// Do nothing
@@ -214,6 +275,53 @@ public class PopupCameraService extends Service implements Handler.Callback {
}, 1200);
}
}
private void showCalibrationResult(int status) {
mHandler.post(() -> {
Toast
.makeText(PopupCameraService.this,
status == Constants.MOTOR_STATUS_CALIB_OK
? R.string.popup_camera_calibrate_success
: R.string.popup_camera_calibrate_failed,
Toast.LENGTH_LONG)
.show();
});
}
private void showErrorDialog() {
if (mErrorDialogShowing) {
return;
}
mErrorDialogShowing = true;
goBackHome();
mHandler.post(() -> {
Resources res = getResources();
String cameraState = "-1";
int dialogMessageResId =
cameraState.equals(Constants.CLOSE_CAMERA_STATE)
? R.string.popup_camera_takeback_failed_times_calibrate
: R.string.popup_camera_popup_failed_times_calibrate;
AlertDialog alertDialog =
new AlertDialog.Builder(this, R.style.SystemAlertDialogTheme)
.setTitle(res.getString(R.string.popup_camera_tip))
.setMessage(res.getString(dialogMessageResId))
.setPositiveButton(
res.getString(R.string.popup_camera_calibrate_now),
(dialog, which) -> { calibrateMotor(); })
.setNegativeButton(res.getString(android.R.string.cancel), null)
.create();
alertDialog.getWindow().setType(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.show();
alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
mErrorDialogShowing = false;
}
});
});
}
private void playSoundEffect(String state) {
int soundEffect =