blob: 5411dad3284a6fe2b36461a4932e7a2606975d64 [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
Vladislav Kaznacheevc14df8e2016-01-22 11:49:13 -080019import android.app.ActivityManagerNative;
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080020import android.os.IBinder;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080021import android.os.RemoteException;
22import com.android.internal.view.IDropPermissions;
23import dalvik.system.CloseGuard;
24
25
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080026/**
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 Kaznacheev9149d2b2015-12-15 12:16:28 -080041public final class DropPermissions {
42
43 private final IDropPermissions mDropPermissions;
44
Vladislav Kaznacheevc14df8e2016-01-22 11:49:13 -080045 private IBinder mPermissionOwnerToken;
46
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080047 private final CloseGuard mCloseGuard = CloseGuard.get();
48
49 /**
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080050 * 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 Kaznacheev9149d2b2015-12-15 12:16:28 -080055 * @hide
56 */
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080057 public static DropPermissions obtain(DragEvent dragEvent) {
58 if (dragEvent.getDropPermissions() == null) {
59 return null;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080060 }
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080061 return new DropPermissions(dragEvent.getDropPermissions());
62 }
63
64 /** @hide */
65 private DropPermissions(IDropPermissions dropPermissions) {
66 mDropPermissions = dropPermissions;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080067 }
68
69 /**
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080070 * 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 Kaznacheevc14df8e2016-01-22 11:49:13 -080086 * 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 Kaznacheevb23a7572015-12-18 12:23:43 -0800103 * Revoke permissions explicitly.
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -0800104 */
105 public void release() {
106 try {
107 mDropPermissions.release();
Vladislav Kaznacheevc14df8e2016-01-22 11:49:13 -0800108 mPermissionOwnerToken = null;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -0800109 } 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}