com.lepin.activity.PincheTrailActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.lepin.activity.PincheTrailActivity.java

Source

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.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.lepin.entity.Car;
import com.lepin.entity.JsonResult;
import com.lepin.entity.Pinche;
import com.lepin.entity.Point;
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.Util;
import com.lepin.util.Util.OnHttpRequestDataCallback;
import com.lepin.widget.CircleImageView;

/**
 * ????????
 * 
 */
@Contextview(R.layout.pinche_trail_details)
public class PincheTrailActivity extends BaseActivity implements OnClickListener {
    @ViewInject(id = R.id.common_title_title)
    private TextView mTitle;// 

    @ViewInject(id = R.id.common_title_back)
    private ImageView mTitleBack;// 

    // ?
    @ViewInject(id = R.id.pinche_trail_details_publisher_img)
    private CircleImageView mPhotoView;
    // 
    @ViewInject(id = R.id.pinche_trail_details_publisher_name)
    private TextView mNameText;

    // ?
    @ViewInject(id = R.id.pinche_trail_details_info_appointment)
    private Button mappointment;//

    // ?

    // ?// 
    @ViewInject(id = R.id.pinche_trail_details_publisher_car_brand)
    private TextView mCarBrandText;

    // 
    @ViewInject(id = R.id.pinche_trail_details_map)
    private ImageView mapImageView;
    // 
    @ViewInject(id = R.id.pinche_trail_details_publisher_car_license)
    private TextView mCarLicenseText;
    // ?
    @ViewInject(id = R.id.pinche_trail_details_info_verified)
    private ImageView mUserISVerified;

    // 
    @ViewInject(id = R.id.pinche_trail_details_publisher_car_license)
    private TextView mGenderText;

    // 
    @ViewInject(id = R.id.pinche_trail_details_start)
    private TextView mStarText;

    // 
    @ViewInject(id = R.id.pinche_trail_details_end)
    private TextView mEndText;

    @ViewInject(id = R.id.point_layout)
    private RelativeLayout pointLayout;

    // ??
    @ViewInject(id = R.id.line)
    private View line1;

    // 1
    @ViewInject(id = R.id.point1)
    private TextView point1;

    // ?
    @ViewInject(id = R.id.line1)
    private View line2;

    // ?2
    @ViewInject(id = R.id.point2)
    private TextView point2;
    // 
    @ViewInject(id = R.id.pinche_trail_details_publisher_driving_years)
    private TextView mDrivingYears;
    // ?
    @ViewInject(id = R.id.item_text_text_title, parentId = R.id.pinche_trail_details_start_time_layout)
    private TextView mStartDateTitleText;

    @ViewInject(id = R.id.item_text_text_value, parentId = R.id.pinche_trail_details_start_time_layout)
    private TextView mStartDateText;

    // 
    @ViewInject(id = R.id.pinche_trail_details_back_time_layout)
    private View mBackDateLayout;

    @ViewInject(id = R.id.item_text_text_title, parentId = R.id.pinche_trail_details_back_time_layout)
    private TextView mBackDateTitleText;

    @ViewInject(id = R.id.item_text_text_value, parentId = R.id.pinche_trail_details_back_time_layout)
    private TextView mBackDateText;

    // 

    @ViewInject(id = R.id.item_text_text_title, parentId = R.id.pinche_trail_details_cost_layout)
    private TextView mCostTitleText;

    @ViewInject(id = R.id.item_text_text_value, parentId = R.id.pinche_trail_details_cost_layout)
    private TextView mCostText;

    // ?
    @ViewInject(id = R.id.pinche_trail_details_people_num_layout)
    private View mPeopleNumLayout;

    @ViewInject(id = R.id.item_text_text_title, parentId = R.id.pinche_trail_details_people_num_layout)
    private TextView mPeopleNumTitle;

    @ViewInject(id = R.id.item_text_text_value, parentId = R.id.pinche_trail_details_people_num_layout)
    private TextView mPeopleNum;

    // 
    @ViewInject(id = R.id.pinche_trail_details_note)
    private EditText mNoteText;

    private int pinche_id;// ??id
    private Pinche mPincheDetails;// ?

    /**
     * 
     */
    private int mStartLat;
    /**
     * ?
     */
    private int mStartLon;
    /**
     * 
     */
    private int mEndLat;
    /**
     * ?
     */
    private int mEndLon;

    private Util util = Util.getInstance();
    /**
     *    /?
     */
    private String role = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewInjectUtil.inject(this);
        initView();
        loadPinche();
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        loadPinche();
    }

    /**
     * ?
     */
    private void initView() {
        final Bundle bundle = getIntent().getExtras();
        pinche_id = bundle.getInt("PincheId");
        this.mTitleBack.setOnClickListener(this);
        this.mPhotoView.setOnClickListener(this);
        this.mapImageView.setOnClickListener(this);
        this.mTitle.setText(getResources().getString(R.string.pick_trail_Title));// ?title
    }

    /**
     * 
     */
    public void setPincheDetails() {
        final Car car = mPincheDetails.getCar();
        final User user = mPincheDetails.getUser();

        if (this.mPincheDetails != null) {
            mStartLat = mPincheDetails.getStartLat();
            mStartLon = mPincheDetails.getStartLon();
            mEndLat = mPincheDetails.getEndLat();
            mEndLon = mPincheDetails.getEndLon();
            this.mStarText.setText(mPincheDetails.getStart_name());
            this.mEndText.setText(mPincheDetails.getEnd_name());
            String urlString = util.getPhotoURL(user.getUserId());
            this.mPhotoView.displayWithUrl(urlString, false, false);
            mNameText.setText(user.getUsername());
            // ?
            this.mStartDateTitleText.setText(getResources().getString(R.string.pick_details_start_date));

            this.mStartDateText.setText(mPincheDetails.getCarpoolType().equals(Pinche.CARPOOLTYPE_ON_OFF_WORK)
                    ? mPincheDetails.getCycle().getTxt() + " " + mPincheDetails.getDepartureTime()
                    : mPincheDetails.getDepartureTime());

            if (user != null && user.isUserStateVerify()) {
                this.mUserISVerified.setVisibility(View.VISIBLE);
            }
            if (mPincheDetails.getInfoType().equals(Pinche.DRIVER)) {
                // (??)
                role = Constant.DRIVER;
                this.mGenderText.setVisibility(View.GONE);
                this.mCarBrandText.setText(car.getCarType().getCarTypeName());
                mDrivingYears.setVisibility(View.VISIBLE);
                mCarLicenseText.setVisibility(View.VISIBLE);
                this.mDrivingYears.setText(user.getDriveAge());
                if (!TextUtils.isEmpty(car.getLicence())) {
                    this.mCarLicenseText.setText(mPincheDetails.isBooking() ? car.getLicence()
                            : car.getLicence().substring(0, 3) + "**" + car.getLicence().substring(5, 6));
                }
                // ?
                if (mPincheDetails.getCarpoolType().equals(Pinche.CARPOOLTYPE_ON_OFF_WORK)) {
                    this.mBackDateTitleText.setText(getResources().getString(R.string.pick_details_back_date));
                    mBackDateLayout.setVisibility(View.VISIBLE);
                    this.mBackDateText
                            .setText(mPincheDetails.getCycle().getTxt() + " " + mPincheDetails.getBackTime());
                }
                this.mCostTitleText.setText(getResources().getString(R.string.pick_details_cost_note));
                this.mCostText.setText(mPincheDetails.getCharge() + "");
                this.mPeopleNumTitle.setText(getResources().getString(R.string.pick_details_total_people));
                this.mPeopleNum.setText(mPincheDetails.getNum() + "");
                // 
                Point[] points = mPincheDetails.getPoints();
                if (null != points) {
                    TextView[] pointsViews = { point1, point2 };
                    for (int i = 0; i < points.length; i++) {
                        pointLayout.setVisibility(View.VISIBLE);
                        line1.setVisibility(View.VISIBLE);
                        pointsViews[i].setVisibility(View.VISIBLE);
                        pointsViews[i].setText(points.length == 1 ? "?" + points[i].getName()
                                : "" + (i + 1) + ":" + points[i].getName());
                        if (i == 1) {
                            line2.setVisibility(View.VISIBLE);
                        }
                    }
                }
            } else {// ?
                role = Constant.PASSENGER;
                mPeopleNumLayout.setVisibility(View.GONE);
                mCarBrandText.setText(user.getGender(this));
                if (mPincheDetails.getCarpoolType().equals(Pinche.CARPOOLTYPE_ON_OFF_WORK)) {
                    mBackDateLayout.setVisibility(View.VISIBLE);
                    this.mBackDateTitleText.setText(getResources().getString(R.string.pick_details_back_date));
                    this.mBackDateText.setText(mPincheDetails.getBackTime());
                }
                this.mCostTitleText.setText(getResources().getString(R.string.pick_details_cost));
                this.mCostText.setText(mPincheDetails.getCharge() + "");
            }
            String note = mPincheDetails.getNote();
            if (!TextUtils.isEmpty(note))
                mNoteText.setText(note);
            if (util.getLoginUser(PincheTrailActivity.this) != null
                    && mPincheDetails.getUser_id() == util.getLoginUser(PincheTrailActivity.this).getUserId()) {// ??
                mappointment.setVisibility(View.GONE);
            } else {
                // ?
                if (mPincheDetails.isBooking()) {// ?
                    mappointment.setFocusable(false);
                    mappointment.setText("?");
                } else {
                    mappointment.setOnClickListener(this);
                }
            }
        }
    }

    /**
     * ?
     */
    private void loadPinche() {
        final String mUrl = "infoId=" + pinche_id;// ?

        util.doGetRequest(PincheTrailActivity.this, new OnHttpRequestDataCallback() {

            @Override
            public void onSuccess(String result) {
                // TODO Auto-generated method stub
                JsonResult<Pinche> jsonResult = util.getObjFromJsonResult(result,
                        new TypeToken<JsonResult<Pinche>>() {
                        });
                Util.printLog(":" + jsonResult.toString());
                if (jsonResult != null && jsonResult.isSuccess()) {
                    mPincheDetails = jsonResult.getData();
                    setPincheDetails();// ??
                } else {
                    Util.showToast(PincheTrailActivity.this, getString(R.string.request_error));
                }
            }
        }, Constant.URL_GET_INFO_BY_ID + mUrl, getString(R.string.lepin_dialog_loading), false);

    }

    @Override
    public void onClick(View view) {
        if (this.mTitleBack == view) {// 
            PincheTrailActivity.this.finish();
        } else if (this.mappointment == view) {// ?
            if (util.isUserLoging(PincheTrailActivity.this)) {
                // ??
                getBook();

            } else {// ?
                util.go2Activity(PincheTrailActivity.this, LoginActivity.class);
            }
        } else if (this.mPhotoView == view) {
            // ???
            // ?
            // 
            if (util.isUserLoging(PincheTrailActivity.this)) {
                // ?
                if (util.getLoginUser(PincheTrailActivity.this) != null
                        && mPincheDetails.getUser_id() == util.getLoginUser(PincheTrailActivity.this).getUserId()) {
                    // 
                    util.go2Activity(PincheTrailActivity.this, PersonalInfoActivity.class);
                } else {
                    int userId = mPincheDetails.getUser().getUserId();
                    Bundle dataBundle = new Bundle();
                    dataBundle.putInt("userId", userId);
                    dataBundle.putString("role", role);
                    util.go2ActivityWithBundle(PincheTrailActivity.this, PersonalInfoTrailActivity.class,
                            dataBundle);

                }
            } else {
                // ?
                Util.getInstance().go2Activity(PincheTrailActivity.this, LoginActivity.class);
            }

        } else if (view == mapImageView) {
            if (null == mPincheDetails.getStartLat() || null == mPincheDetails.getStartLon()
                    || null == mPincheDetails.getEndLat() || null == mPincheDetails.getEndLon()) {
                Util.showToast(PincheTrailActivity.this,
                        getResources().getString(R.string.pinche_trail_no_location));
            } else {
                Util.getInstance().showStartAndEndOnMap(PincheTrailActivity.this, mPincheDetails);
            }
        }
    }

    /**
     * ?
     */
    private void getBook() {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("infoId", String.valueOf(pinche_id)));

        util.doPostRequest(PincheTrailActivity.this, new OnHttpRequestDataCallback() {

            public void onSuccess(String result) {
                String bookId = doBooking(result);
                if (null != bookId) {
                    Bundle bundle = new Bundle();
                    bundle.putString(Constant.BOOK_ID, bookId);
                    bundle.putInt(Constant.START_LAT, mStartLat);
                    bundle.putInt(Constant.START_LON, mStartLon);
                    bundle.putInt(Constant.END_LAT, mEndLat);
                    bundle.putInt(Constant.END_LON, mEndLon);
                    util.go2OrderDetail(PincheTrailActivity.this, bundle);
                }
            }
        }, params, Constant.URL_ADDBOOK, getString(R.string.my_book_ing) + "...", false);
    }

    private String doBooking(String result) {
        JsonResult<String> jsonResult = util.getObjFromJsonResult(result, new TypeToken<JsonResult<String>>() {
        });
        String bookIdString = null;
        if (jsonResult != null) {
            if (jsonResult.isSuccess()) {
                // ????
                bookIdString = jsonResult.getData();
            } else {
                Util.showToast(PincheTrailActivity.this, jsonResult.getErrorMsg());
            }
        }
        return bookIdString;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
    }
}