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