2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-22 09:58:08 +00:00

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
This commit is contained in:
Dmitriy Bogdanov 2017-02-12 21:18:14 +01:00 committed by Albert Vaca
parent c0502803c4
commit 3cc71a69a4
2 changed files with 20 additions and 5 deletions

View File

@ -140,6 +140,7 @@
<activity <activity
android:name="org.kde.kdeconnect.Plugins.FindMyPhonePlugin.FindMyPhoneActivity" android:name="org.kde.kdeconnect.Plugins.FindMyPhonePlugin.FindMyPhoneActivity"
android:label="@string/findmyphone_title" android:label="@string/findmyphone_title"
android:configChanges="orientation|screenSize"
android:excludeFromRecents="true" android:excludeFromRecents="true"
android:launchMode="singleInstance"> android:launchMode="singleInstance">
</activity> </activity>

View File

@ -21,7 +21,12 @@ public class FindMyPhoneActivity extends Activity {
@Override @Override
protected void onNewIntent(Intent intent) { protected void onNewIntent(Intent intent) {
super.onNewIntent(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 @Override
@ -40,6 +45,11 @@ public class FindMyPhoneActivity extends Activity {
finish(); finish();
} }
}); });
}
@Override
protected void onStart() {
super.onStart();
Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
ringtone = RingtoneManager.getRingtone(getApplicationContext(), ringtoneUri); ringtone = RingtoneManager.getRingtone(getApplicationContext(), ringtoneUri);
@ -61,12 +71,16 @@ public class FindMyPhoneActivity extends Activity {
} }
ringtone.play(); ringtone.play();
} }
@Override @Override
public void finish() { protected void onStop() {
super.onStop();
if(ringtone != null) {
ringtone.stop(); ringtone.stop();
super.finish(); ringtone = null;
} }
}
} }