Modified internal api for fault widget case
[platform/core/uifw/widget-viewer-dali.git] / internal / widget_view / widget_view_impl.cpp
index 5e1d5e7..071838d 100644 (file)
@@ -51,6 +51,8 @@ namespace Internal
 namespace
 {
 
+const int DEFAULT_FONT_PIXEL_SIZE = 30; // Referred platform widget viewer evas
+
 #define GET_LOCALE_TEXT(string) dgettext(PKGNAME, string)
 
 #if defined(DEBUG_ENABLED)
@@ -74,6 +76,16 @@ DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "loadingText", MAP, LO
 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "widgetStateFaulted", BOOLEAN, WIDGET_STATE_FAULTED )
 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "permanentDelete", BOOLEAN, PERMANENT_DELETE )
 
+// Internal property
+// ToDo it should be changed public property.
+const int TERMINATE_WIDGET = Dali::WidgetView::WidgetView::Property::PERMANENT_DELETE+1;
+const int RETRY_TEXT = TERMINATE_WIDGET+1;
+const int WIDGET_IMAGE = RETRY_TEXT+1;
+
+Dali::PropertyRegistration internalProperty1( typeRegistration, "terminateWidget", TERMINATE_WIDGET, Property::BOOLEAN, Dali::WidgetView::Internal::WidgetView::SetProperty, Dali::WidgetView::Internal::WidgetView::GetProperty );
+Dali::PropertyRegistration internalProperty2( typeRegistration, "retryText", RETRY_TEXT, Property::MAP, Dali::WidgetView::Internal::WidgetView::SetProperty, Dali::WidgetView::Internal::WidgetView::GetProperty );
+Dali::PropertyRegistration internalProperty3( typeRegistration, "widgetImage", WIDGET_IMAGE, Property::MAP, Dali::WidgetView::Internal::WidgetView::SetProperty, Dali::WidgetView::Internal::WidgetView::GetProperty );
+
 // Signals
 DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetAdded", SIGNAL_WIDGET_ADDED )
 DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetDeleted", SIGNAL_WIDGET_DELETED )
@@ -81,6 +93,7 @@ DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetCreationAborted",
 DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetContentUpdated", SIGNAL_WIDGET_CONTENT_UPDATED )
 DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetUpdatePeriodChanged", SIGNAL_WIDGET_UPDATE_PERIOD_CHANGED )
 DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetFaulted", SIGNAL_WIDGET_FAULTED )
+DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetTerminated", SIGNAL_WIDGET_TERMINATED )
 
 // Actions
 DALI_ACTION_REGISTRATION( Dali::WidgetView, WidgetView, "pauseWidget", ACTION_WIDGETVIEW_PAUSE_WIDGET );
@@ -92,9 +105,11 @@ DALI_TYPE_REGISTRATION_END()
 
 struct wl_buffer* preBuffer;
 
+const char* const STATE_TEXT( "stateText" );
 const char* const FONT_STYLE( "fontStyle" );
 const char* const TEXT_POINT_SIZE( "textPointSize" );
 const char* const TEXT_COLOR( "textColor" );
+const char* const TEXT_VISIBLE( "textVisible" ); //ToDo: it should be removed after retry text property is public one
 
 static void OnBufferUpdated( struct tizen_remote_surface *surface, uint32_t type, struct wl_buffer *buffer,
                               int32_t img_file_fd, uint32_t img_file_size, uint32_t time, struct wl_array *keys,
@@ -135,7 +150,7 @@ static void OnSurfaceRemoved( const char *appid, const char *instance_id, const
 {
   Dali::WidgetView::Internal::WidgetView* widgetView = static_cast< Dali::WidgetView::Internal::WidgetView* >( data );
 
-  if( widgetView )
+  if( widgetView && !widgetView->IsWidgetFaulted() )
   {
     widgetView->CloseRemoteSurface();
     widgetView->RemoveWidgetImage();
@@ -171,7 +186,10 @@ WidgetView::WidgetView()
   mUpdatePeriod( 0.0 ),
   mPreviewVisible( true ),
   mLoadingTextVisible( true ),
-  mTapTextVisible( true ),
+  mLoadingTextString(),
+  mRetryTextVisible( true ),
+  mRetryTextString(),
+  mRetryState( false ),
   mPermanentDelete( false ),
   mWatcherHandle( NULL ),
   mRemoteSurface( NULL )
@@ -190,7 +208,10 @@ WidgetView::WidgetView( const std::string& widgetId, const std::string& contentI
   mUpdatePeriod( updatePeriod ),
   mPreviewVisible( true ),
   mLoadingTextVisible( true ),
-  mTapTextVisible( true ),
+  mLoadingTextString(),
+  mRetryTextVisible( true ),
+  mRetryTextString(),
+  mRetryState( false ),
   mPermanentDelete( false ),
   mWatcherHandle( NULL ),
   mRemoteSurface( NULL )
@@ -199,15 +220,6 @@ WidgetView::WidgetView( const std::string& widgetId, const std::string& contentI
 
 WidgetView::~WidgetView()
 {
-  if( !mWidgetId.empty() && !mInstanceId.empty() )
-  {
-    widget_instance_terminate( mInstanceId.c_str() );
-
-    if( mPermanentDelete )
-    {
-      widget_instance_destroy( mInstanceId.c_str() );
-    }
-  }
 }
 
 bool WidgetView::PauseWidget()
@@ -324,15 +336,31 @@ bool WidgetView::IsPreviewVisible()
 
 void WidgetView::SetLoadingTextVisible( bool visible )
 {
-  if( mStateText && mLoadingTextVisible != visible )
+  if( mLoadingText && mLoadingTextVisible != visible )
   {
     mLoadingTextVisible = visible;
 
     if( visible )
     {
-      mStateText.SetProperty( Toolkit::TextLabel::Property::TEXT, GET_LOCALE_TEXT( "IDS_ST_POP_LOADING_ING" ) );
+      mLoadingText.SetProperty( Toolkit::TextLabel::Property::TEXT, 
+                                ( mLoadingTextString.empty() )? GET_LOCALE_TEXT( "IDS_ST_POP_LOADING_ING" ) : mLoadingTextString );
+    }
+    mLoadingText.SetVisible( visible );
+  }
+}
+
+void WidgetView::SetRetryTextVisible( bool visible )
+{
+  if( mRetryText && mRetryTextVisible != visible )
+  {
+    mRetryTextVisible = visible;
+
+    if( visible )
+    {
+      mRetryText.SetProperty( Toolkit::TextLabel::Property::TEXT,
+                              ( mRetryTextString.empty() )? GET_LOCALE_TEXT( "IDS_HS_BODY_UNABLE_TO_LOAD_DATA_TAP_TO_RETRY" ) : mRetryTextString );
     }
-    mStateText.SetVisible( visible );
+    mRetryText.SetVisible( visible );
   }
 }
 
@@ -341,12 +369,17 @@ bool WidgetView::IsLoadingTextVisible()
   return mLoadingTextVisible;
 }
 
+bool WidgetView::IsRetryTextVisible()
+{
+  return mRetryTextVisible;
+}
+
 void WidgetView::ActivateFaultedWidget()
 {
   if( mPid < 0 )
   {
     // Enable preview and loading text
-    ShowTapWidgetState( false );
+    ShowRetryState( false );
     ShowLoadingState( true );
 
     // launch widget again
@@ -356,16 +389,21 @@ void WidgetView::ActivateFaultedWidget()
       DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ActivateFaultedWidget: widget_instance_launch is failed. [%s]\n", mWidgetId.c_str() );
 
       ShowLoadingState( false );
-      ShowTapWidgetState( true );
+      ShowRetryState( true );
 
       // Emit signal
       Dali::WidgetView::WidgetView handle( GetOwner() );
       mWidgetCreationAbortedSignal.Emit( handle );
 
-
       return;
     }
 
+    screen_connector_toolkit_ops ops;
+    ops.updated_cb = OnBufferUpdated;
+    ops.removed_cb = OnSurfaceRemoved;
+    ops.added_cb = OnBufferAdded;
+    mWatcherHandle = screen_connector_toolkit_add(&ops, (char *)mInstanceId.c_str(), SCREEN_CONNECTOR_SCREEEN_TYPE_WIDGET, this);
+
     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ActivateFaultedWidget: widget_instance_launch is called. [%s, mPid = %d]\n", mWidgetId.c_str(), mPid );
   }
 }
@@ -397,6 +435,11 @@ void WidgetView::CreateWidgetImageView()
   mWidgetImageView.SetAnchorPoint( AnchorPoint::CENTER );
   mWidgetImageView.SetSize( mWidth, mHeight );
 
+  if( !mWidgetImagePropertyMap.Empty() )
+  {
+    mWidgetImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, mWidgetImagePropertyMap );
+  }
+
   Self().Add( mWidgetImageView );
 
   //EFL app should be pre multiplied image.
@@ -414,18 +457,43 @@ void WidgetView::CreateWidgetImageView()
 
 void WidgetView::RemoveWidgetImage()
 {
-  ShowTapWidgetState( true );
-
-  // Emit signal
-  Dali::WidgetView::WidgetView handle( GetOwner() );
-  mWidgetDeletedSignal.Emit( handle );
+  if( mWidgetImageView )
+  {
+    mWidgetImageView.SetVisible( false );
+    mWidgetImageView.Reset();
 
-  mWidgetImageView.SetVisible( false );
-  mWidgetImageView.Reset();
+    Dali::WidgetView::WidgetView handle( GetOwner() );
+    mWidgetDeletedSignal.Emit( handle );
+  }
 
   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::RemoveWidgetImage: Widget image is removed.\n" );
 }
 
+void WidgetView::TerminateWidget()
+{
+  if( !mWidgetId.empty() && !mInstanceId.empty() )
+  {
+    widget_instance_terminate( mInstanceId.c_str() );
+
+    if( mPermanentDelete )
+    {
+      widget_instance_destroy( mInstanceId.c_str() );
+    }
+
+    Dali::WidgetView::WidgetView handle( GetOwner() );
+    mWidgetTerminatedSignal.Emit( handle );
+    mWidgetDeletedSignal.Emit( handle );
+
+    mWidgetId.clear();
+    mInstanceId.clear();
+
+    CloseRemoteSurface();
+  }
+
+  DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::TerminateWidget: Widget is terminated/destroyed and deleted signal is emitted.\n" );
+
+}
+
 void WidgetView::SendWidgetEvent( int event )
 {
   Dali::WidgetView::WidgetView handle( GetOwner() );
@@ -448,7 +516,10 @@ void WidgetView::SendWidgetEvent( int event )
     }
     case WIDGET_INSTANCE_EVENT_FAULT:
     {
+      ShowRetryState( true );
       mWidgetFaultedSignal.Emit( handle );
+      CloseRemoteSurface();
+      RemoveWidgetImage();
       break;
     }
     default:
@@ -573,10 +644,38 @@ void WidgetView::SetProperty( BaseObject* object, Property::Index index, const P
       }
       case Dali::WidgetView::WidgetView::Property::PERMANENT_DELETE:
       {
-        bool del;
-        if( value.Get( del ) )
+        bool permanent;
+        if( value.Get( permanent ) )
+        {
+          impl.SetPermanentDelete( permanent );
+        }
+        break;
+      }
+      case TERMINATE_WIDGET:
+      {
+        bool del = false;
+        if( value.Get( del ) && del )
+        {
+          impl.TerminateWidget();
+        }
+        break;
+      }
+      case RETRY_TEXT:
+      {
+        Property::Map map;
+
+        if( value.Get( map ) )
         {
-          impl.SetPermanentDelete( del );
+          impl.SetRetryTextPropertyMap( map );
+        }
+        break;
+      }
+      case WIDGET_IMAGE:
+      {
+        Property::Map map;
+        if( value.Get( map ) )
+        {
+          impl.SetWidgetImagePropertyMap( map );
         }
         break;
       }
@@ -676,6 +775,11 @@ Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetFaultedSig
   return mWidgetFaultedSignal;
 }
 
+Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetTerminatedSignal()
+{
+  return mWidgetTerminatedSignal;
+}
+
 void WidgetView::OnInitialize()
 {
   char* instanceId = NULL;
@@ -734,24 +838,39 @@ void WidgetView::OnInitialize()
   mStateTextActor.SetAnchorPoint( AnchorPoint::CENTER );
   mStateTextActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
 
-  // State text
-  mStateText = Toolkit::TextLabel::New( GET_LOCALE_TEXT( "IDS_ST_POP_LOADING_ING" ) );
-  mStateText.SetParentOrigin( ParentOrigin::CENTER );
-  mStateText.SetAnchorPoint( AnchorPoint::CENTER );
-  mStateText.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
-  mStateText.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
-  mStateText.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, Dali::Color::WHITE );
-  mStateText.SetProperty( Toolkit::TextLabel::Property::FONT_STYLE, "Bold" );
+  // Loading text
+  mLoadingText = Toolkit::TextLabel::New( ( mLoadingTextString.empty() )? GET_LOCALE_TEXT( "IDS_ST_POP_LOADING_ING" ) : mLoadingTextString );
+  mLoadingText.SetParentOrigin( ParentOrigin::CENTER );
+  mLoadingText.SetAnchorPoint( AnchorPoint::CENTER );
+  mLoadingText.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
+  mLoadingText.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
+  mLoadingText.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, Dali::Color::WHITE );
+  mLoadingText.SetProperty( Toolkit::TextLabel::Property::FONT_STYLE, "Bold" );
+  mLoadingText.SetProperty( Toolkit::TextLabel::Property::POINT_SIZE, TextPixelToPointSize( DEFAULT_FONT_PIXEL_SIZE ) );
 
   mPreviewActor.Add( mStateTextActor );
-  mStateTextActor.Add( mStateText );
+  mStateTextActor.Add( mLoadingText );
+
+  // Retry text
+  mRetryText = Toolkit::TextLabel::New( ( mRetryTextString.empty() )? GET_LOCALE_TEXT( "IDS_HS_BODY_UNABLE_TO_LOAD_DATA_TAP_TO_RETRY" ) : mRetryTextString );
+  mRetryText.SetParentOrigin( ParentOrigin::CENTER );
+  mRetryText.SetAnchorPoint( AnchorPoint::CENTER );
+  mRetryText.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
+  mRetryText.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
+  mRetryText.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, Dali::Color::WHITE );
+  mRetryText.SetProperty( Toolkit::TextLabel::Property::FONT_STYLE, "Bold" );
+  mRetryText.SetProperty( Toolkit::TextLabel::Property::POINT_SIZE, TextPixelToPointSize( DEFAULT_FONT_PIXEL_SIZE ) );
+
+  mStateTextActor.Add( mRetryText );
+  mRetryText.SetVisible( false );
 
   // launch widget
   mPid = widget_instance_launch( instanceId, (char *)mContentInfo.c_str(), mWidth, mHeight );
   if( mPid < 0)
   {
 
-    ShowTapWidgetState( true );
+    ShowLoadingState( false );
+    ShowRetryState( true );
 
     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_launch is failed. [%s]\n", mWidgetId.c_str() );
 
@@ -815,7 +934,7 @@ bool WidgetView::OnTouch( Dali::Actor actor, const Dali::TouchData& event )
 {
   tizen_remote_surface_event_type type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_NONE;
 
-  if( event.GetPointCount() == 0 || mRemoteSurface == NULL )
+  if( event.GetPointCount() == 0 )
   {
     return false;
   }
@@ -825,6 +944,13 @@ bool WidgetView::OnTouch( Dali::Actor actor, const Dali::TouchData& event )
     case Dali::PointState::UP:
     {
       type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_UP;
+
+      if( mRetryState )
+      {
+        ActivateFaultedWidget();
+        return false;
+      }
+
       break;
     }
     case Dali::PointState::DOWN:
@@ -843,6 +969,11 @@ bool WidgetView::OnTouch( Dali::Actor actor, const Dali::TouchData& event )
     }
   }
 
+  if( mRemoteSurface == NULL )
+  {
+    return false;
+  }
+
   int button = 1;
 
   if( type == TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_MOVE )
@@ -931,8 +1062,13 @@ float WidgetView::GetWidthForHeight( float height )
 
 void WidgetView::CloseRemoteSurface()
 {
-  screen_connector_toolkit_remove( mWatcherHandle );
-  mRemoteSurface = NULL;
+  if( mWatcherHandle != NULL )
+  {
+    screen_connector_toolkit_remove( mWatcherHandle );
+    mWatcherHandle = NULL;
+    mRemoteSurface = NULL;
+    mPid = -1;
+  }
 }
 
 void WidgetView::ShowLoadingState( bool show )
@@ -942,37 +1078,86 @@ void WidgetView::ShowLoadingState( bool show )
     mPreviewImage.SetVisible( show );
   }
 
-  if( mStateText && mLoadingTextVisible )
+  if( mLoadingText && mLoadingTextVisible )
   {
-    mStateText.SetProperty( Toolkit::TextLabel::Property::TEXT, GET_LOCALE_TEXT( "IDS_ST_POP_LOADING_ING" ) );
-    mStateText.SetVisible( show );
+    mLoadingText.SetProperty( Toolkit::TextLabel::Property::TEXT,
+                              ( mLoadingTextString.empty() )? GET_LOCALE_TEXT( "IDS_ST_POP_LOADING_ING" ) : mLoadingTextString );
+    mLoadingText.SetVisible( show );
   }
 }
 
-void WidgetView::ShowTapWidgetState( bool show )
+void WidgetView::ShowRetryState( bool show )
 {
   if( mPreviewImage && mPreviewVisible )
   {
     mPreviewImage.SetVisible( false );
   }
 
-  if( mStateText && mTapTextVisible )
+  if( mRetryText && mRetryTextVisible )
   {
-    mStateText.SetProperty( Toolkit::TextLabel::Property::TEXT, GET_LOCALE_TEXT( "IDS_HS_BODY_UNABLE_TO_LOAD_DATA_TAP_TO_RETRY" ) );
-    mStateText.SetVisible( show );
+    mRetryText.SetProperty( Toolkit::TextLabel::Property::TEXT,
+                            ( mRetryTextString.empty() )? GET_LOCALE_TEXT( "IDS_HS_BODY_UNABLE_TO_LOAD_DATA_TAP_TO_RETRY" ) : mRetryTextString );
+    mRetryText.SetVisible( show );
+    mRetryState = show;
   }
 }
 
 void WidgetView::SetLoadingTextPropertyMap( Property::Map map )
 {
-  if( mStateText )
+  if( mLoadingText )
+  {
+    Property::Value* loadingText = map.Find( STATE_TEXT );
+    std::string text;
+    if( loadingText && loadingText->Get( text ) )
+    {
+      mLoadingText.SetProperty( Toolkit::TextLabel::Property::TEXT, text );
+      mLoadingTextString = text;
+    }
+
+    Property::Value* fontStyle = map.Find( FONT_STYLE );
+    std::string style;
+
+    if( fontStyle && fontStyle->Get( style ) )
+    {
+      mLoadingText.SetProperty( Toolkit::TextLabel::Property::FONT_STYLE, style );
+    }
+
+    Property::Value* textPointSize = map.Find( TEXT_POINT_SIZE );
+    float size = 0.f;
+
+    if( textPointSize && textPointSize->Get( size ) )
+    {
+      mLoadingText.SetProperty( Toolkit::TextLabel::Property::POINT_SIZE, size );
+    }
+
+    Property::Value* textColor = map.Find( TEXT_COLOR );
+    Vector4 color;
+
+    if( textColor && textColor->Get( color ) )
+    {
+      mLoadingText.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, color );
+    }
+  }
+}
+
+void WidgetView::SetRetryTextPropertyMap( Property::Map map )
+{
+  if( mRetryText )
   {
+    Property::Value* retryText = map.Find( STATE_TEXT );
+    std::string text;
+    if( retryText && retryText->Get( text ) )
+    {
+      mRetryText.SetProperty( Toolkit::TextLabel::Property::TEXT, text );
+      mRetryTextString = text;
+    }
+
     Property::Value* fontStyle = map.Find( FONT_STYLE );
     std::string style;
 
     if( fontStyle && fontStyle->Get( style ) )
     {
-      mStateText.SetProperty( Toolkit::TextLabel::Property::FONT_STYLE, style );
+      mRetryText.SetProperty( Toolkit::TextLabel::Property::FONT_STYLE, style );
     }
 
     Property::Value* textPointSize = map.Find( TEXT_POINT_SIZE );
@@ -980,7 +1165,7 @@ void WidgetView::SetLoadingTextPropertyMap( Property::Map map )
 
     if( textPointSize && textPointSize->Get( size ) )
     {
-      mStateText.SetProperty( Toolkit::TextLabel::Property::POINT_SIZE, size );
+      mRetryText.SetProperty( Toolkit::TextLabel::Property::POINT_SIZE, size );
     }
 
     Property::Value* textColor = map.Find( TEXT_COLOR );
@@ -988,11 +1173,35 @@ void WidgetView::SetLoadingTextPropertyMap( Property::Map map )
 
     if( textColor && textColor->Get( color ) )
     {
-      mStateText.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, color );
+      mRetryText.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, color );
+    }
+
+    Property::Value* textVisible = map.Find( TEXT_VISIBLE );
+    bool visible = true;
+    if( textVisible && textVisible->Get( visible ) )
+    {
+      SetRetryTextVisible( visible );
     }
   }
 }
 
+void WidgetView::SetWidgetImagePropertyMap( Property::Map map )
+{
+  mWidgetImagePropertyMap = map;
+  if( mWidgetImageView )
+  {
+    mWidgetImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, map );
+  }
+}
+
+float WidgetView::TextPixelToPointSize( int pixelSize )
+{
+  Dali::Vector2 dpi = Dali::Stage::GetCurrent().GetDpi();
+  float meanDpi = (dpi.height + dpi.width) / 2.0f;
+
+  return (pixelSize * 72.0f) / meanDpi;
+}
+
 } // namespace Internal
 
 } // namespace WidgetView