blob: c198e1f3dd84b99d7f1238c5690a6db54599c42f [file] [log] [blame]
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -08001/*
2** Copyright 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
17package android.view;
18
Yorke Leec8ad0cd2016-06-20 15:41:35 -070019import android.app.Activity;
Vladislav Kaznacheev87c60d62016-07-21 15:25:12 -070020import android.os.Binder;
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080021import android.os.IBinder;
Yorke Leec8ad0cd2016-06-20 15:41:35 -070022import android.os.Parcel;
23import android.os.Parcelable;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080024import android.os.RemoteException;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080025
Yorke Leec8ad0cd2016-06-20 15:41:35 -070026import com.android.internal.view.IDragAndDropPermissions;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080027
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080028/**
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070029 * {@link DragAndDropPermissions} controls the access permissions for the content URIs associated
30 * with a {@link DragEvent}.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080031 * <p>
32 * Permission are granted when this object is created by {@link
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070033 * android.app.Activity#requestDragAndDropPermissions(DragEvent)
34 * Activity.requestDragAndDropPermissions}.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080035 * Which permissions are granted is defined by the set of flags passed to {@link
36 * View#startDragAndDrop(android.content.ClipData, View.DragShadowBuilder, Object, int)
37 * View.startDragAndDrop} by the app that started the drag operation.
Yorke Leec8ad0cd2016-06-20 15:41:35 -070038 * </p>
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080039 * <p>
40 * The life cycle of the permissions is bound to the activity used to call {@link
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070041 * android.app.Activity#requestDragAndDropPermissions(DragEvent) requestDragAndDropPermissions}. The
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080042 * permissions are revoked when this activity is destroyed, or when {@link #release()} is called,
43 * whichever occurs first.
Yorke Leec8ad0cd2016-06-20 15:41:35 -070044 * </p>
45 * <p>
46 * If you anticipate that your application will receive a large number of drops (e.g. document
47 * editor), you should try to call {@link #release()} on the obtained permissions as soon as they
48 * are no longer required. Permissions can be added to your activity's
49 * {@link Activity#onSaveInstanceState} bundle and later retrieved in order to manually release
50 * the permissions once they are no longer needed.
51 * </p>
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080052 */
Yorke Leec8ad0cd2016-06-20 15:41:35 -070053public final class DragAndDropPermissions implements Parcelable {
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080054
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070055 private final IDragAndDropPermissions mDragAndDropPermissions;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080056
Vladislav Kaznacheev87c60d62016-07-21 15:25:12 -070057 private IBinder mTransientToken;
Vladislav Kaznacheevc14df8e2016-01-22 11:49:13 -080058
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080059 /**
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070060 * Create a new {@link DragAndDropPermissions} object to control the access permissions for
61 * content URIs associated with {@link DragEvent}.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080062 * @param dragEvent Drag event
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070063 * @return {@link DragAndDropPermissions} object or null if there are no content URIs associated
64 * with the {@link DragEvent}.
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080065 * @hide
66 */
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070067 public static DragAndDropPermissions obtain(DragEvent dragEvent) {
68 if (dragEvent.getDragAndDropPermissions() == null) {
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080069 return null;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080070 }
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070071 return new DragAndDropPermissions(dragEvent.getDragAndDropPermissions());
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080072 }
73
74 /** @hide */
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070075 private DragAndDropPermissions(IDragAndDropPermissions dragAndDropPermissions) {
76 mDragAndDropPermissions = dragAndDropPermissions;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080077 }
78
79 /**
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080080 * Take the permissions and bind their lifetime to the activity.
81 * @param activityToken Binder pointing to an Activity instance to bind the lifetime to.
82 * @return True if permissions are successfully taken.
83 * @hide
84 */
85 public boolean take(IBinder activityToken) {
86 try {
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070087 mDragAndDropPermissions.take(activityToken);
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080088 } catch (RemoteException e) {
89 return false;
90 }
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080091 return true;
92 }
93
94 /**
Vladislav Kaznacheevc14df8e2016-01-22 11:49:13 -080095 * Take the permissions. Must call {@link #release} explicitly.
96 * @return True if permissions are successfully taken.
97 * @hide
98 */
99 public boolean takeTransient() {
100 try {
Vladislav Kaznacheev87c60d62016-07-21 15:25:12 -0700101 mTransientToken = new Binder();
102 mDragAndDropPermissions.takeTransient(mTransientToken);
Vladislav Kaznacheevc14df8e2016-01-22 11:49:13 -0800103 } catch (RemoteException e) {
104 return false;
105 }
Vladislav Kaznacheevc14df8e2016-01-22 11:49:13 -0800106 return true;
107 }
108
109 /**
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -0800110 * Revoke permissions explicitly.
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -0800111 */
112 public void release() {
113 try {
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700114 mDragAndDropPermissions.release();
Vladislav Kaznacheev87c60d62016-07-21 15:25:12 -0700115 mTransientToken = null;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -0800116 } catch (RemoteException e) {
117 }
Yorke Leec8ad0cd2016-06-20 15:41:35 -0700118 }
119
120 public static final Parcelable.Creator<DragAndDropPermissions> CREATOR =
121 new Parcelable.Creator<DragAndDropPermissions> () {
122 @Override
123 public DragAndDropPermissions createFromParcel(Parcel source) {
124 return new DragAndDropPermissions(source);
125 }
126
127 @Override
128 public DragAndDropPermissions[] newArray(int size) {
129 return new DragAndDropPermissions[size];
130 }
131 };
132
133 @Override
134 public int describeContents() {
135 return 0;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -0800136 }
137
138 @Override
Yorke Leec8ad0cd2016-06-20 15:41:35 -0700139 public void writeToParcel(Parcel destination, int flags) {
140 destination.writeStrongInterface(mDragAndDropPermissions);
Vladislav Kaznacheev87c60d62016-07-21 15:25:12 -0700141 destination.writeStrongBinder(mTransientToken);
Yorke Leec8ad0cd2016-06-20 15:41:35 -0700142 }
143
144 private DragAndDropPermissions(Parcel in) {
145 mDragAndDropPermissions = IDragAndDropPermissions.Stub.asInterface(in.readStrongBinder());
Vladislav Kaznacheev87c60d62016-07-21 15:25:12 -0700146 mTransientToken = in.readStrongBinder();
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -0800147 }
148}