/
HaskellActivity.hs
38 lines (30 loc) · 1.3 KB
/
HaskellActivity.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{-# LANGUAGE ForeignFunctionInterface,OverloadedStrings #-}
module HaskellActivity where
import Foreign.JNI
import Foreign.JNI.Lookup
import Control.Monad.IO.Class
import Data.Text (Text, append, pack)
import GHC.Conc
textView_class :: JClass
textView_class = jclass "android/widget/TextView"
textView_new :: JObject -> JNI JObject
textView_new ctx = do
newObject textView_class (jmethodid textView_class "<init>" "(Landroid/content/Context;)V") [jv ctx]
textView_setText :: JObject -> Text -> JNI ()
textView_setText tv txt = do
jtxt <- newString txt
callMethod tv (jmethodid textView_class "setText" "(Ljava/lang/CharSequence;)V") [jv jtxt]
onCreate :: JNIEnv -> JObject -> JObject -> IO ()
onCreate env activity _bundle = runJNISafe () env $ do
msg <- liftIO $ do
getNumProcessors >>= setNumCapabilities
caps <- getNumCapabilities
return $ "Hello World!\nRunning on " `append` pack (show caps) `append` " CPUs!"
activityClass <- getObjectClass activity
tv <- textView_new activity
textView_setText tv msg
activity_setContentView <- getMethodID activityClass "setContentView" "(Landroid/view/View;)V"
callMethod activity activity_setContentView [JVObject tv] :: JNI ()
foreign export ccall
"Java_org_haskell_HaskellActivity_onCreateHS"
onCreate :: JNIEnv -> JObject -> JObject -> IO ()