From 580d70e910646647b0ceb17c40562776ca7bd77f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Drobni=C4=8D?= Date: Tue, 23 Apr 2019 20:12:53 +0200 Subject: [PATCH] add pebblekit extension for reopening last app --- .../pebble/PebbleActiveAppTracker.java | 40 +++++++++++++++++++ .../devices/pebble/PebbleIoThread.java | 20 ++++++++++ .../devices/pebble/PebbleKitSupport.java | 9 ++++- .../devices/pebble/PebbleProtocol.java | 7 +++- 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleActiveAppTracker.java diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleActiveAppTracker.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleActiveAppTracker.java new file mode 100644 index 000000000..2f8f2ce7a --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleActiveAppTracker.java @@ -0,0 +1,40 @@ +package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import java.util.UUID; + +public class PebbleActiveAppTracker { + private @Nullable UUID mPreviousRunningApp = null; + private @Nullable UUID mCurrentRunningApp = null; + + @Nullable + public UUID getPreviousRunningApp() { + return mPreviousRunningApp; + } + + @Nullable + public UUID getCurrentRunningApp() { + return mCurrentRunningApp; + } + + public void markAppClosed(@NonNull UUID app) { + if (mCurrentRunningApp == app) { + if (mPreviousRunningApp != null) { + markAppOpened(mPreviousRunningApp); + } else { + mCurrentRunningApp = null; + } + } + } + + public void markAppOpened(@NonNull UUID openedApp) { + if (openedApp.equals(mCurrentRunningApp)) { + return; + } + + mPreviousRunningApp = mCurrentRunningApp; + mCurrentRunningApp = openedApp; + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java index 0ca80d6a3..7c1ba63c0 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java @@ -27,6 +27,7 @@ import android.os.ParcelUuid; import android.webkit.ValueCallback; import android.webkit.WebView; +import androidx.annotation.NonNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -75,6 +76,7 @@ class PebbleIoThread extends GBDeviceIoThread { private final PebbleProtocol mPebbleProtocol; private final PebbleSupport mPebbleSupport; private PebbleKitSupport mPebbleKitSupport; + private final PebbleActiveAppTracker mPebbleActiveAppTracker; private final boolean mEnablePebblekit; private boolean mIsTCP = false; @@ -149,6 +151,8 @@ class PebbleIoThread extends GBDeviceIoThread { mEnablePebblekit = prefs.getBoolean("pebble_enable_pebblekit", false); mPebbleProtocol.setAlwaysACKPebbleKit(prefs.getBoolean("pebble_always_ack_pebblekit", false)); mPebbleProtocol.setEnablePebbleKit(mEnablePebblekit); + + mPebbleActiveAppTracker = new PebbleActiveAppTracker(); } private int readWithException(InputStream inputStream, byte[] buffer, int byteOffset, int byteCount) throws IOException { @@ -563,6 +567,11 @@ class PebbleIoThread extends GBDeviceIoThread { WebViewSingleton.getInstance().runJavascriptInterface(gbDevice, appMgmt.uuid); } } + + mPebbleActiveAppTracker.markAppOpened(appMgmt.uuid); + break; + case STOP: + mPebbleActiveAppTracker.markAppClosed(appMgmt.uuid); break; default: break; @@ -681,6 +690,17 @@ class PebbleIoThread extends GBDeviceIoThread { } } + void reopenLastApp(@NonNull UUID assumedCurrentApp) { + UUID currentApp = mPebbleActiveAppTracker.getCurrentRunningApp(); + UUID previousApp = mPebbleActiveAppTracker.getPreviousRunningApp(); + + if (previousApp == null || !assumedCurrentApp.equals(currentApp)) { + write(mPebbleProtocol.encodeAppStart(assumedCurrentApp, false)); + } else { + write(mPebbleProtocol.encodeAppStart(previousApp, true)); + } + } + private void finishInstall(boolean hadError) { if (!mIsInstalling) { return; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleKitSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleKitSupport.java index 647a1a2b2..1e2a918b8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleKitSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleKitSupport.java @@ -44,6 +44,8 @@ class PebbleKitSupport { private static final String PEBBLEKIT_ACTION_APP_START = "com.getpebble.action.app.START"; private static final String PEBBLEKIT_ACTION_APP_STOP = "com.getpebble.action.app.STOP"; + private static final String PEBBLEKIT_EXTRA_REOPEN_LAST_APP = "com.getpebble.action.app.REOPEN_LAST_APP"; + private static final String PEBBLEKIT_ACTION_DL_RECEIVE_DATA_NEW = "com.getpebble.action.dl.RECEIVE_DATA_NEW"; //private static final String PEBBLEKIT_ACTION_DL_RECEIVE_DATA = "com.getpebble.action.dl.RECEIVE_DATA"; private static final String PEBBLEKIT_ACTION_DL_ACK_DATA = "com.getpebble.action.dl.ACK_DATA"; @@ -73,7 +75,12 @@ class PebbleKitSupport { case PEBBLEKIT_ACTION_APP_STOP: uuid = (UUID) intent.getSerializableExtra("uuid"); if (uuid != null) { - mPebbleIoThread.write(mPebbleProtocol.encodeAppStart(uuid, action.equals(PEBBLEKIT_ACTION_APP_START))); + if (action.equals(PEBBLEKIT_ACTION_APP_STOP) && + intent.getBooleanExtra(PEBBLEKIT_EXTRA_REOPEN_LAST_APP, false)) { + mPebbleIoThread.reopenLastApp(uuid); + } else { + mPebbleIoThread.write(mPebbleProtocol.encodeAppStart(uuid, action.equals(PEBBLEKIT_ACTION_APP_START))); + } } break; case PEBBLEKIT_ACTION_APP_SEND: diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java index 5d43878f4..6422fff6a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java @@ -2156,7 +2156,12 @@ public class PebbleProtocol extends GBDeviceProtocol { break; case APPRUNSTATE_STOP: LOG.info(ENDPOINT_NAME + ": stopped " + uuid); - break; + + GBDeviceEventAppManagement gbDeviceEventAppManagement = new GBDeviceEventAppManagement(); + gbDeviceEventAppManagement.uuid = uuid; + gbDeviceEventAppManagement.type = GBDeviceEventAppManagement.EventType.STOP; + gbDeviceEventAppManagement.event = GBDeviceEventAppManagement.Event.SUCCESS; + return new GBDeviceEvent[]{gbDeviceEventAppManagement}; default: LOG.info(ENDPOINT_NAME + ": (cmd:" + command + ")" + uuid); break;