1 /*
<lambda>null2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.permission.access.appop
18 
19 import android.util.Slog
20 import com.android.server.LocalServices
21 import com.android.server.appop.AppOpMigrationHelper
22 import com.android.server.permission.access.MutableAccessState
23 import com.android.server.permission.access.collection.* // ktlint-disable no-wildcard-imports
24 import com.android.server.permission.access.immutable.* // ktlint-disable no-wildcard-imports
25 import com.android.server.permission.access.util.PackageVersionMigration
26 
27 class PackageAppOpMigration {
28     fun migrateUserState(state: MutableAccessState, userId: Int) {
29         val legacyAppOpsManager = LocalServices.getService(AppOpMigrationHelper::class.java)!!
30         if (!legacyAppOpsManager.hasLegacyAppOpState()) {
31             return
32         }
33 
34         val legacyPackageAppOpModes = legacyAppOpsManager.getLegacyPackageAppOpModes(userId)
35         val version = PackageVersionMigration.getVersion(userId)
36 
37         val userState = state.mutateUserState(userId)!!
38         val packageAppOpModes = userState.mutatePackageAppOpModes()
39         legacyPackageAppOpModes.forEach { (packageName, legacyAppOpModes) ->
40             if (packageName !in state.externalState.packageStates) {
41                 Slog.w(LOG_TAG, "Dropping unknown package $packageName when migrating app op state")
42                 return@forEach
43             }
44 
45             val appOpModes = MutableIndexedMap<String, Int>()
46             packageAppOpModes[packageName] = appOpModes
47             legacyAppOpModes.forEach { (appOpName, appOpMode) -> appOpModes[appOpName] = appOpMode }
48 
49             userState.mutatePackageVersions()[packageName] = version
50         }
51     }
52 
53     companion object {
54         private val LOG_TAG = PackageAppOpMigration::class.java.simpleName
55     }
56 }
57