1/*
2 * Copyright (C) 2017 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.transition;
18
19import android.annotation.SuppressLint;
20import android.support.annotation.RequiresApi;
21import android.util.Log;
22import android.view.View;
23
24import java.lang.reflect.InvocationTargetException;
25import java.lang.reflect.Method;
26
27@RequiresApi(22)
28class ViewUtilsApi22 extends ViewUtilsApi21 {
29
30    private static final String TAG = "ViewUtilsApi22";
31
32    private static Method sSetLeftTopRightBottomMethod;
33    private static boolean sSetLeftTopRightBottomMethodFetched;
34
35    @Override
36    public void setLeftTopRightBottom(View v, int left, int top, int right, int bottom) {
37        fetchSetLeftTopRightBottomMethod();
38        if (sSetLeftTopRightBottomMethod != null) {
39            try {
40                sSetLeftTopRightBottomMethod.invoke(v, left, top, right, bottom);
41            } catch (IllegalAccessException e) {
42                // Do nothing
43            } catch (InvocationTargetException e) {
44                throw new RuntimeException(e.getCause());
45            }
46        }
47    }
48
49    @SuppressLint("PrivateApi")
50    private void fetchSetLeftTopRightBottomMethod() {
51        if (!sSetLeftTopRightBottomMethodFetched) {
52            try {
53                sSetLeftTopRightBottomMethod = View.class.getDeclaredMethod("setLeftTopRightBottom",
54                        int.class, int.class, int.class, int.class);
55                sSetLeftTopRightBottomMethod.setAccessible(true);
56            } catch (NoSuchMethodException e) {
57                Log.i(TAG, "Failed to retrieve setLeftTopRightBottom method", e);
58            }
59            sSetLeftTopRightBottomMethodFetched = true;
60        }
61    }
62
63}
64
65