1 /* 2 * Copyright (C) 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.support.v4.app; 18 19 import android.app.Activity; 20 import android.app.SharedElementCallback; 21 import android.content.Context; 22 import android.graphics.Matrix; 23 import android.graphics.RectF; 24 import android.os.Parcelable; 25 import android.view.View; 26 27 import java.util.List; 28 import java.util.Map; 29 30 class ActivityCompatApi23 { 31 public interface OnSharedElementsReadyListenerBridge { onSharedElementsReady()32 void onSharedElementsReady(); 33 } 34 35 public interface RequestPermissionsRequestCodeValidator { validateRequestPermissionsRequestCode(int requestCode)36 void validateRequestPermissionsRequestCode(int requestCode); 37 } 38 requestPermissions(Activity activity, String[] permissions, int requestCode)39 public static void requestPermissions(Activity activity, String[] permissions, 40 int requestCode) { 41 if (activity instanceof RequestPermissionsRequestCodeValidator) { 42 ((RequestPermissionsRequestCodeValidator) activity) 43 .validateRequestPermissionsRequestCode(requestCode); 44 } 45 activity.requestPermissions(permissions, requestCode); 46 } 47 shouldShowRequestPermissionRationale(Activity activity, String permission)48 public static boolean shouldShowRequestPermissionRationale(Activity activity, 49 String permission) { 50 return activity.shouldShowRequestPermissionRationale(permission); 51 } 52 setEnterSharedElementCallback(Activity activity, SharedElementCallback23 callback)53 public static void setEnterSharedElementCallback(Activity activity, 54 SharedElementCallback23 callback) { 55 activity.setEnterSharedElementCallback(createCallback(callback)); 56 } 57 setExitSharedElementCallback(Activity activity, SharedElementCallback23 callback)58 public static void setExitSharedElementCallback(Activity activity, 59 SharedElementCallback23 callback) { 60 activity.setExitSharedElementCallback(createCallback(callback)); 61 } 62 createCallback(SharedElementCallback23 callback)63 private static SharedElementCallback createCallback(SharedElementCallback23 callback) { 64 SharedElementCallback newListener = null; 65 if (callback != null) { 66 newListener = new SharedElementCallbackImpl(callback); 67 } 68 return newListener; 69 } 70 71 public abstract static class SharedElementCallback23 72 extends ActivityCompatApi21.SharedElementCallback21 { onSharedElementsArrived(List<String> sharedElementNames, List<View> sharedElements, OnSharedElementsReadyListenerBridge listener)73 public abstract void onSharedElementsArrived(List<String> sharedElementNames, 74 List<View> sharedElements, OnSharedElementsReadyListenerBridge listener); 75 } 76 77 private static class SharedElementCallbackImpl extends SharedElementCallback { 78 private SharedElementCallback23 mCallback; 79 SharedElementCallbackImpl(SharedElementCallback23 callback)80 public SharedElementCallbackImpl(SharedElementCallback23 callback) { 81 mCallback = callback; 82 } 83 84 @Override onSharedElementStart(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots)85 public void onSharedElementStart(List<String> sharedElementNames, 86 List<View> sharedElements, List<View> sharedElementSnapshots) { 87 mCallback.onSharedElementStart(sharedElementNames, sharedElements, 88 sharedElementSnapshots); 89 } 90 91 @Override onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots)92 public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, 93 List<View> sharedElementSnapshots) { 94 mCallback.onSharedElementEnd(sharedElementNames, sharedElements, 95 sharedElementSnapshots); 96 } 97 98 @Override onRejectSharedElements(List<View> rejectedSharedElements)99 public void onRejectSharedElements(List<View> rejectedSharedElements) { 100 mCallback.onRejectSharedElements(rejectedSharedElements); 101 } 102 103 @Override onMapSharedElements(List<String> names, Map<String, View> sharedElements)104 public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) { 105 mCallback.onMapSharedElements(names, sharedElements); 106 } 107 108 @Override onCaptureSharedElementSnapshot(View sharedElement, Matrix viewToGlobalMatrix, RectF screenBounds)109 public Parcelable onCaptureSharedElementSnapshot(View sharedElement, 110 Matrix viewToGlobalMatrix, RectF screenBounds) { 111 return mCallback.onCaptureSharedElementSnapshot(sharedElement, viewToGlobalMatrix, 112 screenBounds); 113 } 114 115 @Override onCreateSnapshotView(Context context, Parcelable snapshot)116 public View onCreateSnapshotView(Context context, Parcelable snapshot) { 117 return mCallback.onCreateSnapshotView(context, snapshot); 118 } 119 120 @Override onSharedElementsArrived(List<String> sharedElementNames, List<View> sharedElements, final OnSharedElementsReadyListener listener)121 public void onSharedElementsArrived(List<String> sharedElementNames, 122 List<View> sharedElements, final OnSharedElementsReadyListener listener) { 123 mCallback.onSharedElementsArrived(sharedElementNames, sharedElements, 124 new OnSharedElementsReadyListenerBridge() { 125 @Override 126 public void onSharedElementsReady() { 127 listener.onSharedElementsReady(); 128 } 129 }); 130 } 131 } 132 } 133