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 
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.TypedValue;
30 import android.view.Menu;
31 import android.view.MenuInflater;
32 import android.view.MenuItem;
33 import android.view.View;
34 import android.view.ViewGroup;
35 import android.view.Window;
36 import android.widget.Button;
37 import android.widget.ScrollView;
38 import android.widget.SearchView;
39 import android.widget.SeekBar;
40 import android.widget.ShareActionProvider;
41 import android.widget.TextView;
42 import android.widget.Toast;
43 import android.widget.SearchView.OnQueryTextListener;
44 
45 import com.example.android.apis.R;
46 
47 /**
48  * This activity demonstrates how to use system UI flags to implement
49  * a content browser style of UI (such as a book reader) that hides the
50  * nav bar as well as the status bar.
51  */
52 public class ContentBrowserNavActivity extends Activity
53         implements OnQueryTextListener, ActionBar.TabListener {
54 
55     /**
56      * Implementation of a view for displaying immersive content, using system UI
57      * flags to transition in and out of modes where the user is focused on that
58      * content.
59      */
60 //BEGIN_INCLUDE(content)
61     public static class Content extends ScrollView
62             implements View.OnSystemUiVisibilityChangeListener, View.OnClickListener {
63         TextView mText;
64         TextView mTitleView;
65         SeekBar mSeekView;
66         boolean mNavVisible;
67         int mBaseSystemUiVisibility = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
68                 | SYSTEM_UI_FLAG_LAYOUT_STABLE | SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
69         int mLastSystemUiVis;
70 
71         Runnable mNavHider = new Runnable() {
72             @Override public void run() {
73                 setNavVisibility(false);
74             }
75         };
76 
Content(Context context, AttributeSet attrs)77         public Content(Context context, AttributeSet attrs) {
78             super(context, attrs);
79 
80             mText = new TextView(context);
81             mText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
82             mText.setText(context.getString(R.string.alert_dialog_two_buttons2ultra_msg));
83             mText.setClickable(false);
84             mText.setOnClickListener(this);
85             mText.setTextIsSelectable(true);
86             addView(mText, new ViewGroup.LayoutParams(
87                     ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
88 
89             setOnSystemUiVisibilityChangeListener(this);
90         }
91 
init(TextView title, SeekBar seek)92         public void init(TextView title, SeekBar seek) {
93             // This called by the containing activity to supply the surrounding
94             // state of the content browser that it will interact with.
95             mTitleView = title;
96             mSeekView = seek;
97             setNavVisibility(true);
98         }
99 
onSystemUiVisibilityChange(int visibility)100         @Override public void onSystemUiVisibilityChange(int visibility) {
101             // Detect when we go out of low-profile mode, to also go out
102             // of full screen.  We only do this when the low profile mode
103             // is changing from its last state, and turning off.
104             int diff = mLastSystemUiVis ^ visibility;
105             mLastSystemUiVis = visibility;
106             if ((diff&SYSTEM_UI_FLAG_LOW_PROFILE) != 0
107                     && (visibility&SYSTEM_UI_FLAG_LOW_PROFILE) == 0) {
108                 setNavVisibility(true);
109             }
110         }
111 
onWindowVisibilityChanged(int visibility)112         @Override protected void onWindowVisibilityChanged(int visibility) {
113             super.onWindowVisibilityChanged(visibility);
114 
115             // When we become visible, we show our navigation elements briefly
116             // before hiding them.
117             setNavVisibility(true);
118             getHandler().postDelayed(mNavHider, 2000);
119         }
120 
onScrollChanged(int l, int t, int oldl, int oldt)121         @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) {
122             super.onScrollChanged(l, t, oldl, oldt);
123 
124             // When the user scrolls, we hide navigation elements.
125             setNavVisibility(false);
126         }
127 
onClick(View v)128         @Override public void onClick(View v) {
129             // When the user clicks, we toggle the visibility of navigation elements.
130             int curVis = getSystemUiVisibility();
131             setNavVisibility((curVis&SYSTEM_UI_FLAG_LOW_PROFILE) != 0);
132         }
133 
setBaseSystemUiVisibility(int visibility)134         void setBaseSystemUiVisibility(int visibility) {
135             mBaseSystemUiVisibility = visibility;
136         }
137 
setNavVisibility(boolean visible)138         void setNavVisibility(boolean visible) {
139             int newVis = mBaseSystemUiVisibility;
140             if (!visible) {
141                 newVis |= SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_FULLSCREEN
142                         | SYSTEM_UI_FLAG_HIDE_NAVIGATION | SYSTEM_UI_FLAG_IMMERSIVE;
143             }
144             final boolean changed = newVis == getSystemUiVisibility();
145 
146             // Unschedule any pending event to hide navigation if we are
147             // changing the visibility, or making the UI visible.
148             if (changed || visible) {
149                 Handler h = getHandler();
150                 if (h != null) {
151                     h.removeCallbacks(mNavHider);
152                 }
153             }
154 
155             // Set the new desired visibility.
156             setSystemUiVisibility(newVis);
157             mTitleView.setVisibility(visible ? VISIBLE : INVISIBLE);
158             mSeekView.setVisibility(visible ? VISIBLE : INVISIBLE);
159         }
160     }
161 //END_INCLUDE(content)
162 
163     Content mContent;
164 
ContentBrowserNavActivity()165     public ContentBrowserNavActivity() {
166     }
167 
168     @Override
onCreate(Bundle savedInstanceState)169     public void onCreate(Bundle savedInstanceState) {
170         super.onCreate(savedInstanceState);
171 
172         getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
173 
174         setContentView(R.layout.content_browser_nav);
175         mContent = (Content)findViewById(R.id.content);
176         mContent.init((TextView)findViewById(R.id.title),
177                 (SeekBar)findViewById(R.id.seekbar));
178 
179         ActionBar bar = getActionBar();
180         bar.addTab(bar.newTab().setText("Tab 1").setTabListener(this));
181         bar.addTab(bar.newTab().setText("Tab 2").setTabListener(this));
182         bar.addTab(bar.newTab().setText("Tab 3").setTabListener(this));
183     }
184 
185     @Override
onCreateOptionsMenu(Menu menu)186     public boolean onCreateOptionsMenu(Menu menu) {
187         MenuInflater inflater = getMenuInflater();
188         inflater.inflate(R.menu.content_actions, menu);
189         SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
190         searchView.setOnQueryTextListener(this);
191 
192         // Set file with share history to the provider and set the share intent.
193         MenuItem actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
194         ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
195         actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
196         // Note that you can set/change the intent any time,
197         // say when the user has selected an image.
198         Intent shareIntent = new Intent(Intent.ACTION_SEND);
199         shareIntent.setType("image/*");
200         Uri uri = Uri.fromFile(getFileStreamPath("shared.png"));
201         shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
202         actionProvider.setShareIntent(shareIntent);
203         return true;
204     }
205 
206     @Override
onAttachedToWindow()207     public void onAttachedToWindow() {
208         super.onAttachedToWindow();
209     }
210 
211     @Override
onResume()212     protected void onResume() {
213         super.onResume();
214     }
215 
216     /**
217      * This method is declared in the menu.
218      */
onSort(MenuItem item)219     public void onSort(MenuItem item) {
220     }
221 
222     @Override
onOptionsItemSelected(MenuItem item)223     public boolean onOptionsItemSelected(MenuItem item) {
224         switch (item.getItemId()) {
225             case R.id.show_tabs:
226                 getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
227                 item.setChecked(true);
228                 return true;
229             case R.id.hide_tabs:
230                 getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
231                 item.setChecked(true);
232                 return true;
233             case R.id.stable_layout:
234                 item.setChecked(!item.isChecked());
235                 mContent.setBaseSystemUiVisibility(item.isChecked()
236                         ? View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
237                                 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
238                         : View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
239                 return true;
240         }
241         return false;
242     }
243 
244     @Override
onQueryTextChange(String newText)245     public boolean onQueryTextChange(String newText) {
246         return true;
247     }
248 
249     @Override
onQueryTextSubmit(String query)250     public boolean onQueryTextSubmit(String query) {
251         Toast.makeText(this, "Searching for: " + query + "...", Toast.LENGTH_SHORT).show();
252         return true;
253     }
254 
255     @Override
onTabSelected(Tab tab, FragmentTransaction ft)256     public void onTabSelected(Tab tab, FragmentTransaction ft) {
257     }
258 
259     @Override
onTabUnselected(Tab tab, FragmentTransaction ft)260     public void onTabUnselected(Tab tab, FragmentTransaction ft) {
261     }
262 
263     @Override
onTabReselected(Tab tab, FragmentTransaction ft)264     public void onTabReselected(Tab tab, FragmentTransaction ft) {
265     }
266 }
267