1/*
2 * Copyright (C) 2012 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
17package com.example.android.lifecycle.util;
18
19import android.os.Handler;
20import android.widget.TextView;
21
22import java.util.List;
23
24public class Utils {
25
26  private static StatusTracker mStatusTracker = StatusTracker.getInstance();
27
28  /**
29   * Helper method to print out the lifecycle state of each Activity.  Note this has
30   * been wrapped in a Handler to delay the output due to overlaps in lifecycle state
31   * changes as one Activity launches another.
32   * @link http://developer.android.com/guide/topics/fundamentals/activities.html#CoordinatingActivities
33   * @param viewMethods TextView to list out the lifecycle methods called
34   * @param viewStatus TextView to list out the status of all Activity classes
35   */
36  public static void printStatus(final TextView viewMethods, final TextView viewStatus) {
37      Handler handler = new Handler();
38      handler.postDelayed(new Runnable() {
39        public void run() {
40          // Get the stack of Activity lifecycle methods called and print to TextView
41          StringBuilder sbMethods = new StringBuilder();
42          List<String> listMethods = mStatusTracker.getMethodList();
43          for (String method : listMethods) {
44              sbMethods.insert(0, method + "\r\n");
45          }
46          if(viewMethods != null) {
47              viewMethods.setText(sbMethods.toString());
48          }
49
50          // Get the status of all Activity classes and print to TextView
51          StringBuilder sbStatus = new StringBuilder();
52          for (String key : mStatusTracker.keySet()) {
53            sbStatus.insert(0,key + ": " + mStatusTracker.getStatus(key) + "\n");
54          }
55          if(viewStatus != null) {
56              viewStatus.setText(sbStatus.toString());
57          }
58        }
59      }, 750);
60    }
61}
62
63
64
65
66