FragmentCompat.java revision abc968f1eba800c34a4008deb43b015da5d23a5f
1/*
2 * Copyright (C) 2011 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.v13.app;
18
19import android.app.Fragment;
20
21/**
22 * Helper class for accessing newer features of Fragment.
23 */
24public class FragmentCompat {
25    interface FragmentCompatImpl {
26        void setMenuVisibility(Fragment f, boolean visible);
27        void setStartDeferred(Fragment f, boolean deferStart);
28    }
29
30    static class BaseFragmentCompatImpl implements FragmentCompatImpl {
31        public void setMenuVisibility(Fragment f, boolean visible) {
32        }
33        public void setStartDeferred(Fragment f, boolean deferStart) {
34        }
35    }
36
37    static class ICSFragmentCompatImpl extends BaseFragmentCompatImpl {
38        public void setMenuVisibility(Fragment f, boolean visible) {
39            FragmentCompatICS.setMenuVisibility(f, visible);
40        }
41    }
42
43    static class ICSMR1FragmentCompatImpl extends ICSFragmentCompatImpl {
44        public void setStartDeferred(Fragment f, boolean deferStart) {
45            FragmentCompatICSMR1.setStartDeferred(f, deferStart);
46        }
47    }
48
49    static final FragmentCompatImpl IMPL;
50    static {
51        if (android.os.Build.VERSION.SDK_INT >= 15) {
52            IMPL = new ICSMR1FragmentCompatImpl();
53        } else if (android.os.Build.VERSION.SDK_INT >= 14) {
54            IMPL = new ICSFragmentCompatImpl();
55        } else {
56            IMPL = new BaseFragmentCompatImpl();
57        }
58    }
59
60    /**
61     * Call {@link Fragment#setMenuVisibility(boolean) Fragment.setMenuVisibility(boolean)}
62     * if running on an appropriate version of the platform.
63     */
64    public static void setMenuVisibility(Fragment f, boolean visible) {
65        IMPL.setMenuVisibility(f, visible);
66    }
67
68    /**
69     * Call {@link Fragment#setStartDeferred(boolean) setStartDeferred(boolean)}
70     * if running on an appropriate version of the platform.
71     */
72    public static void setStartDeferred(Fragment f, boolean deferStart) {
73        IMPL.setStartDeferred(f, deferStart);
74    }
75}
76