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