2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-31 14:15:14 +00:00

The discovery now should be enabled while navigating the app UI

This commit is contained in:
Albert Vaca
2015-09-09 12:34:42 -07:00
parent c837079e03
commit 24561b58f9
10 changed files with 128 additions and 63 deletions

View File

@@ -20,6 +20,7 @@
package org.kde.kdeconnect; package org.kde.kdeconnect;
import android.app.Activity;
import android.app.Service; import android.app.Service;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@@ -40,6 +41,7 @@ import java.security.KeyPair;
import java.security.KeyPairGenerator; import java.security.KeyPairGenerator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
@@ -57,23 +59,40 @@ public class BackgroundService extends Service {
private final ConcurrentHashMap<String, Device> devices = new ConcurrentHashMap<>(); private final ConcurrentHashMap<String, Device> devices = new ConcurrentHashMap<>();
private boolean discoveryEnabled = false; private final HashSet<Object> discoveryModeAcquisitions = new HashSet<>();
public void setDiscoveryEnabled(boolean b) { public void acquireDiscoveryMode(Object key) {
if (discoveryEnabled == b) boolean wasEmpty = discoveryModeAcquisitions.isEmpty();
return; discoveryModeAcquisitions.add(key);
if (wasEmpty) {
discoveryEnabled = b;
if (b) {
onNetworkChange(); onNetworkChange();
} else { }
}
public void releaseDiscoveryMode(Object key) {
boolean removed = discoveryModeAcquisitions.remove(key);
if (removed && discoveryModeAcquisitions.isEmpty()) {
cleanDevices(); cleanDevices();
} }
} }
public boolean isDiscoveryEnabled() { public static void addGuiInUseCounter(final Activity activity) {
return discoveryEnabled; 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() { private final Device.PairingCallback devicePairingCallback = new Device.PairingCallback() {
@@ -157,7 +176,7 @@ public class BackgroundService extends Service {
} else { } else {
Log.i("KDE/BackgroundService", "addLink,unknown device: " + deviceId); Log.i("KDE/BackgroundService", "addLink,unknown device: " + deviceId);
device = new Device(BackgroundService.this, identityPackage, link); device = new Device(BackgroundService.this, identityPackage, link);
if (isDiscoveryEnabled() || device.isPaired()) { if (device.isPaired() || !discoveryModeAcquisitions.isEmpty()) {
devices.put(deviceId, device); devices.put(deviceId, device);
device.addPairingCallback(devicePairingCallback); device.addPairingCallback(devicePairingCallback);
} }

View File

@@ -362,5 +362,17 @@ public class MousePadActivity extends ActionBarActivity implements GestureDetect
imm.toggleSoftInputFromWindow(keyListenerView.getWindowToken(), 0, 0); imm.toggleSoftInputFromWindow(keyListenerView.getWindowToken(), 0, 0);
} }
@Override
protected void onStart() {
super.onStart();
BackgroundService.addGuiInUseCounter(this);
}
@Override
protected void onStop() {
super.onStop();
BackgroundService.removeGuiInUseCounter(this);
}
} }

View File

@@ -453,9 +453,15 @@ public class MprisActivity extends ActionBarActivity {
} }
@Override @Override
protected void onPause() { protected void onStart() {
super.onPause(); super.onStart();
positionSeekUpdateHandler.removeCallbacks(positionSeekUpdateRunnable); BackgroundService.addGuiInUseCounter(this);
}
@Override
protected void onStop() {
super.onStop();
BackgroundService.removeGuiInUseCounter(this);
} }
} }

View File

@@ -30,6 +30,7 @@ import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.ListView; import android.widget.ListView;
import org.kde.kdeconnect.BackgroundService;
import org.kde.kdeconnect_tp.R; import org.kde.kdeconnect_tp.R;
import java.util.List; import java.util.List;
@@ -157,4 +158,16 @@ public class NotificationFilterActivity extends ActionBarActivity {
return true; return true;
} }
@Override
protected void onStart() {
super.onStart();
BackgroundService.addGuiInUseCounter(this);
}
@Override
protected void onStop() {
super.onStop();
BackgroundService.removeGuiInUseCounter(this);
}
} }

View File

@@ -205,6 +205,7 @@ public class ShareActivity extends ActionBarActivity {
@Override @Override
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
BackgroundService.addGuiInUseCounter(this);
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override @Override
public void onServiceStart(BackgroundService service) { public void onServiceStart(BackgroundService service) {
@@ -217,6 +218,7 @@ public class ShareActivity extends ActionBarActivity {
}); });
} }
}); });
updateComputerList();
} }
@Override @Override
@@ -227,13 +229,8 @@ public class ShareActivity extends ActionBarActivity {
service.removeDeviceListChangedCallback("ShareActivity"); service.removeDeviceListChangedCallback("ShareActivity");
} }
}); });
BackgroundService.removeGuiInUseCounter(this);
super.onStop(); super.onStop();
} }
@Override
protected void onResume() {
super.onResume();
updateComputerList();
}
} }

View File

@@ -28,6 +28,8 @@ import android.view.MenuInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import org.kde.kdeconnect.BackgroundService;
/** /**
* A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls * A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls
* to be used with AppCompat. * to be used with AppCompat.
@@ -104,11 +106,6 @@ public abstract class AppCompatPreferenceActivity extends PreferenceActivity {
getDelegate().onConfigurationChanged(newConfig); getDelegate().onConfigurationChanged(newConfig);
} }
@Override
protected void onStop() {
super.onStop();
getDelegate().onStop();
}
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
@@ -126,4 +123,19 @@ public abstract class AppCompatPreferenceActivity extends PreferenceActivity {
} }
return mDelegate; return mDelegate;
} }
@Override
protected void onStart() {
super.onStart();
BackgroundService.addGuiInUseCounter(this);
}
@Override
protected void onStop() {
super.onStop();
BackgroundService.removeGuiInUseCounter(this);
getDelegate().onStop();
}
} }

View File

@@ -37,6 +37,7 @@ import android.widget.EditText;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import org.kde.kdeconnect.BackgroundService;
import org.kde.kdeconnect_tp.R; import org.kde.kdeconnect_tp.R;
import java.util.ArrayList; import java.util.ArrayList;
@@ -176,4 +177,17 @@ public class CustomDevicesActivity extends ActionBarActivity {
ipAddressList = deserializeIpList(deviceListPrefs); ipAddressList = deserializeIpList(deviceListPrefs);
} }
} }
@Override
protected void onStart() {
super.onStart();
BackgroundService.addGuiInUseCounter(this);
}
@Override
protected void onStop() {
super.onStop();
BackgroundService.removeGuiInUseCounter(this);
}
} }

View File

@@ -159,56 +159,21 @@ public class MaterialActivity extends AppCompatActivity {
@Override @Override
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
BackgroundService.RunCommand(MaterialActivity.this, new BackgroundService.InstanceCallback() { BackgroundService.addGuiInUseCounter(this);
@Override
public void onServiceStart(BackgroundService service) {
service.onNetworkChange();
service.addDeviceListChangedCallback("MaterialActivity", new BackgroundService.DeviceListChangedCallback() {
@Override
public void onDeviceListChanged() {
updateComputerList();
}
});
}
});
} }
@Override @Override
protected void onStop() { protected void onStop() {
BackgroundService.RunCommand(MaterialActivity.this, new BackgroundService.InstanceCallback() { BackgroundService.removeGuiInUseCounter(this);
@Override
public void onServiceStart(BackgroundService service) {
service.removeDeviceListChangedCallback("MaterialActivity");
}
});
super.onStop(); super.onStop();
} }
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
service.setDiscoveryEnabled(true);
}
});
updateComputerList(); 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 //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? //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) //This way we can keep adding more options with null plugin id (eg: about)

View File

@@ -23,6 +23,7 @@ package org.kde.kdeconnect.UserInterface;
import android.os.Bundle; import android.os.Bundle;
import android.view.MenuItem; import android.view.MenuItem;
import org.kde.kdeconnect.BackgroundService;
import org.kde.kdeconnect_tp.R; import org.kde.kdeconnect_tp.R;
import java.util.Locale; 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);
}
} }

View File

@@ -22,6 +22,7 @@ package org.kde.kdeconnect.UserInterface;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.util.Log;
import android.view.MenuItem; import android.view.MenuItem;
import org.kde.kdeconnect.BackgroundService; import org.kde.kdeconnect.BackgroundService;
@@ -68,4 +69,17 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
} }
@Override
protected void onStart() {
super.onStart();
BackgroundService.addGuiInUseCounter(this);
}
@Override
protected void onStop() {
super.onStop();
BackgroundService.removeGuiInUseCounter(this);
}
} }