homescreenhandler: keep track of active apps
[apps/homescreen.git] / homescreen / src / homescreenhandler.cpp
index 9111ebb..5ed1fab 100644 (file)
@@ -24,6 +24,9 @@
 
 #define APPLAUNCH_DBUS_IFACE     "org.automotivelinux.AppLaunch"
 #define APPLAUNCH_DBUS_OBJECT    "/org/automotivelinux/AppLaunch"
+/* LAUNCHER_APP_ID shouldn't be started by applaunchd as it is started as a
+ * user session by systemd */
+#define LAUNCHER_APP_ID          "launcher"
 
 void* HomescreenHandler::myThis = 0;
 
@@ -63,19 +66,47 @@ getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
 
 void HomescreenHandler::tapShortcut(QString application_id)
 {
+    QDBusPendingReply<> reply;
     HMI_DEBUG("HomeScreen","tapShortcut %s", application_id.toStdString().c_str());
 
-    QDBusPendingReply<> reply = applaunch_iface->start(application_id);
+    if (application_id == LAUNCHER_APP_ID)
+        goto activate_app;
+
+    reply = applaunch_iface->start(application_id);
     reply.waitForFinished();
+
     if (reply.isError()) {
         HMI_ERROR("HomeScreen","Unable to start application '%s': %s",
             application_id.toStdString().c_str(),
             reply.error().message().toStdString().c_str());
+        return;
+    }
+
+activate_app:
+    if (mp_launcher) {
+        mp_launcher->setCurrent(application_id);
+    }
+    appStarted(application_id);
+}
+
+/*
+ * Keep track of currently running apps and the order in which
+ * they were activated. That way, when an app is closed, we can
+ * switch back to the previously active one.
+ */
+void HomescreenHandler::addAppToStack(const QString& application_id)
+{
+    if (application_id == "homescreen")
+        return;
+
+    if (!apps_stack.contains(application_id)) {
+        apps_stack << application_id;
     } else {
-        if (mp_launcher) {
-            mp_launcher->setCurrent(application_id);
-        }
-        appStarted(application_id);
+        int current_pos = apps_stack.indexOf(application_id);
+        int last_pos = apps_stack.size() - 1;
+
+        if (current_pos != last_pos)
+            apps_stack.move(current_pos, last_pos);
     }
 }
 
@@ -87,10 +118,14 @@ void HomescreenHandler::appStarted(const QString& application_id)
 
     HMI_DEBUG("HomeScreen", "Activating application %s", application_id.toStdString().c_str());
     agl_shell_activate_app(agl_shell, application_id.toStdString().c_str(), output);
+    addAppToStack(application_id);
 }
 
 void HomescreenHandler::appTerminated(const QString& application_id)
 {
-    HMI_DEBUG("HomeScreen", "Application %s terminated, activating launcher", application_id.toStdString().c_str());
-    appStarted("launcher");
+    HMI_DEBUG("HomeScreen", "Application %s terminated, activating last app", application_id.toStdString().c_str());
+    if (apps_stack.contains(application_id)) {
+        apps_stack.removeOne(application_id);
+        appStarted(apps_stack.last());
+    }
 }