1 package net.sf.openrocket.android.simservice;
\r
3 import java.util.List;
\r
5 import net.sf.openrocket.R;
\r
6 import net.sf.openrocket.android.CurrentRocketHolder;
\r
7 import net.sf.openrocket.android.util.AndroidLogWrapper;
\r
8 import net.sf.openrocket.document.Simulation;
\r
9 import net.sf.openrocket.simulation.customexpression.CustomExpression;
\r
10 import net.sf.openrocket.simulation.customexpression.CustomExpressionSimulationListener;
\r
11 import net.sf.openrocket.simulation.exception.SimulationException;
\r
12 import net.sf.openrocket.simulation.listeners.SimulationListener;
\r
13 import android.app.IntentService;
\r
14 import android.app.Notification;
\r
15 import android.app.PendingIntent;
\r
16 import android.content.Context;
\r
17 import android.content.Intent;
\r
18 import android.os.IBinder;
\r
19 import android.widget.Toast;
\r
21 public class SimulationService extends IntentService {
\r
23 // We use an id (from a dummy string) as the notificationID because it is unique.
\r
24 private final static int notificationID = R.string.SimulationServiceNotificationID;
\r
26 private Notification notification;
\r
28 public static void executeSimulationTask( Context c, SimulationTask t ) {
\r
29 AndroidLogWrapper.d(SimulationService.class, "Submitting simulation " + t.simulationId );
\r
31 CurrentRocketHolder.getCurrentRocket().lockSimulation( c, t.simulationId );
\r
33 Intent intent = new Intent( c, SimulationService.class );
\r
34 intent.putExtra("net.sf.openrocket.simulationtask", t);
\r
35 c.startService(intent);
\r
38 public SimulationService() {
\r
39 super("OpenRocket Simulation Execution Service");
\r
43 protected void onHandleIntent(Intent intent) {
\r
44 SimulationTask t = (SimulationTask) intent.getSerializableExtra("net.sf.openrocket.simulationtask");
\r
46 Simulation sim = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getSimulation(t.simulationId);
\r
48 List<CustomExpression> exprs = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getCustomExpressions();
\r
49 SimulationListener exprListener = new CustomExpressionSimulationListener(exprs);
\r
51 AndroidLogWrapper.d(SimulationService.class, "simulating " + t.simulationId );
\r
52 sim.simulate(exprListener);
\r
53 CurrentRocketHolder.getCurrentRocket().unlockSimulation(this, t.simulationId);
\r
55 catch (SimulationException simex) {
\r
56 Toast.makeText(this, "Error in simulation:" + simex.getMessage(), Toast.LENGTH_LONG ).show();
\r
61 public IBinder onBind(Intent intent) {
\r
66 public void onCreate() {
\r
69 this.notification = buildNotification();
\r
70 startForeground(notificationID, notification);
\r
75 public int onStartCommand(Intent intent, int flags, int startId) {
\r
76 super.onStartCommand(intent, flags, startId);
\r
77 return START_STICKY;
\r
82 public void onDestroy() {
\r
84 stopForeground(true);
\r
87 private Notification buildNotification( ) {
\r
88 String message = "OpenRocket Simulation Execution";
\r
89 Notification notification = new Notification(R.drawable.or_launcher, message, System.currentTimeMillis());
\r
91 notification.flags = Notification.FLAG_NO_CLEAR;
\r
92 PendingIntent contentIntent = PendingIntent.getActivity( this, 0 , new Intent( ), PendingIntent.FLAG_UPDATE_CURRENT );
\r
93 notification.setLatestEventInfo(this, "OpenRocket", message, contentIntent);
\r
94 return notification;
\r