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 17 package android.support.v13.view; 18 19 import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP; 20 21 import android.app.Activity; 22 import android.os.Build; 23 import android.support.annotation.RequiresApi; 24 import android.support.annotation.RestrictTo; 25 import android.view.DragAndDropPermissions; 26 import android.view.DragEvent; 27 28 /** 29 * Helper for accessing features in {@link android.view.DragAndDropPermissions} 30 * introduced after API level 13 in a backwards compatible fashion. 31 */ 32 public final class DragAndDropPermissionsCompat { 33 34 interface DragAndDropPermissionsCompatImpl { request(Activity activity, DragEvent dragEvent)35 Object request(Activity activity, DragEvent dragEvent); release(Object dragAndDropPermissions)36 void release(Object dragAndDropPermissions); 37 } 38 39 static class BaseDragAndDropPermissionsCompatImpl implements DragAndDropPermissionsCompatImpl { 40 @Override request(Activity activity, DragEvent dragEvent)41 public Object request(Activity activity, DragEvent dragEvent) { 42 return null; 43 } 44 45 @Override release(Object dragAndDropPermissions)46 public void release(Object dragAndDropPermissions) { 47 // no-op 48 } 49 } 50 51 @RequiresApi(24) 52 static class Api24DragAndDropPermissionsCompatImpl 53 extends BaseDragAndDropPermissionsCompatImpl { 54 @Override request(Activity activity, DragEvent dragEvent)55 public Object request(Activity activity, DragEvent dragEvent) { 56 return activity.requestDragAndDropPermissions(dragEvent); 57 } 58 59 @Override release(Object dragAndDropPermissions)60 public void release(Object dragAndDropPermissions) { 61 ((DragAndDropPermissions) dragAndDropPermissions).release(); 62 } 63 } 64 65 private static DragAndDropPermissionsCompatImpl IMPL; 66 static { 67 if (Build.VERSION.SDK_INT >= 24) { 68 IMPL = new Api24DragAndDropPermissionsCompatImpl(); 69 } else { 70 IMPL = new BaseDragAndDropPermissionsCompatImpl(); 71 } 72 } 73 74 private Object mDragAndDropPermissions; 75 DragAndDropPermissionsCompat(Object dragAndDropPermissions)76 private DragAndDropPermissionsCompat(Object dragAndDropPermissions) { 77 mDragAndDropPermissions = dragAndDropPermissions; 78 } 79 80 /** @hide */ 81 @RestrictTo(LIBRARY_GROUP) request(Activity activity, DragEvent dragEvent)82 public static DragAndDropPermissionsCompat request(Activity activity, DragEvent dragEvent) { 83 Object dragAndDropPermissions = IMPL.request(activity, dragEvent); 84 if (dragAndDropPermissions != null) { 85 return new DragAndDropPermissionsCompat(dragAndDropPermissions); 86 } 87 return null; 88 } 89 90 /* 91 * Revoke the permission grant explicitly. 92 */ release()93 public void release() { 94 IMPL.release(mDragAndDropPermissions); 95 } 96 } 97