Merge pull request #135 from status-im/feature/service

Feature/service
This commit is contained in:
Jarrad 2016-06-17 13:59:25 +02:00 committed by GitHub
commit 7459b63251
3 changed files with 220 additions and 30 deletions

View File

@ -22,6 +22,11 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".GethService"
android:enabled="true"
android:exported="true"
android:process=":geth_process"/>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>

View File

@ -0,0 +1,136 @@
package com.statusim;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.AsyncTask;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.support.annotation.Nullable;
import android.util.Log;
import android.os.Environment;
import java.lang.ref.WeakReference;
import com.github.ethereum.go_ethereum.cmd.Geth;
import java.io.File;
public class GethService extends Service {
private static final String TAG = "GethService";
private static boolean isGethStarted = false;
private static boolean isGethInitialized = false;
private final Handler handler = new Handler();
static class IncomingHandler extends Handler {
private final WeakReference<GethService> service;
IncomingHandler(GethService service) {
this.service = new WeakReference<GethService>(service);
}
@Override
public void handleMessage(Message message) {
GethService service = this.service.get();
if (service != null) {
if (!service.handleMessage(message)) {
super.handleMessage(message);
}
}
}
}
final Messenger serviceMessenger = new Messenger(new IncomingHandler(this));
protected class StartTask extends AsyncTask<Void, Void, Void> {
public StartTask() {
}
protected Void doInBackground(Void... args) {
startGeth();
return null;
}
protected void onPostExecute(Void results) {
onGethStarted();
}
}
protected void onGethStarted() {
Log.d(TAG, "Geth Service started");
isGethStarted = true;
}
protected void startGeth() {
Log.d(TAG, "Starting background Geth Service");
File extStore = Environment.getExternalStorageDirectory();
final String dataFolder = extStore.exists() ?
extStore.getAbsolutePath() :
getApplicationInfo().dataDir;
final Runnable addPeer = new Runnable() {
public void run() {
Log.w("Geth", "adding peer");
Geth.run("--exec admin.addPeer(\"enode://e2f28126720452aa82f7d3083e49e6b3945502cb94d9750a15e27ee310eed6991618199f878e5fbc7dfa0e20f0af9554b41f491dc8f1dbae8f0f2d37a3a613aa@139.162.13.89:55555\") attach http://localhost:8545");
}
};
new Thread(new Runnable() {
public void run() {
Geth.run("--shh --ipcdisable --nodiscover --rpc --rpcapi db,eth,net,web3,shh,admin --fast --datadir=" + dataFolder);
}
}).start();
handler.postDelayed(addPeer, 5000);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return serviceMessenger.getBinder();
}
@Override
public void onCreate() {
super.onCreate();
System.loadLibrary("gethraw");
System.loadLibrary("geth");
if (!isGethInitialized) {
isGethInitialized = true;
new StartTask().execute();
}
}
@Override
public void onDestroy() {
super.onDestroy();
//TODO: stop geth
isGethStarted = false;
isGethInitialized = false;
Log.d(TAG, "Geth Service stopped !");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
protected boolean handleMessage(Message message) {
return false;
}
public static boolean isRunning() {
return isGethInitialized;
}
}

View File

@ -7,17 +7,24 @@ import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.rt2zz.reactnativecontacts.ReactNativeContacts;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnCancelListener;
import com.github.ethereum.go_ethereum.cmd.Geth;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.content.Intent;
import android.content.Context;
import com.bitgo.randombytes.RandomBytesPackage;
import com.BV.LinearGradient.LinearGradientPackage;
import com.centaurwarchief.smslistener.SmsListener;
import android.os.Handler;
import android.util.Log;
import java.util.Arrays;
@ -32,45 +39,69 @@ import io.realm.react.RealmReactPackage;
public class MainActivity extends ReactActivity {
final Handler handler = new Handler();
private static final String TAG = "MainActivity";
/**
* Incoming message handler. Calls to its binder are sequential!
*/
protected final IncomingHandler handler = new IncomingHandler();
/** Flag indicating if the service is bound. */
protected boolean isBound;
/** Sends messages to the service. */
protected Messenger serviceMessenger = null;
/** Receives messages from the service. */
protected Messenger clientMessenger = new Messenger(handler);
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message message) {
boolean isClaimed = false;
Log.d(TAG, "!!!!!!!!!!!!!! Received Service Message !!!!!!!!!!!!!!");
super.handleMessage(message);
}
}
protected ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been
// established, giving us the object we can use to
// interact with the service. We are communicating with the
// service using a Messenger, so here we get a client-side
// representation of that from the raw IBinder object.
serviceMessenger = new Messenger(service);
isBound = true;
onConnected();
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
serviceMessenger = null;
isBound = false;
Log.d(TAG, "!!!!!!!!!!!!!! Geth Service Disconnected !!!!!!!!!!!!!!");
}
};
protected void onConnected() {
Log.d(TAG, "!!!!!!!!!!!!!! Geth Service Connected !!!!!!!!!!!!!!");
}
protected void startStatus() {
// Required because of crazy APN settings redirecting localhost (found in GB)
Properties properties = System.getProperties();
properties.setProperty("http.nonProxyHosts", "localhost|127.0.0.1");
properties.setProperty("https.nonProxyHosts", "localhost|127.0.0.1");
File extStore = Environment.getExternalStorageDirectory();
final String dataFolder = extStore.exists() ?
extStore.getAbsolutePath() :
getApplicationInfo().dataDir;
// Launch!
final Runnable addPeer = new Runnable() {
public void run() {
Log.w("Geth", "adding peer");
Geth.run("--exec admin.addPeer(\"enode://e2f28126720452aa82f7d3083e49e6b3945502cb94d9750a15e27ee310eed6991618199f878e5fbc7dfa0e20f0af9554b41f491dc8f1dbae8f0f2d37a3a613aa@139.162.13.89:55555\") attach http://localhost:8545");
}
};
new Thread(new Runnable() {
public void run() {
Geth.run("--shh --ipcdisable --nodiscover --rpc --rpcapi db,eth,net,web3,shh,admin --fast --datadir=" + dataFolder);
}
}).start();
handler.postDelayed(addPeer, 5000);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Required for android-16 (???)
// Crash if put in startStatus() ?
System.loadLibrary("gethraw");
System.loadLibrary("geth");
if(!RootUtil.isDeviceRooted()) {
startStatus();
} else {
@ -96,9 +127,27 @@ public class MainActivity extends ReactActivity {
}).create();
dialog.show();
}
Intent intent = new Intent(this, GethService.class);
if (!GethService.isRunning()) {
startService(intent);
}
if (serviceConnection != null && GethService.isRunning()) {
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
unbindService(serviceConnection);
}
catch (Throwable t) {
Log.e(TAG, "Failed to unbind from the geth service", t);
}
}
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.