• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 android.annotation.Nullable;
20 import android.content.pm.SuspendDialogInfo;
21 import android.os.BaseBundle;
22 import android.os.PersistableBundle;
23 import android.util.Slog;
24 import android.util.TypedXmlPullParser;
25 import android.util.TypedXmlSerializer;
26 
27 import org.xmlpull.v1.XmlPullParser;
28 import org.xmlpull.v1.XmlPullParserException;
29 import org.xmlpull.v1.XmlSerializer;
30 
31 import java.io.IOException;
32 import java.util.Objects;
33 
34 /**
35  * Container to describe suspension parameters.
36  * @hide
37  */
38 public final class SuspendParams {
39 
40     private static final String LOG_TAG = "FrameworkPackageUserState";
41     private static final String TAG_DIALOG_INFO = "dialog-info";
42     private static final String TAG_APP_EXTRAS = "app-extras";
43     private static final String TAG_LAUNCHER_EXTRAS = "launcher-extras";
44 
45     private final SuspendDialogInfo dialogInfo;
46     private final PersistableBundle appExtras;
47     private final PersistableBundle launcherExtras;
48 
SuspendParams(SuspendDialogInfo dialogInfo, PersistableBundle appExtras, PersistableBundle launcherExtras)49     public SuspendParams(SuspendDialogInfo dialogInfo, PersistableBundle appExtras,
50             PersistableBundle launcherExtras) {
51         this.dialogInfo = dialogInfo;
52         this.appExtras = appExtras;
53         this.launcherExtras = launcherExtras;
54     }
55 
56     @Override
equals(@ullable Object obj)57     public boolean equals(@Nullable Object obj) {
58         if (this == obj) {
59             return true;
60         }
61         if (!(obj instanceof SuspendParams)) {
62             return false;
63         }
64         final SuspendParams other = (SuspendParams) obj;
65         if (!Objects.equals(dialogInfo, other.dialogInfo)) {
66             return false;
67         }
68         if (!BaseBundle.kindofEquals(appExtras, other.appExtras)) {
69             return false;
70         }
71         if (!BaseBundle.kindofEquals(launcherExtras, other.launcherExtras)) {
72             return false;
73         }
74         return true;
75     }
76 
77     @Override
hashCode()78     public int hashCode() {
79         int hashCode = Objects.hashCode(dialogInfo);
80         hashCode = 31 * hashCode + ((appExtras != null) ? appExtras.size() : 0);
81         hashCode = 31 * hashCode + ((launcherExtras != null) ? launcherExtras.size() : 0);
82         return hashCode;
83     }
84 
85     /**
86      * Serializes this object into an xml format
87      *
88      * @param out the {@link XmlSerializer} object
89      */
saveToXml(TypedXmlSerializer out)90     public void saveToXml(TypedXmlSerializer out) throws IOException {
91         if (dialogInfo != null) {
92             out.startTag(null, TAG_DIALOG_INFO);
93             dialogInfo.saveToXml(out);
94             out.endTag(null, TAG_DIALOG_INFO);
95         }
96         if (appExtras != null) {
97             out.startTag(null, TAG_APP_EXTRAS);
98             try {
99                 appExtras.saveToXml(out);
100             } catch (XmlPullParserException e) {
101                 Slog.e(LOG_TAG, "Exception while trying to write appExtras."
102                         + " Will be lost on reboot", e);
103             }
104             out.endTag(null, TAG_APP_EXTRAS);
105         }
106         if (launcherExtras != null) {
107             out.startTag(null, TAG_LAUNCHER_EXTRAS);
108             try {
109                 launcherExtras.saveToXml(out);
110             } catch (XmlPullParserException e) {
111                 Slog.e(LOG_TAG, "Exception while trying to write launcherExtras."
112                         + " Will be lost on reboot", e);
113             }
114             out.endTag(null, TAG_LAUNCHER_EXTRAS);
115         }
116     }
117 
118     /**
119      * Parses this object from the xml format. Returns {@code null} if no object related
120      * information could be read.
121      *
122      * @param in the reader
123      */
restoreFromXml(TypedXmlPullParser in)124     public static SuspendParams restoreFromXml(TypedXmlPullParser in) throws IOException {
125         SuspendDialogInfo readDialogInfo = null;
126         PersistableBundle readAppExtras = null;
127         PersistableBundle readLauncherExtras = null;
128 
129         final int currentDepth = in.getDepth();
130         int type;
131         try {
132             while ((type = in.next()) != XmlPullParser.END_DOCUMENT
133                     && (type != XmlPullParser.END_TAG
134                     || in.getDepth() > currentDepth)) {
135                 if (type == XmlPullParser.END_TAG
136                         || type == XmlPullParser.TEXT) {
137                     continue;
138                 }
139                 switch (in.getName()) {
140                     case TAG_DIALOG_INFO:
141                         readDialogInfo = SuspendDialogInfo.restoreFromXml(in);
142                         break;
143                     case TAG_APP_EXTRAS:
144                         readAppExtras = PersistableBundle.restoreFromXml(in);
145                         break;
146                     case TAG_LAUNCHER_EXTRAS:
147                         readLauncherExtras = PersistableBundle.restoreFromXml(in);
148                         break;
149                     default:
150                         Slog.w(LOG_TAG, "Unknown tag " + in.getName()
151                                 + " in SuspendParams. Ignoring");
152                         break;
153                 }
154             }
155         } catch (XmlPullParserException e) {
156             Slog.e(LOG_TAG, "Exception while trying to parse SuspendParams,"
157                     + " some fields may default", e);
158         }
159         return new SuspendParams(readDialogInfo, readAppExtras, readLauncherExtras);
160     }
161 
getDialogInfo()162     public SuspendDialogInfo getDialogInfo() {
163         return dialogInfo;
164     }
165 
getAppExtras()166     public PersistableBundle getAppExtras() {
167         return appExtras;
168     }
169 
getLauncherExtras()170     public PersistableBundle getLauncherExtras() {
171         return launcherExtras;
172     }
173 }
174