1 /*
2 * Copyright (C) 2013 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 package com.example.android.basicimmersivemode;
17 
18 import android.os.Bundle;
19 import android.support.v4.app.Fragment;
20 import android.view.MenuItem;
21 import android.view.View;
22 
23 import com.example.android.common.logger.Log;
24 
25 public class BasicImmersiveModeFragment extends Fragment {
26 
27     public static final String TAG = "BasicImmersiveModeFragment";
28 
29     @Override
onCreate(Bundle savedInstanceState)30     public void onCreate(Bundle savedInstanceState) {
31         super.onCreate(savedInstanceState);
32         setHasOptionsMenu(true);
33     }
34 
35     @Override
onActivityCreated(Bundle savedInstanceState)36     public void onActivityCreated(Bundle savedInstanceState) {
37         super.onActivityCreated(savedInstanceState);
38         final View decorView = getActivity().getWindow().getDecorView();
39         decorView.setOnSystemUiVisibilityChangeListener(
40                 new View.OnSystemUiVisibilityChangeListener() {
41                     @Override
42                     public void onSystemUiVisibilityChange(int i) {
43                         int height = decorView.getHeight();
44                         Log.i(TAG, "Current height: " + height);
45                     }
46                 });
47     }
48 
49     @Override
onOptionsItemSelected(MenuItem item)50     public boolean onOptionsItemSelected(MenuItem item) {
51         if (item.getItemId() == R.id.sample_action) {
52             toggleHideyBar();
53         }
54         return true;
55     }
56 
57     /**
58      * Detects and toggles immersive mode.
59      */
toggleHideyBar()60     public void toggleHideyBar() {
61         // BEGIN_INCLUDE (get_current_ui_flags)
62         // The UI options currently enabled are represented by a bitfield.
63         // getSystemUiVisibility() gives us that bitfield.
64         int uiOptions = getActivity().getWindow().getDecorView().getSystemUiVisibility();
65         int newUiOptions = uiOptions;
66         // END_INCLUDE (get_current_ui_flags)
67         // BEGIN_INCLUDE (toggle_ui_flags)
68         boolean isImmersiveModeEnabled =
69                 ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
70         if (isImmersiveModeEnabled) {
71             Log.i(TAG, "Turning immersive mode mode off. ");
72         } else {
73             Log.i(TAG, "Turning immersive mode mode on.");
74         }
75 
76         // Immersive mode: Backward compatible to KitKat (API 19).
77         // Note that this flag doesn't do anything by itself, it only augments the behavior
78         // of HIDE_NAVIGATION and FLAG_FULLSCREEN.  For the purposes of this sample
79         // all three flags are being toggled together.
80         // This sample uses the "sticky" form of immersive mode, which will let the user swipe
81         // the bars back in again, but will automatically make them disappear a few seconds later.
82         newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
83         newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
84         newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
85         getActivity().getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
86         //END_INCLUDE (set_ui_flags)
87     }
88 }
89