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; 57 public IV(Context context) { 58 super(context); 59 } 60 public IV(Context context, AttributeSet attrs) { 61 super(context, attrs); 62 } 63 public void setActivity(SystemUIModes act) { 64 setOnSystemUiVisibilityChangeListener(this); 65 mActivity = act; 66 } 67 @Override 68 public void onSizeChanged(int w, int h, int oldw, int oldh) { 69 mActivity.refreshSizes(); 70 } 71 @Override 72 public void onSystemUiVisibilityChange(int visibility) { 73 mActivity.updateCheckControls(); 74 mActivity.refreshSizes(); 75 } 76 77 private class MyActionModeCallback implements ActionMode.Callback { 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 87 @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 88 return true; 89 } 90 91 @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 92 return true; 93 } 94 95 @Override public void onDestroyActionMode(ActionMode mode) { 96 mActionMode = null; 97 mActivity.clearActionMode(); 98 } 99 } 100 101 public void startActionMode() { 102 if (mActionMode == null) { 103 ActionMode.Callback cb = new MyActionModeCallback(); 104 mActionMode = startActionMode(cb); 105 } 106 } 107 108 public void stopActionMode() { 109 if (mActionMode != null) { 110 mActionMode.finish(); 111 } 112 } 113 } 114 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 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 139 private String getDisplaySize() { 140 DisplayMetrics dm = getResources().getDisplayMetrics(); 141 return String.format("DisplayMetrics = (%d x %d)", dm.widthPixels, dm.heightPixels); 142 } 143 private String getViewSize() { 144 return String.format("View = (%d,%d - %d,%d)", 145 mImage.getLeft(), mImage.getTop(), 146 mImage.getRight(), mImage.getBottom()); 147 } 148 void refreshSizes() { 149 mMetricsText.setText(getDisplaySize() + " " + getViewSize()); 150 } 151 152 static int TOAST_LENGTH = 500; 153 IV mImage; 154 CheckBox[] mCheckControls = new CheckBox[6]; 155 int[] mCheckFlags = new int[] { View.SYSTEM_UI_FLAG_LOW_PROFILE, 156 View.SYSTEM_UI_FLAG_FULLSCREEN, View.SYSTEM_UI_FLAG_HIDE_NAVIGATION, 157 View.SYSTEM_UI_FLAG_LAYOUT_STABLE, View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN, 158 View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 159 }; 160 TextView mMetricsText; 161 162 public SystemUIModes() { 163 } 164 165 @Override 166 public void onCreate(Bundle savedInstanceState) { 167 super.onCreate(savedInstanceState); 168 169 setContentView(R.layout.system_ui_modes); 170 mImage = (IV) findViewById(R.id.image); 171 mImage.setActivity(this); 172 173 CompoundButton.OnCheckedChangeListener checkChangeListener 174 = new CompoundButton.OnCheckedChangeListener() { 175 @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 176 updateSystemUi(); 177 } 178 }; 179 mCheckControls[0] = (CheckBox) findViewById(R.id.modeLowProfile); 180 mCheckControls[1] = (CheckBox) findViewById(R.id.modeFullscreen); 181 mCheckControls[2] = (CheckBox) findViewById(R.id.modeHideNavigation); 182 mCheckControls[3] = (CheckBox) findViewById(R.id.layoutStable); 183 mCheckControls[4] = (CheckBox) findViewById(R.id.layoutFullscreen); 184 mCheckControls[5] = (CheckBox) findViewById(R.id.layoutHideNavigation); 185 for (int i=0; i<mCheckControls.length; i++) { 186 mCheckControls[i].setOnCheckedChangeListener(checkChangeListener); 187 } 188 ((CheckBox) findViewById(R.id.windowFullscreen)).setOnCheckedChangeListener( 189 new CompoundButton.OnCheckedChangeListener() { 190 @Override 191 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 192 setFullscreen(isChecked); 193 } 194 } 195 ); 196 ((CheckBox) findViewById(R.id.windowOverscan)).setOnCheckedChangeListener( 197 new CompoundButton.OnCheckedChangeListener() { 198 @Override 199 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 200 setOverscan(isChecked); 201 } 202 } 203 ); 204 ((CheckBox) findViewById(R.id.windowHideActionBar)).setOnCheckedChangeListener( 205 new CompoundButton.OnCheckedChangeListener() { 206 @Override 207 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 208 if (isChecked) { 209 getActionBar().hide(); 210 } else { 211 getActionBar().show(); 212 } 213 } 214 } 215 ); 216 ((CheckBox) findViewById(R.id.windowActionMode)).setOnCheckedChangeListener( 217 new CompoundButton.OnCheckedChangeListener() { 218 @Override 219 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 220 if (isChecked) { 221 mImage.startActionMode(); 222 } else { 223 mImage.stopActionMode(); 224 } 225 } 226 } 227 ); 228 mMetricsText = (TextView) findViewById(R.id.metricsText); 229 } 230 231 @Override 232 public boolean onCreateOptionsMenu(Menu menu) { 233 MenuInflater inflater = getMenuInflater(); 234 inflater.inflate(R.menu.content_actions, menu); 235 SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); 236 searchView.setOnQueryTextListener(this); 237 238 // Set file with share history to the provider and set the share intent. 239 MenuItem actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar); 240 ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider(); 241 actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME); 242 // Note that you can set/change the intent any time, 243 // say when the user has selected an image. 244 Intent shareIntent = new Intent(Intent.ACTION_SEND); 245 shareIntent.setType("image/*"); 246 Uri uri = Uri.fromFile(getFileStreamPath("shared.png")); 247 shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 248 actionProvider.setShareIntent(shareIntent); 249 return true; 250 } 251 252 @Override 253 public void onAttachedToWindow() { 254 updateCheckControls(); 255 } 256 257 @Override 258 protected void onResume() { 259 super.onResume(); 260 } 261 262 public void onSort(MenuItem item) { 263 } 264 265 @Override 266 public boolean onQueryTextChange(String newText) { 267 return true; 268 } 269 270 @Override 271 public boolean onQueryTextSubmit(String query) { 272 Toast.makeText(this, "Searching for: " + query + "...", Toast.LENGTH_SHORT).show(); 273 return true; 274 } 275 276 @Override 277 public boolean onOptionsItemSelected(MenuItem item) { 278 switch (item.getItemId()) { 279 case R.id.show_tabs: 280 getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 281 item.setChecked(true); 282 return true; 283 case R.id.hide_tabs: 284 getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 285 item.setChecked(true); 286 return true; 287 } 288 return false; 289 } 290 291 @Override 292 public void onTabSelected(Tab tab, FragmentTransaction ft) { 293 } 294 295 @Override 296 public void onTabUnselected(Tab tab, FragmentTransaction ft) { 297 } 298 299 @Override 300 public void onTabReselected(Tab tab, FragmentTransaction ft) { 301 } 302 303 public void updateCheckControls() { 304 int visibility = mImage.getSystemUiVisibility(); 305 for (int i=0; i<mCheckControls.length; i++) { 306 mCheckControls[i].setChecked((visibility&mCheckFlags[i]) != 0); 307 } 308 } 309 310 public void updateSystemUi() { 311 int visibility = 0; 312 for (int i=0; i<mCheckControls.length; i++) { 313 if (mCheckControls[i].isChecked()) { 314 visibility |= mCheckFlags[i]; 315 } 316 } 317 mImage.setSystemUiVisibility(visibility); 318 } 319 320 public void clearActionMode() { 321 ((CheckBox) findViewById(R.id.windowActionMode)).setChecked(false); 322 } 323 } 324