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

Commit e3b740e9 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Fix auto revoke re-grant logic" into rvc-dev

parents d3be5162 7941e9b3
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -96,7 +96,8 @@
        <service android:name="com.android.permissioncontroller.permission.service.AutoRevokeService"
                 android:permission="android.permission.BIND_JOB_SERVICE" />

        <service android:name="com.android.permissioncontroller.permission.service.AutoRevokeReGrantService" />
        <service android:name="com.android.permissioncontroller.permission.service.AutoRevokeReGrantService"
                 android:permission="android.permission.BIND_JOB_SERVICE" />


        <activity android:name="com.android.permissioncontroller.permission.ui.GrantPermissionsActivity"
+7 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.permissioncontroller;

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

/**
 * App-global constants
 */
@@ -39,6 +41,11 @@ 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.
     *
+13 −3
Original line number Diff line number Diff line
@@ -519,10 +519,20 @@ private val Context.firstBootTime: Long get() {

private fun reGrantAutoRevokedPermissionsIfNeeded(context: Context) {
    val sharedPreferences = context.sharedPreferences
    val key = "auto_revoke_regrant_done"
    val key = "auto_revoke_regrant2_done"
    if (!sharedPreferences.getBoolean(key, false)) {
        context.startService(
                Intent().setComponent(ComponentName(context, AutoRevokeReGrantService::class.java)))
        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()
    }
}
+10 −13
Original line number Diff line number Diff line
@@ -16,7 +16,8 @@

package com.android.permissioncontroller.permission.service

import android.content.Intent
import android.app.job.JobParameters
import android.app.job.JobService
import android.os.Process.myUserHandle
import android.os.UserHandle
import com.android.permissioncontroller.DumpableLog
@@ -35,34 +36,30 @@ import kotlinx.coroutines.launch
/**
 * A service to re-grant auto revoked permissions on a one-time basis
 */
class AutoRevokeReGrantService : android.app.Service() {
class AutoRevokeReGrantService : JobService() {
    companion object {
        private const val LOG_TAG = "AutoRevokeReGrantService"
    }

    lateinit var job: Job

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

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

    override fun onBind(intent: Intent?) = null

    private suspend fun reGrantAutoRevokedPermissions() {
        DumpableLog.i(LOG_TAG, "reGrantAutoRevokedPermissions")
        val startTime = System.currentTimeMillis()