1 /*
2  * Copyright (C) 2020 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.pkg;
18 
19 import static java.util.Collections.emptyList;
20 
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.content.pm.PackageManager;
24 import android.content.pm.SharedLibraryInfo;
25 
26 import com.android.internal.util.DataClass;
27 import com.android.server.pm.PackageSetting;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 /**
33  * For use by {@link PackageSetting} to maintain functionality that used to exist in
34  * {@link PackageParser.Package}.
35  *
36  * It is assumed that anything inside the package was not cached or written to disk, so none of
37  * these fields are either. They must be set on every boot from other state on the device.
38  *
39  * These fields are also not copied into any cloned PackageSetting, to preserve the old behavior
40  * where they would be lost implicitly by re-generating the package object.
41  */
42 @DataClass(genSetters = true, genConstructor = false, genBuilder = false)
43 public class PackageStateUnserialized {
44 
45     private boolean hiddenUntilInstalled;
46 
47     @NonNull
48     private List<SharedLibraryInfo> usesLibraryInfos = emptyList();
49 
50     @NonNull
51     private List<String> usesLibraryFiles = emptyList();
52 
53     private boolean updatedSystemApp;
54 
55     @NonNull
56     private volatile long[] lastPackageUsageTimeInMills;
57 
58     @Nullable
59     private String overrideSeInfo;
60 
lazyInitLastPackageUsageTimeInMills()61     private long[] lazyInitLastPackageUsageTimeInMills() {
62         return new long[PackageManager.NOTIFY_PACKAGE_USE_REASONS_COUNT];
63     }
64 
setLastPackageUsageTimeInMills(int reason, long time)65     public PackageStateUnserialized setLastPackageUsageTimeInMills(int reason, long time) {
66         if (reason < 0) {
67             return this;
68         }
69         if (reason >= PackageManager.NOTIFY_PACKAGE_USE_REASONS_COUNT) {
70             return this;
71         }
72         getLastPackageUsageTimeInMills()[reason] = time;
73         return this;
74     }
75 
getLatestPackageUseTimeInMills()76     public long getLatestPackageUseTimeInMills() {
77         long latestUse = 0L;
78         for (long use : getLastPackageUsageTimeInMills()) {
79             latestUse = Math.max(latestUse, use);
80         }
81         return latestUse;
82     }
83 
getLatestForegroundPackageUseTimeInMills()84     public long getLatestForegroundPackageUseTimeInMills() {
85         int[] foregroundReasons = {
86                 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY,
87                 PackageManager.NOTIFY_PACKAGE_USE_FOREGROUND_SERVICE
88         };
89 
90         long latestUse = 0L;
91         for (int reason : foregroundReasons) {
92             latestUse = Math.max(latestUse, getLastPackageUsageTimeInMills()[reason]);
93         }
94         return latestUse;
95     }
96 
updateFrom(PackageStateUnserialized other)97     public void updateFrom(PackageStateUnserialized other) {
98         this.hiddenUntilInstalled = other.hiddenUntilInstalled;
99 
100         if (!other.usesLibraryInfos.isEmpty()) {
101             this.usesLibraryInfos = new ArrayList<>(other.usesLibraryInfos);
102         }
103 
104         if (!other.usesLibraryFiles.isEmpty()) {
105             this.usesLibraryFiles = new ArrayList<>(other.usesLibraryFiles);
106         }
107 
108         this.updatedSystemApp = other.updatedSystemApp;
109         this.lastPackageUsageTimeInMills = other.lastPackageUsageTimeInMills;
110         this.overrideSeInfo = other.overrideSeInfo;
111     }
112 
113 
114 
115     // Code below generated by codegen v1.0.14.
116     //
117     // DO NOT MODIFY!
118     // CHECKSTYLE:OFF Generated code
119     //
120     // To regenerate run:
121     // $ codegen $ANDROID_BUILD_TOP/frameworks/base/services/core/java/com/android/server/pm/pkg/PackageStateUnserialized.java
122     //
123     // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
124     //   Settings > Editor > Code Style > Formatter Control
125     //@formatter:off
126 
127 
128     @DataClass.Generated.Member
isHiddenUntilInstalled()129     public boolean isHiddenUntilInstalled() {
130         return hiddenUntilInstalled;
131     }
132 
133     @DataClass.Generated.Member
getUsesLibraryInfos()134     public @NonNull List<SharedLibraryInfo> getUsesLibraryInfos() {
135         return usesLibraryInfos;
136     }
137 
138     @DataClass.Generated.Member
getUsesLibraryFiles()139     public @NonNull List<String> getUsesLibraryFiles() {
140         return usesLibraryFiles;
141     }
142 
143     @DataClass.Generated.Member
isUpdatedSystemApp()144     public boolean isUpdatedSystemApp() {
145         return updatedSystemApp;
146     }
147 
148     @DataClass.Generated.Member
getLastPackageUsageTimeInMills()149     public @NonNull long[] getLastPackageUsageTimeInMills() {
150         long[] _lastPackageUsageTimeInMills = lastPackageUsageTimeInMills;
151         if (_lastPackageUsageTimeInMills == null) {
152             synchronized(this) {
153                 _lastPackageUsageTimeInMills = lastPackageUsageTimeInMills;
154                 if (_lastPackageUsageTimeInMills == null) {
155                     _lastPackageUsageTimeInMills = lastPackageUsageTimeInMills = lazyInitLastPackageUsageTimeInMills();
156                 }
157             }
158         }
159         return _lastPackageUsageTimeInMills;
160     }
161 
162     @DataClass.Generated.Member
getOverrideSeInfo()163     public @Nullable String getOverrideSeInfo() {
164         return overrideSeInfo;
165     }
166 
167     @DataClass.Generated.Member
setHiddenUntilInstalled(boolean value)168     public PackageStateUnserialized setHiddenUntilInstalled(boolean value) {
169         hiddenUntilInstalled = value;
170         return this;
171     }
172 
173     @DataClass.Generated.Member
setUsesLibraryInfos(@onNull List<SharedLibraryInfo> value)174     public PackageStateUnserialized setUsesLibraryInfos(@NonNull List<SharedLibraryInfo> value) {
175         usesLibraryInfos = value;
176         com.android.internal.util.AnnotationValidations.validate(
177                 NonNull.class, null, usesLibraryInfos);
178         return this;
179     }
180 
181     @DataClass.Generated.Member
setUsesLibraryFiles(@onNull List<String> value)182     public PackageStateUnserialized setUsesLibraryFiles(@NonNull List<String> value) {
183         usesLibraryFiles = value;
184         com.android.internal.util.AnnotationValidations.validate(
185                 NonNull.class, null, usesLibraryFiles);
186         return this;
187     }
188 
189     @DataClass.Generated.Member
setUpdatedSystemApp(boolean value)190     public PackageStateUnserialized setUpdatedSystemApp(boolean value) {
191         updatedSystemApp = value;
192         return this;
193     }
194 
195     @DataClass.Generated.Member
setLastPackageUsageTimeInMills(@onNull long... value)196     public PackageStateUnserialized setLastPackageUsageTimeInMills(@NonNull long... value) {
197         lastPackageUsageTimeInMills = value;
198         com.android.internal.util.AnnotationValidations.validate(
199                 NonNull.class, null, lastPackageUsageTimeInMills);
200         return this;
201     }
202 
203     @DataClass.Generated.Member
setOverrideSeInfo(@ullable String value)204     public PackageStateUnserialized setOverrideSeInfo(@Nullable String value) {
205         overrideSeInfo = value;
206         return this;
207     }
208 
209     @DataClass.Generated(
210             time = 1580422870209L,
211             codegenVersion = "1.0.14",
212             sourceFile = "frameworks/base/services/core/java/com/android/server/pm/pkg/PackageStateUnserialized.java",
213             inputSignatures = "private  boolean hiddenUntilInstalled\nprivate @android.annotation.NonNull java.util.List<android.content.pm.SharedLibraryInfo> usesLibraryInfos\nprivate @android.annotation.NonNull java.util.List<java.lang.String> usesLibraryFiles\nprivate  boolean updatedSystemApp\nprivate volatile @android.annotation.NonNull long[] lastPackageUsageTimeInMills\n @android.annotation.Nullable java.lang.String overrideSeInfo\nprivate  long[] lazyInitLastPackageUsageTimeInMills()\npublic  com.android.server.pm.pkg.PackageStateUnserialized setLastPackageUsageTimeInMills(int,long)\npublic  long getLatestPackageUseTimeInMills()\npublic  long getLatestForegroundPackageUseTimeInMills()\nclass PackageStateUnserialized extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genSetters=true, genConstructor=false, genBuilder=false)")
214     @Deprecated
__metadata()215     private void __metadata() {}
216 
217 
218     //@formatter:on
219     // End of generated code
220 
221 }
222