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> |
| 40 | * The life cycle 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> |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 52 | */ |
Yorke Lee | c8ad0cd | 2016-06-20 15:41:35 -0700 | [diff] [blame] | 53 | public final class DragAndDropPermissions implements Parcelable { |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 54 | |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 55 | private final IDragAndDropPermissions mDragAndDropPermissions; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 56 | |
Vladislav Kaznacheev | 87c60d6 | 2016-07-21 15:25:12 -0700 | [diff] [blame] | 57 | private IBinder mTransientToken; |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 58 | |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 59 | /** |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 60 | * Create a new {@link DragAndDropPermissions} object to control the access permissions for |
| 61 | * content URIs associated with {@link DragEvent}. |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 62 | * @param dragEvent Drag event |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 63 | * @return {@link DragAndDropPermissions} object or null if there are no content URIs associated |
| 64 | * with the {@link DragEvent}. |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 65 | * @hide |
| 66 | */ |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 67 | public static DragAndDropPermissions obtain(DragEvent dragEvent) { |
| 68 | if (dragEvent.getDragAndDropPermissions() == null) { |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 69 | return null; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 70 | } |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 71 | return new DragAndDropPermissions(dragEvent.getDragAndDropPermissions()); |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** @hide */ |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 75 | private DragAndDropPermissions(IDragAndDropPermissions dragAndDropPermissions) { |
| 76 | mDragAndDropPermissions = dragAndDropPermissions; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 80 | * 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 Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 87 | mDragAndDropPermissions.take(activityToken); |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 88 | } catch (RemoteException e) { |
| 89 | return false; |
| 90 | } |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 91 | return true; |
| 92 | } |
| 93 | |
| 94 | /** |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 95 | * 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 Kaznacheev | 87c60d6 | 2016-07-21 15:25:12 -0700 | [diff] [blame] | 101 | mTransientToken = new Binder(); |
| 102 | mDragAndDropPermissions.takeTransient(mTransientToken); |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 103 | } catch (RemoteException e) { |
| 104 | return false; |
| 105 | } |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | |
| 109 | /** |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 110 | * Revoke permissions explicitly. |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 111 | */ |
| 112 | public void release() { |
| 113 | try { |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 114 | mDragAndDropPermissions.release(); |
Vladislav Kaznacheev | 87c60d6 | 2016-07-21 15:25:12 -0700 | [diff] [blame] | 115 | mTransientToken = null; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 116 | } catch (RemoteException e) { |
| 117 | } |
Yorke Lee | c8ad0cd | 2016-06-20 15:41:35 -0700 | [diff] [blame] | 118 | } |
| 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 Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
Yorke Lee | c8ad0cd | 2016-06-20 15:41:35 -0700 | [diff] [blame] | 139 | public void writeToParcel(Parcel destination, int flags) { |
| 140 | destination.writeStrongInterface(mDragAndDropPermissions); |
Vladislav Kaznacheev | 87c60d6 | 2016-07-21 15:25:12 -0700 | [diff] [blame] | 141 | destination.writeStrongBinder(mTransientToken); |
Yorke Lee | c8ad0cd | 2016-06-20 15:41:35 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | private DragAndDropPermissions(Parcel in) { |
| 145 | mDragAndDropPermissions = IDragAndDropPermissions.Stub.asInterface(in.readStrongBinder()); |
Vladislav Kaznacheev | 87c60d6 | 2016-07-21 15:25:12 -0700 | [diff] [blame] | 146 | mTransientToken = in.readStrongBinder(); |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 147 | } |
| 148 | } |