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">
|
<facet type="android-gradle" name="Android-Gradle">
|
||||||
<configuration>
|
<configuration>
|
||||||
<option name="GRADLE_PROJECT_PATH" value=":" />
|
<option name="GRADLE_PROJECT_PATH" value=":" />
|
||||||
<option name="PROJECT_ABSOLUTE_PATH" value="$MODULE_DIR$" />
|
|
||||||
<option name="GRADLE_HOME_DIR_PATH" />
|
|
||||||
</configuration>
|
</configuration>
|
||||||
</facet>
|
</facet>
|
||||||
</component>
|
</component>
|
||||||
|
@ -14,8 +14,6 @@
|
|||||||
<facet type="android-gradle" name="Android-Gradle">
|
<facet type="android-gradle" name="Android-Gradle">
|
||||||
<configuration>
|
<configuration>
|
||||||
<option name="GRADLE_PROJECT_PATH" value=":KdeConnect" />
|
<option name="GRADLE_PROJECT_PATH" value=":KdeConnect" />
|
||||||
<option name="PROJECT_ABSOLUTE_PATH" value="$MODULE_DIR$/.." />
|
|
||||||
<option name="GRADLE_HOME_DIR_PATH" />
|
|
||||||
</configuration>
|
</configuration>
|
||||||
</facet>
|
</facet>
|
||||||
</component>
|
</component>
|
||||||
|
@ -3,11 +3,15 @@ package org.kde.connect;
|
|||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
import android.os.Binder;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import org.kde.connect.PackageEmitters.BaseReceiver;
|
import org.kde.connect.PackageEmitters.BasePackageEmitter;
|
||||||
import org.kde.connect.PackageEmitters.PhoneCallReceiver;
|
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 org.kde.connect.Types.NetworkPackage;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -17,15 +21,16 @@ public class BackgroundService extends Service {
|
|||||||
SharedPreferences settings;
|
SharedPreferences settings;
|
||||||
ArrayList<Announcer> announcers = new ArrayList<Announcer>();
|
ArrayList<Announcer> announcers = new ArrayList<Announcer>();
|
||||||
ArrayList<ComputerLink> computerLinks = new ArrayList<ComputerLink>();
|
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) {
|
private void addComputerLink(ComputerLink cl) {
|
||||||
|
|
||||||
computerLinks.add(cl);
|
computerLinks.add(cl);
|
||||||
|
|
||||||
if (settings.getBoolean("listenCalls", true)) {
|
|
||||||
receivers.add(new PhoneCallReceiver(getApplicationContext(), cl));
|
|
||||||
}
|
|
||||||
|
|
||||||
NetworkPackage p = new NetworkPackage(System.currentTimeMillis());
|
NetworkPackage p = new NetworkPackage(System.currentTimeMillis());
|
||||||
p.setType(NetworkPackage.Type.PING);
|
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
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
Log.e("BackgroundService","Starting");
|
Log.e("BackgroundService","Starting");
|
||||||
@ -49,10 +71,17 @@ public class BackgroundService extends Service {
|
|||||||
return Service.START_STICKY;
|
return Service.START_STICKY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void sendPing() {
|
||||||
|
pingEmitter.sendPing();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
Log.e("BackgroundService","Creating");
|
Log.e("BackgroundService","Creating");
|
||||||
|
|
||||||
|
registerEmitters();
|
||||||
|
registerReceivers();
|
||||||
|
|
||||||
settings = getSharedPreferences("KdeConnect", 0);
|
settings = getSharedPreferences("KdeConnect", 0);
|
||||||
|
|
||||||
announcers.add(new AvahiAnnouncer(this));
|
announcers.add(new AvahiAnnouncer(this));
|
||||||
@ -66,10 +95,18 @@ public class BackgroundService extends Service {
|
|||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class LocalBinder extends Binder {
|
||||||
|
public BackgroundService getInstance() {
|
||||||
|
return BackgroundService.this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
IBinder mBinder = new LocalBinder();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
Log.e("BackgroundService", "Binding");
|
return mBinder;
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,26 +1,49 @@
|
|||||||
package org.kde.connect;
|
package org.kde.connect;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.content.ComponentName;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.ServiceConnection;
|
||||||
import android.net.nsd.NsdServiceInfo;
|
import android.net.nsd.NsdServiceInfo;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.IBinder;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.View.OnClickListener;
|
import android.view.View.OnClickListener;
|
||||||
|
|
||||||
public class MainActivity extends Activity {
|
public class MainActivity extends Activity {
|
||||||
|
|
||||||
|
private BackgroundService service = null;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
|
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
|
|
||||||
Intent service = new Intent(MainActivity.this, BackgroundService.class);
|
Intent serviceIntent = new Intent(MainActivity.this, BackgroundService.class);
|
||||||
startService(service);
|
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;
|
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() {
|
PhoneStateListener callStateListener = new PhoneStateListener() {
|
||||||
|
|
||||||
@ -34,7 +34,7 @@ public class PhoneCallReceiver implements BaseReceiver {
|
|||||||
lastPackage.setType(NetworkPackage.Type.RING);
|
lastPackage.setType(NetworkPackage.Type.RING);
|
||||||
lastPackage.setBody(incomingNumber);
|
lastPackage.setBody(incomingNumber);
|
||||||
|
|
||||||
dc.sendPackage(lastPackage);
|
sendPackage(lastPackage);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ public class PhoneCallReceiver implements BaseReceiver {
|
|||||||
|
|
||||||
if (lastPackage != null) {
|
if (lastPackage != null) {
|
||||||
lastPackage.cancel();
|
lastPackage.cancel();
|
||||||
dc.sendPackage(lastPackage);
|
sendPackage(lastPackage);
|
||||||
lastPackage = null;
|
lastPackage = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,7 +53,7 @@ public class PhoneCallReceiver implements BaseReceiver {
|
|||||||
if (lastState != TelephonyManager.CALL_STATE_IDLE && lastPackage != null) {
|
if (lastState != TelephonyManager.CALL_STATE_IDLE && lastPackage != null) {
|
||||||
|
|
||||||
lastPackage.cancel();
|
lastPackage.cancel();
|
||||||
dc.sendPackage(lastPackage);
|
sendPackage(lastPackage);
|
||||||
|
|
||||||
if (lastState == TelephonyManager.CALL_STATE_RINGING) {
|
if (lastState == TelephonyManager.CALL_STATE_RINGING) {
|
||||||
|
|
||||||
@ -64,7 +64,7 @@ public class PhoneCallReceiver implements BaseReceiver {
|
|||||||
lastPackage.setType(NetworkPackage.Type.MISSED);
|
lastPackage.setType(NetworkPackage.Type.MISSED);
|
||||||
lastPackage.setBody(incomingNumber);
|
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"
|
tools:context=".MainActivity"
|
||||||
android:orientation="vertical">
|
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>
|
</LinearLayout>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user