1/*
2 * Copyright (C) 2006 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.android.phone;
18
19import android.os.SystemClock;
20import android.util.Log;
21import android.view.View;
22import android.view.ViewParent;
23import android.view.Window;
24
25/**
26 * Profiling utilities for the Phone app.
27 */
28public class Profiler {
29    private static final String LOG_TAG = PhoneGlobals.LOG_TAG;
30
31    // Let the compiler optimize all this code out unless we're actively
32    // doing profiling runs.
33    // TODO: Instead of doing all these "if (PROFILE)" checks here, every
34    // place that *calls* any of these methods should check the value of
35    // Profiler.PROFILE first, so the method calls will get optimized out
36    // too.
37    private static final boolean PROFILE = false;
38
39    static long sTimeCallScreenRequested;
40    static long sTimeCallScreenOnCreate;
41    static long sTimeCallScreenCreated;
42
43    // TODO: Clean up any usage of these times.  (There's no "incoming call
44    // panel" in the Phone UI any more; incoming calls just go straight to the
45    // regular in-call UI.)
46    static long sTimeIncomingCallPanelRequested;
47    static long sTimeIncomingCallPanelOnCreate;
48    static long sTimeIncomingCallPanelCreated;
49
50    /** This class is never instantiated. */
51    private Profiler() {
52    }
53
54    static void profileViewCreate(Window win, String tag) {
55        if (false) {
56            ViewParent p = (ViewParent) win.getDecorView();
57            while (p instanceof View) {
58                p = ((View) p).getParent();
59            }
60            //((ViewRoot)p).profile();
61            //((ViewRoot)p).setProfileTag(tag);
62        }
63    }
64
65    static void callScreenRequested() {
66        if (PROFILE) {
67            sTimeCallScreenRequested = SystemClock.uptimeMillis();
68        }
69    }
70
71    static void callScreenOnCreate() {
72        if (PROFILE) {
73            sTimeCallScreenOnCreate = SystemClock.uptimeMillis();
74        }
75    }
76
77    static void callScreenCreated() {
78        if (PROFILE) {
79            sTimeCallScreenCreated = SystemClock.uptimeMillis();
80            dumpCallScreenStat();
81        }
82    }
83
84    private static void dumpCallScreenStat() {
85        if (PROFILE) {
86            log(">>> call screen perf stats <<<");
87            log(">>> request -> onCreate = " +
88                    (sTimeCallScreenOnCreate - sTimeCallScreenRequested));
89            log(">>> onCreate -> created = " +
90                    (sTimeCallScreenCreated - sTimeCallScreenOnCreate));
91        }
92    }
93
94    static void incomingCallPanelRequested() {
95        if (PROFILE) {
96            sTimeIncomingCallPanelRequested = SystemClock.uptimeMillis();
97        }
98    }
99
100    static void incomingCallPanelOnCreate() {
101        if (PROFILE) {
102            sTimeIncomingCallPanelOnCreate = SystemClock.uptimeMillis();
103        }
104    }
105
106    static void incomingCallPanelCreated() {
107        if (PROFILE) {
108            sTimeIncomingCallPanelCreated = SystemClock.uptimeMillis();
109            dumpIncomingCallPanelStat();
110        }
111    }
112
113    private static void dumpIncomingCallPanelStat() {
114        if (PROFILE) {
115            log(">>> incoming call panel perf stats <<<");
116            log(">>> request -> onCreate = " +
117                    (sTimeIncomingCallPanelOnCreate - sTimeIncomingCallPanelRequested));
118            log(">>> onCreate -> created = " +
119                    (sTimeIncomingCallPanelCreated - sTimeIncomingCallPanelOnCreate));
120        }
121    }
122
123    private static void log(String msg) {
124        Log.d(LOG_TAG, "[Profiler] " + msg);
125    }
126}
127