FileDocCategorySizeDatePackage
CallTime.javaAPI DocAndroid 1.5 API7193Wed May 06 22:42:46 BST 2009com.android.phone

CallTime

public class CallTime extends android.os.Handler
Helper class used to keep track of various "elapsed time" indications in the Phone app, and also to start and stop tracing / profiling.

Fields Summary
private static final String
LOG_TAG
private static final boolean
DBG
static final boolean
PROFILE
private static final int
PROFILE_STATE_NONE
private static final int
PROFILE_STATE_READY
private static final int
PROFILE_STATE_RUNNING
private static int
sProfileState
private com.android.internal.telephony.Call
mCall
private long
mLastReportedTime
private boolean
mTimerRunning
private long
mInterval
private PeriodicTimerCallback
mTimerCallback
private OnTickListener
mListener
Constructors Summary
public CallTime(OnTickListener listener)


      
          
    

       
        mListener = listener;
        mTimerCallback = new PeriodicTimerCallback();
    
Methods Summary
voidcancelTimer()

        if (DBG) log("cancelTimer()...");
        removeCallbacks(mTimerCallback);
        mTimerRunning = false;
    
static longgetCallDuration(com.android.internal.telephony.Call call)
Returns a "call duration" value for the specified Call, in msec, suitable for display in the UI.

        long duration = 0;
        List connections = call.getConnections();
        int count = connections.size();
        Connection c;

        if (count == 1) {
            c = (Connection) connections.get(0);
            //duration = (state == Call.State.ACTIVE
            //            ? c.getDurationMillis() : c.getHoldDurationMillis());
            duration = c.getDurationMillis();
        } else {
            for (int i = 0; i < count; i++) {
                c = (Connection) connections.get(i);
                //long t = (state == Call.State.ACTIVE
                //          ? c.getDurationMillis() : c.getHoldDurationMillis());
                long t = c.getDurationMillis();
                if (t > duration) {
                    duration = t;
                }
            }
        }

        if (DBG) log("updateElapsedTime, count=" + count + ", duration=" + duration);
        return duration;
    
booleanisTraceReady()

        return sProfileState == PROFILE_STATE_READY;
    
booleanisTraceRunning()

        return sProfileState == PROFILE_STATE_RUNNING;
    
private static voidlog(java.lang.String msg)

        Log.d(LOG_TAG, "[CallTime] " + msg);
    
voidperiodicUpdateTimer()

        if (!mTimerRunning) {
            mTimerRunning = true;

            long now = SystemClock.uptimeMillis();
            long nextReport = mLastReportedTime + mInterval;

            while (now >= nextReport) {
                nextReport += mInterval;
            }

            if (DBG) log("periodicUpdateTimer() @ " + nextReport);
            postAtTime(mTimerCallback, nextReport);
            mLastReportedTime = nextReport;

            if (mCall != null) {
                Call.State state = mCall.getState();

                if (state == Call.State.ACTIVE) {
                    updateElapsedTime(mCall);
                }
            }

            if (PROFILE && isTraceReady()) {
                startTrace();
            }
        } else {
            if (DBG) log("periodicUpdateTimer: timer already running, bail");
        }
    
voidreset()

        if (DBG) log("reset()...");
        mLastReportedTime = SystemClock.uptimeMillis() - mInterval;
    
voidsetActiveCallMode(com.android.internal.telephony.Call call)
Sets the call timer to "active call" mode, where the timer will periodically update the UI to show how long the specified call has been active. After calling this you should also call reset() and periodicUpdateTimer() to get the timer started.

        if (DBG) log("setActiveCallMode(" + call + ")...");
        mCall = call;

        // How frequently should we update the UI?
        mInterval = 1000;  // once per second
    
static voidsetTraceReady()

        if (sProfileState == PROFILE_STATE_NONE) {
            sProfileState = PROFILE_STATE_READY;
            log("trace ready...");
        } else {
            log("current trace state = " + sProfileState);
        }
    
voidstartTrace()

        if (PROFILE & sProfileState == PROFILE_STATE_READY) {
            // For now, we move away from temp directory in favor of
            // the application's data directory to store the trace
            // information (/data/data/com.android.phone).
            File file = PhoneApp.getInstance().getDir ("phoneTrace", Context.MODE_PRIVATE);
            if (file.exists() == false) {
                file.mkdirs();
            }
            String baseName = file.getPath() + File.separator + "callstate";
            String dataFile = baseName + ".data";
            String keyFile = baseName + ".key";

            file = new File(dataFile);
            if (file.exists() == true) {
                file.delete();
            }

            file = new File(keyFile);
            if (file.exists() == true) {
                file.delete();
            }

            sProfileState = PROFILE_STATE_RUNNING;
            log("startTrace");
            Debug.startMethodTracing(baseName, 8 * 1024 * 1024);
        }
    
voidstopTrace()

        if (PROFILE) {
            if (sProfileState == PROFILE_STATE_RUNNING) {
                sProfileState = PROFILE_STATE_NONE;
                log("stopTrace");
                Debug.stopMethodTracing();
            }
        }
    
private voidupdateElapsedTime(com.android.internal.telephony.Call call)

        if (mListener != null) {
            long duration = getCallDuration(call);
            mListener.onTickForCallTimeElapsed(duration / 1000);
        }