1/*
2 * Copyright (C) 2012 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.pm;
18
19import android.content.pm.ManifestDigest;
20import android.net.Uri;
21import android.os.Parcel;
22import android.os.Parcelable;
23
24/**
25 * Represents verification parameters used to verify packages to be installed.
26 *
27 * @hide
28 */
29public class VerificationParams implements Parcelable {
30    /** A constant used to indicate that a uid value is not present. */
31    public static final int NO_UID = -1;
32
33    /** What we print out first when toString() is called. */
34    private static final String TO_STRING_PREFIX = "VerificationParams{";
35
36    /** The location of the supplementary verification file. */
37    private final Uri mVerificationURI;
38
39    /** URI referencing where the package was downloaded from. */
40    private final Uri mOriginatingURI;
41
42    /** HTTP referrer URI associated with the originatingURI. */
43    private final Uri mReferrer;
44
45    /** UID of the application that the install request originated from. */
46    private final int mOriginatingUid;
47
48    /** UID of application requesting the install */
49    private int mInstallerUid;
50
51    /**
52     * An object that holds the digest of the package which can be used to
53     * verify ownership.
54     */
55    private final ManifestDigest mManifestDigest;
56
57    /**
58     * Creates verification specifications for installing with application verification.
59     *
60     * @param verificationURI The location of the supplementary verification
61     *            file. This can be a 'file:' or a 'content:' URI. May be {@code null}.
62     * @param originatingURI URI referencing where the package was downloaded
63     *            from. May be {@code null}.
64     * @param referrer HTTP referrer URI associated with the originatingURI.
65     *            May be {@code null}.
66     * @param originatingUid UID of the application that the install request originated
67     *            from, or NO_UID if not present
68     * @param manifestDigest an object that holds the digest of the package
69     *            which can be used to verify ownership. May be {@code null}.
70     */
71    public VerificationParams(Uri verificationURI, Uri originatingURI, Uri referrer,
72            int originatingUid, ManifestDigest manifestDigest) {
73        mVerificationURI = verificationURI;
74        mOriginatingURI = originatingURI;
75        mReferrer = referrer;
76        mOriginatingUid = originatingUid;
77        mManifestDigest = manifestDigest;
78        mInstallerUid = NO_UID;
79    }
80
81    public Uri getVerificationURI() {
82        return mVerificationURI;
83    }
84
85    public Uri getOriginatingURI() {
86        return mOriginatingURI;
87    }
88
89    public Uri getReferrer() {
90        return mReferrer;
91    }
92
93    /** return NO_UID if not available */
94    public int getOriginatingUid() {
95        return mOriginatingUid;
96    }
97
98    public ManifestDigest getManifestDigest() {
99        return mManifestDigest;
100    }
101
102    /** @return NO_UID when not set */
103    public int getInstallerUid() {
104        return mInstallerUid;
105    }
106
107    public void setInstallerUid(int uid) {
108        mInstallerUid = uid;
109    }
110
111    @Override
112    public int describeContents() {
113        return 0;
114    }
115
116    @Override
117    public boolean equals(Object o) {
118        if (this == o) {
119            return true;
120        }
121
122        if (!(o instanceof VerificationParams)) {
123            return false;
124        }
125
126        final VerificationParams other = (VerificationParams) o;
127
128        if (mVerificationURI == null) {
129            if (other.mVerificationURI != null) {
130                return false;
131            }
132        } else if (!mVerificationURI.equals(other.mVerificationURI)) {
133            return false;
134        }
135
136        if (mOriginatingURI == null) {
137            if (other.mOriginatingURI != null) {
138                return false;
139            }
140        } else if (!mOriginatingURI.equals(other.mOriginatingURI)) {
141            return false;
142        }
143
144        if (mReferrer == null) {
145            if (other.mReferrer != null) {
146                return false;
147            }
148        } else if (!mReferrer.equals(other.mReferrer)) {
149            return false;
150        }
151
152        if (mOriginatingUid != other.mOriginatingUid) {
153            return false;
154        }
155
156        if (mManifestDigest == null) {
157            if (other.mManifestDigest != null) {
158                return false;
159            }
160        } else if (!mManifestDigest.equals(other.mManifestDigest)) {
161            return false;
162        }
163
164        if (mInstallerUid != other.mInstallerUid) {
165            return false;
166        }
167
168        return true;
169    }
170
171    @Override
172    public int hashCode() {
173        int hash = 3;
174
175        hash += 5 * (mVerificationURI == null ? 1 : mVerificationURI.hashCode());
176        hash += 7 * (mOriginatingURI == null ? 1 : mOriginatingURI.hashCode());
177        hash += 11 * (mReferrer == null ? 1 : mReferrer.hashCode());
178        hash += 13 * mOriginatingUid;
179        hash += 17 * (mManifestDigest == null ? 1 : mManifestDigest.hashCode());
180        hash += 19 * mInstallerUid;
181
182        return hash;
183    }
184
185    @Override
186    public String toString() {
187        final StringBuilder sb = new StringBuilder(TO_STRING_PREFIX);
188
189        sb.append("mVerificationURI=");
190        sb.append(mVerificationURI.toString());
191        sb.append(",mOriginatingURI=");
192        sb.append(mOriginatingURI.toString());
193        sb.append(",mReferrer=");
194        sb.append(mReferrer.toString());
195        sb.append(",mOriginatingUid=");
196        sb.append(mOriginatingUid);
197        sb.append(",mManifestDigest=");
198        sb.append(mManifestDigest.toString());
199        sb.append(",mInstallerUid=");
200        sb.append(mInstallerUid);
201        sb.append('}');
202
203        return sb.toString();
204    }
205
206    @Override
207    public void writeToParcel(Parcel dest, int flags) {
208        dest.writeParcelable(mVerificationURI, 0);
209        dest.writeParcelable(mOriginatingURI, 0);
210        dest.writeParcelable(mReferrer, 0);
211        dest.writeInt(mOriginatingUid);
212        dest.writeParcelable(mManifestDigest, 0);
213        dest.writeInt(mInstallerUid);
214    }
215
216
217    private VerificationParams(Parcel source) {
218        mVerificationURI = source.readParcelable(Uri.class.getClassLoader());
219        mOriginatingURI = source.readParcelable(Uri.class.getClassLoader());
220        mReferrer = source.readParcelable(Uri.class.getClassLoader());
221        mOriginatingUid = source.readInt();
222        mManifestDigest = source.readParcelable(ManifestDigest.class.getClassLoader());
223        mInstallerUid = source.readInt();
224    }
225
226    public static final Parcelable.Creator<VerificationParams> CREATOR =
227            new Parcelable.Creator<VerificationParams>() {
228        public VerificationParams createFromParcel(Parcel source) {
229                return new VerificationParams(source);
230        }
231
232        public VerificationParams[] newArray(int size) {
233            return new VerificationParams[size];
234        }
235    };
236}
237