mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-30 05:37:43 +00:00
Implemented activity-service comunication to android app
This commit is contained in:
parent
60c63fe27a
commit
dd2a545e0d
@ -4,8 +4,6 @@
|
||||
<facet type="android-gradle" name="Android-Gradle">
|
||||
<configuration>
|
||||
<option name="GRADLE_PROJECT_PATH" value=":" />
|
||||
<option name="PROJECT_ABSOLUTE_PATH" value="$MODULE_DIR$" />
|
||||
<option name="GRADLE_HOME_DIR_PATH" />
|
||||
</configuration>
|
||||
</facet>
|
||||
</component>
|
||||
|
@ -14,8 +14,6 @@
|
||||
<facet type="android-gradle" name="Android-Gradle">
|
||||
<configuration>
|
||||
<option name="GRADLE_PROJECT_PATH" value=":KdeConnect" />
|
||||
<option name="PROJECT_ABSOLUTE_PATH" value="$MODULE_DIR$/.." />
|
||||
<option name="GRADLE_HOME_DIR_PATH" />
|
||||
</configuration>
|
||||
</facet>
|
||||
</component>
|
||||
|
@ -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<Announcer> announcers = new ArrayList<Announcer>();
|
||||
ArrayList<ComputerLink> computerLinks = new ArrayList<ComputerLink>();
|
||||
ArrayList<BaseReceiver> receivers = new ArrayList<BaseReceiver>();
|
||||
|
||||
ArrayList<BasePackageEmitter> emitters = new ArrayList<BasePackageEmitter>();
|
||||
ArrayList<BasePackageReceiver> receivers = new ArrayList<BasePackageReceiver>();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -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<ComputerLink> computerLinks = new ArrayList<ComputerLink>();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
package org.kde.connect.PackageEmitters;
|
||||
|
||||
public interface BaseReceiver {
|
||||
|
||||
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -9,6 +9,7 @@
|
||||
tools:context=".MainActivity"
|
||||
android:orientation="vertical">
|
||||
|
||||
<Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Launch service"/>
|
||||
<Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Launch service"/>
|
||||
<Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Ping computers"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
Loading…
x
Reference in New Issue
Block a user