Skip to content

Commit

Permalink
Merge pull request #1 from Malinskiy/feature/identify_disconnected
Browse files Browse the repository at this point in the history
Feature/identify disconnected
  • Loading branch information
thinkhy authored Feb 25, 2018
2 parents 4e3270b + 490b9c1 commit e5e91fe
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jp.co.cyberagent.stf">
xmlns:tools="http://schemas.android.com/tools"
package="jp.co.cyberagent.stf">

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Expand All @@ -11,6 +12,8 @@
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.DUMP"
tools:ignore="ProtectedPermissions" />

<application android:allowBackup="true"
android:label="@string/app_name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ private void ensureVisibility() {

window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

unlock();

Expand Down
75 changes: 75 additions & 0 deletions app/src/main/java/jp/co/cyberagent/stf/Service.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package jp.co.cyberagent.stf;

import android.Manifest;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.os.IBinder;
import android.os.Process;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;

import com.google.protobuf.InvalidProtocolBufferException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -156,6 +161,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
addMonitor(new BrowserPackageMonitor(this, writers));

executor.submit(new Server(acceptor));
executor.submit(new AdbMonitor());

started = true;
}
Expand Down Expand Up @@ -367,4 +373,73 @@ public void run() {
}
}
}

/**
* Monitors the adb state by checking /sys/class/android_usb/android0/state
* <p>
* If state is DISCONNECTED then IdentityActivity will be shown to allow easier identification
* of malfunctioning devices
*/
private class AdbMonitor extends Thread {
// If something goes wrong you have 30 seconds to kill the STFService
// Using smaller numbers will basically lock the devices until usb connection is established
private static final int INTERVAL_MS = 30000;

@Override
public void run() {
Log.d(TAG, "Starting adb monitor thread");
String state = "";
try {
while (!isInterrupted()) {
/**
* In order for the monitor to work you need to grant DUMP permission for
* the apk, e.g.
*
* adb -d shell pm grant jp.co.cyberagent.stf android.permission.DUMP
*/
int dumpPermission = ContextCompat.checkSelfPermission(getApplication(), Manifest.permission.DUMP);
if (dumpPermission == PackageManager.PERMISSION_GRANTED) {
String[] cmd = {
"/system/bin/dumpsys",
"usb"
};

java.lang.Process process = Runtime.getRuntime().exec(cmd);

/**
* If the output of the command will change then by default device will be
* considered connected
*/
String currentState = "";
BufferedReader adbdStateReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
for (String line = adbdStateReader.readLine(); line != null; line = adbdStateReader.readLine()) {
if (line.contains("Kernel state:")) {
currentState = line.split(":").length == 2 ? line.split(":")[1] : "";
break;
}
}

if (!currentState.equals(state)) {
Log.d(TAG, "Kernel state changed to" + currentState);
state = currentState;
}

boolean disconnected = state.contains("DISCONNECTED");
if (disconnected) {
startActivity(new Intent(getApplication(), IdentityActivity.class));
}

adbdStateReader.close();
process.waitFor();
}

Thread.sleep(INTERVAL_MS);
}
} catch (IOException e) {
Log.e(TAG, "IO error during exec of adb monitor", e);
} catch (InterruptedException e) {
Log.i(TAG, "Adb monitor thread interrupted");
}
}
}
}

0 comments on commit e5e91fe

Please sign in to comment.