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