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 17 package com.example.android.apis.view; 18 19 import android.app.ActionBar; 20 import android.app.ActionBar.Tab; 21 import android.app.Activity; 22 import android.app.FragmentTransaction; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.net.Uri; 26 import android.os.Bundle; 27 import android.os.Handler; 28 import android.util.AttributeSet; 29 import android.util.Log; 30 import android.view.Menu; 31 import android.view.MenuInflater; 32 import android.view.MenuItem; 33 import android.view.View; 34 import android.view.Window; 35 import android.widget.Button; 36 import android.widget.ImageView; 37 import android.widget.SearchView; 38 import android.widget.SeekBar; 39 import android.widget.ShareActionProvider; 40 import android.widget.TextView; 41 import android.widget.Toast; 42 import android.widget.SearchView.OnQueryTextListener; 43 44 import com.example.android.apis.R; 45 import com.example.android.apis.graphics.TouchPaint; 46 47 /** 48 * This activity demonstrates how to use the system UI flags to 49 * implement an immersive game. 50 */ 51 public class GameActivity extends Activity { 52 53 /** 54 * Implementation of a view for the game, filling the entire screen. 55 */ 56 //BEGIN_INCLUDE(content) 57 public static class Content extends TouchPaint.PaintView implements 58 View.OnSystemUiVisibilityChangeListener, View.OnClickListener { 59 Activity mActivity; 60 Button mPlayButton; 61 boolean mPaused; 62 int mLastSystemUiVis; 63 boolean mUpdateSystemUi; 64 65 Runnable mFader = new Runnable() { 66 @Override public void run() { 67 fade(); 68 if (mUpdateSystemUi) { 69 updateNavVisibility(); 70 } 71 if (!mPaused) { 72 getHandler().postDelayed(mFader, 1000/30); 73 } 74 } 75 }; 76 Content(Context context, AttributeSet attrs)77 public Content(Context context, AttributeSet attrs) { 78 super(context, attrs); 79 setOnSystemUiVisibilityChangeListener(this); 80 } 81 init(Activity activity, Button playButton)82 public void init(Activity activity, Button playButton) { 83 // This called by the containing activity to supply the surrounding 84 // state of the game that it will interact with. 85 mActivity = activity; 86 mPlayButton = playButton; 87 mPlayButton.setOnClickListener(this); 88 setGamePaused(true); 89 } 90 onSystemUiVisibilityChange(int visibility)91 @Override public void onSystemUiVisibilityChange(int visibility) { 92 // Detect when we go out of nav-hidden mode, to reset back to having 93 // it hidden; our game wants those elements to stay hidden as long 94 // as it is being played and stay shown when paused. 95 int diff = mLastSystemUiVis ^ visibility; 96 mLastSystemUiVis = visibility; 97 if (!mPaused && (diff&SYSTEM_UI_FLAG_HIDE_NAVIGATION) != 0 98 && (visibility&SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) { 99 // We are running and the system UI navigation has become 100 // shown... we want it to remain hidden, so update our system 101 // UI state at the next game loop. 102 mUpdateSystemUi = true; 103 } 104 } 105 onWindowVisibilityChanged(int visibility)106 @Override protected void onWindowVisibilityChanged(int visibility) { 107 super.onWindowVisibilityChanged(visibility); 108 109 // When we become visible or invisible, play is paused. 110 setGamePaused(true); 111 } 112 113 @Override onWindowFocusChanged(boolean hasWindowFocus)114 public void onWindowFocusChanged(boolean hasWindowFocus) { 115 super.onWindowFocusChanged(hasWindowFocus); 116 117 // When we become visible or invisible, play is paused. 118 // Optional: pause game when window loses focus. This will cause it to 119 // pause, for example, when the notification shade is pulled down. 120 if (!hasWindowFocus) { 121 //setGamePaused(true); 122 } 123 } 124 onClick(View v)125 @Override public void onClick(View v) { 126 if (v == mPlayButton) { 127 // Clicking on the play/pause button toggles its state. 128 setGamePaused(!mPaused); 129 } 130 } 131 setGamePaused(boolean paused)132 void setGamePaused(boolean paused) { 133 mPaused = paused; 134 mPlayButton.setText(paused ? R.string.play : R.string.pause); 135 setKeepScreenOn(!paused); 136 updateNavVisibility(); 137 Handler h = getHandler(); 138 if (h != null) { 139 getHandler().removeCallbacks(mFader); 140 if (!paused) { 141 mFader.run(); 142 text("Draw!"); 143 } 144 } 145 } 146 updateNavVisibility()147 void updateNavVisibility() { 148 int newVis = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 149 | SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 150 | SYSTEM_UI_FLAG_LAYOUT_STABLE; 151 if (!mPaused) { 152 newVis |= SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_FULLSCREEN 153 | SYSTEM_UI_FLAG_HIDE_NAVIGATION | SYSTEM_UI_FLAG_IMMERSIVE_STICKY; 154 } 155 156 // Set the new desired visibility. 157 setSystemUiVisibility(newVis); 158 mUpdateSystemUi = false; 159 } 160 } 161 //END_INCLUDE(content) 162 163 Content mContent; 164 GameActivity()165 public GameActivity() { 166 } 167 168 @Override onCreate(Bundle savedInstanceState)169 public void onCreate(Bundle savedInstanceState) { 170 super.onCreate(savedInstanceState); 171 172 setContentView(R.layout.game); 173 mContent = (Content)findViewById(R.id.content); 174 mContent.init(this, (Button)findViewById(R.id.play)); 175 } 176 177 @Override onAttachedToWindow()178 public void onAttachedToWindow() { 179 super.onAttachedToWindow(); 180 } 181 182 @Override onPause()183 protected void onPause() { 184 super.onPause(); 185 186 // Pause game when its activity is paused. 187 mContent.setGamePaused(true); 188 } 189 } 190