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

Commit 67a7ec90 authored by Nate Myren's avatar Nate Myren
Browse files

Remove manifest exempt and toggle state from dump

Bug: 157705898
Test: Trigger dump, run "adb shell dumpsys permissionmgr", and ensure
dump is up-to-date, and that no errors were thrown

Change-Id: I4d652bd3f485a8c00b0d61598de288eb187a563e
parent 72caa638
Loading
Loading
Loading
Loading
+0 −77
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.permissioncontroller.permission.data

import android.app.Application
import android.os.UserHandle
import android.permission.PermissionManager
import com.android.permissioncontroller.PermissionControllerApplication
import com.android.permissioncontroller.permission.utils.Utils.getUserContext
import kotlinx.coroutines.Job

/**
 * A Livedata for the list of packages that have permissions that specified
 * {@code allowDontAutoRevokePermissions=true} in their {@code application} manifest declaration.
 *
 * @param app The current application
 * @param user The users the packages belong to
 */
class AutoRevokeManifestExemptPackagesLiveData(
    private val app: Application,
    private val user: UserHandle
) : SmartAsyncMediatorLiveData<Set<String>>(), PackageBroadcastReceiver.PackageBroadcastListener {
    override fun onPackageUpdate(packageName: String) {
        updateAsync()
    }

    override suspend fun loadDataAndPostValue(job: Job) {
        if (job.isCancelled) {
            return
        }

        postValue(getUserContext(app, user)
                .getSystemService<PermissionManager>(PermissionManager::class.java)!!
                .autoRevokeExemptionGrantedPackages)
    }

    override fun onActive() {
        super.onActive()

        PackageBroadcastReceiver.addAllCallback(this)

        updateAsync()
    }

    override fun onInactive() {
        super.onInactive()

        PackageBroadcastReceiver.addAllCallback(this)
    }

    /**
     * Repository for AutoRevokeExemptionGrantedPackages
     *
     * <p> Key value is the user, value is its corresponding LiveData.
     */
    companion object : DataRepositoryForPackage<UserHandle,
            AutoRevokeManifestExemptPackagesLiveData>() {
        override fun newValue(key: UserHandle): AutoRevokeManifestExemptPackagesLiveData {
            return AutoRevokeManifestExemptPackagesLiveData(PermissionControllerApplication.get(),
                    key)
        }
    }
}
 No newline at end of file
+6 −53
Original line number Diff line number Diff line
@@ -85,8 +85,6 @@ import com.android.permissioncontroller.PermissionControllerStatsLog.PERMISSION_
import com.android.permissioncontroller.R
import com.android.permissioncontroller.permission.data.AllPackageInfosLiveData
import com.android.permissioncontroller.permission.data.AppOpLiveData
import com.android.permissioncontroller.permission.data.AutoRevokeManifestExemptPackagesLiveData
import com.android.permissioncontroller.permission.data.AutoRevokeStateLiveData
import com.android.permissioncontroller.permission.data.BroadcastReceiverLiveData
import com.android.permissioncontroller.permission.data.CarrierPrivilegedStatusLiveData
import com.android.permissioncontroller.permission.data.DataRepositoryForPackage
@@ -306,10 +304,6 @@ private suspend fun revokePermissionsOnUnusedApps(
        }
    }

    // TODO: Support more than the current user
    val manifestExemptPackages = AutoRevokeManifestExemptPackagesLiveData[myUserHandle()]
            .getInitializedValue()

    val revokedApps = mutableListOf<Pair<String, UserHandle>>()
    val userManager = context.getSystemService(UserManager::class.java)
    for ((user, userApps) in unusedApps) {
@@ -327,7 +321,7 @@ private suspend fun revokePermissionsOnUnusedApps(
            }

            val packageName = pkg.packageName
            if (isPackageAutoRevokeExempt(context, pkg, manifestExemptPackages)) {
            if (isPackageAutoRevokeExempt(context, pkg)) {
                return@forEachInParallel
            }

@@ -849,8 +843,6 @@ private class AutoRevokeDumpLiveData(context: Context) :
        val packageName: String,
        val firstInstallTime: Long,
        val lastTimeVisible: Long?,
        val isAutoRevokeManifestExempt: Boolean,
        val isAutoRevokeEnabled: Boolean,
        val implementedServices: List<String>,
        val groups: List<AutoRevokeDumpGroupData>
    ) {
@@ -859,8 +851,6 @@ private class AutoRevokeDumpLiveData(context: Context) :
                    .setUid(uid)
                    .setPackageName(packageName)
                    .setFirstInstallTime(firstInstallTime)
                    .setIsAutoRevokeManifestExempt(isAutoRevokeManifestExempt)
                    .setIsAutoRevokeEnabled(isAutoRevokeEnabled)

            lastTimeVisible?.let { dump.lastTimeVisible = lastTimeVisible }

@@ -901,10 +891,6 @@ private class AutoRevokeDumpLiveData(context: Context) :
    /** Exempt services for each user: user -> services */
    private var services: MutableMap<UserHandle, ExemptServicesLiveData>? = null

    /** Exempt packages for each user: user -> list<package-name> */
    private var autoRevokeManifestExemptPackages: MutableMap<UserHandle,
            AutoRevokeManifestExemptPackagesLiveData>? = null

    /** Usage stats: user -> list<usages> */
    private val usages = UsageStatsLiveData[
        getUnusedThresholdMs(context),
@@ -924,13 +910,6 @@ private class AutoRevokeDumpLiveData(context: Context) :
    private var pkgPermGroupNames:
            MutableMap<Pair<UserHandle, String>, PackagePermissionsLiveData>? = null

    /**
     * Group names for packages
     * map<user, pkg-name> -> auto-revoke state. {@code null} before step 1
     */
    private var pkgAutoRevokeState:
            MutableMap<Pair<UserHandle, String>, AutoRevokeStateLiveData>? = null

    /**
     * Group state for packages
     * map<(user, pkg-name) -> map<perm-group-name -> group>>, value {@code null} before step 2
@@ -950,8 +929,6 @@ private class AutoRevokeDumpLiveData(context: Context) :
        addSource(users) {
            services?.values?.forEach { removeSource(it) }
            services = null
            autoRevokeManifestExemptPackages?.values?.forEach { removeSource(it) }
            autoRevokeManifestExemptPackages = null

            updateIfActive()
        }
@@ -963,8 +940,6 @@ private class AutoRevokeDumpLiveData(context: Context) :
        addSource(packages) {
            pkgPermGroupNames?.values?.forEach { removeSource(it) }
            pkgPermGroupNames = null
            pkgAutoRevokeState?.values?.forEach { removeSource(it) }
            pkgAutoRevokeState = null
            pkgPermGroups.values.forEach { it?.values?.forEach { removeSource(it) } }

            updateIfActive()
@@ -981,7 +956,6 @@ private class AutoRevokeDumpLiveData(context: Context) :
        // services/autoRevokeManifestExemptPackages step 1, users is loaded, nothing else
        if (users.isInitialized && services == null) {
            services = mutableMapOf()
            autoRevokeManifestExemptPackages = mutableMapOf()

            for (user in users.value!!) {
                val newServices = ExemptServicesLiveData[user]
@@ -990,20 +964,12 @@ private class AutoRevokeDumpLiveData(context: Context) :
                addSource(newServices) {
                    updateIfActive()
                }

                val newExemptPackages = AutoRevokeManifestExemptPackagesLiveData[user]
                autoRevokeManifestExemptPackages!![user] = newExemptPackages

                addSource(newExemptPackages) {
                    updateIfActive()
                }
            }
        }

        // pkgPermGroupNames step 1, packages is loaded, nothing else
        if (packages.isInitialized && pkgPermGroupNames == null) {
            pkgPermGroupNames = mutableMapOf()
            pkgAutoRevokeState = mutableMapOf()

            for ((user, userPkgs) in packages.value!!) {
                for (pkg in userPkgs) {
@@ -1016,13 +982,6 @@ private class AutoRevokeDumpLiveData(context: Context) :

                        updateIfActive()
                    }

                    val newPkgAutoRevokeState = AutoRevokeStateLiveData[pkg.packageName, user]
                    pkgAutoRevokeState!![user to pkg.packageName] = newPkgAutoRevokeState

                    addSource(newPkgAutoRevokeState) {
                        updateIfActive()
                    }
                }
            }
        }
@@ -1059,9 +1018,7 @@ private class AutoRevokeDumpLiveData(context: Context) :
                pkgPermGroupNames?.values?.all { it.isInitialized } == true &&
                pkgPermGroupNames?.size == pkgPermGroups.size &&
                pkgPermGroups.values.all { it?.values?.all { it.isInitialized } == true } &&
                services?.values?.all { it.isInitialized } == true &&
                autoRevokeManifestExemptPackages?.values?.all { it.isInitialized } == true &&
                pkgAutoRevokeState?.values?.all { it.isInitialized } == true) {
                services?.values?.all { it.isInitialized } == true) {
            val users = mutableListOf<AutoRevokeDumpUserData>()

            for ((user, userPkgs) in packages.value!!) {
@@ -1086,7 +1043,7 @@ private class AutoRevokeDumpLiveData(context: Context) :
                                    revokedPermGroupNames.value?.let {
                                        it[pkg.packageName to user]
                                            ?.contains(groupName)
                                        } ?: false
                                    } == true
                                ))
                            }
                        }
@@ -1095,10 +1052,6 @@ private class AutoRevokeDumpLiveData(context: Context) :
                    pkgs.add(AutoRevokeDumpPackageData(pkg.uid, pkg.packageName,
                            pkg.firstInstallTime,
                            usages.value!![user]?.lastTimeVisible(pkg.packageName),
                            autoRevokeManifestExemptPackages!![user]!!.value!!
                                    .contains(pkg.packageName),
                            pkgAutoRevokeState!![user to pkg.packageName]!!.value
                                    ?.isEnabledForApp == true,
                            services!![user]?.value!![pkg.packageName] ?: emptyList(),
                            groups))
                }
+0 −2
Original line number Diff line number Diff line
@@ -24,8 +24,6 @@ message PackageProto {
  optional int64 first_install_time = 5;
  repeated PermissionGroupProto groups = 6;
  repeated string implemented_services = 7;
  optional bool is_auto_revoke_manifest_exempt = 8;
  optional bool is_auto_revoke_enabled = 9;
}

message PermissionGroupProto {
+7 −4
Original line number Diff line number Diff line
@@ -21,8 +21,6 @@ import android.app.AlertDialog
import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.UserHandle
import android.util.Log
import android.view.MenuItem
@@ -39,7 +37,11 @@ import com.android.permissioncontroller.permission.ui.model.AutoRevokeViewModel
import com.android.permissioncontroller.permission.ui.model.AutoRevokeViewModel.Months
import com.android.permissioncontroller.permission.ui.model.AutoRevokeViewModel.RevokedPackageInfo
import com.android.permissioncontroller.permission.ui.model.AutoRevokeViewModelFactory
import com.android.permissioncontroller.permission.utils.IPC
import com.android.permissioncontroller.permission.utils.KotlinUtils
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.text.Collator

/**
@@ -100,11 +102,12 @@ class AutoRevokeFragment : PermissionsFrameFragment() {
        activity?.getActionBar()?.setDisplayHomeAsUpEnabled(true)

        if (!viewModel.areAutoRevokedPackagesLoaded()) {
            Handler(Looper.getMainLooper()).postDelayed(Runnable {
            GlobalScope.launch(IPC) {
                delay(SHOW_LOAD_DELAY_MS)
                if (!viewModel.areAutoRevokedPackagesLoaded()) {
                    setLoading(true, false)
                }
            }, SHOW_LOAD_DELAY_MS)
            }
        }
    }