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 | |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 19 | import android.app.ActivityManagerNative; |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 20 | import android.os.IBinder; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 21 | import android.os.RemoteException; |
| 22 | import com.android.internal.view.IDropPermissions; |
| 23 | import dalvik.system.CloseGuard; |
| 24 | |
| 25 | |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 26 | /** |
| 27 | * {@link DropPermissions} controls the access permissions for the content URIs associated with a |
| 28 | * {@link DragEvent}. |
| 29 | * <p> |
| 30 | * Permission are granted when this object is created by {@link |
| 31 | * android.app.Activity#requestDropPermissions(DragEvent) Activity.requestDropPermissions}. |
| 32 | * Which permissions are granted is defined by the set of flags passed to {@link |
| 33 | * View#startDragAndDrop(android.content.ClipData, View.DragShadowBuilder, Object, int) |
| 34 | * View.startDragAndDrop} by the app that started the drag operation. |
| 35 | * <p> |
| 36 | * The life cycle of the permissions is bound to the activity used to call {@link |
| 37 | * android.app.Activity#requestDropPermissions(DragEvent) requestDropPermissions}. The |
| 38 | * permissions are revoked when this activity is destroyed, or when {@link #release()} is called, |
| 39 | * whichever occurs first. |
| 40 | */ |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 41 | public final class DropPermissions { |
| 42 | |
| 43 | private final IDropPermissions mDropPermissions; |
| 44 | |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 45 | private IBinder mPermissionOwnerToken; |
| 46 | |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 47 | private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 48 | |
| 49 | /** |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 50 | * Create a new {@link DropPermissions} object to control the access permissions for content |
| 51 | * URIs associated with {@link DragEvent}. |
| 52 | * @param dragEvent Drag event |
| 53 | * @return {@link DropPermissions} object or null if there are no content URIs associated with |
| 54 | * the {@link DragEvent}. |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 55 | * @hide |
| 56 | */ |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 57 | public static DropPermissions obtain(DragEvent dragEvent) { |
| 58 | if (dragEvent.getDropPermissions() == null) { |
| 59 | return null; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 60 | } |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 61 | return new DropPermissions(dragEvent.getDropPermissions()); |
| 62 | } |
| 63 | |
| 64 | /** @hide */ |
| 65 | private DropPermissions(IDropPermissions dropPermissions) { |
| 66 | mDropPermissions = dropPermissions; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 70 | * Take the permissions and bind their lifetime to the activity. |
| 71 | * @param activityToken Binder pointing to an Activity instance to bind the lifetime to. |
| 72 | * @return True if permissions are successfully taken. |
| 73 | * @hide |
| 74 | */ |
| 75 | public boolean take(IBinder activityToken) { |
| 76 | try { |
| 77 | mDropPermissions.take(activityToken); |
| 78 | } catch (RemoteException e) { |
| 79 | return false; |
| 80 | } |
| 81 | mCloseGuard.open("release"); |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | /** |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 86 | * Take the permissions. Must call {@link #release} explicitly. |
| 87 | * @return True if permissions are successfully taken. |
| 88 | * @hide |
| 89 | */ |
| 90 | public boolean takeTransient() { |
| 91 | try { |
| 92 | mPermissionOwnerToken = ActivityManagerNative.getDefault(). |
| 93 | newUriPermissionOwner("drop"); |
| 94 | mDropPermissions.takeTransient(mPermissionOwnerToken); |
| 95 | } catch (RemoteException e) { |
| 96 | return false; |
| 97 | } |
| 98 | mCloseGuard.open("release"); |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | /** |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 103 | * Revoke permissions explicitly. |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 104 | */ |
| 105 | public void release() { |
| 106 | try { |
| 107 | mDropPermissions.release(); |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 108 | mPermissionOwnerToken = null; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 109 | } catch (RemoteException e) { |
| 110 | } |
| 111 | mCloseGuard.close(); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | protected void finalize() throws Throwable { |
| 116 | try { |
| 117 | if (mCloseGuard != null) { |
| 118 | mCloseGuard.warnIfOpen(); |
| 119 | } |
| 120 | release(); |
| 121 | } finally { |
| 122 | super.finalize(); |
| 123 | } |
| 124 | } |
| 125 | } |