11package io.elixirdesktop.example
22
3+ import android.annotation.SuppressLint
34import android.app.PendingIntent
45import android.content.ActivityNotFoundException
56import android.content.Context
@@ -116,23 +117,23 @@ class Bridge(private val applicationContext : Context, private var webview : Web
116117 }
117118
118119 // Cleaning deprecated build-xxx folders
119- for (file in applicationContext.filesDir.list()) {
120+ for (file in applicationContext.filesDir.list()!! ) {
120121 if (file.startsWith(" build-" )) {
121122 File (applicationContext.filesDir.absolutePath + " /" + file).deleteRecursively()
122123 }
123124 }
124125
125- var binDir = " $releaseDir /bin"
126+ val binDir = " $releaseDir /bin"
126127
127- Os .setenv(" UPDATE_DIR" , " $releaseDir /update/" , false );
128- Os .setenv(" BINDIR" , binDir, false );
129- Os .setenv(" LIBERLANG" , " $nativeDir /liberlang.so" , false );
128+ Os .setenv(" UPDATE_DIR" , " $releaseDir /update/" , false )
129+ Os .setenv(" BINDIR" , binDir, false )
130+ Os .setenv(" LIBERLANG" , " $nativeDir /liberlang.so" , false )
130131
131132 if (! doneFile.exists()) {
132133 File (binDir).mkdirs()
133134 if (unpackAsset(releaseDir, " app" ) &&
134135 unpackAsset(releaseDir, runtime)) {
135- for (lib in File (" $releaseDir /lib" ).list()) {
136+ for (lib in File (" $releaseDir /lib" ).list()!! ) {
136137 val parts = lib.split(" -" )
137138 val name = parts[0 ]
138139
@@ -153,7 +154,7 @@ class Bridge(private val applicationContext : Context, private var webview : Web
153154 // Re-creating even on relaunch because we can't
154155 // be sure of the native libs directory
155156 // https://github.com/JeromeDeBretagne/erlanglauncher/issues/2
156- for (file in File (nativeDir).list()) {
157+ for (file in File (nativeDir).list()!! ) {
157158 if (file.startsWith(" lib__" )) {
158159 var name = File (file).name
159160 name = name.substring(5 , name.length - 3 )
@@ -165,7 +166,7 @@ class Bridge(private val applicationContext : Context, private var webview : Web
165166
166167 var logdir = applicationContext.getExternalFilesDir(" " )?.path
167168 if (logdir == null ) {
168- logdir = applicationContext.filesDir.absolutePath;
169+ logdir = applicationContext.filesDir.absolutePath
169170 }
170171 Os .setenv(" LOG_DIR" , logdir!! , true )
171172 Log .d(" ERLANG" , " Starting beam..." )
@@ -206,7 +207,7 @@ class Bridge(private val applicationContext : Context, private var webview : Web
206207
207208 // Need to create directories if not exists, or
208209 // it will generate an Exception...
209- var fullpath = " $releaseDir /$filename "
210+ val fullpath = " $releaseDir /$filename "
210211 if (ze.isDirectory) {
211212 Log .d(" DIR" , fullpath)
212213 File (fullpath).mkdirs()
@@ -239,6 +240,7 @@ class Bridge(private val applicationContext : Context, private var webview : Web
239240 return true
240241 }
241242
243+ @SuppressLint(" SetJavaScriptEnabled" )
242244 fun setWebView (_webview : WebView ) {
243245 webview = _webview
244246 webview.webChromeClient = object : WebChromeClient () {
@@ -248,22 +250,23 @@ class Bridge(private val applicationContext : Context, private var webview : Web
248250 isUserGesture : Boolean ,
249251 resultMsg : Message ?
250252 ): Boolean {
251- val newWebView = WebView (applicationContext);
252- view?.addView(newWebView);
253+ val newWebView = WebView (applicationContext)
254+ view?.addView(newWebView)
253255 val transport = resultMsg?.obj as WebView .WebViewTransport
254256
255- transport.webView = newWebView;
256- resultMsg.sendToTarget();
257+ transport.webView = newWebView
258+ resultMsg.sendToTarget()
257259
258- newWebView.setWebViewClient(object : WebViewClient () {
260+ newWebView.webViewClient = object : WebViewClient () {
261+ @Deprecated(" Deprecated in Java" )
259262 override fun shouldOverrideUrlLoading (view : WebView , url : String ): Boolean {
260- val intent = Intent (Intent .ACTION_VIEW , Uri .parse(url));
263+ val intent = Intent (Intent .ACTION_VIEW , Uri .parse(url))
261264 intent.addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
262- applicationContext.startActivity(intent);
263- return true ;
265+ applicationContext.startActivity(intent)
266+ return true
264267 }
265- });
266- return true ;
268+ }
269+ return true
267270 }
268271 }
269272
@@ -282,23 +285,23 @@ class Bridge(private val applicationContext : Context, private var webview : Web
282285
283286
284287 fun getLocalPort (): Int {
285- return server.localPort;
288+ return server.localPort
286289 }
287290
288291 private fun handle (reader : DataInputStream , writer : DataOutputStream ) {
289- val ref = ByteArray (8 );
292+ val ref = ByteArray (8 )
290293
291294 while (true ) {
292- val length = reader.readInt();
293- reader.readFully(ref);
294- var data = ByteArray (length - ref.size);
295+ val length = reader.readInt()
296+ reader.readFully(ref)
297+ val data = ByteArray (length - ref.size)
295298 reader.readFully(data)
296299
297300 val json = JSONArray (String (data))
298301
299- val module = json.getString(0 );
300- val method = json.getString(1 );
301- val args = json.getJSONArray(2 );
302+ val module = json.getString(0 )
303+ val method = json.getString(1 )
304+ val args = json.getJSONArray(2 )
302305
303306 if (method == " :loadURL" ) {
304307 lastURL = args.getString(1 )
@@ -316,18 +319,22 @@ class Bridge(private val applicationContext : Context, private var webview : Web
316319 }
317320
318321 var response = ref
319- response + = if (method == " :getOsDescription" ) {
320- val info = " Android ${Build .DEVICE } ${Build .BRAND } ${Build .VERSION .BASE_OS } ${
321- Build .SUPPORTED_ABIS .joinToString(" ," )
322- } "
323- stringToList(info).toByteArray()
324- } else if (method == " :getCanonicalName" ) {
325- val primaryLocale = getCurrentLocale(applicationContext)
326- var locale = " ${primaryLocale.language} _${primaryLocale.country} "
327- stringToList(locale).toByteArray()
328-
329- } else {
330- " use_mock" .toByteArray()
322+ response + = when (method) {
323+ " :getOsDescription" -> {
324+ val info = " Android ${Build .DEVICE } ${Build .BRAND } ${Build .VERSION .BASE_OS } ${
325+ Build .SUPPORTED_ABIS .joinToString(" ," )
326+ } "
327+ stringToList(info).toByteArray()
328+ }
329+ " :getCanonicalName" -> {
330+ val primaryLocale = getCurrentLocale(applicationContext)
331+ val locale = " ${primaryLocale.language} _${primaryLocale.country} "
332+ stringToList(locale).toByteArray()
333+
334+ }
335+ else -> {
336+ " use_mock" .toByteArray()
337+ }
331338 }
332339 writerLock.lock()
333340 writer.writeInt(response.size)
@@ -336,7 +343,7 @@ class Bridge(private val applicationContext : Context, private var webview : Web
336343 }
337344
338345 fun sendMessage (message : ByteArray ) {
339- thread() {
346+ thread {
340347 writerLock.lock()
341348 while (writers.isEmpty()) {
342349 writerLock.unlock()
@@ -351,15 +358,15 @@ class Bridge(private val applicationContext : Context, private var webview : Web
351358 }
352359 }
353360
354- fun getCurrentLocale (context : Context ): Locale {
361+ private fun getCurrentLocale (context : Context ): Locale {
355362 return if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
356363 context.resources.configuration.locales[0 ]
357364 } else {
358365 context.resources.configuration.locale
359366 }
360367 }
361368
362- fun openFile (filename : String? ) {
369+ private fun openFile (filename : String? ) {
363370 // Get URI and MIME type of file
364371 val file = File (filename)
365372 val uri =
@@ -382,7 +389,7 @@ class Bridge(private val applicationContext : Context, private var webview : Web
382389 try {
383390 applicationContext.startActivity(intent)
384391 } catch (e : ActivityNotFoundException ) {
385- Log .d(" Exception" , e.toString());
392+ Log .d(" Exception" , e.toString())
386393 }
387394 }
388395
@@ -412,7 +419,7 @@ class Bridge(private val applicationContext : Context, private var webview : Web
412419 * A native method that is implemented by the 'native-lib' native library,
413420 * which is packaged with this application.
414421 */
415- external fun startErlang (releaseDir : String , logdir : String ): String
422+ private external fun startErlang (releaseDir : String , logdir : String ): String
416423
417424 companion object {
418425 // Used to load the 'native-lib' library on application startup.
0 commit comments