3cooking 5 年之前
當前提交
e5bdefcb7e
共有 1 個文件被更改,包括 285 次插入0 次删除
  1. 285 0
      MCPlus/src/main/java/com/lemonhc/mplus/mcare_plus/activity/webView/WebViewCallFunctions.kt

+ 285 - 0
MCPlus/src/main/java/com/lemonhc/mplus/mcare_plus/activity/webView/WebViewCallFunctions.kt

@@ -0,0 +1,285 @@
+package com.lemonhc.mplus.mcare_plus.activity.webView
+
+import android.app.Activity
+import android.content.ComponentName
+import android.content.Intent
+import android.content.pm.PackageManager
+import android.net.Uri
+import android.os.Build
+import android.provider.Settings
+import android.util.Log
+import android.webkit.WebView
+import com.google.gson.Gson
+import com.google.gson.stream.JsonReader
+import com.lemonhc.mplus.MPlusConstants.PACS_CLASS_NAME
+import com.lemonhc.mplus.MPlusConstants.PACS_PACKAGE_NAME
+import com.lemonhc.mplus.MPlusConstants.PACS_SERVER_URL
+import com.lemonhc.mplus.MPlusConstants.USE_LOCK_SCREEN
+import com.lemonhc.mplus.classes.PACSObject
+import com.lemonhc.mplus.framework.constant.FrameworkConstant
+import com.lemonhc.mplus.mcare_plus.activity.MPlusPopupActivity
+import com.lemonhc.mplus.mcare_plus.activity.MainActivity.Companion.checkInstallPACS
+import com.lemonhc.mplus.mcare_plus.activity.PhotoActivity
+import com.lemonhc.mplus.mcare_plus.activity.BarcodeActivity
+import com.lemonhc.mplus.mcare_plus.activity.lockScreen.LockOptionActivity
+import com.lemonhc.mplus.model.User
+import org.json.JSONObject
+import java.io.StringReader
+import java.net.URI
+
+/**
+ * 웹뷰에서 호출한 functionType 명으로 함수를 호출하기 위한 클래스.
+ */
+class WebViewCallFunctions(val activity: Activity, val webView:WebView){
+    var functionName: String? = null
+    var param: Map<String, Any>? = null
+
+    fun exitApp() {
+        activity.moveTaskToBack(true)
+        activity.finish()
+        android.os.Process.killProcess(android.os.Process.myPid())
+    }
+
+    fun callPacs() {
+        if (checkInstallPACS(activity, PACS_PACKAGE_NAME)) {
+            // 암호에 슬래쉬가 들어가서..  JSONObject사용하였음, 밸류에 슬래쉬"/"가 들어있는 경우 json string으로 변환이 필요하다.
+            // 그냥 param.toString()으로 Gson으로 넘기면 파싱하다 에러난다.
+            val jsonObject = JSONObject(this.param)
+
+            var pacsObject = PACSObject()
+            val gson = Gson()
+            val reader = JsonReader(StringReader(jsonObject.toString()))
+            reader.isLenient = true
+            pacsObject = gson.fromJson(reader, PACSObject::class.java)
+
+            val intent = Intent()
+            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+            intent.component = ComponentName(PACS_PACKAGE_NAME, PACS_CLASS_NAME)
+
+            intent.putExtra("ServerUrl", PACS_SERVER_URL)
+            if (pacsObject.user_id.isNullOrEmpty() == false) {
+                intent.putExtra("UserID", pacsObject.user_id)
+            }
+
+            if (pacsObject.user_pw.isNullOrEmpty() == false) {
+                intent.putExtra("UserPassword", pacsObject.user_pw)
+            }
+
+            if (pacsObject.type.isNullOrEmpty() == false) {
+                pacsObject.callType = FrameworkConstant.pacsType.getEnum(pacsObject.type)
+            }
+
+            intent.putExtra("CallType", pacsObject.callType.getTypeName())
+
+            //선택 파라미터.
+            if (pacsObject.pid.isNullOrEmpty() == false) {
+                intent.putExtra("PatientID", pacsObject.pid)
+            }
+
+            if (pacsObject.accno.isNullOrEmpty() == false) {
+                intent.putExtra("AccessionNo", pacsObject.accno)
+            }
+
+            if (pacsObject.study_start_date.isNullOrEmpty() == false) {
+                intent.putExtra("StudyStartDate", pacsObject.study_start_date)
+            }
+
+            if (pacsObject.study_end_date.isNullOrEmpty() == false) {
+                intent.putExtra("StudyEndDate", pacsObject.study_end_date)
+            }
+
+            intent.extras.apply {
+                val keys = keySet()
+                val it = keys.iterator()
+
+                while (it.hasNext()) {
+                    val key = it.next()
+                }
+
+            }
+            activity.startActivity(intent)
+        } else {
+            // 설치페이지로 보냄
+            val intent = Intent(Intent.ACTION_VIEW)
+            intent.data = Uri.parse("market://details?id=$PACS_PACKAGE_NAME")
+            activity.startActivity(intent)
+        }
+    }
+
+    fun lockscreen() {
+        if (USE_LOCK_SCREEN == false) {
+            return
+        }
+
+        val intent = Intent(activity, LockOptionActivity::class.java)
+        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
+        intent.putExtra("lock_yn", false)
+        activity.startActivity(intent)
+    }
+
+    fun popup() {
+        val param = param ?: return
+        val url = param["url"] as String
+
+        val intent = Intent(activity, MPlusPopupActivity::class.java)
+        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
+
+        intent.putExtra("Url", url)
+        activity.startActivityForResult(intent, 0)
+    }
+
+    fun getDeviceInfo() {
+        val param = param ?: return
+        val functionName = param["callbackFn"].toString()
+        val json = JSONObject()
+        val params = JSONObject()
+        try {
+            val pkgInfo = activity.packageManager.getPackageInfo(activity.packageName, 0)
+            params.put("deviceId", Settings.Secure.getString(activity.contentResolver, Settings.Secure.ANDROID_ID))
+            params.put("appVersion", pkgInfo.versionCode.toString())
+            params.put("appKind", "android")
+            params.put("phoneKind", "${Build.MANUFACTURER}, ${Build.MODEL}")
+            params.put("osVersion", "${android.os.Build.VERSION.RELEASE}")
+
+            json.put("success", "true")
+            json.put("result", params)
+        } catch (e: Exception) {
+            e.printStackTrace()
+        }
+
+        val callback = "javascript:" + functionName + "('" + json.toString() + "')"
+        webView.post(Runnable { webView.loadUrl(callback) })
+    }
+
+    fun readBarcode() {
+        BarcodeActivity.callBack = fun(s: String?): Unit {
+            val param = param ?: return
+            val functionName = param["callbackFn"].toString()
+            val json = JSONObject()
+            try {
+                json.put("success", "true")
+                json.put("result", s)
+            } catch (e: Exception) {
+                e.printStackTrace()
+            }
+            val callback = "javascript:$functionName('$json')"
+            Log.e("MPLUS", "READ BARCODE SCAN CALLBACK - $callback")
+            webView.post{webView.loadUrl(callback)}
+        }
+        val intent = Intent(activity, BarcodeActivity::class.java)
+        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
+        activity.startActivity(intent)
+    }
+
+    fun readQrcode() {
+        BarcodeActivity.callBack = fun(s: String?): Unit {
+            val param = param ?: return
+            val functionName = param["callbackFn"].toString()
+            val json = JSONObject()
+            try {
+                json.put("success", "true")
+                json.put("result", s)
+            } catch (e: Exception) {
+                e.printStackTrace()
+            }
+
+            val callback = "javascript:" + functionName + "('" + json.toString() + "')"
+            webView.post(Runnable { webView.loadUrl(callback) })
+        }
+        val intent = Intent(activity, BarcodeActivity::class.java)
+        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
+        activity.startActivity(intent)
+    }
+
+    fun readPicture() {
+        PhotoActivity.callBack = fun(successOrFail: JSONObject): Unit {
+            val param = param ?: return
+            val functionName = param["callbackFn"].toString()
+            val json = JSONObject()
+            try {
+                json.put("success", "true")
+                json.put("result", successOrFail)
+            } catch (e: Exception) {
+                e.printStackTrace()
+            }
+
+            val callback = "javascript:" + functionName + "('" + json.toString() + "')"
+            webView.post(Runnable { webView.loadUrl(callback) })
+        }
+
+        kotlin.run {
+            val param = param ?: return
+            val gson = Gson()
+            val json = gson.toJson(param)
+            PhotoActivity.user = gson.fromJson(json, User::class.java)
+        }
+
+        val intent = Intent(activity, PhotoActivity::class.java)
+        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
+        activity.startActivity(intent)
+    }
+
+    fun showBrowsingUrl() {
+        val param = param ?: return
+        val url = param["url"] as String
+//        val intent = Intent(activity, MPlusPopupActivity::class.java)
+//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
+//        intent.putExtra("Url", url)
+//        activity.startActivityForResult(intent, 0)
+
+        val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
+        activity.startActivity(intent)
+
+    }
+
+    fun appCheck() {
+        val param = param ?: return
+        val url = param["target"] as String
+
+        val packageInfoList = activity.packageManager.getInstalledPackages(PackageManager.GET_ACTIVITIES)
+        val s = packageInfoList.asSequence().filter { it?.packageName == url }.any()
+
+        val functionName = param["callbackFn"].toString()
+        val json = JSONObject()
+        try {
+            json.put("success", s)
+        } catch (e: Exception) {
+            e.printStackTrace()
+        }
+
+
+        val callback = "javascript:" + functionName + "('" + json.toString() + "')"
+        webView.post(Runnable { webView.loadUrl(callback) })
+
+    }
+
+    fun appInstall(){
+        val param = param ?: return
+        val url = param["target"] as String
+
+        try {
+            activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("$url")))
+        } catch (exception: android.content.ActivityNotFoundException) {
+            activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$url")))
+        }
+    }
+
+    fun openApp(){
+        val param = param ?: return
+        val url = param["target"] as String
+        // Get an instance of PackageManager
+        val pm = activity.packageManager
+
+        // Initialize a new Intent
+        val intent:Intent? = pm.getLaunchIntentForPackage(url)
+
+        // Add category to intent
+        intent?.addCategory(Intent.CATEGORY_LAUNCHER)
+
+        // If intent is not null then launch the app
+        if(intent!=null){
+            activity.startActivity(intent)
+
+        }
+    }
+}