1/*
2 * Copyright (C) 2017 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.telephony.mbms;
18
19import android.annotation.SystemApi;
20import android.annotation.TestApi;
21import android.content.ContentResolver;
22import android.net.Uri;
23import android.os.Parcel;
24import android.os.Parcelable;
25import android.telephony.mbms.vendor.VendorUtils;
26
27/**
28 * Wrapper for a pair of {@link Uri}s that describe a temp file used by the middleware to
29 * download files via cell-broadcast.
30 * @hide
31 */
32@SystemApi
33@TestApi
34public final class UriPathPair implements Parcelable {
35    private final Uri mFilePathUri;
36    private final Uri mContentUri;
37
38    /** @hide */
39    public UriPathPair(Uri fileUri, Uri contentUri) {
40        if (fileUri == null || !ContentResolver.SCHEME_FILE.equals(fileUri.getScheme())) {
41            throw new IllegalArgumentException("File URI must have file scheme");
42        }
43        if (contentUri == null || !ContentResolver.SCHEME_CONTENT.equals(contentUri.getScheme())) {
44            throw new IllegalArgumentException("Content URI must have content scheme");
45        }
46
47        mFilePathUri = fileUri;
48        mContentUri = contentUri;
49    }
50
51    /** @hide */
52    private UriPathPair(Parcel in) {
53        mFilePathUri = in.readParcelable(Uri.class.getClassLoader());
54        mContentUri = in.readParcelable(Uri.class.getClassLoader());
55    }
56
57    public static final Creator<UriPathPair> CREATOR = new Creator<UriPathPair>() {
58        @Override
59        public UriPathPair createFromParcel(Parcel in) {
60            return new UriPathPair(in);
61        }
62
63        @Override
64        public UriPathPair[] newArray(int size) {
65            return new UriPathPair[size];
66        }
67    };
68
69    /**
70     * Returns the file-path {@link Uri}. This has scheme {@code file} and points to the actual
71     * location on disk where the temp file resides. Use this when sending {@link Uri}s back to the
72     * app in the intents in {@link VendorUtils}.
73     * @return A {@code file} {@link Uri}.
74     */
75    public Uri getFilePathUri() {
76        return mFilePathUri;
77    }
78
79    /**
80     * Returns the content {@link Uri} that may be used with
81     * {@link ContentResolver#openFileDescriptor(Uri, String)} to obtain a
82     * {@link android.os.ParcelFileDescriptor} to a temp file to write to. This {@link Uri} will
83     * expire if the middleware process dies.
84     * @return A {@code content} {@link Uri}
85     */
86    public Uri getContentUri() {
87        return mContentUri;
88    }
89
90    @Override
91    public int describeContents() {
92        return 0;
93    }
94
95    @Override
96    public void writeToParcel(Parcel dest, int flags) {
97        dest.writeParcelable(mFilePathUri, flags);
98        dest.writeParcelable(mContentUri, flags);
99    }
100}
101