BundleCompat.java revision b31c3281d870e9abb673db239234d580dcc4feff
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 androidx.core.app;
18
19import android.os.Build;
20import android.os.Bundle;
21import android.os.IBinder;
22import androidx.annotation.NonNull;
23import androidx.annotation.Nullable;
24import android.util.Log;
25
26import java.lang.reflect.InvocationTargetException;
27import java.lang.reflect.Method;
28
29/**
30 * Helper for accessing features in {@link Bundle}.
31 */
32public final class BundleCompat {
33
34    static class BundleCompatBaseImpl {
35        private static final String TAG = "BundleCompatBaseImpl";
36
37        private static Method sGetIBinderMethod;
38        private static boolean sGetIBinderMethodFetched;
39
40        private static Method sPutIBinderMethod;
41        private static boolean sPutIBinderMethodFetched;
42
43        public static IBinder getBinder(Bundle bundle, String key) {
44            if (!sGetIBinderMethodFetched) {
45                try {
46                    sGetIBinderMethod = Bundle.class.getMethod("getIBinder", String.class);
47                    sGetIBinderMethod.setAccessible(true);
48                } catch (NoSuchMethodException e) {
49                    Log.i(TAG, "Failed to retrieve getIBinder method", e);
50                }
51                sGetIBinderMethodFetched = true;
52            }
53
54            if (sGetIBinderMethod != null) {
55                try {
56                    return (IBinder) sGetIBinderMethod.invoke(bundle, key);
57                } catch (InvocationTargetException | IllegalAccessException
58                        | IllegalArgumentException e) {
59                    Log.i(TAG, "Failed to invoke getIBinder via reflection", e);
60                    sGetIBinderMethod = null;
61                }
62            }
63            return null;
64        }
65
66        public static void putBinder(Bundle bundle, String key, IBinder binder) {
67            if (!sPutIBinderMethodFetched) {
68                try {
69                    sPutIBinderMethod =
70                            Bundle.class.getMethod("putIBinder", String.class, IBinder.class);
71                    sPutIBinderMethod.setAccessible(true);
72                } catch (NoSuchMethodException e) {
73                    Log.i(TAG, "Failed to retrieve putIBinder method", e);
74                }
75                sPutIBinderMethodFetched = true;
76            }
77
78            if (sPutIBinderMethod != null) {
79                try {
80                    sPutIBinderMethod.invoke(bundle, key, binder);
81                } catch (InvocationTargetException | IllegalAccessException
82                        | IllegalArgumentException e) {
83                    Log.i(TAG, "Failed to invoke putIBinder via reflection", e);
84                    sPutIBinderMethod = null;
85                }
86            }
87        }
88    }
89
90    private BundleCompat() {}
91
92    /**
93     * A convenience method to handle getting an {@link IBinder} inside a {@link Bundle} for all
94     * Android versions.
95     * @param bundle The bundle to get the {@link IBinder}.
96     * @param key    The key to use while getting the {@link IBinder}.
97     * @return       The {@link IBinder} that was obtained.
98     */
99    @Nullable
100    public static IBinder getBinder(@NonNull Bundle bundle, @Nullable String key) {
101        if (Build.VERSION.SDK_INT >= 18) {
102            return bundle.getBinder(key);
103        } else {
104            return BundleCompatBaseImpl.getBinder(bundle, key);
105        }
106    }
107
108    /**
109     * A convenience method to handle putting an {@link IBinder} inside a {@link Bundle} for all
110     * Android versions.
111     * @param bundle The bundle to insert the {@link IBinder}.
112     * @param key    The key to use while putting the {@link IBinder}.
113     * @param binder The {@link IBinder} to put.
114     */
115    public static void putBinder(@NonNull Bundle bundle, @Nullable String key,
116            @Nullable IBinder binder) {
117        if (Build.VERSION.SDK_INT >= 18) {
118            bundle.putBinder(key, binder);
119        } else {
120            BundleCompatBaseImpl.putBinder(bundle, key, binder);
121        }
122    }
123}
124