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