From edf991e8840dedb3ac062139844c51afa365a018 Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Tue, 2 Jul 2013 15:22:05 +0200 Subject: [PATCH] Fixed android app bug --- .../org/kde/connect/BackgroundService.java | 108 +++++++++--------- .../ComputerLinks/UdpComputerLink.java | 4 - .../AvahiLinkProvider.java} | 29 +++-- .../BaseLinkProvider.java} | 4 +- .../java/org/kde/connect/MainActivity.java | 23 ++-- .../PackageReceivers/PingPackageReceiver.java | 5 +- .../java/org/kde/connect/ServiceLauncher.java | 8 +- .../org/kde/connect/Types/NetworkPackage.java | 1 + .../src/main/res/layout/activity_main.xml | 4 +- 9 files changed, 99 insertions(+), 87 deletions(-) rename KdeConnect/src/main/java/org/kde/connect/{Locators/AvahiLocator.java => LinkProviders/AvahiLinkProvider.java} (74%) rename KdeConnect/src/main/java/org/kde/connect/{Locators/BaseLocator.java => LinkProviders/BaseLinkProvider.java} (76%) diff --git a/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java b/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java index 38084146..2c974d49 100644 --- a/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java +++ b/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java @@ -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 locators = new ArrayList(); + ArrayList locators = new ArrayList(); ArrayList computerLinks = new ArrayList(); ArrayList emitters = new ArrayList(); @@ -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 callbacks = new ArrayList(); - //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; } - } diff --git a/KdeConnect/src/main/java/org/kde/connect/ComputerLinks/UdpComputerLink.java b/KdeConnect/src/main/java/org/kde/connect/ComputerLinks/UdpComputerLink.java index f48d5d91..71449a75 100644 --- a/KdeConnect/src/main/java/org/kde/connect/ComputerLinks/UdpComputerLink.java +++ b/KdeConnect/src/main/java/org/kde/connect/ComputerLinks/UdpComputerLink.java @@ -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(); diff --git a/KdeConnect/src/main/java/org/kde/connect/Locators/AvahiLocator.java b/KdeConnect/src/main/java/org/kde/connect/LinkProviders/AvahiLinkProvider.java similarity index 74% rename from KdeConnect/src/main/java/org/kde/connect/Locators/AvahiLocator.java rename to KdeConnect/src/main/java/org/kde/connect/LinkProviders/AvahiLinkProvider.java index cd699522..6f63d315 100644 --- a/KdeConnect/src/main/java/org/kde/connect/Locators/AvahiLocator.java +++ b/KdeConnect/src/main/java/org/kde/connect/LinkProviders/AvahiLinkProvider.java @@ -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 visibleComputers = new ArrayList(); - 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); } }; diff --git a/KdeConnect/src/main/java/org/kde/connect/Locators/BaseLocator.java b/KdeConnect/src/main/java/org/kde/connect/LinkProviders/BaseLinkProvider.java similarity index 76% rename from KdeConnect/src/main/java/org/kde/connect/Locators/BaseLocator.java rename to KdeConnect/src/main/java/org/kde/connect/LinkProviders/BaseLinkProvider.java index a9c85edf..89cde52f 100644 --- a/KdeConnect/src/main/java/org/kde/connect/Locators/BaseLocator.java +++ b/KdeConnect/src/main/java/org/kde/connect/LinkProviders/BaseLinkProvider.java @@ -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); diff --git a/KdeConnect/src/main/java/org/kde/connect/MainActivity.java b/KdeConnect/src/main/java/org/kde/connect/MainActivity.java index 414390e8..4f8c76d9 100644 --- a/KdeConnect/src/main/java/org/kde/connect/MainActivity.java +++ b/KdeConnect/src/main/java/org/kde/connect/MainActivity.java @@ -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(); + } + }); + } }); diff --git a/KdeConnect/src/main/java/org/kde/connect/PackageReceivers/PingPackageReceiver.java b/KdeConnect/src/main/java/org/kde/connect/PackageReceivers/PingPackageReceiver.java index c89c2ccb..04f9527d 100644 --- a/KdeConnect/src/main/java/org/kde/connect/PackageReceivers/PingPackageReceiver.java +++ b/KdeConnect/src/main/java/org/kde/connect/PackageReceivers/PingPackageReceiver.java @@ -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(); + } } } diff --git a/KdeConnect/src/main/java/org/kde/connect/ServiceLauncher.java b/KdeConnect/src/main/java/org/kde/connect/ServiceLauncher.java index f22d08ca..a99cc12f 100644 --- a/KdeConnect/src/main/java/org/kde/connect/ServiceLauncher.java +++ b/KdeConnect/src/main/java/org/kde/connect/ServiceLauncher.java @@ -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()); } diff --git a/KdeConnect/src/main/java/org/kde/connect/Types/NetworkPackage.java b/KdeConnect/src/main/java/org/kde/connect/Types/NetworkPackage.java index 4504f74f..9b931208 100644 --- a/KdeConnect/src/main/java/org/kde/connect/Types/NetworkPackage.java +++ b/KdeConnect/src/main/java/org/kde/connect/Types/NetworkPackage.java @@ -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; diff --git a/KdeConnect/src/main/res/layout/activity_main.xml b/KdeConnect/src/main/res/layout/activity_main.xml index 3cdcbb40..393cb9e3 100644 --- a/KdeConnect/src/main/res/layout/activity_main.xml +++ b/KdeConnect/src/main/res/layout/activity_main.xml @@ -9,7 +9,7 @@ tools:context=".MainActivity" android:orientation="vertical"> -