Java tutorial
package com.lepin.activity; import java.util.ArrayList; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.google.gson.reflect.TypeToken; import com.lepin.entity.Book; import com.lepin.entity.Car; import com.lepin.entity.CarType; import com.lepin.entity.JsonResult; import com.lepin.entity.Pinche; import com.lepin.entity.User; import com.lepin.inject.Contextview; import com.lepin.inject.ViewInject; import com.lepin.inject.ViewInjectUtil; import com.lepin.util.Constant; import com.lepin.util.UMSharingMyOrder; import com.lepin.util.Util; import com.lepin.util.Util.OnHttpRequestDataCallback; import com.lepin.widget.CircleImageView; import com.lepin.widget.PcbConfirmDialog; import com.lepin.widget.PcbConfirmDialog.OnOkOrCancelClickListener; import com.umeng.socialize.bean.RequestType; import com.umeng.socialize.controller.UMServiceFactory; import com.umeng.socialize.controller.UMSocialService; import com.umeng.socialize.sso.UMSsoHandler; /** * ?,??????? * * */ @Contextview(R.layout.my_order_details) public class MyOrderDetailActivity extends BaseActivity implements OnClickListener { // title @ViewInject(id = R.id.common_title_title) private TextView tvTitle;// @ViewInject(id = R.id.common_title_back) private ImageView btnBack;// @ViewInject(id = R.id.common_title_operater) private TextView btnShare;// // ? @ViewInject(id = R.id.my_order_details_publisher_img) private CircleImageView mPhotoView; // @ViewInject(id = R.id.my_order_details_info_verified) private ImageView mVImageView; // @ViewInject(id = R.id.my_order_details_publisher_name) private TextView mNameText; // ?? @ViewInject(id = R.id.my_order_details_info_phone_btn) private Button mPhoneButton;// ? // ? // ? @ViewInject(id = R.id.my_order_details_publisher_car_brand) private TextView mCarBrandText; // @ViewInject(id = R.id.my_order_details_map) private ImageView mapImageView; // @ViewInject(id = R.id.my_order_details_publisher_car_license) private TextView mCarLicenseText; // @ViewInject(id = R.id.my_order_details_start) private TextView mStarText; // @ViewInject(id = R.id.my_order_details_end) private TextView mEndText; // @ViewInject(id = R.id.my_order_details_publisher_driving_years) private TextView mDrivingYears; // ? @ViewInject(id = R.id.item_text_text_title, parentId = R.id.my_order_details_start_time_layout) private TextView mStartDateTitleText; @ViewInject(id = R.id.item_text_text_value, parentId = R.id.my_order_details_start_time_layout) private TextView mStartDateText; // @ViewInject(id = R.id.item_text_text_title, parentId = R.id.my_order_details_back_time_layout) private TextView mBackDateTitleText; @ViewInject(id = R.id.item_text_text_value, parentId = R.id.my_order_details_back_time_layout) private TextView mBackDateText; // @ViewInject(id = R.id.item_text_text_title, parentId = R.id.my_order_details_cost_layout) private TextView mCostTitleText; @ViewInject(id = R.id.item_text_text_value, parentId = R.id.my_order_details_cost_layout) private TextView mCostText; // ? @ViewInject(id = R.id.item_text_text_title, parentId = R.id.my_order_details_people_num_layout) private TextView mPeopleNumTitle; @ViewInject(id = R.id.item_text_text_value, parentId = R.id.my_order_details_people_num_layout) private TextView mPeopleNum; // @ViewInject(id = R.id.my_order_details_note) private TextView mNoteText; private String book_id;// id @ViewInject(id = R.id.my_order_cancel_and_ok_layout) private LinearLayout mCancelAndOkLayout; /** * ??? ?? ?layout */ @ViewInject(id = R.id.my_order_cancel_layout) private LinearLayout mCancelLayout; // /** * ?? new ? ?? complete */ @ViewInject(id = R.id.btn_driver_cancle) private Button mCancleDriverBtn;// ?? // ? ?? @ViewInject(id = R.id.my_order_btn_cancel) private Button mTwoCancelBtn; // ?? /** * ? operateType = 6 operateType = 1 */ @ViewInject(id = R.id.my_order_btn_pay) private Button mTwoPayOrInBtn;// ? @ViewInject(id = R.id.my_order_detail_calendar_layout) private View mCalendarLayout; // ? /** * */ private static final int COMFIRM_IN = 1;// /** * */ private static final int COMPLETE_PAY_CASH_BOOK = 2;// /** * ? */ private static final int GET_ORDER_MSG = 4;// ? /** * ???? */ private static final int DRIVER_CANCLE_ORDER = 7;// ???? /** * ?? */ private static final int CANCEL_ORDER = 5;// ??? /** * ? */ private static final int PAY = 6; // ?? private int operateType; // ? // private int state;// ? private String mBookState;// ?? private Book book;// private Pinche mPinche;// private User mUser; /** * */ private int mStartLat; /** * ? */ private int mStartLon; /** * */ private int mEndLat; /** * ? */ private int mEndLon; /** * true false */ private boolean isDriverOfMe;// ? /** * true ? false ? */ // private boolean isPublisher;// // private boolean isPassgerCanPay = false;// ?? private boolean isCancelOrder = false;// ?? private boolean isShowCalendar = false; private Util util = Util.getInstance(); UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share", RequestType.SOCIAL); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ViewInjectUtil.inject(this); setTitleText(); initView(); } private void setTitleText() { mStartDateTitleText.setText(getString(R.string.pick_details_start_date)); mBackDateTitleText.setText(getString(R.string.pick_details_back_date)); mCostTitleText.setText(getString(R.string.pick_details_cost_note)); } /** * ? */ private void initView() { final Bundle bundle = getIntent().getExtras(); this.book_id = bundle.getString(Constant.BOOK_ID); this.mStartLat = bundle.getInt(Constant.START_LAT); this.mStartLon = bundle.getInt(Constant.START_LON); this.mEndLat = bundle.getInt(Constant.END_LAT); this.mEndLon = bundle.getInt(Constant.END_LON); isShowCalendar = bundle.getBoolean(Constant.SHOW_CALENDAR); Util.printLog("bookId:" + book_id); this.tvTitle.setText(getResources().getString(R.string.order_details_title));// ?title this.btnBack.setOnClickListener(this); this.btnShare.setOnClickListener(this); this.mPhoneButton.setOnClickListener(this); this.mCancleDriverBtn.setOnClickListener(this); mTwoCancelBtn.setOnClickListener(this); mTwoPayOrInBtn.setOnClickListener(this); mapImageView.setOnClickListener(this); mPhotoView.setOnClickListener(this); } @Override public void onClick(View view) { if (this.btnBack == view) {// finish(); } else if (this.mCancleDriverBtn == view) {// ?? operateType = DRIVER_CANCLE_ORDER; doBtnPress(); } else if (this.mPhoneButton == view) { contactEachOther(); } else if (view == this.mTwoPayOrInBtn) { // if (mBookState.equals(Book.STATE_NEW)) { operateType = PAY; } else if (mBookState.equals(Book.STATE_PAYMENT)) { operateType = COMFIRM_IN; } doBtnPress(); } else if (view == this.mTwoCancelBtn) { operateType = CANCEL_ORDER; isCancelOrder = isCanCancleOrder(mBookState.equals(Book.STATE_PAYMENT)); // ?? // ?????? doBtnPress(); } else if (view == this.btnShare) { util.share(MyOrderDetailActivity.this, book_id, mController, UMSharingMyOrder.SHARE_TYPE_ORDER, Constant.URL_SHARE_ORDER, getString(R.string.share_order_content), getString(R.string.share_order_title)); } else if (view == mapImageView) { Util.getInstance().showStartAndEndOnMap(this, mPinche); } else if (view == mPhotoView) {// ? User user = null; String role = ""; if (book.getDriver() != null) { user = book.getDriver(); role = Constant.DRIVER; } else if (book.getPassenger() != null) { user = book.getPassenger(); role = Constant.PASSENGER; } if (user == null) return; Bundle dataBundle = new Bundle(); dataBundle.putInt("userId", user.getUserId()); dataBundle.putString("role", role); util.go2ActivityWithBundle(MyOrderDetailActivity.this, PersonalInfoTrailActivity.class, dataBundle); } } private void contactEachOther() { String number = ""; if (book.getPassenger() != null) number = book.getPassenger().getTel(); if (book.getDriver() != null) number = book.getDriver().getTel(); Util.getInstance().call(this, number); } protected void doBtnPress() { if (util.isNetworkAvailable(this)) {// ? showDialog(); } else { Util.showToast(MyOrderDetailActivity.this, getString(R.string.network_unavaiable)); } } protected void showDialog() { String title = ""; String ok = ""; String cancel = ""; if ((isCancelOrder && operateType == CANCEL_ORDER) || (operateType == DRIVER_CANCLE_ORDER)) {// ????? title = getString(R.string.is_cancel_order); ok = getString(R.string.dialog_confirm); cancel = getString(R.string.my_info_btn_cancel); } else if (mBookState.equals(Book.STATE_NEW) && operateType == PAY) { // ? title = getString(R.string.complete_carpool_dialog_title); ok = getString(R.string.complete_carpool_online); cancel = getString(R.string.complete_carpool_cash); } else if (mBookState.equals(Book.STATE_PAYMENT) && operateType == COMFIRM_IN) { // title = getString(R.string.order_dialog_comfirm_in_title); ok = getString(R.string.dialog_confirm); cancel = getString(R.string.my_info_btn_cancel); } else if (mBookState.equals(Book.STATE_CONFIRM)) { title = getString(R.string.complete_carpool_dialog_title); ok = getString(R.string.complete_carpool_online); cancel = getString(R.string.complete_carpool_cash); } Util.getInstance().showDialog(MyOrderDetailActivity.this, title, ok, cancel, new OnOkOrCancelClickListener() { @Override public void onOkClick(int type) { if (type == PcbConfirmDialog.OK) { if ((isCancelOrder && operateType == CANCEL_ORDER) || (operateType == DRIVER_CANCLE_ORDER)) { operateBook(CANCEL_ORDER, Constant.URL_CANCLEBOOK_NEW); } else if (operateType == PAY) {// ? go2OnLinePay(); // ?? } else if (mBookState.equals(Book.STATE_PAYMENT)) {// operateBook(COMFIRM_IN, Constant.URL_ORDER_IN); } else if (mBookState.equals(Book.STATE_CONFIRM)) {// if (!isDriverOfMe) { go2OnLinePay(); } } } else { if (mBookState.equals(Book.STATE_NEW) && !isDriverOfMe && operateType == PAY) {// operateType = COMPLETE_PAY_CASH_BOOK; operateBook(COMPLETE_PAY_CASH_BOOK, Constant.URL_PAY_NEW); } } } }); } /** * */ private void loadSingleOrder() { operateBook(GET_ORDER_MSG, Constant.URL_GET_ORDER_BY_ID); } /** * ?view */ private void setBookData2View() { if (book != null) { this.mStarText.setText(this.mPinche.getStart_name(this)); this.mEndText.setText(this.mPinche.getEnd_name(this)); showDriverOrPassengerInfo(); showOrderInfo(); if (isShowCalendar) mCalendarLayout.setVisibility(View.VISIBLE); } } private void showOrderInfo() { if (mPinche.getCarpoolType().equals(Pinche.CARPOOLTYPE_LONG_TRIP)) {// this.mStartDateText.setText(mPinche.getDepartureTime());// ? ((View) findViewById(R.id.my_order_details_back_time_layout)).setVisibility(View.GONE);// ??? } else {// ? if ("".equals(mPinche.getBackTime())) { this.mStartDateText.setText(mPinche.getCycle().getTxt() + " " + getString(R.string.moring) + ":" + mPinche.getDepartureTime());// ??? } else { this.mStartDateText.setText(mPinche.getCycle().getTxt() + " " + mPinche.getDepartureTime());// ?? if (mPinche.getBackTime() == null || mPinche.getBackTime().equals("")) { ((LinearLayout) findViewById(R.id.my_order_details_back_time_layout)).setVisibility(View.GONE);// ??? } else { this.mBackDateText.setText(mPinche.getCycle().getTxt() + " " + mPinche.getBackTime());// ? } } } mCostText.setText(String.valueOf(mPinche.getCharge()));// if (book.getBookType().equals(Pinche.DRIVER)) {// ??? this.mPeopleNumTitle.setText(getString(R.string.avaiable_passenger_num)); } else {// ? this.mPeopleNumTitle.setText(getString(R.string.passenger_number)); } if (!TextUtils.isEmpty(String.valueOf(mPinche.getNum()))) { this.mPeopleNum.setText(String.valueOf(mPinche.getNum())); } else { this.mPeopleNum.setText("0" + getString(R.string.human)); } if (!TextUtils.isEmpty(mPinche.getNote())) { this.mNoteText.setText(mPinche.getNote()); } } private void showDriverOrPassengerInfo() { if (!isDriverOfMe) {// ??? final User driver = book.getDriver(); if (driver != null) { this.mNameText.setText(driver.getUsername(this)); mPhotoView.displayWithUrl(Util.getInstance().getPhotoURL(driver.getUserId()), false, false); } final Car car = driver.getCar(); if (car != null) { mCarLicenseText.setText(car.getLicence(this));// mCarLicenseText.setVisibility(View.VISIBLE); CarType carType = driver.getCar().getCarType(); if (carType != null) { mCarBrandText.setText(carType.getCarTypeName());// } else { mCarBrandText.setText(getString(R.string.unknow)); } mCarBrandText.setVisibility(View.VISIBLE); // ?? if (driver.isUserStateVerify()) { mVImageView.setVisibility(View.VISIBLE); } // mDrivingYears.setText(getString(R.string.driving_years, TextUtils.isEmpty(driver.getDriveAge()) ? getString(R.string.unknow) : driver.getDriveAge())); mDrivingYears.setVisibility(View.VISIBLE); } else { mCarLicenseText.setText(getString(R.string.unknow)); mCarLicenseText.setVisibility(View.VISIBLE); mCarBrandText.setText(getString(R.string.unknow)); mCarBrandText.setVisibility(View.VISIBLE); } // ?? } else {// ?? final User passager = book.getPassenger(); if (passager != null) { mPhotoView.displayWithUrl(Util.getInstance().getPhotoURL(passager.getUserId()), false, false); this.mNameText.setText(passager.getUsername(this)); } if (passager.isUserStateVerify()) { mVImageView.setVisibility(View.VISIBLE); } } } protected void operateBook(final int type, String url) { // 1 ? 2 ? 4?? List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("infoOrderId", this.book_id)); if (type == COMPLETE_PAY_CASH_BOOK) { params.add(new BasicNameValuePair("payType", Constant.PAY_OFFLINE));// OFFLINE // ONLINE } String loadingMsg = ""; if (type == COMFIRM_IN) { loadingMsg = getString(R.string.comfirm_order_in); } else if (type == COMPLETE_PAY_CASH_BOOK) { loadingMsg = getString(R.string.complete_order_ing); } else if (type == GET_ORDER_MSG) { loadingMsg = getString(R.string.order_detail_loading); } else if (type == CANCEL_ORDER) { loadingMsg = getString(R.string.order_cancel_ing); } util.doPostRequest(MyOrderDetailActivity.this, new OnHttpRequestDataCallback() { public void onSuccess(String result) { if (type == GET_ORDER_MSG) {// ??? setViewValueAndBtnState(result); } else { JsonResult<String> jsonResult = util.getObjFromJsonResult(result, new TypeToken<JsonResult<String>>() { }); final boolean isSuccess = jsonResult.isSuccess(); if (type == COMFIRM_IN) { if (isSuccess) {// ? ? Util.showLongToast(MyOrderDetailActivity.this, jsonResult.getData()); mCancelAndOkLayout.setVisibility(View.GONE); mCancelLayout.setVisibility(View.VISIBLE); setBtnTextAndColor(R.string.order_state_complete, Color.GRAY, false, View.VISIBLE); } else { mCancelLayout.setVisibility(View.VISIBLE); mCancleDriverBtn.setVisibility(View.VISIBLE); Util.showLongToast(MyOrderDetailActivity.this, jsonResult.getData()); } } else if (type == COMPLETE_PAY_CASH_BOOK) { if (isSuccess) { mCancelAndOkLayout.setVisibility(View.GONE); mCancleDriverBtn.setVisibility(View.VISIBLE); mCancelLayout.setVisibility(View.VISIBLE); setBtnTextAndColor(R.string.order_state_complete, Color.WHITE, false, View.VISIBLE); } Util.showLongToast(MyOrderDetailActivity.this, jsonResult.getData()); } else if (type == CANCEL_ORDER) { if (isSuccess) {// ??? Util.showLongToast(MyOrderDetailActivity.this, jsonResult.getData()); mCancelAndOkLayout.setVisibility(View.GONE); mCancelLayout.setVisibility(View.VISIBLE); setBtnTextAndColor(R.string.has_been_cancel, Color.WHITE, false, View.VISIBLE); } else {// ?? Util.showLongToast(MyOrderDetailActivity.this, jsonResult.getData()); } } } } @Override public void onFail(String errorType, String errorMsg) { // TODO Auto-generated method stub super.onFail(errorType, errorMsg); Util.showToast(MyOrderDetailActivity.this, errorMsg); } }, params, url, loadingMsg, true); } protected void setBtnTextAndColor(int textId, int colorId, boolean isCanPressed, int visibility) { mCancleDriverBtn.setText(getString(textId)); mCancleDriverBtn.setTextColor(colorId); mCancleDriverBtn.setClickable(isCanPressed); mCancleDriverBtn.setVisibility(visibility); mCancelLayout.setVisibility(View.VISIBLE); } @Override protected void onResume() { super.onResume(); mUser = util.getLoginUser(MyOrderDetailActivity.this); loadSingleOrder(); } @Override protected void onPause() { super.onPause(); } protected void go2OnLinePay() { Util.getInstance().go2OnLinePay(this, book.getPrice(), this.book_id, mPinche.getStart_name(this), mPinche.getEnd_name(), mStartLat, mStartLon, mEndLat, mEndLon, mPinche.getCarpoolType()); } /** * @param result */ private void setViewValueAndBtnState(String result) { JsonResult<Book> jsonResult = util.getObjFromJsonResult(result, new TypeToken<JsonResult<Book>>() { }); if (jsonResult != null && jsonResult.isSuccess()) { if (!util.isDataExist(jsonResult)) { Util.showToast(MyOrderDetailActivity.this, getString(R.string.common_no_data)); return; } book = jsonResult.getData();// ?? if (book == null) { Util.showToast(MyOrderDetailActivity.this, getString(R.string.common_no_data)); return; } mBookState = book.getState();// ?? this.mPinche = util.string2Bean(this.book.getSnapshot(), Pinche.class); isDriverOfMe = mUser.getUserId() == book.getDriverId(); setBookData2View();// ?? showBtnState(); } else { Util.showToast(MyOrderDetailActivity.this, jsonResult.getErrorMsg()); } } private void showBtnState() { /* ?? */ if (mBookState.equals(Book.STATE_CANCEL)) { setBtnTextAndColor(R.string.has_been_cancel, Color.WHITE, false, View.VISIBLE); } else if (mBookState.equals(Book.STATE_NEW)) {// ?????? if (!isDriverOfMe) {// ?? mCancelAndOkLayout.setVisibility(View.VISIBLE); operateType = CANCEL_ORDER; } else { // ?? setBtnTextAndColor(R.string.order_operate_cancle, Color.WHITE, true, View.VISIBLE); } } else if (mBookState.equals(Book.STATE_PAYMENT)) {// ?? if (!isDriverOfMe) { // ? mCancelAndOkLayout.setVisibility(View.VISIBLE); mTwoPayOrInBtn.setText(getResources().getString(R.string.order_operate_in)); isCanCancleOrder(true); operateType = COMFIRM_IN; } else { // ?? setBtnTextAndColor(R.string.order_operate_driver_cancle, Color.RED, true, View.VISIBLE); } } else if (mBookState.equals(Book.STATE_COMPLETE)) {// ?? setBtnTextAndColor(R.string.order_state_complete, Color.WHITE, false, View.VISIBLE); } } /** * @return ??????? */ private Boolean isCanCancleOrder(boolean isPaymentState) { // TODO Auto-generated method stub long currentTime = System.currentTimeMillis(); long bookRideTime = book.getRideTime() * 1000L; Boolean isCanCancleOrder = (bookRideTime * 1L - currentTime * 1L) >= 2 * 60 * 60 * 1000L ? true : false; if (!isCanCancleOrder && isPaymentState) { mTwoCancelBtn.setClickable(false); mTwoCancelBtn.setText("???"); } return isCanCancleOrder; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); /** SSO?? */ UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode); if (ssoHandler != null) { ssoHandler.authorizeCallBack(requestCode, resultCode, data); } } @Override public void onBackPressed() { finish(); } }