From dd2a545e0dab64f67cc280a768afda959bb3faa3 Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Tue, 18 Jun 2013 02:14:22 +0200 Subject: [PATCH] Implemented activity-service comunication to android app --- Android.iml | 2 - KdeConnect/KdeConnect.iml | 2 - .../org/kde/connect/BackgroundService.java | 53 ++++++++++++++++--- .../java/org/kde/connect/MainActivity.java | 29 ++++++++-- .../PackageEmitters/BasePackageEmitter.java | 25 +++++++++ .../connect/PackageEmitters/BaseReceiver.java | 6 --- ...lReceiver.java => CallPackageEmitter.java} | 14 ++--- .../PackageEmitters/PingPackageEmitter.java | 30 +++++++++++ .../PackageReceivers/BasePackageReceiver.java | 12 +++++ .../PackageReceivers/PingPackageReceiver.java | 21 ++++++++ .../src/main/res/layout/activity_main.xml | 3 +- 11 files changed, 168 insertions(+), 29 deletions(-) create mode 100644 KdeConnect/src/main/java/org/kde/connect/PackageEmitters/BasePackageEmitter.java delete mode 100644 KdeConnect/src/main/java/org/kde/connect/PackageEmitters/BaseReceiver.java rename KdeConnect/src/main/java/org/kde/connect/PackageEmitters/{PhoneCallReceiver.java => CallPackageEmitter.java} (84%) create mode 100644 KdeConnect/src/main/java/org/kde/connect/PackageEmitters/PingPackageEmitter.java create mode 100644 KdeConnect/src/main/java/org/kde/connect/PackageReceivers/BasePackageReceiver.java create mode 100644 KdeConnect/src/main/java/org/kde/connect/PackageReceivers/PingPackageReceiver.java diff --git a/Android.iml b/Android.iml index 286bd256..a6b20cc8 100644 --- a/Android.iml +++ b/Android.iml @@ -4,8 +4,6 @@ diff --git a/KdeConnect/KdeConnect.iml b/KdeConnect/KdeConnect.iml index 0e40c467..bdbb99c8 100644 --- a/KdeConnect/KdeConnect.iml +++ b/KdeConnect/KdeConnect.iml @@ -14,8 +14,6 @@ diff --git a/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java b/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java index e6627b1d..93595cb5 100644 --- a/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java +++ b/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java @@ -3,11 +3,15 @@ package org.kde.connect; import android.app.Service; import android.content.Intent; import android.content.SharedPreferences; +import android.os.Binder; import android.os.IBinder; import android.util.Log; -import org.kde.connect.PackageEmitters.BaseReceiver; -import org.kde.connect.PackageEmitters.PhoneCallReceiver; +import org.kde.connect.PackageEmitters.BasePackageEmitter; +import org.kde.connect.PackageEmitters.CallPackageEmitter; +import org.kde.connect.PackageEmitters.PingPackageEmitter; +import org.kde.connect.PackageReceivers.BasePackageReceiver; +import org.kde.connect.PackageReceivers.PingPackageReceiver; import org.kde.connect.Types.NetworkPackage; import java.util.ArrayList; @@ -17,15 +21,16 @@ public class BackgroundService extends Service { SharedPreferences settings; ArrayList announcers = new ArrayList(); ArrayList computerLinks = new ArrayList(); - ArrayList receivers = new ArrayList(); + + ArrayList emitters = new ArrayList(); + ArrayList receivers = new ArrayList(); + + PingPackageEmitter pingEmitter; private void addComputerLink(ComputerLink cl) { computerLinks.add(cl); - if (settings.getBoolean("listenCalls", true)) { - receivers.add(new PhoneCallReceiver(getApplicationContext(), cl)); - } NetworkPackage p = new NetworkPackage(System.currentTimeMillis()); p.setType(NetworkPackage.Type.PING); @@ -33,6 +38,23 @@ public class BackgroundService extends Service { } + private void registerEmitters() { + if (settings.getBoolean("emit_call", true)) { + emitters.add(new CallPackageEmitter(getApplicationContext())); + } + + pingEmitter = new PingPackageEmitter(getApplicationContext()); + if (settings.getBoolean("emit_ping", true)) { + emitters.add(pingEmitter); + } + } + + private void registerReceivers() { + if (settings.getBoolean("receive_ping", true)) { + receivers.add(new PingPackageReceiver(getApplicationContext())); + } + } + @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e("BackgroundService","Starting"); @@ -49,10 +71,17 @@ public class BackgroundService extends Service { return Service.START_STICKY; } + public void sendPing() { + pingEmitter.sendPing(); + } + @Override public void onCreate() { Log.e("BackgroundService","Creating"); + registerEmitters(); + registerReceivers(); + settings = getSharedPreferences("KdeConnect", 0); announcers.add(new AvahiAnnouncer(this)); @@ -66,10 +95,18 @@ public class BackgroundService extends Service { super.onDestroy(); } + + public class LocalBinder extends Binder { + public BackgroundService getInstance() { + return BackgroundService.this; + } + } + IBinder mBinder = new LocalBinder(); + @Override public IBinder onBind(Intent intent) { - Log.e("BackgroundService", "Binding"); - return null; + return mBinder; } + } diff --git a/KdeConnect/src/main/java/org/kde/connect/MainActivity.java b/KdeConnect/src/main/java/org/kde/connect/MainActivity.java index 37177865..b5e9ce72 100644 --- a/KdeConnect/src/main/java/org/kde/connect/MainActivity.java +++ b/KdeConnect/src/main/java/org/kde/connect/MainActivity.java @@ -1,26 +1,49 @@ package org.kde.connect; import android.app.Activity; +import android.content.ComponentName; import android.content.Intent; +import android.content.ServiceConnection; import android.net.nsd.NsdServiceInfo; import android.os.Bundle; +import android.os.IBinder; import android.view.Menu; import android.view.View; 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); - findViewById(R.id.button2).setOnClickListener(new OnClickListener() { + findViewById(R.id.button1).setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { - Intent service = new Intent(MainActivity.this, BackgroundService.class); - startService(service); + Intent serviceIntent = new Intent(MainActivity.this, BackgroundService.class); + startService(serviceIntent); + bindService(serviceIntent, new ServiceConnection() { + + public void onServiceDisconnected(ComponentName name) { + service = null; + } + + public void onServiceConnected(ComponentName name, IBinder binder) { + service = ((BackgroundService.LocalBinder)binder).getInstance(); + } + },BIND_AUTO_CREATE); + + } + }); + + findViewById(R.id.button2).setOnClickListener(new OnClickListener() { + @Override + public void onClick(View view) { + if (service != null) service.sendPing(); } }); diff --git a/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/BasePackageEmitter.java b/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/BasePackageEmitter.java new file mode 100644 index 00000000..cbe0211e --- /dev/null +++ b/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/BasePackageEmitter.java @@ -0,0 +1,25 @@ +package org.kde.connect.PackageEmitters; + +import org.kde.connect.ComputerLink; +import org.kde.connect.Types.NetworkPackage; + +import java.util.ArrayList; + +public class BasePackageEmitter { + + private ArrayList computerLinks = new ArrayList(); + + public void addComputerLink(ComputerLink cl) { + computerLinks.add(cl); + } + + protected int countLinkedComputers() { + return computerLinks.size(); + } + + protected void sendPackage(NetworkPackage np) { + for(ComputerLink cl : computerLinks) { + cl.sendPackage(np); + } + } +} diff --git a/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/BaseReceiver.java b/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/BaseReceiver.java deleted file mode 100644 index ccb6ff01..00000000 --- a/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/BaseReceiver.java +++ /dev/null @@ -1,6 +0,0 @@ -package org.kde.connect.PackageEmitters; - -public interface BaseReceiver { - - -} diff --git a/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/PhoneCallReceiver.java b/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/CallPackageEmitter.java similarity index 84% rename from KdeConnect/src/main/java/org/kde/connect/PackageEmitters/PhoneCallReceiver.java rename to KdeConnect/src/main/java/org/kde/connect/PackageEmitters/CallPackageEmitter.java index a4056073..a813708e 100644 --- a/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/PhoneCallReceiver.java +++ b/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/CallPackageEmitter.java @@ -9,11 +9,11 @@ import org.kde.connect.ComputerLink; import org.kde.connect.Types.NetworkPackage; -public class PhoneCallReceiver implements BaseReceiver { +public class CallPackageEmitter extends BasePackageEmitter { - public PhoneCallReceiver(final Context ctx, final ComputerLink dc) { + public CallPackageEmitter(final Context ctx) { - Log.i("PhoneCallReceiver", "Registered"); + Log.i("CallPackageEmitter", "Registered"); PhoneStateListener callStateListener = new PhoneStateListener() { @@ -34,7 +34,7 @@ public class PhoneCallReceiver implements BaseReceiver { lastPackage.setType(NetworkPackage.Type.RING); lastPackage.setBody(incomingNumber); - dc.sendPackage(lastPackage); + sendPackage(lastPackage); break; @@ -42,7 +42,7 @@ public class PhoneCallReceiver implements BaseReceiver { if (lastPackage != null) { lastPackage.cancel(); - dc.sendPackage(lastPackage); + sendPackage(lastPackage); lastPackage = null; } @@ -53,7 +53,7 @@ public class PhoneCallReceiver implements BaseReceiver { if (lastState != TelephonyManager.CALL_STATE_IDLE && lastPackage != null) { lastPackage.cancel(); - dc.sendPackage(lastPackage); + sendPackage(lastPackage); if (lastState == TelephonyManager.CALL_STATE_RINGING) { @@ -64,7 +64,7 @@ public class PhoneCallReceiver implements BaseReceiver { lastPackage.setType(NetworkPackage.Type.MISSED); lastPackage.setBody(incomingNumber); - dc.sendPackage(lastPackage); + sendPackage(lastPackage); } diff --git a/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/PingPackageEmitter.java b/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/PingPackageEmitter.java new file mode 100644 index 00000000..a3717cff --- /dev/null +++ b/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/PingPackageEmitter.java @@ -0,0 +1,30 @@ +package org.kde.connect.PackageEmitters; + +import android.content.Context; +import android.telephony.PhoneStateListener; +import android.telephony.TelephonyManager; +import android.util.Log; + +import org.kde.connect.ComputerLink; +import org.kde.connect.Types.NetworkPackage; + + +public class PingPackageEmitter extends BasePackageEmitter { + + + public PingPackageEmitter(Context ctx) { + + } + + public void sendPing() { + Log.i("PingPackageEmitter", "sendPing to "+countLinkedComputers()); + + NetworkPackage lastPackage = new NetworkPackage(System.currentTimeMillis()); + + lastPackage.setType(NetworkPackage.Type.PING); + lastPackage.setBody("Ping!"); + + sendPackage(lastPackage); + } + +} diff --git a/KdeConnect/src/main/java/org/kde/connect/PackageReceivers/BasePackageReceiver.java b/KdeConnect/src/main/java/org/kde/connect/PackageReceivers/BasePackageReceiver.java new file mode 100644 index 00000000..db930c15 --- /dev/null +++ b/KdeConnect/src/main/java/org/kde/connect/PackageReceivers/BasePackageReceiver.java @@ -0,0 +1,12 @@ +package org.kde.connect.PackageReceivers; + +import org.kde.connect.ComputerLink; +import org.kde.connect.Types.NetworkPackage; + +import java.util.ArrayList; + +public interface BasePackageReceiver { + + public void receivePackage(NetworkPackage np); + +} diff --git a/KdeConnect/src/main/java/org/kde/connect/PackageReceivers/PingPackageReceiver.java b/KdeConnect/src/main/java/org/kde/connect/PackageReceivers/PingPackageReceiver.java new file mode 100644 index 00000000..1fb88077 --- /dev/null +++ b/KdeConnect/src/main/java/org/kde/connect/PackageReceivers/PingPackageReceiver.java @@ -0,0 +1,21 @@ +package org.kde.connect.PackageReceivers; + +import android.content.Context; +import android.widget.Toast; + +import org.kde.connect.Types.NetworkPackage; + +public class PingPackageReceiver implements BasePackageReceiver { + + Context context; + + public PingPackageReceiver(Context ctx) { + context = ctx; + } + + @Override + public void receivePackage(NetworkPackage np) { + + Toast.makeText(context, "Ping!", Toast.LENGTH_LONG).show(); + } +} diff --git a/KdeConnect/src/main/res/layout/activity_main.xml b/KdeConnect/src/main/res/layout/activity_main.xml index bca6eff1..b8eaaa8c 100644 --- a/KdeConnect/src/main/res/layout/activity_main.xml +++ b/KdeConnect/src/main/res/layout/activity_main.xml @@ -9,6 +9,7 @@ tools:context=".MainActivity" android:orientation="vertical"> -