puzhibing
2026-08-21 5fa94b3718b4a78a4fcea4c97652b0ef20b7de3a
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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()
        }
    }
}