Invincible Cooler 2016. 11. 29. 22:18

소스 공개합니다.


public class MainActivity extends Activity implements OnClickListener {

private static final int SEND_START_MESSAGE = 0;

private static final int SEND_STOP_MESSAGE = 1;

private static final int SEND_RESET_MESSAGE = 2;

private TextView mTimerTv;

private Button mStartBtn;

private Button mStopBtn;

private Button mResetBtn;

private Button mCheckBtn;

private int mCount = 1;

private long mStartTime = 0L;

private long mTimeInMillies = 0L;

/**

* stop을 눌렀을때, 지금까지 경과시간을 저장하기 위해서

*/

private long mElapsedTime = 0L;

private long mFinalTime = 0L;

private StopwatchHandler mHandler;

private boolean mIsRunning = false;

private ListView mListView;

private ListAdapter mAdapter;

private ArrayList<InfoData> mInfoList = new ArrayList<InfoData>();


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mTimerTv = (TextView)findViewById(R.id.timerTv);

mTimerTv.setText("00:00.000");

mStartBtn = (Button)findViewById(R.id.startBtn);

mStartBtn.setOnClickListener(this);

mStopBtn = (Button)findViewById(R.id.stopBtn);

mStopBtn.setOnClickListener(this);

mResetBtn = (Button)findViewById(R.id.resetBtn);

mResetBtn.setOnClickListener(this);

mCheckBtn = (Button)findViewById(R.id.checkBtn);

mCheckBtn.setOnClickListener(this);

mHandler = new StopwatchHandler();

mAdapter = new ListAdapter(this, R.layout.stopwatch_list_block, mInfoList);

mListView = (ListView) findViewById(R.id.listView);

mListView.setAdapter(mAdapter);

mListView.setScrollbarFadingEnabled(true);

}

private class StopwatchHandler extends Handler {

@Override

        public void handleMessage(Message msg) {

            super.handleMessage(msg);

            

            switch(msg.what) {

           case SEND_START_MESSAGE:

            mTimeInMillies = System.currentTimeMillis() - mStartTime;

   

    mFinalTime = mElapsedTime + mTimeInMillies;


    int seconds = (int) (mFinalTime / 1000);

    int minutes = seconds / 60;

    seconds = seconds % 60;

    int milliseconds = (int) (mFinalTime % 1000);

    mTimerTv.setText("" + String.format("%02d", minutes) + ":" + String.format("%02d", seconds) + "." + String.format("%03d", milliseconds));

    mHandler.sendEmptyMessage(SEND_START_MESSAGE);

            break;

           

    case SEND_STOP_MESSAGE:

    mHandler.removeMessages(SEND_START_MESSAGE);

    mElapsedTime += mTimeInMillies;

    break;

   

    case SEND_RESET_MESSAGE:

    if(mIsRunning) {

    mHandler.removeMessages(SEND_START_MESSAGE);

    mIsRunning = false;

    }

   

    mStartTime = 0L;

    mTimeInMillies = 0L;

    mElapsedTime = 0L;

    mFinalTime = 0L;

   

    mTimerTv.setText("00:00.000");

    break;

            }

}

}


@Override

public boolean onCreateOptionsMenu(Menu menu) {


// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main_menu, menu);

return true;

}


@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}


@Override

public void onClick(View v) {

switch(v.getId()) {

case R.id.startBtn:

if(!mIsRunning) {

mStartTime = System.currentTimeMillis(); 

mHandler.sendEmptyMessage(SEND_START_MESSAGE);

mIsRunning = true;

}

break;

case R.id.stopBtn:

if(mIsRunning) {

mHandler.sendEmptyMessage(SEND_STOP_MESSAGE);

mIsRunning = false;

}

break;

case R.id.checkBtn:

if(!mIsRunning) {

// if(mInfoList != null && mInfoList.size() > 0) {

// String tmp = mInfoList.get(mInfoList.size()-1).getTitle().trim();

// Logger.print("tmp : " + tmp);

// Logger.print("tmp : " + mTimerTv.getText().toString());

// if(tmp.equals(mTimerTv.getText().toString())) {

// return;

// }

// }

InfoData info = new InfoData();

info.setIndex(mCount);

info.setTitle(mTimerTv.getText().toString());

mInfoList.add(info);

mAdapter.notifyDataSetChanged();

mCount++;

}

break;

case R.id.resetBtn:

mHandler.sendEmptyMessage(SEND_RESET_MESSAGE);

break;

}

}

public class ListAdapter extends ArrayAdapter<InfoData>

{

private ArrayList<InfoData> mItems;

private LayoutInflater mInflater;


public ListAdapter(Context context, int nTextViewResourceId, ArrayList<InfoData> items)

{

super(context, nTextViewResourceId, items);


this.mItems = items;

mInflater = LayoutInflater.from(context);

}


@Override

public View getView(int position, View convertView, ViewGroup parent)

{

ViewHolder holder;

InfoData info = mItems.get(position);


if(convertView == null)

{

convertView = mInflater.inflate(R.layout.stopwatch_list_block, null);


holder = new ViewHolder();

holder.title = (TextView) convertView.findViewById(R.id.title);


convertView.setTag(holder);

}

else

{

holder = (ViewHolder) convertView.getTag();

}

holder.title.setText(info.getIndex() + ". " + info.getTitle());


return convertView;

}


class ViewHolder

{

TextView title;

}

}

private class InfoData

{

private int index;

private String title;

public int getIndex()

{

return index;

}

public void setIndex(int index)

{

this.index = index;

}

public String getTitle()

{

return title;

}

public void setTitle(String title)

{

this.title = title;

}

}

}