Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d607f08a authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Create TogglePermissionAppInfoPage

For example, this could be used for "Install unknown apps".

Bug: 235727273
Test: Manual with Test App
Change-Id: I86227da2d5a909d07fc2526c4dce0dff7ce34509
parent bcdc0217
Loading
Loading
Loading
Loading
+55 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settingslib.spaprivileged.framework.app

import android.app.AppOpsManager
import android.app.AppOpsManager.MODE_ALLOWED
import android.app.AppOpsManager.MODE_ERRORED
import android.app.AppOpsManager.Mode
import android.content.Context
import android.content.pm.ApplicationInfo
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData

class AppOpsController(
    context: Context,
    private val app: ApplicationInfo,
    private val op: Int,
) {
    private val appOpsManager = checkNotNull(context.getSystemService(AppOpsManager::class.java))

    val isAllowed: LiveData<Boolean>
        get() = _isAllowed

    fun setAllowed(allowed: Boolean) {
        val mode = if (allowed) MODE_ALLOWED else MODE_ERRORED
        appOpsManager.setMode(op, app.uid, app.packageName, mode)
        _isAllowed.postValue(allowed)
    }

    @Mode
    private fun getMode(): Int = appOpsManager.checkOpNoThrow(op, app.uid, app.packageName)

    private val _isAllowed = object : MutableLiveData<Boolean>() {
        override fun onActive() {
            postValue(getMode() == MODE_ALLOWED)
        }

        override fun onInactive() {
        }
    }
}
+23 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settingslib.spaprivileged.framework.app

import android.content.pm.ApplicationInfo

interface AppRecord {
    val app: ApplicationInfo
}
+25 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settingslib.spaprivileged.framework.app

import android.content.pm.ApplicationInfo
import android.os.UserHandle

val ApplicationInfo.userId: Int
    get() = UserHandle.getUserId(uid)

fun ApplicationInfo.toRoute() = "$packageName/$userId"
+4 −0
Original line number Diff line number Diff line
@@ -16,10 +16,14 @@

package com.android.settingslib.spaprivileged.framework.app

import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.content.pm.PackageManager

object PackageManagers {
    fun getPackageInfoAsUser(packageName: String, userId: Int): PackageInfo =
        PackageManager.getPackageInfoAsUserCached(packageName, 0, userId)

    fun getApplicationInfoAsUser(packageName: String, userId: Int): ApplicationInfo =
        PackageManager.getApplicationInfoAsUserCached(packageName, 0, userId)
}
+53 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settingslib.spaprivileged.template.app

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.android.settingslib.spa.framework.theme.SettingsDimension
import com.android.settingslib.spa.widget.ui.Footer

@Composable
fun AppInfoPage(
    title: String,
    packageName: String,
    userId: Int,
    footerText: String,
    content: @Composable () -> Unit,
) {
    // TODO: Replace with SettingsScaffold
    Column(Modifier.verticalScroll(rememberScrollState())) {
        Text(
            text = title,
            modifier = Modifier.padding(SettingsDimension.itemPadding),
            color = MaterialTheme.colorScheme.onSurface,
            style = MaterialTheme.typography.headlineMedium,
        )

        AppInfo(packageName, userId)

        content()

        Footer(footerText)
    }
}
Loading