davinci: Initial popup camera impl

Change-Id: I9b34ec87715febedfe173e9f9ef5ccdbf7521f35
This commit is contained in:
TheScarastic
2019-06-14 14:24:30 +05:30
committed by Arian
parent 5d15a19de5
commit 89e3be0eee
8 changed files with 415 additions and 1 deletions

View File

@@ -23,6 +23,9 @@ LOCAL_STATIC_ANDROID_LIBRARIES := \
androidx.core_core \
androidx.preference_preference
LOCAL_STATIC_JAVA_LIBRARIES := \
vendor.xiaomi.hardware.motor-V1.0-java
LOCAL_RESOURCE_DIR := \
$(LOCAL_PATH)/res \
$(TOP)/packages/resources/devicesettings/res

View File

@@ -45,6 +45,10 @@
android:permission="XiaomiDozeService">
</service>
<service android:name=".popupcamera.PopupCameraService"
android:permission="PopupCameraService">
</service>
<activity
android:name=".doze.DozeSettingsActivity"
android:label="@string/ambient_display_title"

View File

@@ -1,3 +1,7 @@
-keep class org.lineageos.settings.doze.* {
*;
}
-keep class org.lineageos.settings.popupcamera.* {
*;
}

View File

@@ -1,6 +1,6 @@
/*
* Copyright (C) 2015 The CyanogenMod Project
* 2017-2019 The LineageOS Project
* 2017-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.
@@ -23,6 +23,7 @@ import android.content.Intent;
import android.util.Log;
import org.lineageos.settings.doze.DozeUtils;
import org.lineageos.settings.popupcamera.PopupCameraUtils;
public class BootCompletedReceiver extends BroadcastReceiver {
@@ -33,5 +34,6 @@ public class BootCompletedReceiver extends BroadcastReceiver {
public void onReceive(final Context context, Intent intent) {
if (DEBUG) Log.d(TAG, "Received boot completed intent");
DozeUtils.checkDozeService(context);
PopupCameraUtils.startService(context);
}
}

View File

@@ -0,0 +1,35 @@
/*
* 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.settings.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_POPUP = 11;
public static final int MOTOR_STATUS_POPUP_JAM = 12;
public static final int MOTOR_STATUS_TAKEBACK = 13;
public static final int MOTOR_STATUS_TAKEBACK_JAM = 14;
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,174 @@
/*
* 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.settings.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.settings.R;
import org.lineageos.settings.utils.FileUtils;
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 long mClosedEvent;
private long mOpenEvent;
private Handler mHandler = new Handler(this);
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 || status == Constants.MOTOR_STATUS_POPUP_JAM
|| status == Constants.MOTOR_STATUS_TAKEBACK_JAM) {
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;
try {
if (cameraState.equals(Constants.OPEN_CAMERA_STATE)
&& mMotor.getMotorStatus() == Constants.MOTOR_STATUS_TAKEBACK) {
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) {
mMotor.takebackMotor(1);
mSensorManager.unregisterListener(mFreeFallListener, mFreeFallSensor);
}
} catch (RemoteException e) {
// Do nothing
}
}
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) 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.settings.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);
}
}

View File

@@ -0,0 +1,160 @@
/*
* Copyright (C) 2016 The CyanogenMod 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.settings.utils;
import android.util.Log;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public final class FileUtils {
private static final String TAG = "FileUtils";
private FileUtils() {
// This class is not supposed to be instantiated
}
/**
* Reads the first line of text from the given file.
* Reference {@link BufferedReader#readLine()} for clarification on what a line is
*
* @return the read line contents, or null on failure
*/
public static String readOneLine(String fileName) {
String line = null;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(fileName), 512);
line = reader.readLine();
} catch (FileNotFoundException e) {
Log.w(TAG, "No such file " + fileName + " for reading", e);
} catch (IOException e) {
Log.e(TAG, "Could not read from file " + fileName, e);
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
// Ignored, not much we can do anyway
}
}
return line;
}
/**
* Writes the given value into the given file
*
* @return true on success, false on failure
*/
public static boolean writeLine(String fileName, String value) {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(fileName));
writer.write(value);
} catch (FileNotFoundException e) {
Log.w(TAG, "No such file " + fileName + " for writing", e);
return false;
} catch (IOException e) {
Log.e(TAG, "Could not write to file " + fileName, e);
return false;
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
// Ignored, not much we can do anyway
}
}
return true;
}
/**
* Checks whether the given file exists
*
* @return true if exists, false if not
*/
public static boolean fileExists(String fileName) {
final File file = new File(fileName);
return file.exists();
}
/**
* Checks whether the given file is readable
*
* @return true if readable, false if not
*/
public static boolean isFileReadable(String fileName) {
final File file = new File(fileName);
return file.exists() && file.canRead();
}
/**
* Checks whether the given file is writable
*
* @return true if writable, false if not
*/
public static boolean isFileWritable(String fileName) {
final File file = new File(fileName);
return file.exists() && file.canWrite();
}
/**
* Deletes an existing file
*
* @return true if the delete was successful, false if not
*/
public static boolean delete(String fileName) {
final File file = new File(fileName);
boolean ok = false;
try {
ok = file.delete();
} catch (SecurityException e) {
Log.w(TAG, "SecurityException trying to delete " + fileName, e);
}
return ok;
}
/**
* Renames an existing file
*
* @return true if the rename was successful, false if not
*/
public static boolean rename(String srcPath, String dstPath) {
final File srcFile = new File(srcPath);
final File dstFile = new File(dstPath);
boolean ok = false;
try {
ok = srcFile.renameTo(dstFile);
} catch (SecurityException e) {
Log.w(TAG, "SecurityException trying to rename " + srcPath + " to " + dstPath, e);
} catch (NullPointerException e) {
Log.e(TAG, "NullPointerException trying to rename " + srcPath + " to " + dstPath, e);
}
return ok;
}
}