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

Commit 1e829af8 authored by Eugene Susla's avatar Eugene Susla Committed by Android (Google) Code Review
Browse files

Merge "Revert re-granting auto revoked permissions for dogfooders" into rvc-dev

parents 5eef187e 773b9736
Loading
Loading
Loading
Loading
+0 −7
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@

package com.android.permissioncontroller;

import com.android.permissioncontroller.permission.service.AutoRevokeReGrantService;

/**
 * App-global constants
 */
@@ -41,11 +39,6 @@ public class Constants {
     */
    public static final int AUTO_REVOKE_JOB_ID = 2;

    /**
     * ID of the one-time job {@link AutoRevokeReGrantService}
     */
    public static final int AUTO_REVOKE_REGRANT_JOB_ID = 3;

    /**
     * Name of file to containing the packages we already showed a notificaiton for.
     *
+0 −22
Original line number Diff line number Diff line
@@ -224,8 +224,6 @@ class AutoRevokeOnBootReceiver : BroadcastReceiver() {
            DumpableLog.e(LOG_TAG,
                "Could not schedule ${AutoRevokeService::class.java.simpleName}: $status")
        }

        reGrantAutoRevokedPermissionsIfNeeded(context)
    }
}

@@ -546,26 +544,6 @@ private val Context.firstBootTime: Long get() {
    return time
}

private fun reGrantAutoRevokedPermissionsIfNeeded(context: Context) {
    val sharedPreferences = context.sharedPreferences
    val key = "auto_revoke_regrant2_done"
    if (!sharedPreferences.getBoolean(key, false)) {
        val jobInfo = JobInfo.Builder(
                Constants.AUTO_REVOKE_REGRANT_JOB_ID,
                ComponentName(context, AutoRevokeReGrantService::class.java))
                .build()
        val status = context
                .getSystemService(JobScheduler::class.java)!!
                .schedule(jobInfo)
        if (status != JobScheduler.RESULT_SUCCESS) {
            DumpableLog.e(LOG_TAG,
                    "Could not schedule ${AutoRevokeReGrantService::class.java.simpleName}: " +
                            "$status")
        }
        sharedPreferences.edit().putBoolean(key, true).apply()
    }
}

/**
 * A job to check for apps unused in the last [getUnusedThresholdMs]ms every
 * [getCheckFrequencyMs]ms and [revokePermissionsOnUnusedApps] for them
+0 −125
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.service

import android.app.job.JobParameters
import android.app.job.JobService
import android.os.Process.myUserHandle
import android.os.UserHandle
import com.android.permissioncontroller.DumpableLog
import com.android.permissioncontroller.PermissionControllerApplication
import com.android.permissioncontroller.permission.data.AllPackageInfosLiveData
import com.android.permissioncontroller.permission.data.LightAppPermGroupLiveData
import com.android.permissioncontroller.permission.data.PackagePermissionsLiveData
import com.android.permissioncontroller.permission.data.get
import com.android.permissioncontroller.permission.model.livedatatypes.LightAppPermGroup
import com.android.permissioncontroller.permission.utils.KotlinUtils
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch

/**
 * A service to re-grant auto revoked permissions on a one-time basis
 */
class AutoRevokeReGrantService : JobService() {
    companion object {
        private const val LOG_TAG = "AutoRevokeReGrantService"
    }

    lateinit var job: Job

    override fun onStartJob(params: JobParameters?): Boolean {
        job = GlobalScope.launch(Main) {
            reGrantAutoRevokedPermissions()
            jobFinished(params, /* wantsReschedule */ false)
        }
        return true
    }

    override fun onStopJob(params: JobParameters?): Boolean {
        if (!job.isCompleted) {
            DumpableLog.e(LOG_TAG, "${javaClass.simpleName} terminated before completing",
                    RuntimeException())
            return true
        }
        return false
    }

    private suspend fun reGrantAutoRevokedPermissions() {
        DumpableLog.i(LOG_TAG, "reGrantAutoRevokedPermissions")
        val startTime = System.currentTimeMillis()
        try {
            for ((user, packageToGroups) in loadAllPermissionGroups()) {
                for ((packageName, groups) in packageToGroups) {
                    for (group in groups) {
                        val autoRevokedPermissions = group
                                .allPermissions
                                .filter { (permName, perm) -> perm.isAutoRevoked }
                                .map { (permName, perm) -> permName }
                        if (autoRevokedPermissions.isNotEmpty()) {
                            DumpableLog.i(LOG_TAG,
                                    "Re-granting to u${user.identifier} $packageName: " +
                                    "$autoRevokedPermissions")
                            KotlinUtils.grantForegroundRuntimePermissions(
                                    PermissionControllerApplication.get(),
                                    group,
                                    autoRevokedPermissions)
                            KotlinUtils.grantBackgroundRuntimePermissions(
                                    PermissionControllerApplication.get(),
                                    group,
                                    autoRevokedPermissions)
                        }
                    }
                }
            }
            DumpableLog.i(LOG_TAG, "Done reGrantAutoRevokedPermissions in " +
                    "${System.currentTimeMillis() - startTime}ms")
        } catch (t: Throwable) {
            DumpableLog.e(LOG_TAG, "Failed reGrantAutoRevokedPermissions in " +
                    "${System.currentTimeMillis() - startTime}ms", t)
            throw t
        }
    }

    /** @return user -> packageName -> [permissionGroup] for all users for all packages */
    private suspend fun loadAllPermissionGroups():
            Map<UserHandle, Map<String, List<LightAppPermGroup>>> {
        DumpableLog.i(LOG_TAG, "loadAllPermissionGroups")
        val startTime = System.currentTimeMillis()
        val result = AllPackageInfosLiveData
                .getInitializedValue()
                .map { (user, packages) ->
                    val packagesToGroups = packages?.map { packageInfo ->
                        val packageName = packageInfo.packageName
                        val permissionGroups = PackagePermissionsLiveData[
                                packageName, myUserHandle()]
                                .getInitializedValue()
                                ?.mapNotNull { (groupName, _) ->
                                    LightAppPermGroupLiveData[
                                            packageName, groupName, myUserHandle()]
                                            .getInitializedValue()
                                } ?: emptyList()
                        packageName to permissionGroups
                    }.toMap()
                    user to packagesToGroups
                }.toMap()
        DumpableLog.i(LOG_TAG, "Done loadAllPermissionGroups in " +
                "${System.currentTimeMillis() - startTime}ms")
        return result
    }
}
 No newline at end of file