|
@@ -1,199 +0,0 @@
|
|
|
-package com.quansu.heifengwuliu.activity
|
|
|
-
|
|
|
-import android.content.Intent
|
|
|
-import android.graphics.Rect
|
|
|
-import android.media.MediaPlayer
|
|
|
-import android.os.Build
|
|
|
-import android.os.Bundle
|
|
|
-import android.text.TextUtils
|
|
|
-import android.view.View
|
|
|
-import android.view.ViewTreeObserver.OnGlobalLayoutListener
|
|
|
-import android.widget.Button
|
|
|
-import android.widget.LinearLayout
|
|
|
-import android.widget.TextView
|
|
|
-import com.quansu.heifengwuliu.R
|
|
|
-import com.quansu.heifengwuliu.base.MBActivity
|
|
|
-import com.quansu.heifengwuliu.databinding.ActivityTranslateBinding
|
|
|
-import com.quansu.heifengwuliu.vmodel.TranslateVModel
|
|
|
-import com.socks.library.KLog
|
|
|
-import com.ysnows.base.utils.Toasts
|
|
|
-import com.zhy.http.okhttp.OkHttpUtils
|
|
|
-import com.zhy.http.okhttp.callback.StringCallback
|
|
|
-import okhttp3.Call
|
|
|
-import org.json.JSONException
|
|
|
-import org.json.JSONObject
|
|
|
-import java.io.IOException
|
|
|
-
|
|
|
-open class TranslateActivity : MBActivity<TranslateVModel, ActivityTranslateBinding>(), View.OnClickListener {
|
|
|
- private var tvWord: TextView? = null
|
|
|
- private var tvUsPron: TextView? = null
|
|
|
- private var tvInterpret: TextView? = null
|
|
|
- private var btnAddToWordbank: Button? = null
|
|
|
- private var layAll: LinearLayout? = null
|
|
|
- private var bundle: Bundle? = null
|
|
|
- private var word: String? = null
|
|
|
- private var mediaPlayer: MediaPlayer? = null
|
|
|
- override fun __before() {
|
|
|
- super.__before()
|
|
|
- overridePendingTransition(R.anim.drop_down_from_top, 0)
|
|
|
- }
|
|
|
-
|
|
|
- override fun initCreate(savedInstanceState: Bundle?) {
|
|
|
- super.initCreate(savedInstanceState)
|
|
|
- val intent = intent
|
|
|
- bundle = Bundle()
|
|
|
- if (intent != null) {
|
|
|
- if (Intent.ACTION_WEB_SEARCH == intent.action) {
|
|
|
- val query = intent.getStringExtra("query")
|
|
|
- bundle!!.putString("query", query)
|
|
|
- bundle!!.putString("action", Intent.ACTION_WEB_SEARCH)
|
|
|
- } else if (Build.VERSION.SDK_INT >= 23 && Intent.ACTION_PROCESS_TEXT == intent.action) {
|
|
|
- bundle!!.putString("query", intent.getStringExtra("android.intent.extra.PROCESS_TEXT"))
|
|
|
- bundle!!.putString("action", Intent.ACTION_PROCESS_TEXT)
|
|
|
- }
|
|
|
- processSearch(bundle)
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- override fun onNewIntent(intent: Intent) {
|
|
|
- super.onNewIntent(intent)
|
|
|
- overridePendingTransition(R.anim.drop_down_from_top, 0)
|
|
|
- if (Intent.ACTION_WEB_SEARCH == intent.action) {
|
|
|
- val query = intent.getStringExtra("query")
|
|
|
- bundle!!.putString("query", query)
|
|
|
- bundle!!.putString("action", Intent.ACTION_WEB_SEARCH)
|
|
|
- } else if (Build.VERSION.SDK_INT >= 23 && Intent.ACTION_PROCESS_TEXT == intent.action) {
|
|
|
- val query = intent.getStringExtra("android.intent.extra.PROCESS_TEXT")
|
|
|
- bundle!!.putString("query", query)
|
|
|
- }
|
|
|
- processSearch(bundle)
|
|
|
- }
|
|
|
-
|
|
|
- private fun processSearch(bundle: Bundle?) {
|
|
|
- val query = bundle!!.getString("query")
|
|
|
- vm?.searchWord(query)
|
|
|
- }
|
|
|
-
|
|
|
- override fun initView(view: View?) {
|
|
|
- super.initView(view)
|
|
|
- tvWord = findViewById(R.id.tv_word)
|
|
|
- tvUsPron = findViewById<View>(R.id.tv_us_pron) as TextView
|
|
|
- tvInterpret = findViewById<View>(R.id.tv_interpret) as TextView
|
|
|
- btnAddToWordbank = findViewById<View>(R.id.btn_add_to_wordbank) as Button
|
|
|
- btnAddToWordbank!!.setOnClickListener(this@TranslateActivity)
|
|
|
- layAll = findViewById<View>(R.id.lay_all) as LinearLayout
|
|
|
- layAll!!.setOnClickListener(this@TranslateActivity)
|
|
|
- window.decorView.setOnClickListener { v: View? -> onBackPressed() }
|
|
|
- val decorView = window.decorView
|
|
|
- val onGlobalLayoutListener: OnGlobalLayoutListener = object : OnGlobalLayoutListener {
|
|
|
- private var keyboardHeight = 0
|
|
|
- override fun onGlobalLayout() {
|
|
|
- val rect = Rect()
|
|
|
- decorView.getWindowVisibleDisplayFrame(rect)
|
|
|
- //计算出可见屏幕的高度
|
|
|
- val displayHight = rect.bottom - rect.top
|
|
|
- //获得屏幕整体的高度
|
|
|
- val hight = decorView.height
|
|
|
-// KLog.a(rect.bottom)
|
|
|
-// KLog.a(hight)
|
|
|
- val keybordVisible = rect.bottom != hight
|
|
|
- if (keybordVisible) {
|
|
|
- var statusBarHeight = 0
|
|
|
- try {
|
|
|
- val c = Class.forName("com.android.internal.R\$dimen")
|
|
|
- val obj = c.newInstance()
|
|
|
- val field = c.getField("status_bar_height")
|
|
|
- val x = field[obj].toString().toInt()
|
|
|
- statusBarHeight = context()!!.resources.getDimensionPixelSize(x)
|
|
|
- } catch (e: Exception) {
|
|
|
- e.printStackTrace()
|
|
|
- }
|
|
|
-
|
|
|
- //获得键盘高度
|
|
|
- keyboardHeight = hight - displayHight - statusBarHeight
|
|
|
- } else if (keyboardHeight != 0) {
|
|
|
- keyboardHeight = 0
|
|
|
- onBackPressed()
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- decorView.viewTreeObserver.addOnGlobalLayoutListener(onGlobalLayoutListener)
|
|
|
- }
|
|
|
-
|
|
|
- override fun binding(): ActivityTranslateBinding {
|
|
|
- return ActivityTranslateBinding.inflate(layoutInflater)
|
|
|
- }
|
|
|
-
|
|
|
- override val isRxbus: Boolean
|
|
|
- get() = true
|
|
|
-
|
|
|
-
|
|
|
- override fun title(): String? {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- override fun onClick(view: View) {
|
|
|
- if (view.id == R.id.btn_add_to_wordbank) {
|
|
|
-
|
|
|
-// {"word":"Saturday","opcode":"1"}
|
|
|
- if (TextUtils.isEmpty(word)) {
|
|
|
- Toasts.toast(context(), "word is empty")
|
|
|
- return
|
|
|
- }
|
|
|
- var exception = false
|
|
|
- val jsonObject = JSONObject()
|
|
|
- try {
|
|
|
- jsonObject.put("word", word)
|
|
|
- jsonObject.put("opcode", "1")
|
|
|
- } catch (e: JSONException) {
|
|
|
- e.printStackTrace()
|
|
|
- exception = true
|
|
|
- }
|
|
|
- if (exception) {
|
|
|
- Toasts.toast(context(), "sth is wrong")
|
|
|
- return
|
|
|
- }
|
|
|
- OkHttpUtils.get()
|
|
|
- .url("https://1tyy.cn/insertNewWord.action")
|
|
|
- .addParams("newwordlist", jsonObject.toString())
|
|
|
- .addHeader("Cookie", "JSESSIONID=A652871FA3B0B7655AD87C9701396A29.tomcat1; user_web_token=UmtRK1FiWEl0cG1uNzdkL09PTmd6c0o5aldObjJ0VnFkM0tJOVE0TmZvWEFoclZaY3E3Ym9xRXRMYkpqIGJWc1p0Ty85SGdhbkQyRmNxckY5ZHVRMHVRPT0")
|
|
|
- .build()
|
|
|
- .execute(object : StringCallback() {
|
|
|
- override fun onError(call: Call, e: Exception, id: Int) {
|
|
|
- KLog.a(e.message)
|
|
|
- Toasts.toast(context(), e.message)
|
|
|
- }
|
|
|
-
|
|
|
- override fun onResponse(response: String, id: Int) {
|
|
|
- KLog.a(response)
|
|
|
- btnAddToWordbank!!.text = resources.getString(R.string.added)
|
|
|
- Toasts.toast(context(), resources.getString(R.string.add_success))
|
|
|
- onBackPressed()
|
|
|
- }
|
|
|
- })
|
|
|
- } else if (view.id == R.id.lay_all) {
|
|
|
- Thread {
|
|
|
- try {
|
|
|
- if (!TextUtils.isEmpty(word)) {
|
|
|
- mediaPlayer = MediaPlayer()
|
|
|
- mediaPlayer!!.setDataSource("https://audio2.beingfine.cn/speeches/US/US-speech/$word.mp3")
|
|
|
- mediaPlayer!!.prepare()
|
|
|
- mediaPlayer!!.start()
|
|
|
- }
|
|
|
- } catch (e: IOException) {
|
|
|
- e.printStackTrace()
|
|
|
- }
|
|
|
- }.start()
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- override fun onBackPressed() {
|
|
|
- super.onBackPressed()
|
|
|
- overridePendingTransition(0, R.anim.hide_to_top)
|
|
|
- }
|
|
|
-
|
|
|
- override fun vmClass(): Class<TranslateVModel> {
|
|
|
- return TranslateVModel::class.java
|
|
|
- }
|
|
|
-}
|