1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 #include <android/log.h>
13 #include <android/looper.h>
14 #include <android/bitmap.h>
16 #include <android/androidinst.hxx>
17 #include <headless/svpdummies.hxx>
18 #include <unx/gendata.hxx>
19 #include <osl/detail/android-bootstrap.h>
20 #include <rtl/strbuf.hxx>
21 #include <vcl/settings.hxx>
22 #include <vcl/layout.hxx>
24 #define LOGTAG "LibreOffice/androidinst"
25 #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, LOGTAG, __VA_ARGS__))
26 #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOGTAG, __VA_ARGS__))
29 static int viewWidth
= 1, viewHeight
= 1;
31 class AndroidSalData
: public SalGenericData
34 explicit AndroidSalData( SalInstance
*pInstance
) : SalGenericData( SAL_DATA_ANDROID
, pInstance
) {}
35 virtual void ErrorTrapPush() {}
36 virtual bool ErrorTrapPop( bool ) { return false; }
39 void AndroidSalInstance::GetWorkArea(tools::Rectangle
& rRect
)
41 rRect
= tools::Rectangle( Point( 0, 0 ),
42 Size( viewWidth
, viewHeight
) );
45 AndroidSalInstance
*AndroidSalInstance::getInstance()
49 AndroidSalData
*pData
= static_cast<AndroidSalData
*>(ImplGetSVData()->mpSalData
);
52 return static_cast<AndroidSalInstance
*>(pData
->m_pInstance
);
55 AndroidSalInstance::AndroidSalInstance( SalYieldMutex
*pMutex
)
56 : SvpSalInstance( pMutex
)
58 int res
= (lo_get_javavm())->AttachCurrentThread(&m_pJNIEnv
, NULL
);
59 LOGI("AttachCurrentThread res=%d env=%p", res
, m_pJNIEnv
);
62 AndroidSalInstance::~AndroidSalInstance()
64 int res
= (lo_get_javavm())->DetachCurrentThread();
65 LOGI("DetachCurrentThread res=%d", res
);
66 LOGI("destroyed Android Sal Instance");
69 bool AndroidSalInstance::AnyInput( VclInputFlags nType
)
71 if( nType
& VclInputFlags::TIMER
)
72 return CheckTimeout( false );
74 // Unfortunately there is no way to check for a specific type of
75 // input being queued. That information is too hidden, sigh.
76 return SvpSalInstance::s_pDefaultInstance
->PostedEventsInQueue();
79 class AndroidSalSystem
: public SvpSalSystem
{
81 AndroidSalSystem() : SvpSalSystem() {}
82 virtual ~AndroidSalSystem() {}
83 virtual int ShowNativeDialog( const OUString
& rTitle
,
84 const OUString
& rMessage
,
85 const std::list
< OUString
>& rButtons
,
89 SalSystem
*AndroidSalInstance::CreateSalSystem()
91 return new AndroidSalSystem();
94 class AndroidSalFrame
: public SvpSalFrame
97 AndroidSalFrame( AndroidSalInstance
*pInstance
,
99 SalFrameStyleFlags nSalFrameStyle
)
100 : SvpSalFrame(pInstance
, pParent
, nSalFrameStyle
)
102 if (pParent
== NULL
&& viewWidth
> 1 && viewHeight
> 1)
103 SetPosSize(0, 0, viewWidth
, viewHeight
, SAL_FRAME_POSSIZE_WIDTH
| SAL_FRAME_POSSIZE_HEIGHT
);
106 virtual void GetWorkArea(tools::Rectangle
& rRect
)
108 AndroidSalInstance::getInstance()->GetWorkArea( rRect
);
111 virtual void UpdateSettings( AllSettings
&rSettings
)
113 // Clobber the UI fonts
115 psp::FastPrintFontInfo aInfo
;
116 aInfo
.m_aFamilyName
= "Roboto";
117 aInfo
.m_eItalic
= ITALIC_NORMAL
;
118 aInfo
.m_eWeight
= WEIGHT_NORMAL
;
119 aInfo
.m_eWidth
= WIDTH_NORMAL
;
120 psp::PrintFontManager::get().matchFont( aInfo
, rSettings
.GetUILocale() );
123 // FIXME: is 14 point enough ?
124 vcl::Font
aFont( OUString( "Roboto" ), Size( 0, 14 ) );
126 StyleSettings aStyleSet
= rSettings
.GetStyleSettings();
127 aStyleSet
.SetAppFont( aFont
);
128 aStyleSet
.SetHelpFont( aFont
);
129 aStyleSet
.SetMenuFont( aFont
);
130 aStyleSet
.SetToolFont( aFont
);
131 aStyleSet
.SetLabelFont( aFont
);
132 aStyleSet
.SetRadioCheckFont( aFont
);
133 aStyleSet
.SetPushButtonFont( aFont
);
134 aStyleSet
.SetFieldFont( aFont
);
135 aStyleSet
.SetIconFont( aFont
);
136 aStyleSet
.SetTabFont( aFont
);
137 aStyleSet
.SetGroupFont( aFont
);
139 rSettings
.SetStyleSettings( aStyleSet
);
143 SalFrame
*AndroidSalInstance::CreateChildFrame( SystemParentData
* /*pParent*/, SalFrameStyleFlags nStyle
)
145 return new AndroidSalFrame( this, NULL
, nStyle
);
148 SalFrame
*AndroidSalInstance::CreateFrame( SalFrame
* pParent
, SalFrameStyleFlags nStyle
)
150 return new AndroidSalFrame( this, pParent
, nStyle
);
153 // All the interesting stuff is slaved from the AndroidSalInstance
154 void InitSalData() {}
155 void DeInitSalData() {}
156 void InitSalMain() {}
158 void SalAbort( const OUString
& rErrorText
, bool bDumpCore
)
160 OUString
aError( rErrorText
);
161 if( aError
.isEmpty() )
162 aError
= "Unknown application error";
163 LOGI("%s", OUStringToOString(rErrorText
, osl_getThreadTextEncoding()).getStr() );
165 LOGI("SalAbort: '%s'",
166 OUStringToOString(aError
, RTL_TEXTENCODING_ASCII_US
).getStr());
173 const OUString
& SalGetDesktopEnvironment()
175 static OUString
aEnv( "android" );
190 // This is our main entry point:
191 SalInstance
*CreateSalInstance()
193 LOGI("Android: CreateSalInstance!");
194 AndroidSalInstance
* pInstance
= new AndroidSalInstance( new SalYieldMutex() );
195 new AndroidSalData( pInstance
);
196 pInstance
->AcquireYieldMutex(1);
200 void DestroySalInstance( SalInstance
*pInst
)
202 pInst
->ReleaseYieldMutex();
206 int AndroidSalSystem::ShowNativeDialog( const OUString
& rTitle
,
207 const OUString
& rMessage
,
208 const std::list
< OUString
>& rButtons
,
211 (void)rButtons
; (void)nDefButton
;
212 LOGI("LibreOffice native dialog '%s': '%s'",
213 OUStringToOString(rTitle
, RTL_TEXTENCODING_ASCII_US
).getStr(),
214 OUStringToOString(rMessage
, RTL_TEXTENCODING_ASCII_US
).getStr());
215 LOGI("Dialog '%s': '%s'",
216 OUStringToOString(rTitle
, RTL_TEXTENCODING_ASCII_US
).getStr(),
217 OUStringToOString(rMessage
, RTL_TEXTENCODING_ASCII_US
).getStr());
219 if (AndroidSalInstance::getInstance() != NULL
)
221 // Does Android have a native dialog ? if not,. we have to do this ...
223 // Of course it has. android.app.AlertDialog seems like a good
224 // choice, it even has one, two or three buttons. Naturally,
225 // it intended to be used from Java, so some verbose JNI
226 // horror would be needed to use it directly here. Probably we
227 // want some easier to use magic wrapper, hmm.
229 MessageDialog
aVclErrBox(NULL
, rMessage
);
230 aVclErrBox
.SetText(rTitle
);
231 aVclErrBox
.Execute();
234 LOGE("VCL not initialized");
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */