From 3cc71a69a4aaf2fded16fdce9714682cc0308ee1 Mon Sep 17 00:00:00 2001 From: Dmitriy Bogdanov Date: Sun, 12 Feb 2017 21:18:14 +0100 Subject: [PATCH] FindMyPhone: better activity lifecycle handling Summary: There are some minor problems with current implementation: if device configuration changes (screen orientation, etc.) while the "FindMyPhone" ringtone is playing, the activity gets recreated and starts to play a new ringtone again, but the first ringtone is not stopped. Also if the user leaves the activity the ringtone continues to play, the user has no way to get back to the activity to stop it. With these changes the ringtone starts playing when the activity becomes visible and stops when the activity is being hidden/destroyed. If the user leaves the activity (without destroying it) and then presses "Ring my phone" button again, the activity becomes visible again and starts to play the ringtone. There are other ways to improve it that I did not touch: use a Service to play the ringtone (so not to depend on the activity's lifecycle) or handle configuration changes in activity (so it is not recreated on orientation changes). Test Plan: Activate "Find My Phone" feature and try to turn phone or leave activity. Reviewers: albertvaka Reviewed By: albertvaka Tags: #kde_connect Differential Revision: https://phabricator.kde.org/D4548 --- AndroidManifest.xml | 1 + .../FindMyPhoneActivity.java | 24 +++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 40a8fa6d..8fcca82c 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -140,6 +140,7 @@ diff --git a/src/org/kde/kdeconnect/Plugins/FindMyPhonePlugin/FindMyPhoneActivity.java b/src/org/kde/kdeconnect/Plugins/FindMyPhonePlugin/FindMyPhoneActivity.java index 2ebba699..fa4f6a77 100644 --- a/src/org/kde/kdeconnect/Plugins/FindMyPhonePlugin/FindMyPhoneActivity.java +++ b/src/org/kde/kdeconnect/Plugins/FindMyPhonePlugin/FindMyPhoneActivity.java @@ -21,7 +21,12 @@ public class FindMyPhoneActivity extends Activity { @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); - finish(); //If this activity was already open and we received the ring packet again, just finish it + + if(ringtone != null) { + // If this activity was already open and we received the ring packet again, just finish it + finish(); + } + // otherwise the activity will become active again } @Override @@ -40,6 +45,11 @@ public class FindMyPhoneActivity extends Activity { finish(); } }); + } + + @Override + protected void onStart() { + super.onStart(); Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); ringtone = RingtoneManager.getRingtone(getApplicationContext(), ringtoneUri); @@ -61,12 +71,16 @@ public class FindMyPhoneActivity extends Activity { } ringtone.play(); - } @Override - public void finish() { - ringtone.stop(); - super.finish(); + protected void onStop() { + super.onStop(); + + if(ringtone != null) { + ringtone.stop(); + ringtone = null; + } } + }