Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / vcl / android / androidinst.cxx
blob8946ff330b38f9270ab6a57b19d231a42b835be2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <jni.h>
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/svapp.hxx>
23 #include <vcl/weld.hxx>
24 #include <memory>
26 #define LOGTAG "LibreOffice/androidinst"
27 #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, LOGTAG, __VA_ARGS__))
28 #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOGTAG, __VA_ARGS__))
30 // Horrible hack
31 static int viewWidth = 1, viewHeight = 1;
33 class AndroidSalData : public GenericUnixSalData
35 public:
36 explicit AndroidSalData( SalInstance *pInstance ) : GenericUnixSalData( SAL_DATA_ANDROID, pInstance ) {}
37 virtual void ErrorTrapPush() {}
38 virtual bool ErrorTrapPop( bool ) { return false; }
41 void AndroidSalInstance::GetWorkArea(tools::Rectangle& rRect)
43 rRect = tools::Rectangle( Point( 0, 0 ),
44 Size( viewWidth, viewHeight ) );
47 AndroidSalInstance *AndroidSalInstance::getInstance()
49 if (!ImplGetSVData())
50 return NULL;
51 AndroidSalData *pData = static_cast<AndroidSalData *>(ImplGetSVData()->mpSalData);
52 if (!pData)
53 return NULL;
54 return static_cast<AndroidSalInstance *>(pData->m_pInstance);
57 AndroidSalInstance::AndroidSalInstance( SalYieldMutex *pMutex )
58 : SvpSalInstance( pMutex )
60 int res = (lo_get_javavm())->AttachCurrentThread(&m_pJNIEnv, NULL);
61 LOGI("AttachCurrentThread res=%d env=%p", res, m_pJNIEnv);
64 AndroidSalInstance::~AndroidSalInstance()
66 int res = (lo_get_javavm())->DetachCurrentThread();
67 LOGI("DetachCurrentThread res=%d", res);
68 LOGI("destroyed Android Sal Instance");
71 bool AndroidSalInstance::AnyInput( VclInputFlags nType )
73 if( nType & VclInputFlags::TIMER )
74 return CheckTimeout( false );
76 // Unfortunately there is no way to check for a specific type of
77 // input being queued. That information is too hidden, sigh.
78 return SvpSalInstance::s_pDefaultInstance->HasUserEvents();
81 class AndroidSalSystem : public SvpSalSystem {
82 public:
83 AndroidSalSystem() : SvpSalSystem() {}
84 virtual ~AndroidSalSystem() {}
85 virtual int ShowNativeDialog( const OUString& rTitle,
86 const OUString& rMessage,
87 const std::vector< OUString >& rButtons );
90 SalSystem *AndroidSalInstance::CreateSalSystem()
92 return new AndroidSalSystem();
95 class AndroidSalFrame : public SvpSalFrame
97 public:
98 AndroidSalFrame( AndroidSalInstance *pInstance,
99 SalFrame *pParent,
100 SalFrameStyleFlags nSalFrameStyle )
101 : SvpSalFrame(pInstance, pParent, nSalFrameStyle)
103 if (pParent == NULL && viewWidth > 1 && viewHeight > 1)
104 SetPosSize(0, 0, viewWidth, viewHeight, SAL_FRAME_POSSIZE_WIDTH | SAL_FRAME_POSSIZE_HEIGHT);
107 virtual void GetWorkArea(tools::Rectangle& rRect)
109 AndroidSalInstance::getInstance()->GetWorkArea( rRect );
112 virtual void UpdateSettings( AllSettings &rSettings )
114 // Clobber the UI fonts
115 #if 0
116 psp::FastPrintFontInfo aInfo;
117 aInfo.m_aFamilyName = "Roboto";
118 aInfo.m_eItalic = ITALIC_NORMAL;
119 aInfo.m_eWeight = WEIGHT_NORMAL;
120 aInfo.m_eWidth = WIDTH_NORMAL;
121 psp::PrintFontManager::get().matchFont( aInfo, rSettings.GetUILocale() );
122 #endif
124 // FIXME: is 14 point enough ?
125 vcl::Font aFont( OUString( "Roboto" ), Size( 0, 14 ) );
127 StyleSettings aStyleSet = rSettings.GetStyleSettings();
128 aStyleSet.SetAppFont( aFont );
129 aStyleSet.SetHelpFont( aFont );
130 aStyleSet.SetMenuFont( aFont );
131 aStyleSet.SetToolFont( aFont );
132 aStyleSet.SetLabelFont( aFont );
133 aStyleSet.SetRadioCheckFont( aFont );
134 aStyleSet.SetPushButtonFont( aFont );
135 aStyleSet.SetFieldFont( aFont );
136 aStyleSet.SetIconFont( aFont );
137 aStyleSet.SetTabFont( aFont );
138 aStyleSet.SetGroupFont( aFont );
140 rSettings.SetStyleSettings( aStyleSet );
144 SalFrame *AndroidSalInstance::CreateChildFrame( SystemParentData* /*pParent*/, SalFrameStyleFlags nStyle )
146 return new AndroidSalFrame( this, NULL, nStyle );
149 SalFrame *AndroidSalInstance::CreateFrame( SalFrame* pParent, SalFrameStyleFlags nStyle )
151 return new AndroidSalFrame( this, pParent, nStyle );
154 // All the interesting stuff is slaved from the AndroidSalInstance
155 void InitSalData() {}
156 void DeInitSalData() {}
157 void InitSalMain() {}
159 void SalAbort( const OUString& rErrorText, bool bDumpCore )
161 OUString aError( rErrorText );
162 if( aError.isEmpty() )
163 aError = "Unknown application error";
164 LOGI("%s", OUStringToOString(rErrorText, osl_getThreadTextEncoding()).getStr() );
166 LOGI("SalAbort: '%s'",
167 OUStringToOString(aError, RTL_TEXTENCODING_ASCII_US).getStr());
168 if( bDumpCore )
169 abort();
170 else
171 _exit(1);
174 const OUString& SalGetDesktopEnvironment()
176 static OUString aEnv( "android" );
177 return aEnv;
180 SalData::SalData() :
181 m_pInstance( 0 ),
182 m_pPIManager(0 )
186 SalData::~SalData()
190 // This is our main entry point:
191 SalInstance *CreateSalInstance()
193 LOGI("Android: CreateSalInstance!");
194 AndroidSalInstance* pInstance = new AndroidSalInstance( new SvpSalYieldMutex() );
195 new AndroidSalData( pInstance );
196 pInstance->AcquireYieldMutex();
197 return pInstance;
200 void DestroySalInstance( SalInstance *pInst )
202 pInst->ReleaseYieldMutexAll();
203 delete pInst;
206 int AndroidSalSystem::ShowNativeDialog( const OUString& rTitle,
207 const OUString& rMessage,
208 const std::vector< OUString >& rButtons )
210 (void)rButtons;
211 LOGI("LibreOffice native dialog '%s': '%s'",
212 OUStringToOString(rTitle, RTL_TEXTENCODING_ASCII_US).getStr(),
213 OUStringToOString(rMessage, RTL_TEXTENCODING_ASCII_US).getStr());
214 LOGI("Dialog '%s': '%s'",
215 OUStringToOString(rTitle, RTL_TEXTENCODING_ASCII_US).getStr(),
216 OUStringToOString(rMessage, RTL_TEXTENCODING_ASCII_US).getStr());
218 if (AndroidSalInstance::getInstance() != NULL)
220 // Does Android have a native dialog ? if not,. we have to do this ...
222 // Of course it has. android.app.AlertDialog seems like a good
223 // choice, it even has one, two or three buttons. Naturally,
224 // it intended to be used from Java, so some verbose JNI
225 // horror would be needed to use it directly here. Probably we
226 // want some easier to use magic wrapper, hmm.
227 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(nullptr,
228 VclMessageType::Warning, VclButtonsType::Ok,
229 rMessage));
230 xBox->set_title(rTitle);
231 xBox->run();
233 else
234 LOGE("VCL not initialized");
235 return 0;
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */