Skip to content

Commit

Permalink
DouniaBerrada: Binding Jetty server to Android main activity so the O…
Browse files Browse the repository at this point in the history
…S does not kill jetty when running low on resources when the application is on the foreground.

r11645
  • Loading branch information
Dounia Berrada committed Mar 9, 2011
1 parent 7887122 commit abcad81
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@
import com.google.common.collect.Iterables;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Window;
Expand All @@ -41,6 +46,7 @@
import org.openqa.selenium.android.intents.IntentReceiver.IntentReceiverListener;
import org.openqa.selenium.android.intents.IntentReceiverRegistrar;
import org.openqa.selenium.android.intents.IntentSender;
import org.openqa.selenium.android.server.JettyService;
import org.openqa.selenium.android.sessions.SessionCookieManager;

import java.io.ByteArrayOutputStream;
Expand All @@ -61,9 +67,19 @@ public class WebDriverActivity extends Activity implements IntentReceiverListene
private WebDriverWebView currentView;
private WebViewManager viewManager = new WebViewManager();
private final IntentReceiverRegistrar intentReg;


private final IntentSender sender = new IntentSender(this);
private boolean bound;

private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
bound = true;
}

public void onServiceDisconnected(ComponentName arg0) {
bound = false;
}
};

public void sendIntent(String action, Object... args) {
sender.broadcast(action, args);
Expand Down Expand Up @@ -119,6 +135,22 @@ protected void onCreate(Bundle savedInstanceState) {

initIntentReceivers();
}

@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, JettyService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onStop() {
super.onStop();
if (bound) {
unbindService(mConnection);
bound = false;
}
}

private void displayProgressBar() {
// Request the progress bar to be shown in the title and set it to 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;
Expand All @@ -51,37 +52,28 @@ public class JettyService extends Service {

private PowerManager.WakeLock wakeLock;

private final IBinder binder = new WebDriverBinder();

public class WebDriverBinder extends Binder {
public JettyService getService() {
return JettyService.this;
}
}

@Override
public IBinder onBind(Intent intent) {
return binder;
}

/**
* Android Service Start
*
* @see android.app.Service#onStart(android.content.Intent, int)
*/
@Override
public void onStart(Intent intent, int startId) {
if (server!= null && server.isRunning()) {
Toast.makeText(JettyService.this, R.string.jetty_already_started, Toast.LENGTH_SHORT).show();
return;
}

try {
// Get a wake lock to stop the cpu going to sleep
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "IJetty");
wakeLock.acquire();

AndroidDriver.setContext(this);

startJetty();

notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Toast.makeText(JettyService.this, R.string.jetty_started, Toast.LENGTH_SHORT).show();

Logger.log(Log.INFO, LOG_TAG, "Jetty started");
super.onStart(intent, startId);
} catch (Exception e) {
Logger.log(Log.ERROR, LOG_TAG, "Error starting jetty" + e);
Toast.makeText(this, getText(R.string.jetty_not_started), Toast.LENGTH_SHORT).show();
}
startServer();
super.onStart(intent, startId);
}


Expand Down Expand Up @@ -120,11 +112,6 @@ public void onLowMemory() {
super.onLowMemory();
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

/**
* Get a reference to the Jetty Server instance
*/
Expand Down Expand Up @@ -187,16 +174,39 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
}
}

protected void startJetty() throws Exception {
System.setProperty("org.mortbay.log.class", "org.mortbay.log.AndroidLog");
server = new Server();
public void startServer() {
if (server!= null && server.isRunning()) {
Toast.makeText(JettyService.this, R.string.jetty_already_started, Toast.LENGTH_SHORT).show();
return;
}

try {
// Get a wake lock to stop the cpu going to sleep
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "IJetty");
wakeLock.acquire();

AndroidDriver.setContext(this);

System.setProperty("org.mortbay.log.class", "org.mortbay.log.AndroidLog");
server = new Server();

configureConnectors();
configureHandlers();
configureConnectors();
configureHandlers();

server.start();
server.start();

HttpGenerator.setServerVersion("WebDriver jetty");
HttpGenerator.setServerVersion("WebDriver jetty");

notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Toast.makeText(JettyService.this, R.string.jetty_started, Toast.LENGTH_SHORT).show();

Logger.log(Log.INFO, LOG_TAG, "Jetty started");
} catch (Exception e) {
Logger.log(Log.ERROR, LOG_TAG, "Error starting jetty" + e);
Toast.makeText(this, getText(R.string.jetty_not_started), Toast.LENGTH_SHORT).show();
throw new RuntimeException("Jetty failed to start!");
}
}

protected void stopJetty() throws Exception {
Expand Down

0 comments on commit abcad81

Please sign in to comment.