1 package net.sf.openrocket.android.simservice;
\r
3 import net.sf.openrocket.R;
\r
4 import net.sf.openrocket.android.CurrentRocketHolder;
\r
5 import net.sf.openrocket.android.util.AndroidLogWrapper;
\r
6 import net.sf.openrocket.document.Simulation;
\r
7 import net.sf.openrocket.simulation.exception.SimulationException;
\r
8 import android.app.IntentService;
\r
9 import android.app.Notification;
\r
10 import android.app.PendingIntent;
\r
11 import android.content.Context;
\r
12 import android.content.Intent;
\r
13 import android.os.IBinder;
\r
14 import android.widget.Toast;
\r
16 public class SimulationService extends IntentService {
\r
18 // We use an id (from a dummy string) as the notificationID because it is unique.
\r
19 private final static int notificationID = R.string.SimulationServiceNotificationID;
\r
21 private Notification notification;
\r
23 public static void executeSimulationTask( Context c, SimulationTask t ) {
\r
24 AndroidLogWrapper.d(SimulationService.class, "Submitting simulation " + t.simulationId );
\r
26 Intent intent = new Intent( c, SimulationService.class );
\r
27 intent.putExtra("net.sf.openrocket.simulationtask", t);
\r
28 c.startService(intent);
\r
31 public SimulationService() {
\r
32 super("OpenRocket Simulation Execution Service");
\r
36 protected void onHandleIntent(Intent intent) {
\r
37 SimulationTask t = (SimulationTask) intent.getSerializableExtra("net.sf.openrocket.simulationtask");
\r
39 Simulation sim = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getSimulation(t.simulationId);
\r
40 AndroidLogWrapper.d(SimulationService.class, "simulating " + t.simulationId );
\r
42 CurrentRocketHolder.getCurrentRocket().unlockSimulation(t.simulationId);
\r
43 CurrentRocketHolder.getCurrentRocket().notifySimsChanged();
\r
45 catch (SimulationException simex) {
\r
46 Toast.makeText(this, "Error in simulation:" + simex.getMessage(), Toast.LENGTH_LONG ).show();
\r
51 public IBinder onBind(Intent intent) {
\r
56 public void onCreate() {
\r
59 this.notification = buildNotification();
\r
60 startForeground(notificationID, notification);
\r
65 public int onStartCommand(Intent intent, int flags, int startId) {
\r
66 super.onStartCommand(intent, flags, startId);
\r
67 return START_STICKY;
\r
72 public void onDestroy() {
\r
74 stopForeground(true);
\r
77 private Notification buildNotification( ) {
\r
78 String message = "OpenRocket Simulation Execution";
\r
79 Notification notification = new Notification(R.drawable.or_launcher, message, System.currentTimeMillis());
\r
81 notification.flags = Notification.FLAG_NO_CLEAR;
\r
82 PendingIntent contentIntent = PendingIntent.getActivity( this, 0 , new Intent( ), PendingIntent.FLAG_UPDATE_CURRENT );
\r
83 notification.setLatestEventInfo(this, "OpenRocket", message, contentIntent);
\r
84 return notification;
\r