aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/moe/yuuta/workmode/access/HostInfo.kt
blob: 05b1622ffea1e9de0fd07919558e84c4e41fa1f4 (plain)
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
package moe.yuuta.workmode.access

import android.content.Context
import android.os.Binder
import moe.yuuta.workmode.BuildConfig
import moe.yuuta.workmode.Manifest

internal data class HostInfo(
    val userId: Int,
    val hostContext: Context
) {
    companion object {

        /**
         * Generate the HostInfo from API caller. If it is NOT an authorized host, throw a SecurityException.
         */
        @Throws(SecurityException::class)
        internal fun getHostInfoFromCaller(systemContext: Context): HostInfo {
            val user = Binder.getCallingUserHandle()
            systemContext.enforceCallingPermission(Manifest.permission.ACCESS, null)
            return HostInfo(user.hashCode(),
                systemContext.createPackageContextAsUser(BuildConfig.APPLICATION_ID, 0, user))
        }
    }
}