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 for accessing features in {@link Fragment} introduced after
23 * API level 13 in a backwards compatible fashion.
24 */
25public class FragmentCompat {
26    interface FragmentCompatImpl {
27        void setMenuVisibility(Fragment f, boolean visible);
28        void setUserVisibleHint(Fragment f, boolean deferStart);
29    }
30
31    static class BaseFragmentCompatImpl implements FragmentCompatImpl {
32        public void setMenuVisibility(Fragment f, boolean visible) {
33        }
34        public void setUserVisibleHint(Fragment f, boolean deferStart) {
35        }
36    }
37
38    static class ICSFragmentCompatImpl extends BaseFragmentCompatImpl {
39        @Override
40        public void setMenuVisibility(Fragment f, boolean visible) {
41            FragmentCompatICS.setMenuVisibility(f, visible);
42        }
43    }
44
45    static class ICSMR1FragmentCompatImpl extends ICSFragmentCompatImpl {
46        @Override
47        public void setUserVisibleHint(Fragment f, boolean deferStart) {
48            FragmentCompatICSMR1.setUserVisibleHint(f, deferStart);
49        }
50    }
51
52    static final FragmentCompatImpl IMPL;
53    static {
54        if (android.os.Build.VERSION.SDK_INT >= 15) {
55            IMPL = new ICSMR1FragmentCompatImpl();
56        } else if (android.os.Build.VERSION.SDK_INT >= 14) {
57            IMPL = new ICSFragmentCompatImpl();
58        } else {
59            IMPL = new BaseFragmentCompatImpl();
60        }
61    }
62
63    /**
64     * Call {@link Fragment#setMenuVisibility(boolean) Fragment.setMenuVisibility(boolean)}
65     * if running on an appropriate version of the platform.
66     */
67    public static void setMenuVisibility(Fragment f, boolean visible) {
68        IMPL.setMenuVisibility(f, visible);
69    }
70
71    /**
72     * Call {@link Fragment#setUserVisibleHint(boolean) setUserVisibleHint(boolean)}
73     * if running on an appropriate version of the platform.
74     */
75    public static void setUserVisibleHint(Fragment f, boolean deferStart) {
76        IMPL.setUserVisibleHint(f, deferStart);
77    }
78}
79