Back to project page sthlmtraveling.
The source code is released under:
Apache License
If you think the Android project sthlmtraveling listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2009 Johan Nilsson <http://markupartist.com> *// w ww . j a va 2s .c om * Licensed 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 com.markupartist.sthlmtraveling; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.TimePickerDialog; import android.content.Intent; import android.os.Bundle; import android.text.format.Time; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.DatePicker; import android.widget.Spinner; import android.widget.TimePicker; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; import com.markupartist.sthlmtraveling.provider.planner.JourneyQuery; public class ChangeRouteTimeActivity extends BaseActivity { static final String TAG = "ChangeRouteTimeActivity"; static final int DIALOG_DATE = 0; static final int DIALOG_TIME = 1; private Time mTime; private Button mDateButton; private Button mTimeButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.change_route_time); registerScreen("Change route time"); //setTitle(getText(R.string.change_date_and_time)); //ActionBar actionBar = initActionBar(); //actionBar.setDisplayHomeAsUpEnabled(true); Bundle extras = getIntent().getExtras(); final JourneyQuery journeyQuery = extras.getParcelable(RoutesActivity.EXTRA_JOURNEY_QUERY); mTime = journeyQuery.time; mDateButton = (Button) findViewById(R.id.change_route_date); mDateButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showDialog(DIALOG_DATE); } }); mTimeButton = (Button) findViewById(R.id.change_route_time); mTimeButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showDialog(DIALOG_TIME); } }); int selectedPosition = journeyQuery.isTimeDeparture ? 0 : 1; final Spinner whenSpinner = (Spinner) findViewById(R.id.departure_arrival_choice); ArrayAdapter<CharSequence> whenChoiceAdapter = ArrayAdapter.createFromResource( this, R.array.when_choice, android.R.layout.simple_spinner_item); whenChoiceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); whenSpinner.setAdapter(whenChoiceAdapter); whenSpinner.setSelection(selectedPosition); // Inflate a "Done/Discard" custom action bar view. LayoutInflater inflater = (LayoutInflater) getSupportActionBar().getThemedContext() .getSystemService(LAYOUT_INFLATER_SERVICE); final View customActionBarView = inflater.inflate( R.layout.actionbar_custom_view_done_discard, null); customActionBarView.findViewById(R.id.actionbar_done).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { journeyQuery.isTimeDeparture = whenSpinner.getSelectedItemId() == 0 ? true : false; journeyQuery.time = mTime; setResult(RESULT_OK, (new Intent()) .putExtra(RoutesActivity.EXTRA_JOURNEY_QUERY, journeyQuery)); finish(); } }); customActionBarView.findViewById(R.id.actionbar_discard).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { setResult(RESULT_CANCELED); finish(); } }); // Show the custom action bar view and hide the normal Home icon and title. final ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayOptions( ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE); actionBar.setCustomView(customActionBarView, new ActionBar.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); updateDisplay(); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getSupportMenuInflater(); inflater.inflate(R.menu.actionbar_change_route_time, menu); return true; } /** * Update time on the buttons. */ private void updateDisplay() { String formattedDate = mTime.format("%x"); String formattedTime = mTime.format("%R"); mDateButton.setText(formattedDate); mTimeButton.setText(formattedTime); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return true; } return super.onOptionsItemSelected(item); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG_DATE: return new DatePickerDialog(this, mDateSetListener, mTime.year, mTime.month, mTime.monthDay); case DIALOG_TIME: // TODO: Base 24 hour on locale, same with the format. return new TimePickerDialog(this, mTimeSetListener, mTime.hour, mTime.minute, true); } return null; } @Override protected void onPrepareDialog(int id, Dialog dialog) { switch (id) { case DIALOG_DATE: ((DatePickerDialog) dialog).updateDate(mTime.year, mTime.month, mTime.monthDay); break; case DIALOG_TIME: ((TimePickerDialog) dialog).updateTime(mTime.hour, mTime.minute); break; } } @Override public boolean onSearchRequested() { Intent i = new Intent(this, StartActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); return true; } private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mTime.year = year; mTime.month = monthOfYear; mTime.monthDay = dayOfMonth; updateDisplay(); } }; private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { mTime.hour = hourOfDay; mTime.minute = minute; updateDisplay(); } }; }