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
17package android.support.v4.app;
18
19import android.app.Activity;
20import android.app.SharedElementCallback;
21import android.content.Context;
22import android.graphics.Matrix;
23import android.graphics.RectF;
24import android.os.Parcelable;
25import android.view.View;
26
27import java.util.List;
28import java.util.Map;
29
30class ActivityCompatApi23 {
31    public interface OnSharedElementsReadyListenerBridge {
32        void onSharedElementsReady();
33    }
34
35    public interface RequestPermissionsRequestCodeValidator {
36        void validateRequestPermissionsRequestCode(int requestCode);
37    }
38
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
48    public static boolean shouldShowRequestPermissionRationale(Activity activity,
49            String permission) {
50        return activity.shouldShowRequestPermissionRationale(permission);
51    }
52
53    public static void setEnterSharedElementCallback(Activity activity,
54            SharedElementCallback23 callback) {
55        activity.setEnterSharedElementCallback(createCallback(callback));
56    }
57
58    public static void setExitSharedElementCallback(Activity activity,
59            SharedElementCallback23 callback) {
60        activity.setExitSharedElementCallback(createCallback(callback));
61    }
62
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 {
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
80        public SharedElementCallbackImpl(SharedElementCallback23 callback) {
81            mCallback = callback;
82        }
83
84        @Override
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
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
99        public void onRejectSharedElements(List<View> rejectedSharedElements) {
100            mCallback.onRejectSharedElements(rejectedSharedElements);
101        }
102
103        @Override
104        public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
105            mCallback.onMapSharedElements(names, sharedElements);
106        }
107
108        @Override
109        public Parcelable onCaptureSharedElementSnapshot(View sharedElement,
110                Matrix viewToGlobalMatrix, RectF screenBounds) {
111            return mCallback.onCaptureSharedElementSnapshot(sharedElement, viewToGlobalMatrix,
112                            screenBounds);
113        }
114
115        @Override
116        public View onCreateSnapshotView(Context context, Parcelable snapshot) {
117            return mCallback.onCreateSnapshotView(context, snapshot);
118        }
119
120        @Override
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