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

Fixed a race condition where onConnectionLost was called before onConnectionAccepted

+ MprisActivity now tries to reconnect to the plugin when there is a new connection,
just in case the new connection replaced the old one (the plugin would have been reloaded)
This commit is contained in:
Albert Vaca
2013-08-19 23:59:13 +02:00
parent b22d753410
commit 76c9def95b
6 changed files with 87 additions and 59 deletions

View File

@@ -58,38 +58,37 @@ public class BackgroundService extends Service {
@Override
public void onConnectionReceived(final NetworkPackage identityPackage, final BaseComputerLink link) {
Log.i("BackgroundService", "Connection accepted!");
Log.e("BackgroundService", "Connection accepted!");
runOnMainThread(new Runnable() { //Some plugins that create Handlers will crash if started from a different thread!
@Override
public void run() {
new Throwable().printStackTrace();
String deviceId = identityPackage.getString("deviceId");
String name = identityPackage.getString("deviceName");
String deviceId = identityPackage.getString("deviceId");
if (devices.containsKey(deviceId)) {
Log.i("BackgroundService", "known device");
Device device = devices.get(deviceId);
if (!device.hasName()) device.setName(identityPackage.getString("deviceName"));
device.addLink(link);
} else {
Log.i("BackgroundService", "unknown device");
Device device = new Device(getBaseContext(), deviceId, name, link);
devices.put(deviceId, device);
}
}
});
Device device = devices.get(deviceId);
if (device != null) {
Log.e("BackgroundService", "addLink, known device: "+deviceId);
if (!device.hasName()) device.setName(identityPackage.getString("deviceName"));
device.addLink(link);
} else {
Log.e("BackgroundService", "addLink,unknown device: "+deviceId);
String name = identityPackage.getString("deviceName");
device = new Device(getBaseContext(), deviceId, name, link);
devices.put(deviceId, device);
}
}
@Override
public void onConnectionLost(BaseComputerLink link) {
Device d = devices.get(link.getDeviceId());
Log.e("onConnectionLost","removeLink, deviceId: "+link.getDeviceId());
if (d != null) {
d.removeLink(link);
if (!d.isReachable() && !d.isTrusted()) {
devices.remove(link.getDeviceId());
}
} else {
Log.e("onConnectionLost","Removing connection to unknown device, this should not happen");
}
}
@@ -189,10 +188,4 @@ public class BackgroundService extends Service {
c.startService(serviceIntent);
}
Handler mainHandler = new Handler(Looper.getMainLooper());
private void runOnMainThread(Runnable runnable) {
mainHandler.post(runnable);
}
}

View File

@@ -117,10 +117,12 @@ public class Device implements BaseComputerLink.PackageReceiver {
//
public void addLink(BaseComputerLink link) {
Log.e("Device","addLink "+link.getLinkProvider().getName()+" -> "+getName());
links.add(link);
Log.e("Device","addLink "+link.getLinkProvider().getName()+" -> "+getName() + " active links: "+ links.size());
Collections.sort(links, new Comparator<BaseComputerLink>() {
@Override
public int compare(BaseComputerLink o, BaseComputerLink o2) {
@@ -138,6 +140,7 @@ public class Device implements BaseComputerLink.PackageReceiver {
public void removeLink(BaseComputerLink link) {
link.removePackageReceiver(this);
links.remove(link);
Log.e("Device","removeLink: "+link.getLinkProvider().getName() + " -> "+getName() + " active links: "+ links.size());
if (links.isEmpty()) {
reloadPluginsFromSettings();
}

View File

@@ -48,6 +48,7 @@ public class BroadcastTcpLinkProvider extends BaseLinkProvider {
String deviceId = brokenLink.getDeviceId();
if (visibleComputers.get(deviceId) == brokenLink) {
visibleComputers.remove(deviceId);
connectionLost(brokenLink);
}
}
@@ -158,8 +159,8 @@ public class BroadcastTcpLinkProvider extends BaseLinkProvider {
};
private void addLink(NetworkPackage identityPackage, NioSessionComputerLink link) {
Log.e("BroadcastTcpLinkProvider","addLink to "+identityPackage.getString("deviceName"));
String deviceId = identityPackage.getString("deviceId");
Log.e("BroadcastTcpLinkProvider","addLink to "+deviceId);
BaseComputerLink oldLink = visibleComputers.get(deviceId);
if (oldLink != null) {
Log.e("BroadcastTcpLinkProvider","Removing old connection to same device");

View File

@@ -13,6 +13,8 @@ import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import org.kde.connect.ComputerLinks.BaseComputerLink;
import org.kde.connect.LinkProviders.BaseLinkProvider;
import org.kde.connect.Plugins.MprisPlugin;
import org.kde.kdeconnect.R;
@@ -20,23 +22,20 @@ import java.util.ArrayList;
public class MprisActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mpris_control);
protected void connectToPlugin() {
final String deviceId = getIntent().getStringExtra("deviceId");
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
Log.e("MprisActivity", "onService start");
Device device = service.getDevice(deviceId);
final MprisPlugin mpris = (MprisPlugin) device.getPlugin("plugin_mpris");
if (mpris == null) {
Log.e("MprisActivity","device has no mpris plugin!");
//TODO: Show error
Log.e("MprisActivity", "device has no mpris plugin!");
return;
}
@@ -66,6 +65,7 @@ public class MprisActivity extends Activity {
mpris.setPlayerListUpdatedHandler(new Handler() {
boolean firstLoad = true;
@Override
public void handleMessage(Message msg) {
final ArrayList<String> playerList = mpris.getPlayerList();
@@ -81,11 +81,14 @@ public class MprisActivity extends Activity {
//String prevPlayer = (String)spinner.getSelectedItem();
spinner.setAdapter(adapter);
Log.e("MprisActivity", "playerListUpdatedHandler");
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long id) {
((TextView) findViewById(R.id.now_playing_textview)).setText("");
String player = playerList.get(pos);
Log.e("MprisActivity", "onPlayerSelected: " + player);
mpris.setPlayer(player);
//Spotify doesn't support changing the volume yet...
if (player.equals("Spotify")) {
@@ -114,6 +117,33 @@ public class MprisActivity extends Activity {
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mpris_control);
final String deviceId = getIntent().getStringExtra("deviceId");
BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
service.addConnectionListener(new BaseLinkProvider.ConnectionReceiver() {
@Override
public void onConnectionReceived(NetworkPackage identityPackage, BaseComputerLink link) {
connectToPlugin();
}
@Override
public void onConnectionLost(BaseComputerLink link) {
}
});
}
});
connectToPlugin();
findViewById(R.id.play_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

View File

@@ -92,6 +92,7 @@ public class ClipboardPlugin extends Plugin {
@Override
public boolean onPackageReceived(NetworkPackage np) {
if (!np.getType().equals(NetworkPackage.PACKAGE_TYPE_CLIPBOARD)) {
return false;
}

View File

@@ -190,11 +190,11 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
byte[] bitmapData = outStream.toByteArray();
byte[] serializedBitmapData = Base64.encode(bitmapData, Base64.NO_WRAP);
String stringBitmapData = new String(serializedBitmapData, Charset.defaultCharset());
//Es super gran lol, millor ho fem quan puguem enviar arxius
//The icon is super big, better sending it as a file transfer when we support that
//np.set("base64icon", stringBitmapData);
} catch(Exception e) {
e.printStackTrace();
Log.e("NotificationsPlugin","Error retrieveing icon");
Log.e("NotificationsPlugin","Error retrieving icon");
}
np.set("id", id.serialize());
@@ -251,33 +251,33 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
if (Build.VERSION.SDK_INT < 18) {
return new AlertDialog.Builder(baseContext)
.setTitle("Notifications Plugin")
.setMessage("This plugin is not compatible with Android 4.3")
.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
.setTitle("Notifications Plugin")
.setMessage("This plugin is not compatible with Android 4.3")
.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.create();
}
})
.create();
} else {
return new AlertDialog.Builder(baseContext)
.setTitle("Notifications Plugin")
.setMessage("You need to grant permission to access notifications")
.setPositiveButton("Open settings",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent=new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
baseContext.startActivity(intent);
}
})
.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Do nothing
}
})
.create();
.setTitle("Notifications Plugin")
.setMessage("You need to grant permission to access notifications")
.setPositiveButton("Open settings",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent=new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
baseContext.startActivity(intent);
}
})
.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Do nothing
}
})
.create();
}
}