Home | History | Annotate | Download | only in cardemulation
      1 /*
      2 * Copyright 2013 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.cardemulation;
     18 
     19 import android.os.Bundle;
     20 import android.support.v4.app.FragmentTransaction;
     21 import android.view.Menu;
     22 import android.view.MenuItem;
     23 import android.widget.ViewAnimator;
     24 
     25 import com.example.android.common.activities.SampleActivityBase;
     26 import com.example.android.common.logger.Log;
     27 import com.example.android.common.logger.LogFragment;
     28 import com.example.android.common.logger.LogWrapper;
     29 import com.example.android.common.logger.MessageOnlyLogFilter;
     30 
     31 /**
     32  * A simple launcher activity containing a summary sample description, sample log and a custom
     33  * {@link android.support.v4.app.Fragment} which can display a view.
     34  * <p>
     35  * For devices with displays with a width of 720dp or greater, the sample log is always visible,
     36  * on other devices it's visibility is controlled by an item on the Action Bar.
     37  */
     38 public class MainActivity extends SampleActivityBase {
     39 
     40     public static final String TAG = "MainActivity";
     41 
     42     // Whether the Log Fragment is currently shown
     43     private boolean mLogShown;
     44 
     45     @Override
     46     protected void onCreate(Bundle savedInstanceState) {
     47         super.onCreate(savedInstanceState);
     48         setContentView(R.layout.activity_main);
     49 
     50         if (savedInstanceState == null) {
     51             FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
     52             CardEmulationFragment fragment = new CardEmulationFragment();
     53             transaction.replace(R.id.sample_content_fragment, fragment);
     54             transaction.commit();
     55         }
     56     }
     57 
     58     @Override
     59     public boolean onCreateOptionsMenu(Menu menu) {
     60         getMenuInflater().inflate(R.menu.main, menu);
     61         return true;
     62     }
     63 
     64     @Override
     65     public boolean onPrepareOptionsMenu(Menu menu) {
     66         MenuItem logToggle = menu.findItem(R.id.menu_toggle_log);
     67         logToggle.setVisible(findViewById(R.id.sample_output) instanceof ViewAnimator);
     68         logToggle.setTitle(mLogShown ? R.string.sample_hide_log : R.string.sample_show_log);
     69 
     70         return super.onPrepareOptionsMenu(menu);
     71     }
     72 
     73     @Override
     74     public boolean onOptionsItemSelected(MenuItem item) {
     75         switch(item.getItemId()) {
     76             case R.id.menu_toggle_log:
     77                 mLogShown = !mLogShown;
     78                 ViewAnimator output = (ViewAnimator) findViewById(R.id.sample_output);
     79                 if (mLogShown) {
     80                     output.setDisplayedChild(1);
     81                 } else {
     82                     output.setDisplayedChild(0);
     83                 }
     84                 supportInvalidateOptionsMenu();
     85                 return true;
     86         }
     87         return super.onOptionsItemSelected(item);
     88     }
     89 
     90     /** Create a chain of targets that will receive log data */
     91     @Override
     92     public void initializeLogging() {
     93         // Wraps Android's native log framework.
     94         LogWrapper logWrapper = new LogWrapper();
     95         // Using Log, front-end to the logging chain, emulates android.util.log method signatures.
     96         Log.setLogNode(logWrapper);
     97 
     98         // Filter strips out everything except the message text.
     99         MessageOnlyLogFilter msgFilter = new MessageOnlyLogFilter();
    100         logWrapper.setNext(msgFilter);
    101 
    102         // On screen logging via a fragment with a TextView.
    103         LogFragment logFragment = (LogFragment) getSupportFragmentManager()
    104                 .findFragmentById(R.id.log_fragment);
    105         msgFilter.setNext(logFragment.getLogView());
    106 
    107         Log.i(TAG, "Ready");
    108     }
    109 }
    110