blob: 7d8629872fea5e947a506c61d46d37770a4519e7 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.scxml.samples.android;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.commons.scxml.env.StopWatch;
import org.apache.commons.scxml.samples.android.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class StopWatchActivity extends Activity {
private static final int UPDATE_TIME = 1;
private static final int UPDATE_STATE = 2;
private StopWatch mStopWatch;
private Button mStartButton;
private Button mSplitButton;
private TextView mState = null;
private TextView mTime = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.stopwatch_display);
mStartButton = (Button) findViewById(R.id.startstop);
mSplitButton = (Button) findViewById(R.id.split);
mState = (TextView) findViewById(R.id.status);
mTime = (TextView) findViewById(R.id.time);
mStopWatch = new StopWatch();
mStartButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Button startButton = (Button) v;
Button splitButton = (Button) findViewById(R.id.split);
String action = startButton.getText().toString();
if (action == getString(R.string.start)) {
mStopWatch.fireEvent(StopWatch.EVENT_START);
startButton.setText(R.string.stop);
splitButton.setEnabled(true);
} else if (action == getString(R.string.stop)) {
mStopWatch.fireEvent(StopWatch.EVENT_STOP);
startButton.setText(R.string.reset);
splitButton.setEnabled(false);
} else if (action == getString(R.string.reset)){
mStopWatch.fireEvent(StopWatch.EVENT_RESET);
startButton.setText(R.string.start);
splitButton.setText(R.string.split);
}
}
});
mSplitButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Button splitButton = (Button) v;
String action = splitButton.getText().toString();
if (action == getString(R.string.split)) {
mStopWatch.fireEvent(StopWatch.EVENT_SPLIT);
splitButton.setText(R.string.unsplit);
} else if(action == getString(R.string.unsplit)){
mStopWatch.fireEvent(StopWatch.EVENT_UNSPLIT);
splitButton.setText(R.string.split);
}
}
});
Timer displayTimer = new Timer();
displayTimer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.sendMessage(mHandler.obtainMessage(UPDATE_STATE, mStopWatch.getCurrentState()));
mHandler.sendMessage(mHandler.obtainMessage(UPDATE_TIME, mStopWatch.getDisplay()));
}
}, 100, 100);
}
private Handler mHandler = new Handler() {
@Override public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_STATE:
mState.setText((String)msg.obj);
break;
case UPDATE_TIME:
mTime.setText((String)msg.obj);
break;
default:
super.handleMessage(msg);
}
}
};
}