[Tizen][AT-SPI] do not keep window in ApplicationAccessible
[platform/core/uifw/dali-adaptor.git] / dali / internal / accessibility / bridge / bridge-impl.cpp
index a4144ab..27525b4 100644 (file)
 
 using namespace Dali::Accessibility;
 
+namespace // unnamed namespace
+{
+
+const int RETRY_INTERVAL = 1000;
+
+} // unnamed namespace
+
 /**
  * @brief The BridgeImpl class is to implement some Bridge functions.
  */
@@ -68,6 +75,9 @@ class BridgeImpl : public virtual BridgeBase,
   Dali::Actor                                                   mHighlightedActor;
   std::function<void(Dali::Actor)>                              mHighlightClearAction;
   Dali::CallbackBase*                                           mIdleCallback          = NULL;
+  Dali::Timer                                                   mInitializeTimer;
+  Dali::Timer                                                   mReadIsEnabledTimer;
+  Dali::Timer                                                   mReadScreenReaderEnabledTimer;
 
 public:
   BridgeImpl()
@@ -208,6 +218,8 @@ public:
       }
       mData->mCurrentlyHighlightedActor = {};
       mData->mHighlightActor            = {};
+
+      mDisabledSignal.Emit();
     }
     mHighlightedActor     = {};
     mHighlightClearAction = {};
@@ -215,6 +227,25 @@ public:
     mRegistryClient       = {};
     mDirectReadingClient  = {};
     mDirectReadingCallbacks.clear();
+    mApplication.mChildren.clear();
+  }
+
+  void StopTimer()
+  {
+    if(mInitializeTimer)
+    {
+      mInitializeTimer.Stop();
+    }
+
+    if(mReadIsEnabledTimer)
+    {
+      mReadIsEnabledTimer.Stop();
+    }
+
+    if(mReadScreenReaderEnabledTimer)
+    {
+      mReadScreenReaderEnabledTimer.Stop();
+    }
   }
 
   /**
@@ -228,6 +259,7 @@ public:
       mData->mHighlightActor            = {};
     }
     ForceDown();
+    StopTimer();
     if((NULL != mIdleCallback) && Dali::Adaptor::IsAvailable())
     {
       Dali::Adaptor::Get().RemoveIdle(mIdleCallback);
@@ -285,91 +317,156 @@ public:
     assert(res);
 
     mApplication.mParent.SetAddress(std::move(std::get<0>(res)));
-    if(mIsShown)
+
+    mEnabledSignal.Emit();
+
+    return ForceUpResult::JUST_STARTED;
+  }
+
+  /**
+   * @brief Sends a signal to dbus that the window is shown.
+   *
+   * @param[in] window The window to be shown
+   * @see Accessible::EmitShowing() and BridgeObject::EmitStateChanged()
+   */
+  void EmitShown(Dali::Window window)
+  {
+    auto windowAccessible = mApplication.GetWindowAccessible(window);
+    if(windowAccessible)
     {
-      EmitActivate();
+      windowAccessible->EmitShowing(true);
     }
-    return ForceUpResult::JUST_STARTED;
   }
 
   /**
-   * @brief Sends a signal to dbus that the default window is activated.
+   * @brief Sends a signal to dbus that the window is hidden.
    *
-   * TODO : This is subject to change if/when we implement multi-window support.
-   * @see BridgeObject::Emit()
+   * @param[in] window The window to be hidden
+   * @see Accessible::EmitShowing() and BridgeObject::EmitStateChanged()
    */
-  void EmitActivate()
+  void EmitHidden(Dali::Window window)
   {
-    auto win = mApplication.GetActiveWindow();
-    if(win)
+    auto windowAccessible = mApplication.GetWindowAccessible(window);
+    if(windowAccessible)
     {
-      win->Emit(WindowEvent::ACTIVATE, 0);
+      windowAccessible->EmitShowing(false);
     }
   }
 
   /**
-   * @brief Sends a signal to dbus that the default window is deactivated.
+   * @brief Sends a signal to dbus that the window is activated.
    *
-   * TODO : This is subject to change if/when we implement multi-window support.
+   * @param[in] window The window to be activated
    * @see BridgeObject::Emit()
    */
-  void EmitDeactivate()
+  void EmitActivate(Dali::Window window)
   {
-    auto win = mApplication.GetActiveWindow();
-    if(win)
+    auto windowAccessible = mApplication.GetWindowAccessible(window);
+    if(windowAccessible)
     {
-      win->Emit(WindowEvent::DEACTIVATE, 0);
+      windowAccessible->Emit(WindowEvent::ACTIVATE, 0);
     }
   }
 
   /**
-   * @copydoc Dali::Accessibility::Bridge::WindowHidden()
+   * @brief Sends a signal to dbus that the window is deactivated.
+   *
+   * @param[in] window The window to be deactivated
+   * @see BridgeObject::Emit()
    */
-  void WindowHidden() override
+  void EmitDeactivate(Dali::Window window)
   {
-    if(mIsShown && IsUp())
+    auto windowAccessible = mApplication.GetWindowAccessible(window);
+    if(windowAccessible)
     {
-      EmitDeactivate();
+      windowAccessible->Emit(WindowEvent::DEACTIVATE, 0);
     }
-    mIsShown = false;
   }
 
   /**
    * @copydoc Dali::Accessibility::Bridge::WindowShown()
    */
-  void WindowShown() override
+  void WindowShown(Dali::Window window) override
   {
     if(!mIsShown && IsUp())
     {
-      EmitActivate();
+      EmitShown(window);
     }
     mIsShown = true;
   }
 
-  void ReadAndListenProperty()
+  /**
+   * @copydoc Dali::Accessibility::Bridge::WindowHidden()
+   */
+  void WindowHidden(Dali::Window window) override
   {
-    // read property
-    auto enabled = mAccessibilityStatusClient.property<bool>("ScreenReaderEnabled").get();
-    if(enabled)
+    if(mIsShown && IsUp())
     {
-      mIsScreenReaderEnabled = std::get<0>(enabled);
+      EmitHidden(window);
     }
+    mIsShown = false;
+  }
 
-    enabled = mAccessibilityStatusClient.property<bool>("IsEnabled").get();
-    if(enabled)
+  /**
+   * @copydoc Dali::Accessibility::Bridge::WindowFocused()
+   */
+  void WindowFocused(Dali::Window window) override
+  {
+    if(mIsShown && IsUp())
     {
-      mIsEnabled = std::get<0>(enabled);
+      EmitActivate(window);
     }
+  }
 
-    if(mIsScreenReaderEnabled || mIsEnabled)
+  /**
+   * @copydoc Dali::Accessibility::Bridge::WindowUnfocused()
+   */
+  void WindowUnfocused(Dali::Window window) override
+  {
+    if(mIsShown && IsUp())
     {
-      ForceUp();
+      EmitDeactivate(window);
     }
+  }
 
-    // listen property change
-    mAccessibilityStatusClient.addPropertyChangedEvent<bool>("ScreenReaderEnabled", [this](bool res) {
-      mIsScreenReaderEnabled = res;
-      if(mIsScreenReaderEnabled || mIsEnabled)
+  /**
+   * @copydoc Dali::Accessibility::Bridge::SuppressScreenReader()
+   */
+  void SuppressScreenReader(bool suppress) override
+  {
+    if(mIsScreenReaderSuppressed == suppress)
+    {
+      return;
+    }
+    mIsScreenReaderSuppressed = suppress;
+    ReadScreenReaderEnabledProperty();
+  }
+
+  bool ReadIsEnabledTimerCallback()
+  {
+    ReadIsEnabledProperty();
+    return false;
+  }
+
+  void ReadIsEnabledProperty()
+  {
+    mAccessibilityStatusClient.property<bool>("IsEnabled").asyncGet([this](DBus::ValueOrError<bool> msg) {
+      if(!msg)
+      {
+        DALI_LOG_ERROR("Get IsEnabled property error: %s\n", msg.getError().message.c_str());
+        if(msg.getError().errorType == DBus::ErrorType::INVALID_REPLY)
+        {
+          if(!mReadIsEnabledTimer)
+          {
+            mReadIsEnabledTimer = Dali::Timer::New(RETRY_INTERVAL);
+            mReadIsEnabledTimer.TickSignal().Connect(this, &BridgeImpl::ReadIsEnabledTimerCallback);
+          }
+          mReadIsEnabledTimer.Start();
+        }
+        return;
+      }
+      mIsEnabled = std::get<0>(msg);
+      if((!mIsScreenReaderSuppressed && mIsScreenReaderEnabled) || mIsEnabled)
       {
         ForceUp();
       }
@@ -378,7 +475,10 @@ public:
         ForceDown();
       }
     });
+  }
 
+  void ListenIsEnabledProperty()
+  {
     mAccessibilityStatusClient.addPropertyChangedEvent<bool>("IsEnabled", [this](bool res) {
       mIsEnabled = res;
       if(mIsScreenReaderEnabled || mIsEnabled)
@@ -392,6 +492,71 @@ public:
     });
   }
 
+  bool ReadScreenReaderEnabledTimerCallback()
+  {
+    ReadScreenReaderEnabledProperty();
+    return false;
+  }
+
+  void ReadScreenReaderEnabledProperty()
+  {
+    // can be true because of SuppressScreenReader before init
+    if (!mAccessibilityStatusClient)
+    {
+      return;
+    }
+
+    mAccessibilityStatusClient.property<bool>("ScreenReaderEnabled").asyncGet([this](DBus::ValueOrError<bool> msg) {
+      if(!msg)
+      {
+        DALI_LOG_ERROR("Get ScreenReaderEnabled property error: %s\n", msg.getError().message.c_str());
+        if(msg.getError().errorType == DBus::ErrorType::INVALID_REPLY)
+        {
+          if(!mReadScreenReaderEnabledTimer)
+          {
+            mReadScreenReaderEnabledTimer = Dali::Timer::New(RETRY_INTERVAL);
+            mReadScreenReaderEnabledTimer.TickSignal().Connect(this, &BridgeImpl::ReadScreenReaderEnabledTimerCallback);
+          }
+          mReadScreenReaderEnabledTimer.Start();
+        }
+        return;
+      }
+      mIsScreenReaderEnabled = std::get<0>(msg);
+      if((!mIsScreenReaderSuppressed && mIsScreenReaderEnabled) || mIsEnabled)
+      {
+        ForceUp();
+      }
+      else
+      {
+        ForceDown();
+      }
+    });
+  }
+
+  void ListenScreenReaderEnabledProperty()
+  {
+    mAccessibilityStatusClient.addPropertyChangedEvent<bool>("ScreenReaderEnabled", [this](bool res) {
+      mIsScreenReaderEnabled = res;
+      if((!mIsScreenReaderSuppressed && mIsScreenReaderEnabled) || mIsEnabled)
+      {
+        ForceUp();
+      }
+      else
+      {
+        ForceDown();
+      }
+    });
+  }
+
+  void ReadAndListenProperties()
+  {
+    ReadIsEnabledProperty();
+    ListenIsEnabledProperty();
+
+    ReadScreenReaderEnabledProperty();
+    ListenScreenReaderEnabledProperty();
+  }
+
   bool InitializeAccessibilityStatusClient()
   {
     mAccessibilityStatusClient = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
@@ -405,16 +570,34 @@ public:
     return true;
   }
 
+  bool InitializeTimerCallback()
+  {
+    if ( InitializeAccessibilityStatusClient() )
+    {
+      ReadAndListenProperties();
+      return false;
+    }
+    return true;
+  }
+
   bool OnIdleSignal()
   {
     if ( InitializeAccessibilityStatusClient() )
     {
-      ReadAndListenProperty();
+      ReadAndListenProperties();
       mIdleCallback = NULL;
       return false;
     }
 
-    return true;
+    if(!mInitializeTimer)
+    {
+      mInitializeTimer = Dali::Timer::New(RETRY_INTERVAL);
+      mInitializeTimer.TickSignal().Connect(this, &BridgeImpl::InitializeTimerCallback);
+    }
+    mInitializeTimer.Start();
+
+    mIdleCallback = NULL;
+    return false;
   }
 
   /**
@@ -424,7 +607,7 @@ public:
   {
     if ( InitializeAccessibilityStatusClient() )
     {
-      ReadAndListenProperty();
+      ReadAndListenProperties();
       return;
     }
 
@@ -538,17 +721,19 @@ void Bridge::EnableAutoInit()
     return;
   }
 
-  auto rootLayer       = Dali::Stage::GetCurrent().GetRootLayer();
+  auto rootLayer       = Dali::Stage::GetCurrent().GetRootLayer(); // A root layer of the default window.
   auto window          = Dali::DevelWindow::Get(rootLayer);
   auto applicationName = Dali::Internal::Adaptor::Adaptor::GetApplicationPackageName();
 
+  auto accessible = Accessibility::Accessible::Get(rootLayer, true);
+
   auto* bridge = Bridge::GetCurrentBridge();
-  bridge->AddTopLevelWindow(Dali::Accessibility::Accessible::Get(rootLayer, true));
+  bridge->AddTopLevelWindow(accessible);
   bridge->SetApplicationName(applicationName);
   bridge->Initialize();
 
   if(window && window.IsVisible())
   {
-    bridge->WindowShown();
+    bridge->WindowShown(window);
   }
 }