1 /*
2  * Copyright (C) 2015 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.car.content.pm;
18 
19 import android.annotation.SystemApi;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import java.lang.reflect.Method;
24 import java.util.Arrays;
25 
26 /**
27  * Contains application blocking policy
28  * @hide
29  */
30 @SystemApi
31 public final class CarAppBlockingPolicy implements Parcelable {
32     private static final String TAG = CarAppBlockingPolicy.class.getSimpleName();
33 
34     public final AppBlockingPackageInfo[] whitelists;
35     public final AppBlockingPackageInfo[] blacklists;
36 
37     private static final Method sReadBlobMethod;
38     private static final Method sWriteBlobMethod;
39 
40     static {
41         Class parcelClass = Parcel.class;
42         Method readBlob = null;
43         Method writeBlob = null;
44         try {
45             readBlob = parcelClass.getMethod("readBlob", new Class[] {});
46             writeBlob = parcelClass.getMethod("writeBlob", new Class[] {byte[].class});
47         } catch (NoSuchMethodException e) {
48             // use it only when both methods are available.
49             readBlob = null;
50             writeBlob = null;
51         }
52         sReadBlobMethod = readBlob;
53         sWriteBlobMethod = writeBlob;
54     }
55 
CarAppBlockingPolicy(AppBlockingPackageInfo[] whitelists, AppBlockingPackageInfo[] blacklists)56     public CarAppBlockingPolicy(AppBlockingPackageInfo[] whitelists,
57             AppBlockingPackageInfo[] blacklists) {
58         this.whitelists = whitelists;
59         this.blacklists = blacklists;
60     }
61 
CarAppBlockingPolicy(Parcel in)62     public CarAppBlockingPolicy(Parcel in) {
63         byte[] payload =  in.readBlob();
64         Parcel payloadParcel = Parcel.obtain();
65         payloadParcel.unmarshall(payload, 0, payload.length);
66         // reset to initial position to read
67         payloadParcel.setDataPosition(0);
68         whitelists = payloadParcel.createTypedArray(AppBlockingPackageInfo.CREATOR);
69         blacklists = payloadParcel.createTypedArray(AppBlockingPackageInfo.CREATOR);
70         payloadParcel.recycle();
71     }
72 
73     @Override
describeContents()74     public int describeContents() {
75         return 0;
76     }
77 
78     @Override
writeToParcel(Parcel dest, int flags)79     public void writeToParcel(Parcel dest, int flags) {
80         Parcel payloadParcel = Parcel.obtain();
81         payloadParcel.writeTypedArray(whitelists, 0);
82         payloadParcel.writeTypedArray(blacklists, 0);
83         byte[] payload = payloadParcel.marshall();
84         dest.writeBlob(payload);
85         payloadParcel.recycle();
86     }
87 
88     public static final Parcelable.Creator<CarAppBlockingPolicy> CREATOR =
89             new Parcelable.Creator<CarAppBlockingPolicy>() {
90 
91                 @Override
92                 public CarAppBlockingPolicy createFromParcel(Parcel in) {
93                     return new CarAppBlockingPolicy(in);
94                 }
95 
96                 @Override
97                 public CarAppBlockingPolicy[] newArray(int size) {
98                     return new CarAppBlockingPolicy[size];
99                 }
100             };
101 
102     @Override
hashCode()103     public int hashCode() {
104         final int prime = 31;
105         int result = 1;
106         result = prime * result + Arrays.hashCode(blacklists);
107         result = prime * result + Arrays.hashCode(whitelists);
108         return result;
109     }
110 
111     @Override
equals(Object obj)112     public boolean equals(Object obj) {
113         if (this == obj) {
114             return true;
115         }
116         if (obj == null) {
117             return false;
118         }
119         if (getClass() != obj.getClass()) {
120             return false;
121         }
122         CarAppBlockingPolicy other = (CarAppBlockingPolicy) obj;
123         if (!Arrays.equals(blacklists, other.blacklists)) {
124             return false;
125         }
126         if (!Arrays.equals(whitelists, other.whitelists)) {
127             return false;
128         }
129         return true;
130     }
131 
132     @Override
toString()133     public String toString() {
134         return "CarAppBlockingPolicy [whitelists=" + Arrays.toString(whitelists) + ", blacklists="
135                 + Arrays.toString(blacklists) + "]";
136     }
137 }
138