mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-30 05:37:43 +00:00
Fixed android app bug
This commit is contained in:
parent
1692fb7e14
commit
edf991e884
@ -11,8 +11,8 @@ import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
|
||||
import org.kde.connect.ComputerLinks.BaseComputerLink;
|
||||
import org.kde.connect.Locators.AvahiLocator;
|
||||
import org.kde.connect.Locators.BaseLocator;
|
||||
import org.kde.connect.LinkProviders.BaseLinkProvider;
|
||||
import org.kde.connect.LinkProviders.AvahiLinkProvider;
|
||||
import org.kde.connect.PackageEmitters.BasePackageEmitter;
|
||||
import org.kde.connect.PackageEmitters.CallPackageEmitter;
|
||||
import org.kde.connect.PackageEmitters.PingPackageEmitter;
|
||||
@ -26,7 +26,7 @@ public class BackgroundService extends Service {
|
||||
|
||||
SharedPreferences settings;
|
||||
|
||||
ArrayList<BaseLocator> locators = new ArrayList<BaseLocator>();
|
||||
ArrayList<BaseLinkProvider> locators = new ArrayList<BaseLinkProvider>();
|
||||
ArrayList<BaseComputerLink> computerLinks = new ArrayList<BaseComputerLink>();
|
||||
|
||||
ArrayList<BasePackageEmitter> emitters = new ArrayList<BasePackageEmitter>();
|
||||
@ -42,7 +42,6 @@ public class BackgroundService extends Service {
|
||||
|
||||
computerLinks.add(cl);
|
||||
|
||||
|
||||
for(BasePackageEmitter pe : emitters) pe.addComputerLink(cl);
|
||||
for(BasePackageReceiver pr : receivers) cl.addPackageReceiver(pr);
|
||||
|
||||
@ -72,25 +71,26 @@ public class BackgroundService extends Service {
|
||||
|
||||
public void registerAnnouncers() {
|
||||
if (settings.getBoolean("announce_avahi", true)) {
|
||||
locators.add(new AvahiLocator(this));
|
||||
locators.add(new AvahiLinkProvider(this));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
Log.e("BackgroundService","Starting");
|
||||
|
||||
instance=this;
|
||||
attendCallbacks();
|
||||
return Service.START_STICKY;
|
||||
}
|
||||
|
||||
public void reachComputers() {
|
||||
for (BaseLocator a : locators) {
|
||||
a.reachComputers(new BaseLocator.ConnectionReceiver() {
|
||||
for (BaseLinkProvider a : locators) {
|
||||
a.reachComputers(new BaseLinkProvider.ConnectionReceiver() {
|
||||
@Override
|
||||
public void onConnectionAccepted(BaseComputerLink link) {
|
||||
Log.e("BackgroundService","Connection accepted!");
|
||||
//TODO: Check if there are other links available, and keep the best one
|
||||
addComputerLink(link);
|
||||
Log.e("BackgroundService", "Connection accepted!");
|
||||
//TODO: Check if there are other links available, and keep the best one
|
||||
addComputerLink(link);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -102,6 +102,8 @@ public class BackgroundService extends Service {
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
|
||||
Log.e("BackgroundService","Creating");
|
||||
|
||||
settings = getSharedPreferences("KdeConnect", 0);
|
||||
@ -112,13 +114,15 @@ public class BackgroundService extends Service {
|
||||
registerReceivers();
|
||||
registerAnnouncers();
|
||||
|
||||
super.onCreate();
|
||||
instance = this;
|
||||
attendCallbacks();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
Log.e("BackgroundService","Destroying");
|
||||
super.onDestroy();
|
||||
instance = null;
|
||||
}
|
||||
|
||||
|
||||
@ -126,10 +130,50 @@ public class BackgroundService extends Service {
|
||||
|
||||
|
||||
|
||||
//All kind of black magic to make the service a singleton
|
||||
|
||||
public interface InstanceCallback {
|
||||
void onServiceStart(BackgroundService service);
|
||||
}
|
||||
|
||||
private static BackgroundService instance = null;
|
||||
private static ArrayList<InstanceCallback> callbacks = new ArrayList<InstanceCallback>();
|
||||
|
||||
//Singleton service auxiliars
|
||||
private static void attendCallbacks() {
|
||||
for (InstanceCallback c : callbacks) {
|
||||
c.onServiceStart(instance);
|
||||
}
|
||||
callbacks.clear();
|
||||
}
|
||||
|
||||
public static void Start(Context c) {
|
||||
RunCommand(c, null);
|
||||
}
|
||||
|
||||
public static void RunCommand(Context c, final InstanceCallback callback) {
|
||||
|
||||
if (callback != null) callbacks.add(callback);
|
||||
|
||||
if (instance == null) {
|
||||
Intent serviceIntent = new Intent(c, BackgroundService.class);
|
||||
c.startService(serviceIntent);
|
||||
try {
|
||||
c.bindService(serviceIntent, new ServiceConnection() {
|
||||
public void onServiceDisconnected(ComponentName name) {
|
||||
instance = null;
|
||||
}
|
||||
public void onServiceConnected(ComponentName name, IBinder binder) {
|
||||
instance = ((LocalBinder) binder).getInstance();
|
||||
attendCallbacks();
|
||||
}
|
||||
}, Service.BIND_AUTO_CREATE);
|
||||
} catch(Exception e) {
|
||||
|
||||
}
|
||||
} else {
|
||||
attendCallbacks();
|
||||
}
|
||||
}
|
||||
|
||||
private class LocalBinder extends Binder {
|
||||
public BackgroundService getInstance() {
|
||||
@ -137,48 +181,10 @@ public class BackgroundService extends Service {
|
||||
}
|
||||
}
|
||||
|
||||
public interface ServiceStartCallback {
|
||||
void onServiceStart(BackgroundService service);
|
||||
}
|
||||
|
||||
private IBinder mBinder = new LocalBinder();
|
||||
private static BackgroundService instance = null;
|
||||
|
||||
public static BackgroundService GetInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static void Start(Context c) {
|
||||
Start(c,null);
|
||||
}
|
||||
|
||||
public static void Start(Context c, final ServiceStartCallback callback) {
|
||||
|
||||
if (instance != null) {
|
||||
Log.e("SingletonService","Already started");
|
||||
callback.onServiceStart(instance);
|
||||
}
|
||||
|
||||
Intent serviceIntent = new Intent(c, BackgroundService.class);
|
||||
c.startService(serviceIntent);
|
||||
c.bindService(serviceIntent, new ServiceConnection() {
|
||||
|
||||
public void onServiceDisconnected(ComponentName name) {
|
||||
instance = null;
|
||||
}
|
||||
|
||||
public void onServiceConnected(ComponentName name, IBinder binder) {
|
||||
instance = ((LocalBinder) binder).getInstance();
|
||||
if (callback != null) callback.onServiceStart(instance);
|
||||
}
|
||||
|
||||
}, Service.BIND_AUTO_CREATE);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return mBinder;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -53,15 +53,11 @@ public class UdpComputerLink extends BaseComputerLink {
|
||||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
try {
|
||||
Log.e("UdpComputerLink","A");
|
||||
DatagramSocket s = new DatagramSocket();
|
||||
int msg_length = messageStr.length();
|
||||
byte[] message = messageStr.getBytes();
|
||||
Log.e("UdpComputerLink","B");
|
||||
DatagramPacket p = new DatagramPacket(message, msg_length,IP,UDP_PORT);
|
||||
Log.e("UdpComputerLink","C");
|
||||
s.send(p);
|
||||
Log.e("UdpComputerLink","D");
|
||||
Log.e("Sent", messageStr);
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
|
@ -1,4 +1,4 @@
|
||||
package org.kde.connect.Locators;
|
||||
package org.kde.connect.LinkProviders;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.nsd.NsdManager;
|
||||
@ -6,14 +6,13 @@ import android.net.nsd.NsdServiceInfo;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
|
||||
import org.kde.connect.ComputerLinks.BaseComputerLink;
|
||||
import org.kde.connect.ComputerLinks.UdpComputerLink;
|
||||
import org.kde.connect.Types.NetworkPackage;
|
||||
|
||||
import java.lang.Override;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class AvahiLocator implements BaseLocator {
|
||||
public class AvahiLinkProvider implements BaseLinkProvider {
|
||||
|
||||
String serviceType = "_kdeconnect._udp";
|
||||
|
||||
@ -22,25 +21,25 @@ public class AvahiLocator implements BaseLocator {
|
||||
ArrayList<UdpComputerLink> visibleComputers = new ArrayList<UdpComputerLink>();
|
||||
|
||||
|
||||
public AvahiLocator(Context context) {
|
||||
public AvahiLinkProvider(Context context) {
|
||||
mNsdManager = (NsdManager)context.getSystemService(Context.NSD_SERVICE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reachComputers(final ConnectionReceiver cr) {
|
||||
|
||||
Log.e("AvahiLocator", "Discovering computers...");
|
||||
Log.e("AvahiLinkProvider", "Discovering computers...");
|
||||
|
||||
final NsdManager.ResolveListener mResolveListener = new NsdManager.ResolveListener() {
|
||||
|
||||
@Override
|
||||
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
|
||||
Log.e("AvahiLocator", "Resolve failed" + errorCode);
|
||||
Log.e("AvahiLinkProvider", "Resolve failed" + errorCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceResolved(NsdServiceInfo serviceInfo) {
|
||||
Log.e("AvahiLocator", "Resolve Succeeded. " + serviceInfo);
|
||||
Log.e("AvahiLinkProvider", "Resolve Succeeded. " + serviceInfo);
|
||||
|
||||
//Computer found
|
||||
|
||||
@ -67,17 +66,17 @@ public class AvahiLocator implements BaseLocator {
|
||||
|
||||
@Override
|
||||
public void onDiscoveryStarted(String regType) {
|
||||
Log.e("AvahiLocator", "Service discovery started");
|
||||
Log.e("AvahiLinkProvider", "Service discovery started");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceFound(NsdServiceInfo service) {
|
||||
Log.e("AvahiLocator", "Service discovery success" + service);
|
||||
Log.e("AvahiLinkProvider", "Service discovery success" + service);
|
||||
|
||||
if (!service.getServiceType().startsWith(serviceType)) {
|
||||
Log.e("AvahiLocator", "Unknown Service Type: " + service.getServiceType());
|
||||
Log.e("AvahiLinkProvider", "Unknown Service Type: " + service.getServiceType());
|
||||
} else {
|
||||
Log.e("AvahiLocator", "Computer found, resolving...");
|
||||
Log.e("AvahiLinkProvider", "Computer found, resolving...");
|
||||
mNsdManager.resolveService(service, mResolveListener);
|
||||
}
|
||||
|
||||
@ -85,24 +84,24 @@ public class AvahiLocator implements BaseLocator {
|
||||
|
||||
@Override
|
||||
public void onServiceLost(NsdServiceInfo service) {
|
||||
Log.e("AvahiLocator", "service lost" + service);
|
||||
Log.e("AvahiLinkProvider", "service lost" + service);
|
||||
visibleComputers.remove(service);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDiscoveryStopped(String serviceType) {
|
||||
Log.e("AvahiLocator", "Discovery stopped: " + serviceType);
|
||||
Log.e("AvahiLinkProvider", "Discovery stopped: " + serviceType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
|
||||
Log.e("AvahiLocator", "Discovery failed: Error code:" + errorCode);
|
||||
Log.e("AvahiLinkProvider", "Discovery failed: Error code:" + errorCode);
|
||||
mNsdManager.stopServiceDiscovery(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
|
||||
Log.e("AvahiLocator", "Discovery failed: Error code:" + errorCode);
|
||||
Log.e("AvahiLinkProvider", "Discovery failed: Error code:" + errorCode);
|
||||
mNsdManager.stopServiceDiscovery(this);
|
||||
}
|
||||
};
|
@ -1,8 +1,8 @@
|
||||
package org.kde.connect.Locators;
|
||||
package org.kde.connect.LinkProviders;
|
||||
|
||||
import org.kde.connect.ComputerLinks.BaseComputerLink;
|
||||
|
||||
public interface BaseLocator {
|
||||
public interface BaseLinkProvider {
|
||||
|
||||
public interface ConnectionReceiver {
|
||||
public void onConnectionAccepted(BaseComputerLink link);
|
@ -14,25 +14,24 @@ import android.view.View.OnClickListener;
|
||||
|
||||
public class MainActivity extends Activity {
|
||||
|
||||
private BackgroundService service = null;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
BackgroundService.Start(MainActivity.this, new BackgroundService.ServiceStartCallback() {
|
||||
@Override
|
||||
public void onServiceStart(BackgroundService s) {
|
||||
service = s;
|
||||
}
|
||||
});
|
||||
BackgroundService.Start(MainActivity.this);
|
||||
|
||||
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.i("MainActivity","Button2");
|
||||
if (service != null) service.reachComputers();
|
||||
BackgroundService.RunCommand(MainActivity.this, new BackgroundService.InstanceCallback() {
|
||||
@Override
|
||||
public void onServiceStart(BackgroundService service) {
|
||||
service.reachComputers();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@ -40,7 +39,13 @@ public class MainActivity extends Activity {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.i("MainActivity","Button3");
|
||||
if (service != null) service.sendPing();
|
||||
BackgroundService.RunCommand(MainActivity.this, new BackgroundService.InstanceCallback() {
|
||||
@Override
|
||||
public void onServiceStart(BackgroundService service) {
|
||||
service.sendPing();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -15,7 +15,8 @@ public class PingPackageReceiver implements BasePackageReceiver {
|
||||
|
||||
@Override
|
||||
public void onPackageReceived(NetworkPackage np) {
|
||||
|
||||
Toast.makeText(context, "Ping!", Toast.LENGTH_LONG).show();
|
||||
if (np.getType() == NetworkPackage.Type.PING) {
|
||||
Toast.makeText(context, "Ping!", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,8 +29,12 @@ public class ServiceLauncher extends BroadcastReceiver
|
||||
BackgroundService.Start(context);
|
||||
} else if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
|
||||
Log.e("KdeConnect", "Connection state changed, trying to connect");
|
||||
BackgroundService service = BackgroundService.GetInstance();
|
||||
service.reachComputers();
|
||||
BackgroundService.RunCommand(context, new BackgroundService.InstanceCallback() {
|
||||
@Override
|
||||
public void onServiceStart(BackgroundService service) {
|
||||
service.reachComputers();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
Log.e("KdeConnect", "Ignoring broadcast event: "+intent.getAction());
|
||||
}
|
||||
|
@ -111,6 +111,7 @@ public class NetworkPackage {
|
||||
static public NetworkPackage fromString(String s/*, Context context*/) {
|
||||
Log.i("NetworkPackage.fromString",s);
|
||||
|
||||
//TODO: Implement
|
||||
NetworkPackage np = new NetworkPackage(123456789 /*, context*/);
|
||||
np.mType = Type.PAIR_REQUEST;
|
||||
return np;
|
||||
|
@ -9,7 +9,7 @@
|
||||
tools:context=".MainActivity"
|
||||
android:orientation="vertical">
|
||||
|
||||
<Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start pairing"/>
|
||||
<Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send ping to connected computers"/>
|
||||
<Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Announce to network"/>
|
||||
<Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send ping"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
Loading…
x
Reference in New Issue
Block a user