1 /* 2 * 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.util 18 19 import com.android.server.LocalServices 20 import com.android.server.appop.AppOpMigrationHelper 21 import com.android.server.pm.permission.PermissionMigrationHelper 22 23 object PackageVersionMigration { 24 /** 25 * Maps existing permission and app-op version to a unified version during OTA upgrade. The new 26 * unified version is used in determining the upgrade steps for a package (for both permission 27 * and app-ops). 28 * 29 * @return unified permission/app-op version 30 * @throws IllegalStateException if the method is called when there is nothing to migrate i.e. 31 * permission and app-op file does not exist. 32 */ getVersionnull33 internal fun getVersion(userId: Int): Int { 34 val permissionMigrationHelper = 35 LocalServices.getService(PermissionMigrationHelper::class.java) 36 val permissionVersion = permissionMigrationHelper.getLegacyPermissionStateVersion(userId) 37 38 val appOpMigrationHelper = LocalServices.getService(AppOpMigrationHelper::class.java) 39 val appOpVersion = appOpMigrationHelper.legacyAppOpVersion 40 41 return when { 42 // Both files don't exist. 43 permissionVersion == -1 && appOpVersion == -1 -> 44 error("getVersion() called when there are no legacy files") 45 // merging combination of versions based on released android version 46 // permissions version 1-8 were released in Q, 9 in S, 10 in T and 11 in U 47 // app ops version 1 was released in P, 3 in U. 48 permissionVersion >= 11 && appOpVersion >= 3 -> 15 49 permissionVersion >= 10 && appOpVersion >= 3 -> 14 50 permissionVersion >= 10 && appOpVersion >= 1 -> 13 51 permissionVersion >= 9 && appOpVersion >= 1 -> 12 52 permissionVersion >= 8 && appOpVersion >= 1 -> 11 53 permissionVersion >= 7 && appOpVersion >= 1 -> 10 54 permissionVersion >= 6 && appOpVersion >= 1 -> 9 55 permissionVersion >= 5 && appOpVersion >= 1 -> 8 56 permissionVersion >= 4 && appOpVersion >= 1 -> 7 57 permissionVersion >= 3 && appOpVersion >= 1 -> 6 58 permissionVersion >= 2 && appOpVersion >= 1 -> 5 59 permissionVersion >= 1 && appOpVersion >= 1 -> 4 60 // Permission file exist w/o version, app op file has version as 1. 61 permissionVersion >= 0 && appOpVersion >= 1 -> 3 62 // Both file exist but w/o any version. 63 permissionVersion >= 0 && appOpVersion >= 0 -> 2 64 // Permission file doesn't exit, app op file exist w/o version. 65 permissionVersion >= -1 && appOpVersion >= 0 -> 1 66 // Re-run all upgrades to be safe. 67 else -> 0 68 } 69 } 70 } 71