diff --git a/src/org/kde/kdeconnect/BackgroundService.java b/src/org/kde/kdeconnect/BackgroundService.java index c99925a1..df04e7d1 100644 --- a/src/org/kde/kdeconnect/BackgroundService.java +++ b/src/org/kde/kdeconnect/BackgroundService.java @@ -20,6 +20,7 @@ package org.kde.kdeconnect; +import android.app.Activity; import android.app.Service; import android.content.Context; import android.content.Intent; @@ -40,6 +41,7 @@ import java.security.KeyPair; import java.security.KeyPairGenerator; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.Lock; @@ -57,23 +59,40 @@ public class BackgroundService extends Service { private final ConcurrentHashMap devices = new ConcurrentHashMap<>(); - private boolean discoveryEnabled = false; + private final HashSet discoveryModeAcquisitions = new HashSet<>(); - public void setDiscoveryEnabled(boolean b) { - if (discoveryEnabled == b) - return; - - discoveryEnabled = b; - - if (b) { + public void acquireDiscoveryMode(Object key) { + boolean wasEmpty = discoveryModeAcquisitions.isEmpty(); + discoveryModeAcquisitions.add(key); + if (wasEmpty) { onNetworkChange(); - } else { + } + } + + public void releaseDiscoveryMode(Object key) { + boolean removed = discoveryModeAcquisitions.remove(key); + if (removed && discoveryModeAcquisitions.isEmpty()) { cleanDevices(); } } - public boolean isDiscoveryEnabled() { - return discoveryEnabled; + public static void addGuiInUseCounter(final Activity activity) { + BackgroundService.RunCommand(activity, new BackgroundService.InstanceCallback() { + @Override + public void onServiceStart(BackgroundService service) { + service.acquireDiscoveryMode(activity); + } + }); + } + + public static void removeGuiInUseCounter(final Activity activity) { + BackgroundService.RunCommand(activity, new BackgroundService.InstanceCallback() { + @Override + public void onServiceStart(BackgroundService service) { + //If no user interface is open, close the connections open to other devices + service.releaseDiscoveryMode(activity); + } + }); } private final Device.PairingCallback devicePairingCallback = new Device.PairingCallback() { @@ -157,7 +176,7 @@ public class BackgroundService extends Service { } else { Log.i("KDE/BackgroundService", "addLink,unknown device: " + deviceId); device = new Device(BackgroundService.this, identityPackage, link); - if (isDiscoveryEnabled() || device.isPaired()) { + if (device.isPaired() || !discoveryModeAcquisitions.isEmpty()) { devices.put(deviceId, device); device.addPairingCallback(devicePairingCallback); } diff --git a/src/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadActivity.java b/src/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadActivity.java index c3d3f74e..71a22473 100644 --- a/src/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadActivity.java +++ b/src/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadActivity.java @@ -362,5 +362,17 @@ public class MousePadActivity extends ActionBarActivity implements GestureDetect imm.toggleSoftInputFromWindow(keyListenerView.getWindowToken(), 0, 0); } + @Override + protected void onStart() { + super.onStart(); + BackgroundService.addGuiInUseCounter(this); + } + + @Override + protected void onStop() { + super.onStop(); + BackgroundService.removeGuiInUseCounter(this); + } + } diff --git a/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisActivity.java b/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisActivity.java index 3cfbe828..748d690b 100644 --- a/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisActivity.java +++ b/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisActivity.java @@ -453,9 +453,15 @@ public class MprisActivity extends ActionBarActivity { } @Override - protected void onPause() { - super.onPause(); - positionSeekUpdateHandler.removeCallbacks(positionSeekUpdateRunnable); + protected void onStart() { + super.onStart(); + BackgroundService.addGuiInUseCounter(this); + } + + @Override + protected void onStop() { + super.onStop(); + BackgroundService.removeGuiInUseCounter(this); } } diff --git a/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationFilterActivity.java b/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationFilterActivity.java index 292abdcc..e3090c35 100644 --- a/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationFilterActivity.java +++ b/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationFilterActivity.java @@ -30,6 +30,7 @@ import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; +import org.kde.kdeconnect.BackgroundService; import org.kde.kdeconnect_tp.R; import java.util.List; @@ -157,4 +158,16 @@ public class NotificationFilterActivity extends ActionBarActivity { return true; } + @Override + protected void onStart() { + super.onStart(); + BackgroundService.addGuiInUseCounter(this); + } + + @Override + protected void onStop() { + super.onStop(); + BackgroundService.removeGuiInUseCounter(this); + } + } diff --git a/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareActivity.java b/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareActivity.java index f395b86d..f3910360 100644 --- a/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareActivity.java +++ b/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareActivity.java @@ -205,6 +205,7 @@ public class ShareActivity extends ActionBarActivity { @Override protected void onStart() { super.onStart(); + BackgroundService.addGuiInUseCounter(this); BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { @Override public void onServiceStart(BackgroundService service) { @@ -217,6 +218,7 @@ public class ShareActivity extends ActionBarActivity { }); } }); + updateComputerList(); } @Override @@ -227,13 +229,8 @@ public class ShareActivity extends ActionBarActivity { service.removeDeviceListChangedCallback("ShareActivity"); } }); + BackgroundService.removeGuiInUseCounter(this); super.onStop(); } - @Override - protected void onResume() { - super.onResume(); - updateComputerList(); - } - } diff --git a/src/org/kde/kdeconnect/UserInterface/AppCompatPreferenceActivity.java b/src/org/kde/kdeconnect/UserInterface/AppCompatPreferenceActivity.java index 68f526d8..d626f3fc 100644 --- a/src/org/kde/kdeconnect/UserInterface/AppCompatPreferenceActivity.java +++ b/src/org/kde/kdeconnect/UserInterface/AppCompatPreferenceActivity.java @@ -28,6 +28,8 @@ import android.view.MenuInflater; import android.view.View; import android.view.ViewGroup; +import org.kde.kdeconnect.BackgroundService; + /** * A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls * to be used with AppCompat. @@ -104,11 +106,6 @@ public abstract class AppCompatPreferenceActivity extends PreferenceActivity { getDelegate().onConfigurationChanged(newConfig); } - @Override - protected void onStop() { - super.onStop(); - getDelegate().onStop(); - } @Override protected void onDestroy() { super.onDestroy(); @@ -126,4 +123,19 @@ public abstract class AppCompatPreferenceActivity extends PreferenceActivity { } return mDelegate; } + + @Override + protected void onStart() { + super.onStart(); + BackgroundService.addGuiInUseCounter(this); + } + + @Override + protected void onStop() { + super.onStop(); + BackgroundService.removeGuiInUseCounter(this); + getDelegate().onStop(); + } + + } diff --git a/src/org/kde/kdeconnect/UserInterface/CustomDevicesActivity.java b/src/org/kde/kdeconnect/UserInterface/CustomDevicesActivity.java index b06b2287..f0f1de38 100644 --- a/src/org/kde/kdeconnect/UserInterface/CustomDevicesActivity.java +++ b/src/org/kde/kdeconnect/UserInterface/CustomDevicesActivity.java @@ -37,6 +37,7 @@ import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; +import org.kde.kdeconnect.BackgroundService; import org.kde.kdeconnect_tp.R; import java.util.ArrayList; @@ -176,4 +177,17 @@ public class CustomDevicesActivity extends ActionBarActivity { ipAddressList = deserializeIpList(deviceListPrefs); } } + + @Override + protected void onStart() { + super.onStart(); + BackgroundService.addGuiInUseCounter(this); + } + + @Override + protected void onStop() { + super.onStop(); + BackgroundService.removeGuiInUseCounter(this); + } + } diff --git a/src/org/kde/kdeconnect/UserInterface/MaterialActivity.java b/src/org/kde/kdeconnect/UserInterface/MaterialActivity.java index 8e18e8e8..a04eb2ab 100644 --- a/src/org/kde/kdeconnect/UserInterface/MaterialActivity.java +++ b/src/org/kde/kdeconnect/UserInterface/MaterialActivity.java @@ -159,56 +159,21 @@ public class MaterialActivity extends AppCompatActivity { @Override protected void onStart() { super.onStart(); - BackgroundService.RunCommand(MaterialActivity.this, new BackgroundService.InstanceCallback() { - @Override - public void onServiceStart(BackgroundService service) { - service.onNetworkChange(); - service.addDeviceListChangedCallback("MaterialActivity", new BackgroundService.DeviceListChangedCallback() { - @Override - public void onDeviceListChanged() { - updateComputerList(); - } - }); - } - }); + BackgroundService.addGuiInUseCounter(this); } @Override protected void onStop() { - BackgroundService.RunCommand(MaterialActivity.this, new BackgroundService.InstanceCallback() { - @Override - public void onServiceStart(BackgroundService service) { - service.removeDeviceListChangedCallback("MaterialActivity"); - } - }); + BackgroundService.removeGuiInUseCounter(this); super.onStop(); } @Override protected void onResume() { super.onResume(); - - BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { - @Override - public void onServiceStart(BackgroundService service) { - service.setDiscoveryEnabled(true); - } - }); - updateComputerList(); } - @Override - public void onPause() { - super.onPause(); - BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { - @Override - public void onServiceStart(BackgroundService service) { - service.setDiscoveryEnabled(false); - } - }); - } - //TODO: Make it accept two parameters, a constant with the type of screen and the device id in //case the screen is for a device, or even three parameters and the third one be the plugin id? //This way we can keep adding more options with null plugin id (eg: about) diff --git a/src/org/kde/kdeconnect/UserInterface/PluginSettingsActivity.java b/src/org/kde/kdeconnect/UserInterface/PluginSettingsActivity.java index e35320c0..a9aded05 100644 --- a/src/org/kde/kdeconnect/UserInterface/PluginSettingsActivity.java +++ b/src/org/kde/kdeconnect/UserInterface/PluginSettingsActivity.java @@ -23,6 +23,7 @@ package org.kde.kdeconnect.UserInterface; import android.os.Bundle; import android.view.MenuItem; +import org.kde.kdeconnect.BackgroundService; import org.kde.kdeconnect_tp.R; import java.util.Locale; @@ -52,4 +53,16 @@ public class PluginSettingsActivity extends AppCompatPreferenceActivity { } } + @Override + protected void onStart() { + super.onStart(); + BackgroundService.addGuiInUseCounter(this); + } + + @Override + protected void onStop() { + super.onStop(); + BackgroundService.removeGuiInUseCounter(this); + } + } diff --git a/src/org/kde/kdeconnect/UserInterface/SettingsActivity.java b/src/org/kde/kdeconnect/UserInterface/SettingsActivity.java index 928af8da..f0315793 100644 --- a/src/org/kde/kdeconnect/UserInterface/SettingsActivity.java +++ b/src/org/kde/kdeconnect/UserInterface/SettingsActivity.java @@ -22,6 +22,7 @@ package org.kde.kdeconnect.UserInterface; import android.os.Bundle; import android.preference.PreferenceScreen; +import android.util.Log; import android.view.MenuItem; import org.kde.kdeconnect.BackgroundService; @@ -68,4 +69,17 @@ public class SettingsActivity extends AppCompatPreferenceActivity { return super.onOptionsItemSelected(item); } } + + @Override + protected void onStart() { + super.onStart(); + BackgroundService.addGuiInUseCounter(this); + } + + @Override + protected void onStop() { + super.onStop(); + BackgroundService.removeGuiInUseCounter(this); + } + }