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 46 /** 47 * This activity demonstrates how to use system UI flags to implement 48 * a video player style of UI (where the navigation bar should be hidden 49 * when the user isn't interacting with the screen to achieve full screen 50 * video playback). 51 */ 52 public class VideoPlayerActivity extends Activity 53 implements OnQueryTextListener, ActionBar.TabListener { 54 55 /** 56 * Implementation of a view for displaying full-screen video playback, 57 * using system UI flags to transition in and out of modes where the entire 58 * screen can be filled with content (at the expense of no user interaction). 59 */ 60 //BEGIN_INCLUDE(content) 61 public static class Content extends ImageView implements 62 View.OnSystemUiVisibilityChangeListener, View.OnClickListener, 63 ActionBar.OnMenuVisibilityListener { 64 Activity mActivity; 65 TextView mTitleView; 66 Button mPlayButton; 67 SeekBar mSeekView; 68 boolean mAddedMenuListener; 69 boolean mMenusOpen; 70 boolean mPaused; 71 boolean mNavVisible; 72 int mLastSystemUiVis; 73 74 Runnable mNavHider = new Runnable() { 75 @Override public void run() { 76 setNavVisibility(false); 77 } 78 }; 79 Content(Context context, AttributeSet attrs)80 public Content(Context context, AttributeSet attrs) { 81 super(context, attrs); 82 setOnSystemUiVisibilityChangeListener(this); 83 setOnClickListener(this); 84 } 85 init(Activity activity, TextView title, Button playButton, SeekBar seek)86 public void init(Activity activity, TextView title, Button playButton, 87 SeekBar seek) { 88 // This called by the containing activity to supply the surrounding 89 // state of the video player that it will interact with. 90 mActivity = activity; 91 mTitleView = title; 92 mPlayButton = playButton; 93 mSeekView = seek; 94 mPlayButton.setOnClickListener(this); 95 setPlayPaused(true); 96 } 97 onAttachedToWindow()98 @Override protected void onAttachedToWindow() { 99 super.onAttachedToWindow(); 100 if (mActivity != null) { 101 mAddedMenuListener = true; 102 mActivity.getActionBar().addOnMenuVisibilityListener(this); 103 } 104 } 105 onDetachedFromWindow()106 @Override protected void onDetachedFromWindow() { 107 super.onDetachedFromWindow(); 108 if (mAddedMenuListener) { 109 mActivity.getActionBar().removeOnMenuVisibilityListener(this); 110 } 111 } 112 onSystemUiVisibilityChange(int visibility)113 @Override public void onSystemUiVisibilityChange(int visibility) { 114 // Detect when we go out of nav-hidden mode, to clear our state 115 // back to having the full UI chrome up. Only do this when 116 // the state is changing and nav is no longer hidden. 117 int diff = mLastSystemUiVis ^ visibility; 118 mLastSystemUiVis = visibility; 119 if ((diff&SYSTEM_UI_FLAG_HIDE_NAVIGATION) != 0 120 && (visibility&SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) { 121 setNavVisibility(true); 122 } 123 } 124 onWindowVisibilityChanged(int visibility)125 @Override protected void onWindowVisibilityChanged(int visibility) { 126 super.onWindowVisibilityChanged(visibility); 127 128 // When we become visible or invisible, play is paused. 129 setPlayPaused(true); 130 } 131 onClick(View v)132 @Override public void onClick(View v) { 133 if (v == mPlayButton) { 134 // Clicking on the play/pause button toggles its state. 135 setPlayPaused(!mPaused); 136 } else { 137 // Clicking elsewhere makes the navigation visible. 138 setNavVisibility(true); 139 } 140 } 141 onMenuVisibilityChanged(boolean isVisible)142 @Override public void onMenuVisibilityChanged(boolean isVisible) { 143 mMenusOpen = isVisible; 144 setNavVisibility(true); 145 } 146 setPlayPaused(boolean paused)147 void setPlayPaused(boolean paused) { 148 mPaused = paused; 149 mPlayButton.setText(paused ? R.string.play : R.string.pause); 150 setKeepScreenOn(!paused); 151 setNavVisibility(true); 152 } 153 setNavVisibility(boolean visible)154 void setNavVisibility(boolean visible) { 155 int newVis = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 156 | SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 157 | SYSTEM_UI_FLAG_LAYOUT_STABLE; 158 if (!visible) { 159 newVis |= SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_FULLSCREEN 160 | SYSTEM_UI_FLAG_HIDE_NAVIGATION; 161 } 162 163 // If we are now visible, schedule a timer for us to go invisible. 164 if (visible) { 165 Handler h = getHandler(); 166 if (h != null) { 167 h.removeCallbacks(mNavHider); 168 if (!mMenusOpen && !mPaused) { 169 // If the menus are open or play is paused, we will not auto-hide. 170 h.postDelayed(mNavHider, 3000); 171 } 172 } 173 } 174 175 // Set the new desired visibility. 176 setSystemUiVisibility(newVis); 177 mTitleView.setVisibility(visible ? VISIBLE : INVISIBLE); 178 mPlayButton.setVisibility(visible ? VISIBLE : INVISIBLE); 179 mSeekView.setVisibility(visible ? VISIBLE : INVISIBLE); 180 } 181 } 182 //END_INCLUDE(content) 183 184 Content mContent; 185 VideoPlayerActivity()186 public VideoPlayerActivity() { 187 } 188 189 @Override onCreate(Bundle savedInstanceState)190 public void onCreate(Bundle savedInstanceState) { 191 super.onCreate(savedInstanceState); 192 193 getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); 194 195 setContentView(R.layout.video_player); 196 mContent = (Content)findViewById(R.id.content); 197 mContent.init(this, (TextView)findViewById(R.id.title), 198 (Button)findViewById(R.id.play), 199 (SeekBar)findViewById(R.id.seekbar)); 200 201 ActionBar bar = getActionBar(); 202 bar.addTab(bar.newTab().setText("Tab 1").setTabListener(this)); 203 bar.addTab(bar.newTab().setText("Tab 2").setTabListener(this)); 204 bar.addTab(bar.newTab().setText("Tab 3").setTabListener(this)); 205 } 206 207 @Override onCreateOptionsMenu(Menu menu)208 public boolean onCreateOptionsMenu(Menu menu) { 209 MenuInflater inflater = getMenuInflater(); 210 inflater.inflate(R.menu.content_actions, menu); 211 SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); 212 searchView.setOnQueryTextListener(this); 213 214 // Set file with share history to the provider and set the share intent. 215 MenuItem actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar); 216 ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider(); 217 actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME); 218 // Note that you can set/change the intent any time, 219 // say when the user has selected an image. 220 Intent shareIntent = new Intent(Intent.ACTION_SEND); 221 shareIntent.setType("image/*"); 222 Uri uri = Uri.fromFile(getFileStreamPath("shared.png")); 223 shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 224 actionProvider.setShareIntent(shareIntent); 225 return true; 226 } 227 228 @Override onAttachedToWindow()229 public void onAttachedToWindow() { 230 super.onAttachedToWindow(); 231 } 232 233 @Override onResume()234 protected void onResume() { 235 super.onResume(); 236 } 237 238 /** 239 * This method is declared in the menu. 240 */ onSort(MenuItem item)241 public void onSort(MenuItem item) { 242 } 243 244 @Override onOptionsItemSelected(MenuItem item)245 public boolean onOptionsItemSelected(MenuItem item) { 246 switch (item.getItemId()) { 247 case R.id.show_tabs: 248 getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 249 item.setChecked(true); 250 return true; 251 case R.id.hide_tabs: 252 getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 253 item.setChecked(true); 254 return true; 255 } 256 return false; 257 } 258 259 @Override onQueryTextChange(String newText)260 public boolean onQueryTextChange(String newText) { 261 return true; 262 } 263 264 @Override onQueryTextSubmit(String query)265 public boolean onQueryTextSubmit(String query) { 266 Toast.makeText(this, "Searching for: " + query + "...", Toast.LENGTH_SHORT).show(); 267 return true; 268 } 269 270 @Override onTabSelected(Tab tab, FragmentTransaction ft)271 public void onTabSelected(Tab tab, FragmentTransaction ft) { 272 } 273 274 @Override onTabUnselected(Tab tab, FragmentTransaction ft)275 public void onTabUnselected(Tab tab, FragmentTransaction ft) { 276 } 277 278 @Override onTabReselected(Tab tab, FragmentTransaction ft)279 public void onTabReselected(Tab tab, FragmentTransaction ft) { 280 } 281 } 282