blob: 7e5532c8e9bb74ade63fb54d64e1f84ab5330c99 [file] [log] [blame]
Richard Uhlerb29f1452018-09-12 16:38:15 +01001/*
2 * Copyright (C) 2018 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
17package android.content.rollback;
18
Narayan Kamathc034fe92019-01-23 10:48:17 +000019import android.annotation.NonNull;
Richard Uhlerc739c8c2018-12-12 11:03:34 +000020import android.annotation.SystemApi;
Richard Uhlera7e9b2d2019-01-22 17:20:58 +000021import android.content.pm.VersionedPackage;
Richard Uhlerb29f1452018-09-12 16:38:15 +010022import android.os.Parcel;
23import android.os.Parcelable;
Narayan Kamathc034fe92019-01-23 10:48:17 +000024import android.util.IntArray;
Nikita Ioffe952aa7b2019-01-28 19:49:56 +000025import android.util.SparseLongArray;
Narayan Kamathc034fe92019-01-23 10:48:17 +000026
27import java.util.ArrayList;
Richard Uhlerb29f1452018-09-12 16:38:15 +010028
Richard Uhlerb29f1452018-09-12 16:38:15 +010029/**
30 * Information about a rollback available for a particular package.
31 *
Richard Uhlerc739c8c2018-12-12 11:03:34 +000032 * @hide
Richard Uhlerb29f1452018-09-12 16:38:15 +010033 */
Richard Uhlerc739c8c2018-12-12 11:03:34 +000034@SystemApi
Richard Uhlerb29f1452018-09-12 16:38:15 +010035public final class PackageRollbackInfo implements Parcelable {
Richard Uhlera7e9b2d2019-01-22 17:20:58 +000036
37 private final VersionedPackage mVersionRolledBackFrom;
38 private final VersionedPackage mVersionRolledBackTo;
Richard Uhlerb29f1452018-09-12 16:38:15 +010039
40 /**
Narayan Kamathc034fe92019-01-23 10:48:17 +000041 * Encapsulates information required to restore a snapshot of an app's userdata.
42 *
43 * @hide
44 */
45 public static class RestoreInfo {
46 public final int userId;
47 public final int appId;
48 public final String seInfo;
49
50 public RestoreInfo(int userId, int appId, String seInfo) {
51 this.userId = userId;
52 this.appId = appId;
53 this.seInfo = seInfo;
54 }
55 }
56
57 /*
58 * The list of users for which we need to backup userdata for this package. Backups of
59 * credential encrypted data are listed as pending if the user hasn't unlocked their device
60 * with credentials yet.
61 */
62 // NOTE: Not a part of the Parcelable representation of this object.
63 private final IntArray mPendingBackups;
64
65 /**
66 * The list of users for which we need to restore userdata for this package. This field is
67 * non-null only after a rollback for this package has been committed.
68 */
69 // NOTE: Not a part of the Parcelable representation of this object.
70 private final ArrayList<RestoreInfo> mPendingRestores;
71
72 /**
Narayan Kamathfcd4a042019-02-01 14:16:37 +000073 * Whether this instance represents the PackageRollbackInfo for an APEX module.
74 */
75 private final boolean mIsApex;
76
Nikita Ioffe952aa7b2019-01-28 19:49:56 +000077 /*
78 * The list of users the package is installed for.
79 */
80 // NOTE: Not a part of the Parcelable representation of this object.
81 private final IntArray mInstalledUsers;
82
83 /**
84 * A mapping between user and an inode of theirs CE data snapshot.
85 */
86 // NOTE: Not a part of the Parcelable representation of this object.
87 private final SparseLongArray mCeSnapshotInodes;
88
Narayan Kamathfcd4a042019-02-01 14:16:37 +000089 /**
Richard Uhlera7e9b2d2019-01-22 17:20:58 +000090 * Returns the name of the package to roll back from.
Richard Uhlerb29f1452018-09-12 16:38:15 +010091 */
Richard Uhlera7e9b2d2019-01-22 17:20:58 +000092 public String getPackageName() {
93 return mVersionRolledBackFrom.getPackageName();
Richard Uhlerb29f1452018-09-12 16:38:15 +010094 }
95
Richard Uhlera7e9b2d2019-01-22 17:20:58 +000096 /**
97 * Returns the version of the package rolled back from.
98 */
99 public VersionedPackage getVersionRolledBackFrom() {
100 return mVersionRolledBackFrom;
101 }
102
103 /**
104 * Returns the version of the package rolled back to.
105 */
106 public VersionedPackage getVersionRolledBackTo() {
107 return mVersionRolledBackTo;
108 }
109
110 /** @hide */
Nikita Ioffe5dcd17972019-02-04 11:08:13 +0000111 public void addPendingBackup(int userId) {
112 mPendingBackups.add(userId);
113 }
114
115 /** @hide */
Narayan Kamathc034fe92019-01-23 10:48:17 +0000116 public IntArray getPendingBackups() {
117 return mPendingBackups;
118 }
119
120 /** @hide */
121 public ArrayList<RestoreInfo> getPendingRestores() {
122 return mPendingRestores;
123 }
124
125 /** @hide */
126 public RestoreInfo getRestoreInfo(int userId) {
127 for (RestoreInfo ri : mPendingRestores) {
128 if (ri.userId == userId) {
129 return ri;
130 }
131 }
132
133 return null;
134 }
135
136 /** @hide */
137 public void removeRestoreInfo(RestoreInfo ri) {
138 mPendingRestores.remove(ri);
139 }
140
141 /** @hide */
Narayan Kamathfcd4a042019-02-01 14:16:37 +0000142 public boolean isApex() {
143 return mIsApex;
144 }
145
146 /** @hide */
Nikita Ioffe952aa7b2019-01-28 19:49:56 +0000147 public IntArray getInstalledUsers() {
148 return mInstalledUsers;
149 }
150
151 /** @hide */
152 public SparseLongArray getCeSnapshotInodes() {
153 return mCeSnapshotInodes;
154 }
155
156 /** @hide */
157 public void putCeSnapshotInode(int userId, long ceSnapshotInode) {
158 mCeSnapshotInodes.put(userId, ceSnapshotInode);
159 }
160
161 /** @hide */
Nikita Ioffe5dcd17972019-02-04 11:08:13 +0000162 public void removePendingBackup(int userId) {
163 int idx = mPendingBackups.indexOf(userId);
164 if (idx != -1) {
165 mPendingBackups.remove(idx);
166 }
167 }
168
169 /** @hide */
170 public void removePendingRestoreInfo(int userId) {
171 removeRestoreInfo(getRestoreInfo(userId));
172 }
173
174 /** @hide */
Richard Uhlera7e9b2d2019-01-22 17:20:58 +0000175 public PackageRollbackInfo(VersionedPackage packageRolledBackFrom,
Narayan Kamathc034fe92019-01-23 10:48:17 +0000176 VersionedPackage packageRolledBackTo,
Narayan Kamathfcd4a042019-02-01 14:16:37 +0000177 @NonNull IntArray pendingBackups, @NonNull ArrayList<RestoreInfo> pendingRestores,
Nikita Ioffe952aa7b2019-01-28 19:49:56 +0000178 boolean isApex, @NonNull IntArray installedUsers,
179 @NonNull SparseLongArray ceSnapshotInodes) {
Richard Uhlera7e9b2d2019-01-22 17:20:58 +0000180 this.mVersionRolledBackFrom = packageRolledBackFrom;
181 this.mVersionRolledBackTo = packageRolledBackTo;
Narayan Kamathc034fe92019-01-23 10:48:17 +0000182 this.mPendingBackups = pendingBackups;
183 this.mPendingRestores = pendingRestores;
Narayan Kamathfcd4a042019-02-01 14:16:37 +0000184 this.mIsApex = isApex;
Nikita Ioffe952aa7b2019-01-28 19:49:56 +0000185 this.mInstalledUsers = installedUsers;
186 this.mCeSnapshotInodes = ceSnapshotInodes;
Richard Uhlerb29f1452018-09-12 16:38:15 +0100187 }
188
189 private PackageRollbackInfo(Parcel in) {
Richard Uhlera7e9b2d2019-01-22 17:20:58 +0000190 this.mVersionRolledBackFrom = VersionedPackage.CREATOR.createFromParcel(in);
191 this.mVersionRolledBackTo = VersionedPackage.CREATOR.createFromParcel(in);
Narayan Kamathfcd4a042019-02-01 14:16:37 +0000192 this.mIsApex = in.readBoolean();
Narayan Kamathc034fe92019-01-23 10:48:17 +0000193 this.mPendingRestores = null;
194 this.mPendingBackups = null;
Nikita Ioffe952aa7b2019-01-28 19:49:56 +0000195 this.mInstalledUsers = null;
196 this.mCeSnapshotInodes = null;
Richard Uhlerb29f1452018-09-12 16:38:15 +0100197 }
198
199 @Override
200 public int describeContents() {
201 return 0;
202 }
203
204 @Override
205 public void writeToParcel(Parcel out, int flags) {
Richard Uhlera7e9b2d2019-01-22 17:20:58 +0000206 mVersionRolledBackFrom.writeToParcel(out, flags);
207 mVersionRolledBackTo.writeToParcel(out, flags);
Narayan Kamathfcd4a042019-02-01 14:16:37 +0000208 out.writeBoolean(mIsApex);
Richard Uhlerb29f1452018-09-12 16:38:15 +0100209 }
210
211 public static final Parcelable.Creator<PackageRollbackInfo> CREATOR =
212 new Parcelable.Creator<PackageRollbackInfo>() {
213 public PackageRollbackInfo createFromParcel(Parcel in) {
214 return new PackageRollbackInfo(in);
215 }
216
217 public PackageRollbackInfo[] newArray(int size) {
218 return new PackageRollbackInfo[size];
219 }
220 };
221}