1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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 */
9
10 #include <jni.h>
11
12 #include <android/looper.h>
13 #include <android/bitmap.h>
14
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>
25
26 // Horrible hack
27 static int viewWidth = 1, viewHeight = 1;
28
GetWorkArea(AbsoluteScreenPixelRectangle & rRect)29 void AndroidSalInstance::GetWorkArea(AbsoluteScreenPixelRectangle& rRect)
30 {
31 rRect = AbsoluteScreenPixelRectangle( AbsoluteScreenPixelPoint( 0, 0 ),
32 AbsoluteScreenPixelSize( viewWidth, viewHeight ) );
33 }
34
getInstance()35 AndroidSalInstance *AndroidSalInstance::getInstance()
36 {
37 if (!ImplGetSVData())
38 return NULL;
39 return static_cast<AndroidSalInstance *>(GetSalInstance());
40 }
41
AndroidSalInstance(std::unique_ptr<SalYieldMutex> pMutex)42 AndroidSalInstance::AndroidSalInstance( std::unique_ptr<SalYieldMutex> pMutex )
43 : SvpSalInstance( std::move(pMutex) )
44 {
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);
48 }
49
50 // This is never called on Android until app exit.
~AndroidSalInstance()51 AndroidSalInstance::~AndroidSalInstance()
52 {
53 int res = (lo_get_javavm())->DetachCurrentThread();
54 LOGI("DetachCurrentThread res=%d", res);
55 LOGI("destroyed Android Sal Instance");
56 }
57
AnyInput(VclInputFlags nType)58 bool AndroidSalInstance::AnyInput( VclInputFlags nType )
59 {
60 if( nType & VclInputFlags::TIMER )
61 return CheckTimeout( false );
62
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();
66 }
67
updateMainThread()68 void AndroidSalInstance::updateMainThread()
69 {
70 int res = (lo_get_javavm())->AttachCurrentThread(&m_pJNIEnv, NULL);
71 LOGI("updateMainThread AttachCurrentThread res=%d env=%p", res, m_pJNIEnv);
72 SvpSalInstance::updateMainThread();
73 }
74
releaseMainThread()75 void AndroidSalInstance::releaseMainThread()
76 {
77 int res = (lo_get_javavm())->DetachCurrentThread();
78 LOGI("releaseMainThread DetachCurrentThread res=%d", res);
79
80 SvpSalInstance::releaseMainThread();
81 }
82
83 class AndroidSalSystem : public SvpSalSystem {
84 public:
AndroidSalSystem()85 AndroidSalSystem() : SvpSalSystem() {}
~AndroidSalSystem()86 virtual ~AndroidSalSystem() {}
87 virtual int ShowNativeDialog( const OUString& rTitle,
88 const OUString& rMessage,
89 const std::vector< OUString >& rButtons );
90 };
91
CreateSalSystem()92 SalSystem *AndroidSalInstance::CreateSalSystem()
93 {
94 return new AndroidSalSystem();
95 }
96
97 class AndroidSalFrame : public SvpSalFrame
98 {
99 public:
AndroidSalFrame(AndroidSalInstance * pInstance,SalFrame * pParent,SalFrameStyleFlags nSalFrameStyle)100 AndroidSalFrame( AndroidSalInstance *pInstance,
101 SalFrame *pParent,
102 SalFrameStyleFlags nSalFrameStyle )
103 : SvpSalFrame(pInstance, pParent, nSalFrameStyle)
104 {
105 if (pParent == NULL && viewWidth > 1 && viewHeight > 1)
106 SetPosSize(0, 0, viewWidth, viewHeight, SAL_FRAME_POSSIZE_WIDTH | SAL_FRAME_POSSIZE_HEIGHT);
107 }
108
GetWorkArea(AbsoluteScreenPixelRectangle & rRect)109 virtual void GetWorkArea(AbsoluteScreenPixelRectangle& rRect)
110 {
111 AndroidSalInstance::getInstance()->GetWorkArea( rRect );
112 }
113
UpdateSettings(AllSettings & rSettings)114 virtual void UpdateSettings( AllSettings &rSettings )
115 {
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
125
126 // FIXME: is 14 point enough ?
127 vcl::Font aFont( OUString( "Roboto" ), Size( 0, 14 ) );
128
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 );
141
142 rSettings.SetStyleSettings( aStyleSet );
143 }
144 };
145
CreateChildFrame(SystemParentData *,SalFrameStyleFlags nStyle)146 SalFrame *AndroidSalInstance::CreateChildFrame( SystemParentData* /*pParent*/, SalFrameStyleFlags nStyle )
147 {
148 return new AndroidSalFrame( this, NULL, nStyle );
149 }
150
CreateFrame(SalFrame * pParent,SalFrameStyleFlags nStyle)151 SalFrame *AndroidSalInstance::CreateFrame( SalFrame* pParent, SalFrameStyleFlags nStyle )
152 {
153 return new AndroidSalFrame( this, pParent, nStyle );
154 }
155
156 // This is our main entry point:
create_SalInstance()157 extern "C" SalInstance *create_SalInstance()
158 {
159 LOGI("Android: create_SalInstance!");
160 AndroidSalInstance* pInstance = new AndroidSalInstance( std::make_unique<SvpSalYieldMutex>() );
161 new SvpSalData();
162 return pInstance;
163 }
164
ShowNativeDialog(const OUString & rTitle,const OUString & rMessage,const std::vector<OUString> & rButtons)165 int AndroidSalSystem::ShowNativeDialog( const OUString& rTitle,
166 const OUString& rMessage,
167 const std::vector< OUString >& rButtons )
168 {
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());
176
177 if (AndroidSalInstance::getInstance() != NULL)
178 {
179 // Does Android have a native dialog ? if not,. we have to do this ...
180
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();
191 }
192 else
193 LOGE("VCL not initialized");
194 return 0;
195 }
196
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
198