blob: d47604d6ec417995183037fa9e5d562d8fd2cf6a [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>
Kevin Hufnagle36ef6972019-09-27 23:51:27 +000040 * The lifecycle 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>
Kevin Hufnagle36ef6972019-09-27 23:51:27 +000052 * <p>
53 * Learn more about <a href="/guide/topics/ui/drag-drop#DragPermissionsMultiWindow">drag permissions
54 * in multi-window mode</a>.
55 * </p>
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080056 */
Yorke Leec8ad0cd2016-06-20 15:41:35 -070057public final class DragAndDropPermissions implements Parcelable {
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080058
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070059 private final IDragAndDropPermissions mDragAndDropPermissions;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080060
Vladislav Kaznacheev87c60d62016-07-21 15:25:12 -070061 private IBinder mTransientToken;
Vladislav Kaznacheevc14df8e2016-01-22 11:49:13 -080062
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080063 /**
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070064 * Create a new {@link DragAndDropPermissions} object to control the access permissions for
65 * content URIs associated with {@link DragEvent}.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080066 * @param dragEvent Drag event
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070067 * @return {@link DragAndDropPermissions} object or null if there are no content URIs associated
68 * with the {@link DragEvent}.
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080069 * @hide
70 */
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070071 public static DragAndDropPermissions obtain(DragEvent dragEvent) {
72 if (dragEvent.getDragAndDropPermissions() == null) {
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080073 return null;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080074 }
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070075 return new DragAndDropPermissions(dragEvent.getDragAndDropPermissions());
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080076 }
77
78 /** @hide */
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070079 private DragAndDropPermissions(IDragAndDropPermissions dragAndDropPermissions) {
80 mDragAndDropPermissions = dragAndDropPermissions;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080081 }
82
83 /**
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080084 * Take the permissions and bind their lifetime to the activity.
85 * @param activityToken Binder pointing to an Activity instance to bind the lifetime to.
86 * @return True if permissions are successfully taken.
87 * @hide
88 */
89 public boolean take(IBinder activityToken) {
90 try {
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070091 mDragAndDropPermissions.take(activityToken);
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080092 } catch (RemoteException e) {
93 return false;
94 }
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080095 return true;
96 }
97
98 /**
Vladislav Kaznacheevc14df8e2016-01-22 11:49:13 -080099 * Take the permissions. Must call {@link #release} explicitly.
100 * @return True if permissions are successfully taken.
101 * @hide
102 */
103 public boolean takeTransient() {
104 try {
Vladislav Kaznacheev87c60d62016-07-21 15:25:12 -0700105 mTransientToken = new Binder();
106 mDragAndDropPermissions.takeTransient(mTransientToken);
Vladislav Kaznacheevc14df8e2016-01-22 11:49:13 -0800107 } catch (RemoteException e) {
108 return false;
109 }
Vladislav Kaznacheevc14df8e2016-01-22 11:49:13 -0800110 return true;
111 }
112
113 /**
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -0800114 * Revoke permissions explicitly.
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -0800115 */
116 public void release() {
117 try {
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700118 mDragAndDropPermissions.release();
Vladislav Kaznacheev87c60d62016-07-21 15:25:12 -0700119 mTransientToken = null;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -0800120 } catch (RemoteException e) {
121 }
Yorke Leec8ad0cd2016-06-20 15:41:35 -0700122 }
123
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700124 public static final @android.annotation.NonNull Parcelable.Creator<DragAndDropPermissions> CREATOR =
Yorke Leec8ad0cd2016-06-20 15:41:35 -0700125 new Parcelable.Creator<DragAndDropPermissions> () {
126 @Override
127 public DragAndDropPermissions createFromParcel(Parcel source) {
128 return new DragAndDropPermissions(source);
129 }
130
131 @Override
132 public DragAndDropPermissions[] newArray(int size) {
133 return new DragAndDropPermissions[size];
134 }
135 };
136
137 @Override
138 public int describeContents() {
139 return 0;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -0800140 }
141
142 @Override
Yorke Leec8ad0cd2016-06-20 15:41:35 -0700143 public void writeToParcel(Parcel destination, int flags) {
144 destination.writeStrongInterface(mDragAndDropPermissions);
Vladislav Kaznacheev87c60d62016-07-21 15:25:12 -0700145 destination.writeStrongBinder(mTransientToken);
Yorke Leec8ad0cd2016-06-20 15:41:35 -0700146 }
147
148 private DragAndDropPermissions(Parcel in) {
149 mDragAndDropPermissions = IDragAndDropPermissions.Stub.asInterface(in.readStrongBinder());
Vladislav Kaznacheev87c60d62016-07-21 15:25:12 -0700150 mTransientToken = in.readStrongBinder();
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -0800151 }
152}