package com.xw.common.utils import android.annotation.SuppressLint import android.app.Activity import android.app.Service import android.content.Context import android.graphics.Point import android.os.Vibrator import android.util.DisplayMetrics import android.view.WindowManager import android.view.inputmethod.InputMethodManager import android.widget.EditText import com.xw.common.utils.LogUtils.e import java.io.File import java.io.FileInputStream import java.io.FileOutputStream import java.io.IOException object ToolUtil { val TAG = "zmc-ToolUtil" private var dm: DisplayMetrics? = null fun getDefaultDisplay(context: Context): DisplayMetrics { if (dm == null) { dm = context.resources.displayMetrics } return dm!! } /** * 显示与隐藏顶部状态栏 * * @param enable */ fun fullscreen(activity: Activity, enable: Boolean) { val lp = activity.getWindow().getAttributes() if (enable) { // 全屏隐藏状态栏 lp.flags = lp.flags or WindowManager.LayoutParams.FLAG_FULLSCREEN activity.getWindow().setAttributes(lp) activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS) } else { //显示状态栏 lp.flags = lp.flags and (WindowManager.LayoutParams.FLAG_FULLSCREEN.inv()) activity.getWindow().setAttributes(lp) activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS) } } //打开软键盘 fun openKeyBord(editText: EditText, context: Context) { editText.setFocusable(true) editText.setFocusableInTouchMode(true) editText.requestFocus() val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED) imm.toggleSoftInput( InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY ) } //关闭软键盘 fun closeKeybord(editText: EditText, context: Context) { val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(editText.getWindowToken(), 0) } // 屏幕像素点 private val screenSize = Point() // 获取屏幕像素点 fun getScreenSize(context: Activity?): Point { if (context == null) { return screenSize } val wm = context .getSystemService(Context.WINDOW_SERVICE) as WindowManager? if (wm != null) { val mDisplayMetrics = DisplayMetrics() val diplay = wm.getDefaultDisplay() if (diplay != null) { diplay.getMetrics(mDisplayMetrics) val W = mDisplayMetrics.widthPixels val H = mDisplayMetrics.heightPixels if (W * H > 0 && (W > screenSize.x || H > screenSize.y)) { screenSize.set(W, H) } } } return screenSize } /** * 震动milliseconds毫秒 */ @SuppressLint("MissingPermission") fun vibrate(milliseconds: Long) { val vib = AppGlobals.getApplication().getSystemService(Service.VIBRATOR_SERVICE) as Vibrator vib.vibrate(milliseconds) } /** * 复制单个文件 * * @param oldName String 原文件路径+文件名 如:data/user/0/com.test/files/abc.txt * @param newName String 复制后路径+文件名 如:data/user/0/com.test/cache/abc.txt * @return `true` if and only if the file was copied; * `false` otherwise */ fun copyFile(oldName: String, newName: String?): Boolean { var fileInputStream: FileInputStream? = null var fileOutputStream: FileOutputStream? = null try { val oldFile = File(oldName) if (!oldFile.exists()) { e(TAG, "copyFile: oldFile not exist.") return false } else if (!oldFile.isFile()) { e(TAG, "copyFile: oldFile not file.") return false } else if (!oldFile.canRead()) { e(TAG, "copyFile: oldFile cannot read.") return false } fileInputStream = FileInputStream(oldName) //读入原文件 fileOutputStream = FileOutputStream(newName) val buffer = ByteArray(1024) var byteRead: Int while ((fileInputStream.read(buffer).also { byteRead = it }) != -1) { fileOutputStream.write(buffer, 0, byteRead) } setPermission(newName) return true } catch (e: Exception) { e.printStackTrace() return false } finally { try { if (fileInputStream != null) { fileInputStream.close() } if (fileOutputStream != null) { fileOutputStream.flush() fileOutputStream.close() } } catch (e: IOException) { e.printStackTrace() } } } //修改文件权限 fun setPermission(absolutePath: String?) { val command = "chmod " + "777" + " " + absolutePath val runtime = Runtime.getRuntime() try { runtime.exec(command) } catch (e: IOException) { e.printStackTrace() } } }