1/*
2 * Copyright (C) 2013 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;
18
19import android.net.Uri;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23/**
24 * Description of a single Uri permission grant. This grants may have been
25 * created via {@link Intent#FLAG_GRANT_READ_URI_PERMISSION}, etc when sending
26 * an {@link Intent}, or explicitly through
27 * {@link Context#grantUriPermission(String, android.net.Uri, int)}.
28 *
29 * @see ContentResolver#getPersistedUriPermissions()
30 */
31public final class UriPermission implements Parcelable {
32    private final Uri mUri;
33    private final int mModeFlags;
34    private final long mPersistedTime;
35
36    /**
37     * Value returned when a permission has not been persisted.
38     */
39    public static final long INVALID_TIME = Long.MIN_VALUE;
40
41    /** {@hide} */
42    public UriPermission(Uri uri, int modeFlags, long persistedTime) {
43        mUri = uri;
44        mModeFlags = modeFlags;
45        mPersistedTime = persistedTime;
46    }
47
48    /** {@hide} */
49    public UriPermission(Parcel in) {
50        mUri = in.readParcelable(null);
51        mModeFlags = in.readInt();
52        mPersistedTime = in.readLong();
53    }
54
55    /**
56     * Return the Uri this permission pertains to.
57     */
58    public Uri getUri() {
59        return mUri;
60    }
61
62    /**
63     * Returns if this permission offers read access.
64     */
65    public boolean isReadPermission() {
66        return (mModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0;
67    }
68
69    /**
70     * Returns if this permission offers write access.
71     */
72    public boolean isWritePermission() {
73        return (mModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0;
74    }
75
76    /**
77     * Return the time when this permission was first persisted, in milliseconds
78     * since January 1, 1970 00:00:00.0 UTC. Returns {@link #INVALID_TIME} if
79     * not persisted.
80     *
81     * @see ContentResolver#takePersistableUriPermission(Uri, int)
82     * @see System#currentTimeMillis()
83     */
84    public long getPersistedTime() {
85        return mPersistedTime;
86    }
87
88    @Override
89    public String toString() {
90        return "UriPermission {uri=" + mUri + ", modeFlags=" + mModeFlags + ", persistedTime="
91                + mPersistedTime + "}";
92    }
93
94    @Override
95    public int describeContents() {
96        return 0;
97    }
98
99    @Override
100    public void writeToParcel(Parcel dest, int flags) {
101        dest.writeParcelable(mUri, flags);
102        dest.writeInt(mModeFlags);
103        dest.writeLong(mPersistedTime);
104    }
105
106    public static final Creator<UriPermission> CREATOR = new Creator<UriPermission>() {
107        @Override
108        public UriPermission createFromParcel(Parcel source) {
109            return new UriPermission(source);
110        }
111
112        @Override
113        public UriPermission[] newArray(int size) {
114            return new UriPermission[size];
115        }
116    };
117}
118