tags:

views:

14

answers:

1

The function below is part of the code for Google's Camera app. It is supposed to update a text view that displays dynamically the time elapsed since start of recording. But this function does not have a loop so how does it do it? Please help.

private static final int UPDATE_RECORD_TIME = 5;

private final Handler mHandler = new MainHandler();

 private class MainHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {

                case UPDATE_RECORD_TIME: {
                    updateRecordingTime();
                    break;
                }

                default:
                    Log.v(TAG, "Unhandled message: " + msg.what);
                    break;
            }
        }
    }  

int seconds = intent.getIntExtra(MediaStore.EXTRA_DURATION_LIMIT, 0); mMaxVideoDurationInMs = 1000 * seconds;

mMediaRecorder.setMaxDuration(mMaxVideoDurationInMs);

//this function is to update the recording time

private void updateRecordingTime() {

if (!mMediaRecorderRecording) { return; } 
long now = SystemClock.uptimeMillis(); 
long delta = now - mRecordingStartTime;

// Starting a minute before reaching the max duration 
// limit, we'll countdown the remaining time instead. 
boolean countdownRemainingTime = (mMaxVideoDurationInMs != 0 
        && delta >= mMaxVideoDurationInMs - 60000); 

long next_update_delay = 1000 - (delta % 1000); 
long seconds; 
if (countdownRemainingTime) { 
    delta = Math.max(0, mMaxVideoDurationInMs - delta); 
    seconds = (delta + 999) / 1000;
} else { 
    seconds = delta / 1000; // round to nearest
} 

long minutes = seconds / 60; 
long hours = minutes / 60; 
long remainderMinutes = minutes - (hours * 60); 
long remainderSeconds = seconds - (minutes * 60); 

String secondsString = Long.toString(remainderSeconds); 
if (secondsString.length() < 2) { 
    secondsString = "0" + secondsString; 
} 
String minutesString = Long.toString(remainderMinutes); 
if (minutesString.length() < 2) { 
    minutesString = "0" + minutesString; 
} 
String text = minutesString + ":" + secondsString; 
if (hours > 0) { 
    String hoursString = Long.toString(hours); 
    if (hoursString.length() < 2) { 
        hoursString = "0" + hoursString; 
    } 
    text = hoursString + ":" + text; 
} 
mRecordingTimeView.setText(text); 

if (mRecordingTimeCountsDown != countdownRemainingTime) { 

    // Avoid setting the color on every update, do it only 
    // when it needs changing. 
    mRecordingTimeCountsDown = countdownRemainingTime; 

    int color = getResources().getColor(countdownRemainingTime 
            ? R.color.recording_time_remaining_text 
            : R.color.recording_time_elapsed_text); 

    mRecordingTimeView.setTextColor(color); 
} 

// Work around a limitation of the T-Mobile G1: The T-Mobile 
// hardware blitter can't pixel-accurately scale and clip at the 
// same time, and the SurfaceFlinger doesn't attempt to work around 
// this limitation. In order to avoid visual corruption we must 
// manually refresh the entire surface view when changing any 
// overlapping view's contents. 

mVideoPreview.invalidate(); 
mHandler.sendEmptyMessageDelayed( 
        UPDATE_RECORD_TIME, next_update_delay); 
} 
A: 
mHandler.sendEmptyMessageDelayed(UPDATE_RECORD_TIME, next_update_delay); 

This line sends an event after next_update_delay timespan. I suppose, there's a declaration of mHandler somewhere and code that handles the UPDATE_RECORD_TIME event. I'd bet it calls updateRecordingTime, which in turn sends the message after a period of time.

This is the asynchronous version of a loop. The method does its work and then schedules itself to be executed after some time.

There must, however, be an initial call to either updateRecordingTime or to the sendMessageDelayed method somewhere outside updateRecordingTime to initially start the loop.

Thorsten Dittmar
is the following piece of code enough to make the above code work?private static final int UPDATE_RECORD_TIME = 5; private final Handler mHandler = new MainHandler(); private class MainHandler extends Handler { @Override public void handleMessage(Message msg) { switch (msg.what){ case UPDATE_RECORD_TIME: { updateRecordingTime(); break; } default:Log.v(TAG, "Unhandled message:" +msg.what); break; } } }
Namratha
I've added it to the edited code for a clearer view
Namratha
It should be. But remember: to initiate the loop, you either have to call `updateRecordingTime` once or you have to send the `UPDATE_RECORD_TIME` message once from the Activity's code.
Thorsten Dittmar
I did that and it's working fine but the loop never ends because the updateRecordingTimer() keeps calling itself(via an event). How do I make it stop via an event? Is it possible to do that?
Namratha
Well, don't schedule the event :-) If you look at the above function, there's a line `if (!mMediaRecorderRecording) { return; }` that basically means: When we're no longer recording, exit right away. That way the event is no longer scheduled and the loop stops.You could send another event that sets a flag which is evaluated like I said. You'd have to react to another event as you did for the update event (can be done in same handler).
Thorsten Dittmar
Oh! thank you :-)
Namratha