DragAndDropPermissionsCompat.java revision 9dede51868bbbe16aadcd65e04860bea8ea50e05
1/*
2 * Copyright (C) 2016 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 androidx.core.view;
18
19import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
20
21import android.app.Activity;
22import android.os.Build;
23import android.view.DragAndDropPermissions;
24import android.view.DragEvent;
25
26import androidx.annotation.Nullable;
27import androidx.annotation.RestrictTo;
28
29/**
30 * Helper for accessing features in {@link android.view.DragAndDropPermissions} a backwards
31 * compatible fashion.
32 */
33public final class DragAndDropPermissionsCompat {
34    private Object mDragAndDropPermissions;
35
36    private DragAndDropPermissionsCompat(Object dragAndDropPermissions) {
37        mDragAndDropPermissions = dragAndDropPermissions;
38    }
39
40    /** @hide */
41    @RestrictTo(LIBRARY_GROUP)
42    @Nullable
43    public static DragAndDropPermissionsCompat request(Activity activity, DragEvent dragEvent) {
44        if (Build.VERSION.SDK_INT >= 24) {
45            DragAndDropPermissions dragAndDropPermissions =
46                    activity.requestDragAndDropPermissions(dragEvent);
47            if (dragAndDropPermissions != null) {
48                return new DragAndDropPermissionsCompat(dragAndDropPermissions);
49            }
50        }
51        return null;
52    }
53
54    /**
55     * Revoke the permission grant explicitly.
56     */
57    public void release() {
58        if (Build.VERSION.SDK_INT >= 24) {
59            ((DragAndDropPermissions) mDragAndDropPermissions).release();
60        }
61    }
62}
63