lmw
2023-04-03 16ea883d3c03fd8b910f9282aa1bc08378d40d54
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package cn.sinata.xldutils.utils
 
import android.app.Activity
import android.content.ContentUris
import android.content.Context
import android.content.Intent
import android.database.Cursor
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.os.StatFs
import android.provider.DocumentsContract
import android.provider.MediaStore
import android.text.Editable
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextWatcher
import android.text.style.ForegroundColorSpan
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.core.content.ContextCompat
import cn.sinata.xldutils.utils.ViewClickDelay.SPACE_TIME
import cn.sinata.xldutils.utils.ViewClickDelay.hash
import cn.sinata.xldutils.utils.ViewClickDelay.lastClickTime
import com.zhaoyang.driver.utils.PackageUtil
import org.jetbrains.anko.internals.AnkoInternals
import java.util.*
 
/**
 *
 */
 
fun Any.getSDFreeSize(): Long {
    //取得SD卡文件路径
    val path = Environment.getExternalStorageDirectory()
    val sf = StatFs(path.path)
    //获取单个数据块的大小(Byte)
    val blockSize: Long
    //空闲的数据块的数量
    val freeBlocks: Long
    if (Build.VERSION.SDK_INT >= 18) {
        blockSize = sf.blockSizeLong
        freeBlocks = sf.availableBlocksLong
    } else {
        blockSize = sf.blockSize.toLong()
        freeBlocks = sf.availableBlocks.toLong()
    }
    //返回SD卡空闲大小
    //return freeBlocks * blockSize;  //单位Byte
    //return (freeBlocks * blockSize)/1024;   //单位KB
    return freeBlocks * blockSize / 1024 / 1024 //单位MB
}
 
//设置double精确到小数点后1位
fun doubleOne(price: Double): String? {
    return String.format("%.1f", price)
}
 
fun doubleTwo(price: Double): String? {
    return String.format("%.2f", price)
}
 
 
fun Activity.getUrlPath(imageUri: Uri?): String? {
    if (imageUri == null) {
        return null
    }
    Log.e("mmp", "文档uri:" + imageUri)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(
            this,
            imageUri
        )
    ) {
        if (isExternalStorageDocument(imageUri)) {
            val docId = DocumentsContract.getDocumentId(imageUri)
            Log.e("mmp", "外存文件,文档id:" + docId)
            val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            val type = split[0]
            if ("primary".equals(type, ignoreCase = true)) {
                return Environment.getExternalStorageDirectory().toString() + "/" + split[1]
            }
        } else if (isDownloadsDocument(imageUri)) {
            val id = DocumentsContract.getDocumentId(imageUri)
            Log.e("mmp", "下载文件,文档id:" + id)
            return try {
                val contentUri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads"),
                    java.lang.Long.valueOf(id)
                )
                getDataColumn(this, contentUri, null, null)
            } catch (e: NumberFormatException) {
                null
            }
        } else if (isMediaDocument(imageUri)) {
            val docId = DocumentsContract.getDocumentId(imageUri)
            Log.e("mmp", "多媒体文件,文档id:" + docId)
            val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            val type = split[0]
            var contentUri: Uri? = null
            if ("image" == type) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
            } else if ("video" == type) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
            } else if ("audio" == type) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
            }
            val selection = MediaStore.Images.Media._ID + "=?"
            val selectionArgs = arrayOf(split[1])
            return getDataColumn(this, contentUri, selection, selectionArgs)
        }
    } // MediaStore (and general)
    else if ("content".equals(imageUri.scheme, ignoreCase = true)) {
        Log.e("mmp", "content文件")
        // Return the remote address
        if (isGooglePhotosUri(imageUri))
            return imageUri.lastPathSegment
        return getDataColumn(this, imageUri, null, null)
    } else if ("file".equals(imageUri.scheme, ignoreCase = true)) {
        Log.e("mmp", "file")
        return imageUri.path
    }// File
    return null
}
 
/**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is Google Photos.
 */
fun isGooglePhotosUri(uri: Uri): Boolean = "com.google.android.apps.photos.content" == uri.authority
 
/**
 * @param uri The Uri to check.
 * *
 * @return Whether the Uri authority is ExternalStorageProvider.
 */
fun isExternalStorageDocument(uri: Uri): Boolean =
    "com.android.externalstorage.documents" == uri.authority
 
/**
 * @param uri The Uri to check.
 * *
 * @return Whether the Uri authority is DownloadsProvider.
 */
fun isDownloadsDocument(uri: Uri): Boolean =
    "com.android.providers.downloads.documents" == uri.authority
 
/**
 * @param uri The Uri to check.
 * *
 * @return Whether the Uri authority is MediaProvider.
 */
fun isMediaDocument(uri: Uri): Boolean = "com.android.providers.media.documents" == uri.authority
 
fun getDataColumn(
    context: Context,
    uri: Uri?,
    selection: String?,
    selectionArgs: Array<String>?
): String? {
    if (uri == null) {
        return null
    }
    var cursor: Cursor? = null
    val column = MediaStore.Images.Media.DATA
    val projection = arrayOf(column)
    try {
        cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
        if (cursor != null && cursor.moveToFirst()) {
            val index = cursor.getColumnIndexOrThrow(column)
            return cursor.getString(index)
        }
    } finally {
        if (cursor != null)
            cursor.close()
    }
    return null
}
 
fun callPhone(context: Activity, phone: String) {
    if (phone.isEmpty()) {
        return
    }
    var intent = Intent(Intent.ACTION_CALL, Uri.parse("tel:$phone"))
    if (PackageUtil.isMi()) {
        intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$phone"))
    }
    context.startActivity(intent!!)
}
 
fun Activity.getVersionName(): String {
    return try {
        val manager = this.packageManager
        val info = manager.getPackageInfo(this.packageName, 0)
        info.versionName
    } catch (e: Exception) {
        e.printStackTrace()
        "--"
    }
 
}
 
object ViewClickDelay {
    var hash: Int = 0
    var lastClickTime: Long = 0
    var SPACE_TIME: Long = 1000
}
 
infix fun View.clickDelay(clickAction: () -> Unit) {
    this.setOnClickListener {
        if (this.hashCode() != hash) {
            hash = this.hashCode()
            lastClickTime = System.currentTimeMillis()
            clickAction()
        } else {
            val currentTime = System.currentTimeMillis()
            if (currentTime - lastClickTime > SPACE_TIME) {
                lastClickTime = System.currentTimeMillis()
                clickAction()
            }
        }
    }
}
 
fun View.gone() {
    this.visibility = View.GONE
}
 
 
fun View.visible() {
    this.visibility = View.VISIBLE
}
 
fun TextView.textColor(context: Context, resourse: Int) {
    this.setTextColor(ContextCompat.getColor(context, resourse))
}
 
fun EditText.getContent(): String {
    return this.text.toString().trim()
}
 
fun TextView.setDrawableLeft(drawableResourseId: Int) {
    val topAct: Drawable = context.resources.getDrawable(drawableResourseId)
    this.setCompoundDrawablesWithIntrinsicBounds(topAct, null, null, null)
}
 
 
fun TextView.setDrawableBottom(drawableResourseId: Int) {
    val topAct: Drawable = context.resources.getDrawable(drawableResourseId)
    this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, topAct)
}
 
fun TextView.setDrawableRight(drawableResourseId: Int) {
    val topAct: Drawable = context.resources.getDrawable(drawableResourseId)
    this.setCompoundDrawablesWithIntrinsicBounds(null, null, topAct, null)
}
 
fun TextView.setDrawableTop(drawableResourseId: Int) {
    val topAct: Drawable = context.resources.getDrawable(drawableResourseId)
    this.setCompoundDrawablesWithIntrinsicBounds(null, topAct, null, null)
}
 
fun TextView.setDrawableNull() {
    this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
}
 
fun TextView.setColor(context: Context, resourse: Int) {
    this.setTextColor(ContextCompat.getColor(context, resourse))
}
 
fun TextView.setColorAndSelect(select: Boolean, context: Context, resourse: Int) {
    this.isSelected = select
    this.setTextColor(ContextCompat.getColor(context, resourse))
}
 
fun EditText.getString(): String {
    return this.text.toString().trim()
}
 
fun EditText.setTextChange(onClick: (text: String) -> Unit) {
    this.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
        }
 
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }
 
        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            var s = this@setTextChange.text.toString().trim()
            onClick(s)
        }
    })
}
 
var View.backgroundDrawable: Drawable?
    inline get() = background
    set(value) = setBackgroundDrawable(value)
 
var View.backgroundColorResource: Int
    @Deprecated(
        AnkoInternals.NO_GETTER,
        level = DeprecationLevel.ERROR
    ) get() = AnkoInternals.noGetter()
    set(colorId) = setBackgroundColor(context.resources.getColor(colorId))
 
fun Any?.sysErr(msg: Any?) {
    Log.e("OkTrip----trip", "--------" + msg)
}
 
fun getFormatOne(value: Double?): String {
    return Formatter().format("%.1f", value).toString()
}
 
fun getFormatTwo(value: Double?): String {
    return Formatter().format("%.2f", value).toString()
}
 
fun TextView.setColorBuild(context: Context, content: String, color: Int, start: Int, end: Int) {
    var span = SpannableStringBuilder(content);
    span.setSpan(
        ForegroundColorSpan(ContextCompat.getColor(context, color)),
        start,
        end,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
    )
    this.text = span
}
 
//多个参数
//showCityPop(object :( String, String) -> Unit {
//    override fun invoke(province: String, city: String) {
//
//    }
//})