PageRenderTime 25ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/development/samples/ApiDemos/src/com/example/android/apis/view/SystemUIModes.java

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