From ef57584bd6788886761a94627ce653a58807998f Mon Sep 17 00:00:00 2001 From: Matthew Date: Wed, 9 Oct 2024 12:48:44 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=96=87=E4=BB=B6=E9=94=81?= =?UTF-8?q?=E6=9D=A5=E6=A3=80=E6=B5=8Bapp=E6=98=AF=E5=90=A6=E5=9C=A8?= =?UTF-8?q?=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xypower/mpapp/MicroPhotoService.java | 42 ++++++++++++++++ .../com/xypower/mpmaster/MpMasterService.java | 48 +++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index 8db63f51..0fad6831 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -65,8 +65,11 @@ import com.xypower.mpapp.v2.Camera2VideoActivity; import com.xypower.mpapp.video.RawActivity; import java.io.File; +import java.io.FileOutputStream; +import java.io.RandomAccessFile; import java.lang.reflect.Method; import java.net.InetAddress; +import java.nio.channels.FileLock; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -147,6 +150,9 @@ public class MicroPhotoService extends Service { public static boolean isRunning = false; + FileOutputStream mAppRunningFile; + FileLock mAppLock; + private Runnable delayedSleep = new Runnable() { @Override public void run() { @@ -202,6 +208,30 @@ public class MicroPhotoService extends Service { public void onCreate() { super.onCreate(); + try { + final String appPath = MicroPhotoContext.buildMpAppDir(this); + File lockerFile = new File(appPath); + lockerFile = new File(lockerFile, "data/alive/running"); + mAppRunningFile = new FileOutputStream(lockerFile); + + for (int idx = 0; idx < 3; idx++) { + try { + mAppLock = mAppRunningFile.getChannel().tryLock(); + if (mAppLock != null && mAppLock.isValid()) { + break; + } + try { + Thread.sleep(16); + } catch (Exception ex) { + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + mHander = new ServiceHandler(); mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); @@ -272,6 +302,18 @@ public class MicroPhotoService extends Service { @Override public void onDestroy() { + try { + + if (mAppLock != null) { + mAppLock.close(); + } + if (mAppRunningFile != null) { + mAppRunningFile.close(); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + mStateService = STATE_SERVICE.NOT_CONNECTED; Log.w(TAG, "MicroPhotoService::onDestroy called"); diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java index afc19ba8..98498a46 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java @@ -45,11 +45,14 @@ import org.json.JSONObject; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; +import java.io.RandomAccessFile; import java.lang.reflect.Method; +import java.nio.channels.FileLock; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import java.util.List; +import java.util.concurrent.ExecutionException; import java.util.logging.Level; import java.util.logging.Logger; @@ -386,6 +389,51 @@ public class MpMasterService extends Service { final Context context = getApplicationContext(); long ts = System.currentTimeMillis(); + FileOutputStream runningFile = null; + FileLock fileLock = null; + try { + boolean isMpAppRunning = true; + String mpappDir = MicroPhotoContext.buildMpAppDir(getApplicationContext()); + File file = new File(mpappDir); + file = new File(file, "data/alive/running"); + if (file.exists()) { + runningFile = new FileOutputStream(file); + fileLock = runningFile.getChannel().tryLock(); + if (fileLock != null && fileLock.isValid()) { + isMpAppRunning = false; + } + try { + if (fileLock != null) { + fileLock.close(); + fileLock = null; + } + } catch (Exception ex) { + ex.printStackTrace(); + } + FilesUtils.closeFriendly(runningFile); + } else { + isMpAppRunning = false; + } + + if (!isMpAppRunning) { + // Restart MpApp + MicroPhotoContext.restartMpApp(context, "MpMST Keep Alive Detection: NO Lock"); + logger.warning("Restart MpAPP as There is NO Lock"); + return; + } + } catch (Exception ex) { + ex.printStackTrace(); + } finally { + try { + if (fileLock != null) { + fileLock.close(); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + FilesUtils.closeFriendly(runningFile); + } + if (mPreviousMpHbTime <= ts && ts - mPreviousMpHbTime > mMpHeartbeatDuration * 2) { // MpApp is not running if (ts - mTimeToStartMpApp >= 30000) {