mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-30 05:37:43 +00:00
The discovery now should be enabled while navigating the app UI
This commit is contained in:
parent
c837079e03
commit
24561b58f9
@ -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<String, Device> devices = new ConcurrentHashMap<>();
|
||||
|
||||
private boolean discoveryEnabled = false;
|
||||
private final HashSet<Object> 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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user