1 /*
2  * Copyright (C) 2022 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.pm;
18 
19 import android.annotation.IntDef;
20 import android.content.pm.PackageManager;
21 import android.os.Build;
22 import android.os.SystemProperties;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 
27 /**
28  * A utility class hosting code configuring shared UID migration behavior.
29  */
30 public final class SharedUidMigration {
31 
32     /**
33      * The system property key used to configure the shared UID migration strategy.
34      */
35     public static final String PROPERTY_KEY = "persist.debug.pm.shared_uid_migration_strategy";
36 
37     /**
38      * Only leave shared UID for newly installed packages.
39      */
40     public static final int NEW_INSTALL_ONLY = 1;
41     /**
42      * Opportunistically migrate any single-package shared UID group.
43      */
44     public static final int BEST_EFFORT = 2;
45     /**
46      * Run appId transitions when the device reboots.
47      */
48     public static final int TRANSITION_AT_BOOT = 3;
49     /**
50      * Run appId transitions as soon as the upgrade is installed.
51      */
52     public static final int LIVE_TRANSITION = 4;
53 
54     @IntDef({
55             NEW_INSTALL_ONLY,
56             BEST_EFFORT,
57             TRANSITION_AT_BOOT,
58             LIVE_TRANSITION,
59     })
60     @Retention(RetentionPolicy.SOURCE)
61     public @interface Strategy {}
62 
63     @Strategy
64     private static final int DEFAULT = NEW_INSTALL_ONLY;
65 
66     /**
67      * Whether shared UID migration is fully disabled. Disabled means the sharedUserMaxSdkVersion
68      * attribute will be directly ignored in the parsing phase.
69      */
isDisabled()70     public static boolean isDisabled() {
71         return !PackageManager.ENABLE_SHARED_UID_MIGRATION;
72     }
73 
74     /**
75      * If the system is userdebug, returns the strategy to use by getting the system property
76      * {@link #PROPERTY_KEY}, or else returns the default strategy enabled in the build.
77      */
getCurrentStrategy()78     public static int getCurrentStrategy() {
79         if (!Build.IS_USERDEBUG) {
80             return DEFAULT;
81         }
82 
83         final int s = SystemProperties.getInt(PROPERTY_KEY, DEFAULT);
84         // No transition strategies can be used (http://b/221088088)
85         if (s > BEST_EFFORT || s < NEW_INSTALL_ONLY) {
86             return DEFAULT;
87         }
88         return s;
89     }
90 
91     /**
92      * Should the strategy be used based on the current active strategy.
93      */
applyStrategy(@trategy int strategy)94     public static boolean applyStrategy(@Strategy int strategy) {
95         return !isDisabled() && getCurrentStrategy() >= strategy;
96     }
97 
SharedUidMigration()98     private SharedUidMigration() {}
99 }
100