diff options
author | Joey Grover <joeygrover@gmail.com> | 2018-10-04 17:09:45 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-04 17:09:45 -0400 |
commit | dc002b9a2f5e2f86327bcc9ba7909e62c80e917f (patch) | |
tree | 74b59c7a5ce1bd501b9b7c0b5b3ec22f671e4301 | |
parent | 655d17916ff0dd504bf5941efd29fbb0a48fd01d (diff) | |
parent | 435aa9e13c1aada75399de9947566ccb73a2636a (diff) | |
download | sdl_android-dc002b9a2f5e2f86327bcc9ba7909e62c80e917f.tar.gz |
Merge pull request #892 from smartdevicelink/feature/fix_lockscreen_memory
add lifecycle listeners
-rwxr-xr-x | hello_sdl_android/src/main/AndroidManifest.xml | 2 | ||||
-rw-r--r-- | sdl_android/build.gradle | 2 | ||||
-rw-r--r-- | sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen/LockScreenManager.java | 55 |
3 files changed, 47 insertions, 12 deletions
diff --git a/hello_sdl_android/src/main/AndroidManifest.xml b/hello_sdl_android/src/main/AndroidManifest.xml index e556cd680..72a191542 100755 --- a/hello_sdl_android/src/main/AndroidManifest.xml +++ b/hello_sdl_android/src/main/AndroidManifest.xml @@ -62,7 +62,7 @@ </intent-filter> </receiver> <activity android:name="com.smartdevicelink.managers.lockscreen.SDLLockScreenActivity" - android:launchMode="singleInstance"/> + android:launchMode="singleTop"/> </application> </manifest>
\ No newline at end of file diff --git a/sdl_android/build.gradle b/sdl_android/build.gradle index 8bcd77a76..152ece995 100644 --- a/sdl_android/build.gradle +++ b/sdl_android/build.gradle @@ -42,6 +42,8 @@ dependencies { testImplementation 'org.mockito:mockito-core:2.9.0' androidTestImplementation 'org.mockito:mockito-android:2.9.0' api 'com.android.support:support-annotations:28.0.0' + api "android.arch.lifecycle:extensions:1.1.1" + annotationProcessor "android.arch.lifecycle:compiler:1.1.1" } buildscript { diff --git a/sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen/LockScreenManager.java b/sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen/LockScreenManager.java index 7419e9e17..e15d757e5 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen/LockScreenManager.java +++ b/sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen/LockScreenManager.java @@ -1,6 +1,9 @@ package com.smartdevicelink.managers.lockscreen; -import android.app.ActivityManager; +import android.arch.lifecycle.Lifecycle; +import android.arch.lifecycle.LifecycleObserver; +import android.arch.lifecycle.OnLifecycleEvent; +import android.arch.lifecycle.ProcessLifecycleOwner; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; @@ -41,6 +44,8 @@ public class LockScreenManager extends BaseSubManager { private OnRPCNotificationListener systemRequestListener, ddListener, hmiListener; private String deviceIconUrl; private boolean driverDistStatus; + private volatile boolean isApplicationForegrounded; + private LifecycleObserver lifecycleObserver; protected boolean lockScreenEnabled, deviceLogoEnabled; protected int lockScreenIcon, lockScreenColor, customView; protected Bitmap deviceLogo; @@ -84,6 +89,19 @@ public class LockScreenManager extends BaseSubManager { } deviceLogo = null; deviceIconUrl = null; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { + try { + if (ProcessLifecycleOwner.get() != null && lifecycleObserver != null) { + ProcessLifecycleOwner.get().getLifecycle().removeObserver(lifecycleObserver); + } + } catch (Exception e) { + e.printStackTrace(); + } + + lifecycleObserver = null; + } + super.dispose(); } @@ -152,6 +170,30 @@ public class LockScreenManager extends BaseSubManager { }; internalInterface.addOnRPCNotificationListener(FunctionID.ON_SYSTEM_REQUEST, systemRequestListener); } + + // Set up listener for Application Foreground / Background events + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { + try { + lifecycleObserver = new LifecycleObserver() { + @OnLifecycleEvent(Lifecycle.Event.ON_START) + public void onMoveToForeground() { + isApplicationForegrounded = true; + launchLockScreenActivity(); + } + + @OnLifecycleEvent(Lifecycle.Event.ON_STOP) + public void onMoveToBackground() { + isApplicationForegrounded = false; + } + }; + + if (ProcessLifecycleOwner.get() != null) { + ProcessLifecycleOwner.get().getLifecycle().addObserver(lifecycleObserver); + } + } catch (Exception e) { + e.printStackTrace(); + } + } } //// @@ -168,7 +210,7 @@ public class LockScreenManager extends BaseSubManager { private void launchLockScreenActivity(){ // intent to open SDLLockScreenActivity // pass in icon, background color, and custom view - if (lockScreenEnabled && isForegrounded() && context.get() != null) { + if (lockScreenEnabled && isApplicationForegrounded && context.get() != null) { LockScreenStatus status = getLockScreenStatus(); if (status == LockScreenStatus.REQUIRED) { Intent showLockScreenIntent = new Intent(context.get(), SDLLockScreenActivity.class); @@ -187,15 +229,6 @@ public class LockScreenManager extends BaseSubManager { } } - private boolean isForegrounded() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - ActivityManager.RunningAppProcessInfo myProcess = new ActivityManager.RunningAppProcessInfo(); - ActivityManager.getMyMemoryState(myProcess); - return myProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; - } - return true; - } - //// // HELPERS //// |