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.Activity; 21 import android.app.FragmentTransaction; 22 import android.app.ActionBar.Tab; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.net.Uri; 26 import android.os.Bundle; 27 import android.util.AttributeSet; 28 import android.util.DisplayMetrics; 29 import android.view.ActionMode; 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.view.WindowManager; 36 import android.widget.CheckBox; 37 import android.widget.CompoundButton; 38 import android.widget.ImageView; 39 import android.widget.SearchView; 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 some of the available ways to reduce the size or visual contrast of 49 * the system decor, in order to better focus the user's attention or use available screen real 50 * estate on the task at hand. 51 */ 52 public class SystemUIModes extends Activity 53 implements OnQueryTextListener, ActionBar.TabListener { 54 public static class IV extends ImageView implements View.OnSystemUiVisibilityChangeListener { 55 private SystemUIModes mActivity; 56 private ActionMode mActionMode; IV(Context context)57 public IV(Context context) { 58 super(context); 59 } IV(Context context, AttributeSet attrs)60 public IV(Context context, AttributeSet attrs) { 61 super(context, attrs); 62 } setActivity(SystemUIModes act)63 public void setActivity(SystemUIModes act) { 64 setOnSystemUiVisibilityChangeListener(this); 65 mActivity = act; 66 } 67 @Override onSizeChanged(int w, int h, int oldw, int oldh)68 public void onSizeChanged(int w, int h, int oldw, int oldh) { 69 mActivity.refreshSizes(); 70 } 71 @Override onSystemUiVisibilityChange(int visibility)72 public void onSystemUiVisibilityChange(int visibility) { 73 mActivity.updateCheckControls(); 74 mActivity.refreshSizes(); 75 } 76 77 private class MyActionModeCallback implements ActionMode.Callback { onCreateActionMode(ActionMode mode, Menu menu)78 @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { 79 mode.setTitle("My Action Mode!"); 80 mode.setSubtitle(null); 81 mode.setTitleOptionalHint(false); 82 menu.add("Sort By Size").setIcon(android.R.drawable.ic_menu_sort_by_size); 83 menu.add("Sort By Alpha").setIcon(android.R.drawable.ic_menu_sort_alphabetically); 84 return true; 85 } 86 onPrepareActionMode(ActionMode mode, Menu menu)87 @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 88 return true; 89 } 90 onActionItemClicked(ActionMode mode, MenuItem item)91 @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 92 return true; 93 } 94 onDestroyActionMode(ActionMode mode)95 @Override public void onDestroyActionMode(ActionMode mode) { 96 mActionMode = null; 97 mActivity.clearActionMode(); 98 } 99 } 100 startActionMode()101 public void startActionMode() { 102 if (mActionMode == null) { 103 ActionMode.Callback cb = new MyActionModeCallback(); 104 mActionMode = startActionMode(cb); 105 } 106 } 107 stopActionMode()108 public void stopActionMode() { 109 if (mActionMode != null) { 110 mActionMode.finish(); 111 } 112 } 113 } 114 setFullscreen(boolean on)115 private void setFullscreen(boolean on) { 116 Window win = getWindow(); 117 WindowManager.LayoutParams winParams = win.getAttributes(); 118 final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN; 119 if (on) { 120 winParams.flags |= bits; 121 } else { 122 winParams.flags &= ~bits; 123 } 124 win.setAttributes(winParams); 125 } 126 setOverscan(boolean on)127 private void setOverscan(boolean on) { 128 Window win = getWindow(); 129 WindowManager.LayoutParams winParams = win.getAttributes(); 130 final int bits = WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN; 131 if (on) { 132 winParams.flags |= bits; 133 } else { 134 winParams.flags &= ~bits; 135 } 136 win.setAttributes(winParams); 137 } 138 setTranslucentStatus(boolean on)139 private void setTranslucentStatus(boolean on) { 140 Window win = getWindow(); 141 WindowManager.LayoutParams winParams = win.getAttributes(); 142 final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; 143 if (on) { 144 winParams.flags |= bits; 145 } else { 146 winParams.flags &= ~bits; 147 } 148 win.setAttributes(winParams); 149 } 150 setTranslucentNavigation(boolean on)151 private void setTranslucentNavigation(boolean on) { 152 Window win = getWindow(); 153 WindowManager.LayoutParams winParams = win.getAttributes(); 154 final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION; 155 if (on) { 156 winParams.flags |= bits; 157 } else { 158 winParams.flags &= ~bits; 159 } 160 win.setAttributes(winParams); 161 } 162 getDisplaySize()163 private String getDisplaySize() { 164 DisplayMetrics dm = getResources().getDisplayMetrics(); 165 return String.format("DisplayMetrics = (%d x %d)", dm.widthPixels, dm.heightPixels); 166 } getViewSize()167 private String getViewSize() { 168 return String.format("View = (%d,%d - %d,%d)", 169 mImage.getLeft(), mImage.getTop(), 170 mImage.getRight(), mImage.getBottom()); 171 } refreshSizes()172 void refreshSizes() { 173 mMetricsText.setText(getDisplaySize() + " " + getViewSize()); 174 } 175 176 static int TOAST_LENGTH = 500; 177 IV mImage; 178 CheckBox[] mCheckControls = new CheckBox[8]; 179 int[] mCheckFlags = new int[] { View.SYSTEM_UI_FLAG_LOW_PROFILE, 180 View.SYSTEM_UI_FLAG_FULLSCREEN, View.SYSTEM_UI_FLAG_HIDE_NAVIGATION, 181 View.SYSTEM_UI_FLAG_IMMERSIVE, View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY, 182 View.SYSTEM_UI_FLAG_LAYOUT_STABLE, View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN, 183 View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 184 }; 185 TextView mMetricsText; 186 SystemUIModes()187 public SystemUIModes() { 188 } 189 190 @Override onCreate(Bundle savedInstanceState)191 public void onCreate(Bundle savedInstanceState) { 192 super.onCreate(savedInstanceState); 193 194 setContentView(R.layout.system_ui_modes); 195 mImage = (IV) findViewById(R.id.image); 196 mImage.setActivity(this); 197 198 CompoundButton.OnCheckedChangeListener checkChangeListener 199 = new CompoundButton.OnCheckedChangeListener() { 200 @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 201 updateSystemUi(); 202 } 203 }; 204 mCheckControls[0] = (CheckBox) findViewById(R.id.modeLowProfile); 205 mCheckControls[1] = (CheckBox) findViewById(R.id.modeFullscreen); 206 mCheckControls[2] = (CheckBox) findViewById(R.id.modeHideNavigation); 207 mCheckControls[3] = (CheckBox) findViewById(R.id.modeImmersive); 208 mCheckControls[4] = (CheckBox) findViewById(R.id.modeImmersiveSticky); 209 mCheckControls[5] = (CheckBox) findViewById(R.id.layoutStable); 210 mCheckControls[6] = (CheckBox) findViewById(R.id.layoutFullscreen); 211 mCheckControls[7] = (CheckBox) findViewById(R.id.layoutHideNavigation); 212 for (int i=0; i<mCheckControls.length; i++) { 213 mCheckControls[i].setOnCheckedChangeListener(checkChangeListener); 214 } 215 ((CheckBox) findViewById(R.id.windowFullscreen)).setOnCheckedChangeListener( 216 new CompoundButton.OnCheckedChangeListener() { 217 @Override 218 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 219 setFullscreen(isChecked); 220 } 221 } 222 ); 223 ((CheckBox) findViewById(R.id.windowOverscan)).setOnCheckedChangeListener( 224 new CompoundButton.OnCheckedChangeListener() { 225 @Override 226 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 227 setOverscan(isChecked); 228 } 229 } 230 ); 231 ((CheckBox) findViewById(R.id.windowTranslucentStatus)).setOnCheckedChangeListener( 232 new CompoundButton.OnCheckedChangeListener() { 233 @Override 234 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 235 setTranslucentStatus(isChecked); 236 } 237 } 238 ); 239 ((CheckBox) findViewById(R.id.windowTranslucentNav)).setOnCheckedChangeListener( 240 new CompoundButton.OnCheckedChangeListener() { 241 @Override 242 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 243 setTranslucentNavigation(isChecked); 244 } 245 } 246 ); 247 ((CheckBox) findViewById(R.id.windowHideActionBar)).setOnCheckedChangeListener( 248 new CompoundButton.OnCheckedChangeListener() { 249 @Override 250 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 251 if (isChecked) { 252 getActionBar().hide(); 253 } else { 254 getActionBar().show(); 255 } 256 } 257 } 258 ); 259 ((CheckBox) findViewById(R.id.windowActionMode)).setOnCheckedChangeListener( 260 new CompoundButton.OnCheckedChangeListener() { 261 @Override 262 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 263 if (isChecked) { 264 mImage.startActionMode(); 265 } else { 266 mImage.stopActionMode(); 267 } 268 } 269 } 270 ); 271 mMetricsText = (TextView) findViewById(R.id.metricsText); 272 } 273 274 @Override onCreateOptionsMenu(Menu menu)275 public boolean onCreateOptionsMenu(Menu menu) { 276 MenuInflater inflater = getMenuInflater(); 277 inflater.inflate(R.menu.content_actions, menu); 278 SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); 279 searchView.setOnQueryTextListener(this); 280 281 // Set file with share history to the provider and set the share intent. 282 MenuItem actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar); 283 ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider(); 284 actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME); 285 // Note that you can set/change the intent any time, 286 // say when the user has selected an image. 287 Intent shareIntent = new Intent(Intent.ACTION_SEND); 288 shareIntent.setType("image/*"); 289 Uri uri = Uri.fromFile(getFileStreamPath("shared.png")); 290 shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 291 actionProvider.setShareIntent(shareIntent); 292 return true; 293 } 294 295 @Override onAttachedToWindow()296 public void onAttachedToWindow() { 297 updateCheckControls(); 298 } 299 300 @Override onResume()301 protected void onResume() { 302 super.onResume(); 303 } 304 onSort(MenuItem item)305 public void onSort(MenuItem item) { 306 } 307 308 @Override onQueryTextChange(String newText)309 public boolean onQueryTextChange(String newText) { 310 return true; 311 } 312 313 @Override onQueryTextSubmit(String query)314 public boolean onQueryTextSubmit(String query) { 315 Toast.makeText(this, "Searching for: " + query + "...", Toast.LENGTH_SHORT).show(); 316 return true; 317 } 318 319 @Override onOptionsItemSelected(MenuItem item)320 public boolean onOptionsItemSelected(MenuItem item) { 321 switch (item.getItemId()) { 322 case R.id.show_tabs: 323 getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 324 item.setChecked(true); 325 return true; 326 case R.id.hide_tabs: 327 getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 328 item.setChecked(true); 329 return true; 330 } 331 return false; 332 } 333 334 @Override onTabSelected(Tab tab, FragmentTransaction ft)335 public void onTabSelected(Tab tab, FragmentTransaction ft) { 336 } 337 338 @Override onTabUnselected(Tab tab, FragmentTransaction ft)339 public void onTabUnselected(Tab tab, FragmentTransaction ft) { 340 } 341 342 @Override onTabReselected(Tab tab, FragmentTransaction ft)343 public void onTabReselected(Tab tab, FragmentTransaction ft) { 344 } 345 updateCheckControls()346 public void updateCheckControls() { 347 int visibility = mImage.getSystemUiVisibility(); 348 for (int i=0; i<mCheckControls.length; i++) { 349 mCheckControls[i].setChecked((visibility&mCheckFlags[i]) != 0); 350 } 351 } 352 updateSystemUi()353 public void updateSystemUi() { 354 int visibility = 0; 355 for (int i=0; i<mCheckControls.length; i++) { 356 if (mCheckControls[i].isChecked()) { 357 visibility |= mCheckFlags[i]; 358 } 359 } 360 mImage.setSystemUiVisibility(visibility); 361 } 362 clearActionMode()363 public void clearActionMode() { 364 ((CheckBox) findViewById(R.id.windowActionMode)).setChecked(false); 365 } 366 } 367