-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathUiKernelParam.kt
More file actions
69 lines (64 loc) · 2.18 KB
/
Copy pathUiKernelParam.kt
File metadata and controls
69 lines (64 loc) · 2.18 KB
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
package com.androidvip.sysctlgui.models
import android.os.Build
import android.os.Parcelable
import androidx.compose.runtime.Immutable
import com.androidvip.sysctlgui.domain.models.KernelParam
import com.androidvip.sysctlgui.utils.Consts
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
import java.io.File
import java.nio.file.Paths
import kotlin.io.path.isDirectory
/**
* Represents a kernel parameter with additional UI-specific properties.
*/
@Immutable
@Parcelize
data class UiKernelParam(
override val name: String = "",
override val path: String = "",
override val value: String = "",
override val isFavorite: Boolean = false,
override val isTaskerParam: Boolean = false,
override val taskerList: Int = Consts.LIST_NUMBER_PRIMARY_TASKER
) : KernelParam(name, path, value, isFavorite, isTaskerParam, taskerList), Parcelable {
/**
* Lazily determines if the [path] represents a directory.
* Uses [Paths] for Android O and above, otherwise falls back to [File].
*/
@IgnoredOnParcel
val isDirectory by lazy {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Paths.get(path).isDirectory()
} else {
File(path).isDirectory
}
}
/**
* The last segment of the parameter's name or path.
*
* If the parameter represents a directory, this will be the last segment of its [path]
* after the last `/`. For example: `/proc/sys/vm/` -> `vm`.
*
* If the parameter represents a file, this will be the last segment of its [name]
* after the last `.`. For example: `vm.swappiness` -> `swappiness`.
*
* If there is no `.` in the name, the full [name] is returned.
*/
override val lastNameSegment: String
get() = if (isDirectory) {
path.substringAfterLast('/')
} else {
name.substringAfterLast('.', name)
}
}
fun KernelParam.toUiKernelParam(): UiKernelParam {
return UiKernelParam(
name = this.name,
path = this.path,
value = this.value,
isFavorite = this.isFavorite,
isTaskerParam = this.isTaskerParam,
taskerList = this.taskerList
)
}