Update git submodules
[LibreOffice.git] / vcl / android / androidinst.cxx
blobca130fb1b19fdd43cb1d5edd530963e6773b164a
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/looper.h>
13 #include <android/bitmap.h>
15 #include <android/androidinst.hxx>
16 #include <headless/svpdummies.hxx>
17 #include <headless/svpdata.hxx>
18 #include <osl/detail/android-bootstrap.h>
19 #include <rtl/strbuf.hxx>
20 #include <vcl/settings.hxx>
21 #include <vcl/svapp.hxx>
22 #include <vcl/weld.hxx>
23 #include <memory>
24 #include <unistd.h>
26 // Horrible hack
27 static int viewWidth = 1, viewHeight = 1;
29 void AndroidSalInstance::GetWorkArea(tools::Rectangle& rRect)
31 rRect = tools::Rectangle( Point( 0, 0 ),
32 Size( viewWidth, viewHeight ) );
35 AndroidSalInstance *AndroidSalInstance::getInstance()
37 if (!ImplGetSVData())
38 return NULL;
39 return static_cast<AndroidSalInstance *>(GetSalInstance());
42 AndroidSalInstance::AndroidSalInstance( std::unique_ptr<SalYieldMutex> pMutex )
43 : SvpSalInstance( std::move(pMutex) )
45 // FIXME: remove when uniPoll & runLoop is the only Android entry point.
46 int res = (lo_get_javavm())->AttachCurrentThread(&m_pJNIEnv, NULL);
47 LOGI("AttachCurrentThread res=%d env=%p", res, m_pJNIEnv);
50 // This is never called on Android until app exit.
51 AndroidSalInstance::~AndroidSalInstance()
53 int res = (lo_get_javavm())->DetachCurrentThread();
54 LOGI("DetachCurrentThread res=%d", res);
55 LOGI("destroyed Android Sal Instance");
58 bool AndroidSalInstance::AnyInput( VclInputFlags nType )
60 if( nType & VclInputFlags::TIMER )
61 return CheckTimeout( false );
63 // Unfortunately there is no way to check for a specific type of
64 // input being queued. That information is too hidden, sigh.
65 return SvpSalInstance::s_pDefaultInstance->HasUserEvents();
68 void AndroidSalInstance::updateMainThread()
70 int res = (lo_get_javavm())->AttachCurrentThread(&m_pJNIEnv, NULL);
71 LOGI("updateMainThread AttachCurrentThread res=%d env=%p", res, m_pJNIEnv);
72 SvpSalInstance::updateMainThread();
75 void AndroidSalInstance::releaseMainThread()
77 int res = (lo_get_javavm())->DetachCurrentThread();
78 LOGI("releaseMainThread DetachCurrentThread res=%d", res);
80 SvpSalInstance::releaseMainThread();
83 class AndroidSalSystem : public SvpSalSystem {
84 public:
85 AndroidSalSystem() : SvpSalSystem() {}
86 virtual ~AndroidSalSystem() {}
87 virtual int ShowNativeDialog( const OUString& rTitle,
88 const OUString& rMessage,
89 const std::vector< OUString >& rButtons );
92 SalSystem *AndroidSalInstance::CreateSalSystem()
94 return new AndroidSalSystem();
97 class AndroidSalFrame : public SvpSalFrame
99 public:
100 AndroidSalFrame( AndroidSalInstance *pInstance,
101 SalFrame *pParent,
102 SalFrameStyleFlags nSalFrameStyle )
103 : SvpSalFrame(pInstance, pParent, nSalFrameStyle)
105 if (pParent == NULL && viewWidth > 1 && viewHeight > 1)
106 SetPosSize(0, 0, viewWidth, viewHeight, SAL_FRAME_POSSIZE_WIDTH | SAL_FRAME_POSSIZE_HEIGHT);
109 virtual void GetWorkArea(tools::Rectangle& rRect)
111 AndroidSalInstance::getInstance()->GetWorkArea( rRect );
114 virtual void UpdateSettings( AllSettings &rSettings )
116 // Clobber the UI fonts
117 #if 0
118 psp::FastPrintFontInfo aInfo;
119 aInfo.m_aFamilyName = "Roboto";
120 aInfo.m_eItalic = ITALIC_NORMAL;
121 aInfo.m_eWeight = WEIGHT_NORMAL;
122 aInfo.m_eWidth = WIDTH_NORMAL;
123 psp::PrintFontManager::get().matchFont( aInfo, rSettings.GetUILocale() );
124 #endif
126 // FIXME: is 14 point enough ?
127 vcl::Font aFont( OUString( "Roboto" ), Size( 0, 14 ) );
129 StyleSettings aStyleSet = rSettings.GetStyleSettings();
130 aStyleSet.SetAppFont( aFont );
131 aStyleSet.SetHelpFont( aFont );
132 aStyleSet.SetMenuFont( aFont );
133 aStyleSet.SetToolFont( aFont );
134 aStyleSet.SetLabelFont( aFont );
135 aStyleSet.SetRadioCheckFont( aFont );
136 aStyleSet.SetPushButtonFont( aFont );
137 aStyleSet.SetFieldFont( aFont );
138 aStyleSet.SetIconFont( aFont );
139 aStyleSet.SetTabFont( aFont );
140 aStyleSet.SetGroupFont( aFont );
142 rSettings.SetStyleSettings( aStyleSet );
146 SalFrame *AndroidSalInstance::CreateChildFrame( SystemParentData* /*pParent*/, SalFrameStyleFlags nStyle )
148 return new AndroidSalFrame( this, NULL, nStyle );
151 SalFrame *AndroidSalInstance::CreateFrame( SalFrame* pParent, SalFrameStyleFlags nStyle )
153 return new AndroidSalFrame( this, pParent, nStyle );
156 // This is our main entry point:
157 extern "C" SalInstance *create_SalInstance()
159 LOGI("Android: create_SalInstance!");
160 AndroidSalInstance* pInstance = new AndroidSalInstance( std::make_unique<SvpSalYieldMutex>() );
161 new SvpSalData();
162 return pInstance;
165 int AndroidSalSystem::ShowNativeDialog( const OUString& rTitle,
166 const OUString& rMessage,
167 const std::vector< OUString >& rButtons )
169 (void)rButtons;
170 LOGI("LibreOffice native dialog '%s': '%s'",
171 OUStringToOString(rTitle, RTL_TEXTENCODING_ASCII_US).getStr(),
172 OUStringToOString(rMessage, RTL_TEXTENCODING_ASCII_US).getStr());
173 LOGI("Dialog '%s': '%s'",
174 OUStringToOString(rTitle, RTL_TEXTENCODING_ASCII_US).getStr(),
175 OUStringToOString(rMessage, RTL_TEXTENCODING_ASCII_US).getStr());
177 if (AndroidSalInstance::getInstance() != NULL)
179 // Does Android have a native dialog ? if not,. we have to do this ...
181 // Of course it has. android.app.AlertDialog seems like a good
182 // choice, it even has one, two or three buttons. Naturally,
183 // it intended to be used from Java, so some verbose JNI
184 // horror would be needed to use it directly here. Probably we
185 // want some easier to use magic wrapper, hmm.
186 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(nullptr,
187 VclMessageType::Warning, VclButtonsType::Ok,
188 rMessage));
189 xBox->set_title(rTitle);
190 xBox->run();
192 else
193 LOGE("VCL not initialized");
194 return 0;
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */