• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.example.android.swiperefreshlayoutbasic;
19 
20 import android.os.Bundle;
21 import android.support.v4.app.FragmentTransaction;
22 import android.view.Menu;
23 import android.view.MenuItem;
24 import android.widget.ViewAnimator;
25 
26 import com.example.android.common.activities.SampleActivityBase;
27 import com.example.android.common.logger.Log;
28 import com.example.android.common.logger.LogFragment;
29 import com.example.android.common.logger.LogWrapper;
30 import com.example.android.common.logger.MessageOnlyLogFilter;
31 
32 /**
33  * A simple launcher activity containing a summary sample description, sample log and a custom
34  * {@link android.support.v4.app.Fragment} which can display a view.
35  * <p>
36  * For devices with displays with a width of 720dp or greater, the sample log is always visible,
37  * on other devices it's visibility is controlled by an item on the Action Bar.
38  */
39 public class MainActivity extends SampleActivityBase {
40 
41     public static final String TAG = "MainActivity";
42 
43     // Whether the Log Fragment is currently shown
44     private boolean mLogShown;
45 
46     @Override
onCreate(Bundle savedInstanceState)47     protected void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49         setContentView(R.layout.activity_main);
50 
51         if (savedInstanceState == null) {
52             FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
53             SwipeRefreshLayoutBasicFragment fragment = new SwipeRefreshLayoutBasicFragment();
54             transaction.replace(R.id.sample_content_fragment, fragment);
55             transaction.commit();
56         }
57     }
58 
59     @Override
onCreateOptionsMenu(Menu menu)60     public boolean onCreateOptionsMenu(Menu menu) {
61         getMenuInflater().inflate(R.menu.main, menu);
62         return true;
63     }
64 
65     @Override
onPrepareOptionsMenu(Menu menu)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
onOptionsItemSelected(MenuItem item)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
initializeLogging()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 }
111