Skip to content

Commit

Permalink
Touch event streaming with nearby (#56)
Browse files Browse the repository at this point in the history
* Deleting demos and p2p code

Removal of old code for wifi p2p as well as activities used for older demos.

* Adapting existing code for deleted files

* Fixing whitespace

* Refactoring nearbyservice to singleton

* Initial binding of eventwriter to a payload stream

* Run ADB during start as persistent process

* Working event sending

* Fixing some tests

* Fixing linting errors and formatting

* Reordering gradle build

* Push async task into Thread

* Adding empty test scaffolds and adding powermock as dependency

* Letting empty tests fail

* Properly mocked tests for serverconnection:
  • Loading branch information
xiamaz authored Jan 9, 2019
1 parent f9c7ef3 commit 112362b
Show file tree
Hide file tree
Showing 41 changed files with 931 additions and 1,752 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
</intent-filter>
</activity>

<service android:name=".summoner.ADBService" />
<service android:name=".summoner.ADBService" android:enabled="true" android:exported="true"/>
<service android:name=".nearbyservice.NearbyService" android:enabled="true" android:exported="true"/>
<activity android:name=".ConnectionSetupActivity" />
<activity android:name=".ShowCodeConnAuth">
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/amos/flyinn/ADBActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected void onCreate(Bundle savedInstanceState) {
try {
Point p = new Point();
getWindowManager().getDefaultDisplay().getRealSize(p);
Demo.start(getApplicationContext(), "127.0.0.1", p);
Demo.start(getApplicationContext(), p);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,12 @@ private void restartAPP() {
/**
* This method is to create an ADB service
*
* @param addr the address where the ADB service is going to listen for connections
* @return returns Daemon service to work with the ADB service
*/
protected Daemon createADBService(String addr) {
protected Daemon createADBService() {
Point p = new Point();
getWindowManager().getDefaultDisplay().getRealSize(p);
Daemon d = new Daemon(getApplicationContext(), addr, p);
Daemon d = new Daemon(getApplicationContext(), p);
try {
d.writeFakeInputToFilesystem();
d.spawn_adb();
Expand Down
15 changes: 3 additions & 12 deletions app/src/main/java/com/amos/flyinn/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.amos.flyinn;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.StrictMode;
import android.provider.Settings;
import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
Expand Down Expand Up @@ -117,10 +113,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
System.loadLibrary("native-lib");
}

protected Daemon createADBService(String addr) {
protected Daemon createADBService() {
Point p = new Point();
getWindowManager().getDefaultDisplay().getRealSize(p);
Daemon d = new Daemon(getApplicationContext(), addr, p);
Daemon d = new Daemon(getApplicationContext(), p);
try {
d.writeFakeInputToFilesystem();
d.spawn_adb();
Expand All @@ -146,13 +142,11 @@ public void onClick(View view) {

// Example of a call to a native method
connectionStatus = findViewById(R.id.connectionStatus);
String addr;
try {
addr = "127.0.0.1";
while (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
}
adbDaemon = createADBService(addr);
adbDaemon = createADBService();

} catch (Exception e) {
}
Expand All @@ -169,9 +163,6 @@ public void onClick(View view) {
public native String stringFromJNI();





private void checkForDebuggingMode() {
if (Settings.Secure.getInt(this.getContentResolver(), Settings.Global.ADB_ENABLED, 0) != 1) {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
Expand Down
33 changes: 29 additions & 4 deletions app/src/main/java/com/amos/flyinn/ShowCodeActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.amos.flyinn;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.CallSuper;
Expand All @@ -13,6 +15,7 @@
import android.widget.Toast;

import com.amos.flyinn.nearbyservice.NearbyService;
import com.amos.flyinn.summoner.Daemon;

import java.util.concurrent.ThreadLocalRandom;

Expand Down Expand Up @@ -47,18 +50,40 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_show_code);
display = findViewById(R.id.textView2);

if (!hasPermissions(NearbyService.getRequiredPermissions()) &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!hasPermissions(NearbyService.getRequiredPermissions()) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(NearbyService.getRequiredPermissions(), REQUEST_CODE_REQUIRED_PERMISSIONS);
} else {
Log.w(TAG, "Could not check permissions due to version");
}

String[] perms = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
if (!hasPermissions(perms) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(perms, REQUEST_CODE_REQUIRED_PERMISSIONS);
}

try {
createADBService();
} catch (Exception e) {
Log.d("ShowCodeActivity", "Failed to start ADB service");
e.printStackTrace();
}
nameNum = String.valueOf(ThreadLocalRandom.current().nextInt(1000, 9998 + 1));
display.setText(nameNum);
setService();
}

protected Daemon createADBService() throws Exception {
Point p = new Point();
getWindowManager().getDefaultDisplay().getRealSize(p);
Daemon d = new Daemon(getApplicationContext(), p);
d.writeFakeInputToFilesystem();
Log.d("ShowCodeActivity", "Wrote to FS");
Log.d("ShowCodeActivity", "Going to spawn ADB service");
d.spawn_adb();
Log.d("ShowCodeActivity", "Spawned ADB service");
return d;
}

@Override
public void onResume() {
super.onResume();
Expand All @@ -75,8 +100,8 @@ protected void onDestroy() {
/**
* Handles user acceptance (or denial) of our permission request.
*
* @param requestCode The request code passed in requestPermissions()
* @param permissions Permissions that must be granted to run nearby connections
* @param requestCode The request code passed in requestPermissions()
* @param permissions Permissions that must be granted to run nearby connections
* @param grantResults Results of granting permissions
*/
@CallSuper
Expand Down
44 changes: 26 additions & 18 deletions app/src/main/java/com/amos/flyinn/nearbyservice/NearbyService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.amos.flyinn.nearbyservice;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
Expand All @@ -11,21 +9,19 @@
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.util.Log;


import com.amos.flyinn.ConnectionSetupActivity;
import com.amos.flyinn.R;
import com.amos.flyinn.ShowCodeActivity;
import com.amos.flyinn.summoner.ADBService;
import com.amos.flyinn.summoner.ConnectionSigleton;
import com.google.android.gms.nearby.connection.Payload;
import com.google.android.gms.nearby.connection.PayloadTransferUpdate;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ThreadLocalRandom;
import java.util.Objects;

/**
* Manage nearby connections with a server.
Expand All @@ -35,7 +31,6 @@
* initiate a connection over nearby.
*/
public class NearbyService extends IntentService {

public static final String TAG = NearbyService.class.getPackage().getName();

public static final String ACTION_START = "nearby_start";
Expand Down Expand Up @@ -72,19 +67,19 @@ public int onStartCommand(@Nullable Intent intent, int flags, int startId) {

/**
* Create a notification channel.
*
* <p>
* Notifications are organized into different channels to theoretically enable the user to
* individually set what they want to be informed about.
*
* <p>
* This is required since Android 8.
*/
private void createChannel() {
NotificationManager mgr=
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O &&
mgr.getNotificationChannel(CHANNEL_ID)==null) {
NotificationManager mgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
mgr.getNotificationChannel(CHANNEL_ID) == null) {

NotificationChannel c=new NotificationChannel(CHANNEL_ID,
NotificationChannel c = new NotificationChannel(CHANNEL_ID,
"flyinn_channel", NotificationManager.IMPORTANCE_HIGH);

c.enableLights(true);
Expand All @@ -96,8 +91,9 @@ private void createChannel() {

/**
* Create a sticky notification that won't go away.
*
* @param message String message shown in the notification.
* @param target Optional target intent to switch to after tapping the notification.
* @param target Optional target intent to switch to after tapping the notification.
* @return
*/
private Notification buildForegroundNotification(String message, @Nullable Intent target) {
Expand Down Expand Up @@ -127,6 +123,7 @@ private void raiseNotification(Notification notification) {

/**
* Create or update shown notification.
*
* @param message
*/
public void notify(String message) {
Expand Down Expand Up @@ -154,6 +151,7 @@ public void notify(String message) {

/**
* Activate the given activity
*
* @param cls Class of the target activity
*/
private void switchActivity(Class<?> cls) {
Expand All @@ -168,13 +166,14 @@ public NearbyState getServiceState() {

/**
* Set state of the nearby service. This is used by the nearby server.
*
* @param state
* @param message
*/
public void setServiceState(NearbyState state, @Nullable String message) {
// do extra things if we are switching state
if (serviceState != state) {
switch(serviceState) {
switch (serviceState) {
case CONNECTING:
switchActivity(ConnectionSetupActivity.class);
break;
Expand Down Expand Up @@ -209,6 +208,15 @@ public void handleResponse(boolean error, String message) {

public void handlePayload(Payload payload) {
Log.d(TAG, "Received payload");
if (payload.asStream() == null) {
Log.wtf("AAAAAAAAAAAAAAAAAAA", "Payload is null!");
return;
}
Intent i = new Intent(this, ADBService.class);
i.setAction("stream");
ConnectionSigleton.getInstance().inputStream = Objects.requireNonNull(payload.asStream()).asInputStream();
startService(i);
Log.d(TAG, "Send payload to activity");
}

public void handlePayloadTransferUpdate(PayloadTransferUpdate update) {
Expand Down
Loading

0 comments on commit 112362b

Please sign in to comment.