Code example for Chronometer

Methods: setBase, setVisibility, start

0
                case TelephonyManager.CALL_STATE_OFFHOOK: 
                    mMuteOn.setEnabled(true); 
                    mMuteOff.setEnabled(true); 
                    show(); 
                    hideMusic(); 
                    mTimer.setVisibility(View.VISIBLE); 
                    mTimer.setBase(SystemClock.elapsedRealtime()); 
                    mTimer.start(); 
                    break; 
                case TelephonyManager.CALL_STATE_IDLE: 
                    hide(); 
                    showMusic(); 
                    mTimer.setVisibility(View.GONE); 
                    mMuteOn.setEnabled(true); 
                    mMuteOff.setEnabled(true); 
                    break; 
            } 
            updateWidget(); 
        } 
 
    }; 
    private LayoutInflater mInflater;
 
    private static String TAG = "CallWidget";
 
    private CallHandler mHandler = new CallHandler();
 
    public CallWidget(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        hide(); 
        Slog.i(TAG, "Creating call widget from XML");
 
        mStatusBar = (StatusBarManager) context.getSystemService(Context.STATUS_BAR_SERVICE);
        telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
 
        setupButtons(); 
 
        IntentFilter filter = new IntentFilter();
        filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
        context.registerReceiver(new PhoneStateReceiver(), filter);
 
    } 
 
    protected void showMusic() { 
        mContext.sendOrderedBroadcast(new Intent(MusicControls.CALL_ENDED_INTENT), null);
    } 
 
    protected void hideMusic() { 
        mContext.sendOrderedBroadcast(new Intent(MusicControls.CALL_STARTED_INTENT), null);
    } 
 
    public void setupButtons() { 
        Slog.i(TAG, "Setting up call widget");
        View callView = mInflater.inflate(R.layout.call_ongoing, null, false);
        addView(callView, BUTTON_LAYOUT_PARAMS);
 
        mPhoneButton = (Button) findViewById(R.id.ongoing_call_button);
        mPhoneButton.setOnClickListener(this);
 
        mMuteOff = (Button) findViewById(R.id.quickpanel_call_mute_off);
        mMuteOff.setOnClickListener(this);
 
        mMuteOn = (Button) findViewById(R.id.quickpanel_call_mute_on);
        mMuteOn.setOnClickListener(this);
 
        mSpeakerOff = (Button) findViewById(R.id.quickpanel_call_speaker_off);
        mSpeakerOff.setOnClickListener(this);
 
        mSpeakerOn = (Button) findViewById(R.id.quickpanel_call_speaker_on);
        mSpeakerOn.setOnClickListener(this);
 
        mEndCallButton = (Button) findViewById(R.id.quickpanel_call_end);
        mEndCallButton.setOnClickListener(this);
 
        mTimer = (Chronometer) findViewById(R.id.quickpanel_time);
    } 
 
    public void updateWidget() { 
        am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
 
        if (am.isSpeakerphoneOn()) {
            Slog.i(TAG, "Speaker is on");
            mSpeakerOn.setVisibility(View.VISIBLE);
            mSpeakerOff.setVisibility(View.GONE);
        } else { 
            Slog.i(TAG, "Speaker is off");
            mSpeakerOff.setVisibility(View.VISIBLE);
            mSpeakerOn.setVisibility(View.GONE);
        } 
 
        if (am.isMicrophoneMute()) {
            mMuteOn.setVisibility(View.VISIBLE);
            mMuteOff.setVisibility(View.GONE);
        } else { 
            mMuteOff.setVisibility(View.VISIBLE);
            mMuteOn.setVisibility(View.GONE);
        } 
 
        // mSpeakerPhoneButton.setChecked(am.isSpeakerphoneOn()); 
        // mMuteButton.setChecked(am.isMicrophoneMute()); 
    } 
 
    public void show() { 
        Slog.i(TAG, "Show call widget");
        setVisibility(View.VISIBLE);
 
        if (mTimer != null) {
            mTimer.setVisibility(View.VISIBLE);
        } 
    } 
 
    public void hide() { 
        Slog.i(TAG, "Hide call widget");