Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view; |
| 18 | |
Yorke Lee | c8ad0cd | 2016-06-20 15:41:35 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
Vladislav Kaznacheev | 87c60d6 | 2016-07-21 15:25:12 -0700 | [diff] [blame] | 20 | import android.os.Binder; |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 21 | import android.os.IBinder; |
Yorke Lee | c8ad0cd | 2016-06-20 15:41:35 -0700 | [diff] [blame] | 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 24 | import android.os.RemoteException; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 25 | |
Yorke Lee | c8ad0cd | 2016-06-20 15:41:35 -0700 | [diff] [blame] | 26 | import com.android.internal.view.IDragAndDropPermissions; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 27 | |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 28 | /** |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 29 | * {@link DragAndDropPermissions} controls the access permissions for the content URIs associated |
| 30 | * with a {@link DragEvent}. |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 31 | * <p> |
| 32 | * Permission are granted when this object is created by {@link |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 33 | * android.app.Activity#requestDragAndDropPermissions(DragEvent) |
| 34 | * Activity.requestDragAndDropPermissions}. |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 35 | * 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 Lee | c8ad0cd | 2016-06-20 15:41:35 -0700 | [diff] [blame] | 38 | * </p> |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 39 | * <p> |
Kevin Hufnagle | 36ef697 | 2019-09-27 23:51:27 +0000 | [diff] [blame] | 40 | * The lifecycle of the permissions is bound to the activity used to call {@link |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 41 | * android.app.Activity#requestDragAndDropPermissions(DragEvent) requestDragAndDropPermissions}. The |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 42 | * permissions are revoked when this activity is destroyed, or when {@link #release()} is called, |
| 43 | * whichever occurs first. |
Yorke Lee | c8ad0cd | 2016-06-20 15:41:35 -0700 | [diff] [blame] | 44 | * </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 Hufnagle | 36ef697 | 2019-09-27 23:51:27 +0000 | [diff] [blame] | 52 | * <p> |
| 53 | * Learn more about <a href="/guide/topics/ui/drag-drop#DragPermissionsMultiWindow">drag permissions |
| 54 | * in multi-window mode</a>. |
| 55 | * </p> |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 56 | */ |
Yorke Lee | c8ad0cd | 2016-06-20 15:41:35 -0700 | [diff] [blame] | 57 | public final class DragAndDropPermissions implements Parcelable { |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 58 | |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 59 | private final IDragAndDropPermissions mDragAndDropPermissions; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 60 | |
Vladislav Kaznacheev | 87c60d6 | 2016-07-21 15:25:12 -0700 | [diff] [blame] | 61 | private IBinder mTransientToken; |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 62 | |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 63 | /** |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 64 | * Create a new {@link DragAndDropPermissions} object to control the access permissions for |
| 65 | * content URIs associated with {@link DragEvent}. |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 66 | * @param dragEvent Drag event |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 67 | * @return {@link DragAndDropPermissions} object or null if there are no content URIs associated |
| 68 | * with the {@link DragEvent}. |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 69 | * @hide |
| 70 | */ |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 71 | public static DragAndDropPermissions obtain(DragEvent dragEvent) { |
| 72 | if (dragEvent.getDragAndDropPermissions() == null) { |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 73 | return null; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 74 | } |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 75 | return new DragAndDropPermissions(dragEvent.getDragAndDropPermissions()); |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** @hide */ |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 79 | private DragAndDropPermissions(IDragAndDropPermissions dragAndDropPermissions) { |
| 80 | mDragAndDropPermissions = dragAndDropPermissions; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 84 | * 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 Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 91 | mDragAndDropPermissions.take(activityToken); |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 92 | } catch (RemoteException e) { |
| 93 | return false; |
| 94 | } |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 95 | return true; |
| 96 | } |
| 97 | |
| 98 | /** |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 99 | * 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 Kaznacheev | 87c60d6 | 2016-07-21 15:25:12 -0700 | [diff] [blame] | 105 | mTransientToken = new Binder(); |
| 106 | mDragAndDropPermissions.takeTransient(mTransientToken); |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 107 | } catch (RemoteException e) { |
| 108 | return false; |
| 109 | } |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 110 | return true; |
| 111 | } |
| 112 | |
| 113 | /** |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 114 | * Revoke permissions explicitly. |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 115 | */ |
| 116 | public void release() { |
| 117 | try { |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 118 | mDragAndDropPermissions.release(); |
Vladislav Kaznacheev | 87c60d6 | 2016-07-21 15:25:12 -0700 | [diff] [blame] | 119 | mTransientToken = null; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 120 | } catch (RemoteException e) { |
| 121 | } |
Yorke Lee | c8ad0cd | 2016-06-20 15:41:35 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 124 | public static final @android.annotation.NonNull Parcelable.Creator<DragAndDropPermissions> CREATOR = |
Yorke Lee | c8ad0cd | 2016-06-20 15:41:35 -0700 | [diff] [blame] | 125 | 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 Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
Yorke Lee | c8ad0cd | 2016-06-20 15:41:35 -0700 | [diff] [blame] | 143 | public void writeToParcel(Parcel destination, int flags) { |
| 144 | destination.writeStrongInterface(mDragAndDropPermissions); |
Vladislav Kaznacheev | 87c60d6 | 2016-07-21 15:25:12 -0700 | [diff] [blame] | 145 | destination.writeStrongBinder(mTransientToken); |
Yorke Lee | c8ad0cd | 2016-06-20 15:41:35 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | private DragAndDropPermissions(Parcel in) { |
| 149 | mDragAndDropPermissions = IDragAndDropPermissions.Stub.asInterface(in.readStrongBinder()); |
Vladislav Kaznacheev | 87c60d6 | 2016-07-21 15:25:12 -0700 | [diff] [blame] | 150 | mTransientToken = in.readStrongBinder(); |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 151 | } |
| 152 | } |