Merge commit '42b2e5ca519766e37ce6941ba4faecc9691cc403' into upstream
[debian/openrocket] / android / src / net / sf / openrocket / android / simservice / SimulationService.java
diff --git a/android/src/net/sf/openrocket/android/simservice/SimulationService.java b/android/src/net/sf/openrocket/android/simservice/SimulationService.java
new file mode 100644 (file)
index 0000000..9a1dd49
--- /dev/null
@@ -0,0 +1,96 @@
+package net.sf.openrocket.android.simservice;\r
+\r
+import java.util.List;\r
+\r
+import net.sf.openrocket.R;\r
+import net.sf.openrocket.android.CurrentRocketHolder;\r
+import net.sf.openrocket.android.util.AndroidLogWrapper;\r
+import net.sf.openrocket.document.Simulation;\r
+import net.sf.openrocket.simulation.customexpression.CustomExpression;\r
+import net.sf.openrocket.simulation.customexpression.CustomExpressionSimulationListener;\r
+import net.sf.openrocket.simulation.exception.SimulationException;\r
+import net.sf.openrocket.simulation.listeners.SimulationListener;\r
+import android.app.IntentService;\r
+import android.app.Notification;\r
+import android.app.PendingIntent;\r
+import android.content.Context;\r
+import android.content.Intent;\r
+import android.os.IBinder;\r
+import android.widget.Toast;\r
+\r
+public class SimulationService extends IntentService {\r
+\r
+       // We use an id (from a dummy string) as the notificationID because it is unique.\r
+       private final static int notificationID = R.string.SimulationServiceNotificationID;\r
+       \r
+       private Notification notification;\r
+       \r
+       public static void executeSimulationTask( Context c, SimulationTask t ) {\r
+               AndroidLogWrapper.d(SimulationService.class, "Submitting simulation " + t.simulationId );\r
+\r
+               CurrentRocketHolder.getCurrentRocket().lockSimulation( c, t.simulationId );\r
+               \r
+               Intent intent = new Intent( c, SimulationService.class );\r
+               intent.putExtra("net.sf.openrocket.simulationtask", t);\r
+               c.startService(intent);\r
+       }\r
+       \r
+       public SimulationService() {\r
+               super("OpenRocket Simulation Execution Service");\r
+       }\r
+\r
+       @Override\r
+       protected void onHandleIntent(Intent intent) {\r
+               SimulationTask t = (SimulationTask) intent.getSerializableExtra("net.sf.openrocket.simulationtask");\r
+               try {\r
+                       Simulation sim = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getSimulation(t.simulationId);\r
+\r
+                       List<CustomExpression> exprs = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getCustomExpressions();\r
+                       SimulationListener exprListener = new CustomExpressionSimulationListener(exprs);\r
+\r
+                       AndroidLogWrapper.d(SimulationService.class, "simulating " + t.simulationId );\r
+                       sim.simulate(exprListener);\r
+                       CurrentRocketHolder.getCurrentRocket().unlockSimulation(this, t.simulationId);\r
+               }\r
+               catch (SimulationException simex) {\r
+                       Toast.makeText(this, "Error in simulation:" + simex.getMessage(), Toast.LENGTH_LONG ).show();\r
+               }\r
+       }\r
+\r
+       @Override\r
+       public IBinder onBind(Intent intent) {\r
+               return null;\r
+       }\r
+\r
+       @Override\r
+       public void onCreate() {\r
+               super.onCreate();\r
+               \r
+               this.notification = buildNotification();\r
+               startForeground(notificationID, notification);\r
+               \r
+       }\r
+\r
+       @Override\r
+       public int onStartCommand(Intent intent, int flags, int startId) {\r
+               super.onStartCommand(intent, flags, startId);\r
+               return START_STICKY;\r
+       }\r
+\r
+       \r
+       @Override\r
+       public void onDestroy() {\r
+               super.onDestroy();\r
+               stopForeground(true);\r
+       }\r
+\r
+       private Notification buildNotification( ) {\r
+               String message = "OpenRocket Simulation Execution";\r
+               Notification notification = new Notification(R.drawable.or_launcher, message, System.currentTimeMillis());\r
+               \r
+               notification.flags = Notification.FLAG_NO_CLEAR;\r
+               PendingIntent contentIntent = PendingIntent.getActivity( this, 0 , new Intent( ), PendingIntent.FLAG_UPDATE_CURRENT );\r
+               notification.setLatestEventInfo(this, "OpenRocket", message, contentIntent);\r
+               return notification;\r
+       }\r
+}\r