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