diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index d7b36981..0d4d2a25 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -337,61 +337,17 @@ public class MicroPhotoService extends Service { Log.i(TAG, "PhotoTimer Fired: CH=" + channel + " PR=" + preset); mService.notifyToTakePhoto(mService.mNativeHandle, channel, preset, ts, photoOrVideo); + } - if (channel == 2) { - final String appPath = MicroPhotoContext.buildMpAppDir(context); - - new Thread(new Runnable() { - @Override - public void run() { - - try { - Thread.sleep(20000); - } catch (Exception ex) { - ex.printStackTrace(); - } - - final long requestTime = System.currentTimeMillis() / 1000; - - CameraAdb cameraAdb = new CameraAdb(context, appPath); - cameraAdb.takePhoto(); - - mService.mHander.postDelayed(new Runnable() { - @Override - public void run() { - long takingTime = System.currentTimeMillis() / 1000; - String photoPath = appPath + "photos/"; - String photoFile = "IMG_" + Long.toHexString(requestTime).toUpperCase() + "_2_FF_0_" + Long.toHexString(requestTime).toUpperCase() + "_" + Long.toHexString(takingTime).toUpperCase() + ".jpg"; - - File cameraPath = new File("/sdcard/DCIM/Camera/"); - - Optional opFile = Arrays.stream(cameraPath.listFiles(File::isFile)) - .max((f1, f2) -> Long.compare(f1.lastModified(), f2.lastModified())); - - boolean res = false; - if (opFile.isPresent()) { - File targetFile = new File(new File(photoPath), photoFile); - try { - File srcFile = opFile.get(); - // FilesUtils.copyFile(srcFile, targetFile); - res = srcFile.renameTo(targetFile); - - SysApi.forceStopApp(context, "com.mediatek.camera"); - - restartApp(context, MicroPhotoContext.PACKAGE_NAME_MPAPP); - } catch (Exception e) { - e.printStackTrace(); - } - if (!res) { - Log.e(TAG, "Failed to copy photo from Camera"); - } - } - } - }, 4000); - } - }).start(); - - } + { + final String appPath = MicroPhotoContext.buildMpAppDir(context); + mService.mHander.postDelayed(new Runnable() { + @Override + public void run() { + CameraAdb cameraAdb = new CameraAdb(context, appPath); + cameraAdb.takePhoto(); + } + }, 20000); } } diff --git a/app/src/main/java/com/xypower/mpapp/adb/CameraAdb.java b/app/src/main/java/com/xypower/mpapp/adb/CameraAdb.java index 90b246d5..e3a09c2c 100644 --- a/app/src/main/java/com/xypower/mpapp/adb/CameraAdb.java +++ b/app/src/main/java/com/xypower/mpapp/adb/CameraAdb.java @@ -4,10 +4,15 @@ import android.content.Context; import android.content.Intent; import android.util.Log; +import com.dev.devapi.api.SysApi; import com.xypower.common.FilesUtils; +import com.xypower.common.MicroPhotoContext; +import com.xypower.mpapp.MicroPhotoService; import com.xypower.mpapp.v2.Camera2VideoActivity; import java.io.File; +import java.util.Arrays; +import java.util.Optional; import dadb.AdbKeyPair; import dadb.AdbShellResponse; @@ -41,63 +46,110 @@ public class CameraAdb { } public void takePhoto() { - (new Thread(new Runnable() { + new Thread(new Runnable() { @Override public void run() { + takePhotoImpl(); + } + }).start(); + } + + private void takePhotoImpl() { + + long requestTime = System.currentTimeMillis() / 1000; + takePhoto(false); + long takingTime = System.currentTimeMillis() / 1000; + movePhoto(false, requestTime, takingTime); + + SysApi.forceStopApp(mContext, "com.mediatek.camera"); + + try { + Thread.sleep(2000); + } catch (Exception ex) { + ex.printStackTrace(); + } + requestTime = System.currentTimeMillis() / 1000; + takePhoto(true); + takingTime = System.currentTimeMillis() / 1000; + movePhoto(true, requestTime, takingTime); + + SysApi.forceStopApp(mContext, "com.mediatek.camera"); + MicroPhotoService.restartApp(mContext, MicroPhotoContext.PACKAGE_NAME_MPAPP); + } + + private void movePhoto(boolean frontCamera, long requestTime, long takingTime) { + + String photoPath = mAppPath + "photos/"; + String photoFile = "IMG_" + Long.toHexString(requestTime).toUpperCase() + "_" + + (frontCamera ? "2" : "1") + "_FF_0_" + Long.toHexString(requestTime).toUpperCase() + + "_" + Long.toHexString(takingTime).toUpperCase() + ".jpg"; + + File cameraPath = new File("/sdcard/DCIM/Camera/"); + + Optional opFile = Arrays.stream(cameraPath.listFiles(File::isFile)) + .max((f1, f2) -> Long.compare(f1.lastModified(), f2.lastModified())); + + boolean res = false; + if (opFile.isPresent()) { + File targetFile = new File(new File(photoPath), photoFile); + try { + File srcFile = opFile.get(); + res = srcFile.renameTo(targetFile); + } catch (Exception e) { + e.printStackTrace(); + } + if (!res) { + Log.e(TAG, "Failed to copy photo from Camera"); + } + } + } + + public void takePhoto(final boolean frontCamera) { + Dadb adb = Dadb.discover(mDeviceIp, mAdbKeyPair); + + if (adb == null) { + return; + } + + Log.d(TAG, mDeviceIp + " Connected"); + boolean res = false; + + String[] cmds = { + "content insert --uri content://settings/system --bind name:s:accelerometer_rotation --bind value:i:0", + "content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:1", + "am force-stop com.mediatek.camera", + "setprop mtk.camera.app.keycode.enable 1", + "setprop mtk.camera.switch.camera.debug 1", + "setprop mtk.camera.switch.id.debug back-1", + "am start -a android.media.action.STILL_IMAGE_CAMERA " + + (frontCamera ? "--ez com.google.assistant.extra.USE_FRONT_CAMERA true" : "") + " -f " + Integer.toString(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP), + "wait", + "input keyevent 27"}; + + AdbShellResponse adbShellResponse = null; + for (String cmd : cmds) { + if (cmd.equals("wait")) { try { - runImpl(); + Thread.sleep(3000); } catch (Exception ex) { ex.printStackTrace(); } + continue; + } + try { + adbShellResponse = adb.shell(cmd); + } catch (Exception ex) { + ex.printStackTrace(); } - private void runImpl() { - Dadb adb = Dadb.discover(mDeviceIp, mAdbKeyPair); - - if (adb == null) { - return; - } - - Log.d(TAG, mDeviceIp + " Connected"); - boolean res = false; - - String[] cmds = { - "am force-stop com.mediatek.camera", - "setprop mtk.camera.app.keycode.enable 1", - "setprop mtk.camera.switch.camera.debug 1", - "setprop mtk.camera.switch.id.debug back-1", - "am start -a android.media.action.STILL_IMAGE_CAMERA --ez com.google.assistant.extra.USE_FRONT_CAMERA true -f " + Integer.toString(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP), - "wait", - "input keyevent 27"}; - - AdbShellResponse adbShellResponse = null; - for (String cmd : cmds) { - if (cmd.equals("wait")) { - try { - Thread.sleep(3000); - } catch (Exception ex) { - ex.printStackTrace(); - } - continue; - } - try { - adbShellResponse = adb.shell(cmd); - } catch (Exception ex) { - ex.printStackTrace(); - } - - Log.i(TAG, "CMD: " + cmd); - if (adbShellResponse.getExitCode() == 0) { - String[] lines = FilesUtils.splitLines(adbShellResponse.getAllOutput()); - for (String line : lines) { - Log.i(TAG, line); - } - } - - + Log.i(TAG, "CMD: " + cmd); + if (adbShellResponse.getExitCode() == 0) { + String[] lines = FilesUtils.splitLines(adbShellResponse.getAllOutput()); + for (String line : lines) { + Log.i(TAG, line); } } - })).start(); + } } }