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 android.content.pm.parsing.component;
18 
19 import static android.content.pm.parsing.ParsingPackageImpl.sForInternedString;
20 
21 import android.annotation.Nullable;
22 import android.content.ComponentName;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 import android.text.TextUtils;
26 
27 import com.android.internal.util.DataClass;
28 import com.android.internal.util.Parcelling.BuiltIn.ForInternedString;
29 
30 /** @hide **/
31 public class ParsedService extends ParsedMainComponent {
32 
33     int foregroundServiceType;
34     @Nullable
35     @DataClass.ParcelWith(ForInternedString.class)
36     private String permission;
37 
ParsedService(ParsedService other)38     public ParsedService(ParsedService other) {
39         super(other);
40         this.foregroundServiceType = other.foregroundServiceType;
41         this.permission = other.permission;
42     }
43 
setPermission(String permission)44     public ParsedMainComponent setPermission(String permission) {
45         // Empty string must be converted to null
46         this.permission = TextUtils.isEmpty(permission) ? null : permission.intern();
47         return this;
48     }
49 
toString()50     public String toString() {
51         StringBuilder sb = new StringBuilder(128);
52         sb.append("Service{");
53         sb.append(Integer.toHexString(System.identityHashCode(this)));
54         sb.append(' ');
55         ComponentName.appendShortString(sb, getPackageName(), getName());
56         sb.append('}');
57         return sb.toString();
58     }
59 
60     @Override
describeContents()61     public int describeContents() {
62         return 0;
63     }
64 
65     @Override
writeToParcel(Parcel dest, int flags)66     public void writeToParcel(Parcel dest, int flags) {
67         super.writeToParcel(dest, flags);
68         dest.writeInt(this.foregroundServiceType);
69         sForInternedString.parcel(this.permission, dest, flags);
70     }
71 
ParsedService()72     public ParsedService() {
73     }
74 
ParsedService(Parcel in)75     protected ParsedService(Parcel in) {
76         super(in);
77         this.foregroundServiceType = in.readInt();
78         this.permission = sForInternedString.unparcel(in);
79     }
80 
81     public static final Parcelable.Creator<ParsedService> CREATOR = new Creator<ParsedService>() {
82         @Override
83         public ParsedService createFromParcel(Parcel source) {
84             return new ParsedService(source);
85         }
86 
87         @Override
88         public ParsedService[] newArray(int size) {
89             return new ParsedService[size];
90         }
91     };
92 
getForegroundServiceType()93     public int getForegroundServiceType() {
94         return foregroundServiceType;
95     }
96 
97     @Nullable
getPermission()98     public String getPermission() {
99         return permission;
100     }
101 }
102