|
package com.electric.chargingpile.activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.Spanned;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.blankj.utilcode.util.EmptyUtils;
import com.blankj.utilcode.util.LogUtils;
import com.electric.chargingpile.R;
import com.electric.chargingpile.application.MainApplication;
import com.electric.chargingpile.data.PileData;
import com.electric.chargingpile.data.ZhuangStatus;
import com.electric.chargingpile.util.BarColorUtil;
import com.electric.chargingpile.util.CustomImageSpan;
import com.electric.chargingpile.util.JsonUtils;
import com.electric.chargingpile.util.ToastUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import java.net.URLEncoder;
import java.util.Formatter;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import me.codeboy.android.aligntextview.AlignTextView;
import okhttp3.Call;
public class PileDetailsActivity extends AppCompatActivity implements View.OnClickListener {
private String pileId, zhanId;
private ImageView ivBack;
private TextView tvPileOperation, tvPileSearch, tvOrderTime, tvPileId, tvPileStatus, tvPileInfo, tvPilePower, tvPileVoltage, tvCurrentCharging, tvCurrentService, tvOrderCharging, tvLockTime, tvPileTips, tvStartCharging, tvChargingInfo;
private LinearLayout llOrderTime, llOperateLock;
private AlignTextView tvCurrentPark;
private Timer timer;
private int time;
private PileData pileData;
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 2:
tvOrderTime.setText(msg.obj.toString());
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pile_details);
BarColorUtil.initStatusBarColor(PileDetailsActivity.this);
initViews();
initEvents();
getIntentData();
// getPTData();
}
private void getIntentData() {
pileId = getIntent().getStringExtra("pileId");
zhanId = getIntent().getStringExtra("zhanId");
if (EmptyUtils.isNotEmpty(pileId) && EmptyUtils.isNotEmpty(zhanId)) {
getPileInfo();
}
}
private void initViews() {
ivBack = findViewById(R.id.iv_back);
tvPileOperation = findViewById(R.id.tv_pile_operation);
tvPileSearch = findViewById(R.id.tv_pile_search);
llOrderTime = findViewById(R.id.ll_order_time);
tvOrderTime = findViewById(R.id.tv_order_time);
tvPileId = findViewById(R.id.tv_pile_id);
tvPileStatus = findViewById(R.id.tv_pile_status);
tvPileInfo = findViewById(R.id.tv_pile_info);
tvPilePower = findViewById(R.id.tv_pile_power);
tvPileVoltage = findViewById(R.id.tv_pile_voltage);
tvCurrentCharging = findViewById(R.id.tv_current_charging);
tvCurrentService = findViewById(R.id.tv_current_service);
tvCurrentPark = findViewById(R.id.tv_current_park);
tvOrderCharging = findViewById(R.id.tv_order_charging);
tvLockTime = findViewById(R.id.tv_lock_time);
llOperateLock = findViewById(R.id.ll_operate_lock);
tvPileTips = findViewById(R.id.tv_pile_tips);
tvStartCharging = findViewById(R.id.tv_start_charging);
tvChargingInfo = findViewById(R.id.tv_charging_info);
timer = new Timer();
// setBtnViewShow(tvPileSearch,"找地锁",R.drawable.icon_search_lock,getApplicationContext());
}
private void initEvents() {
ivBack.setOnClickListener(this);
tvPileOperation.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.iv_back:
finish();
break;
case R.id.tv_order_charging:
break;
case R.id.tv_pile_operation:
break;
}
}
private void getPileInfo() {
String url = MainApplication.url + "/zhannew/basic/web/index.php/zhanlist/pile-info";
OkHttpUtils.get().url(url)
.addParams("pile_id", pileId)
.addParams("zhan_id", zhanId)
.build().execute(new StringCallback() {
@Override
public void onError(Call call, Exception e) {
}
@Override
public void onResponse(String response) {
LogUtils.e(response);
String rtnCode = JsonUtils.getKeyResult(response, "rtnCode");
if ("01".equals(rtnCode)) {
String data = JsonUtils.getKeyResult(response, "data");
pileData = JsonUtils.parseToObjectBean(data, PileData.class);
// time = Integer.parseInt(pileData.getRes_timer())*60;
// setTimerTasks();
// setBtnViewShow(tvPileOperation,"降地锁",R.drawable.icon_down_lock,getApplicationContext());
// tvOrderCharging.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// showOrderDialog(pileData);
// }
// });
// tvChargingInfo.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Intent intent = new Intent(getApplicationContext(),PileCostDetailsActivity.class);
// intent.putExtra("PileData",pileData);
// startActivity(intent);
//
// }
// });
showPileInfo(pileData);
}
}
});
}
private void setTimerTasks() {
if (timer != null) {
timer.schedule(new TimerTask() {
@Override
public void run() {
time--;
int sm = 1000 * time;
String smh = stringForTime(sm);
Message message = new Message();
message.obj = smh;
message.what = 2;
mHandler.sendMessage(message);
}
}, 0, 1000/* 表示1000毫秒之後,每隔1000毫秒執行一次 */);
}
}
private void setBtnViewShow(TextView view, String s, int drawableId, Context context) {
SpannableString spannableString = new SpannableString(" " + s);
//调用自定义的imageSpan,实现文字与图片的横向居中对齐
CustomImageSpan imageSpan;
imageSpan = new CustomImageSpan(context, drawableId, 2);
spannableString.setSpan(imageSpan, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
view.setText(spannableString);
}
private void showPileInfo(PileData pileData) {
//判断桩状态的显示
tvPileStatus.setText(pileData.getStatus());
if (pileData.getStatus().equals("空闲中")) {
tvPileStatus.setBackgroundColor(MainApplication.context.getResources().getColor(R.color.lvse));
} else if (pileData.getStatus().equals("充电中") || pileData.getStatus().equals("已预约")) {
tvPileStatus.setBackgroundColor(MainApplication.context.getResources().getColor(R.color.juhuang));
} else {
tvPileStatus.setBackgroundColor(MainApplication.context.getResources().getColor(R.color.ui_68));
}
String type, yearType = null, have_gun;
if ("1".equals(pileData.getEleType())) {
type = "快充";
} else {
type = "慢充";
}
switch (pileData.getNationalStandard()) {
case "1":
yearType = " | 国标2011";
break;
case "2":
yearType = " | 国标2015";
break;
case "3":
yearType = " | 国标2015,国标2011";
case "0":
yearType = "";
break;
}
//"has_gun": "0",//1有枪,2没有枪,0 没有这个桩的信息
switch (pileData.getHas_gun()) {
case "1":
have_gun = " | 有枪";
break;
case "2":
have_gun = " | 无枪,需自带";
break;
default:
have_gun = "";
break;
}
tvPileInfo.setText(type + yearType + have_gun);
tvCurrentPark.setText(pileData.getFee().getStop_cost());
ViewTreeObserver vto = tvCurrentPark.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
tvCurrentPark.getViewTreeObserver().removeGlobalOnLayoutListener(this);
tvCurrentPark.getHeight();
double w0 = tvCurrentPark.getWidth();//控件宽度
double w1 = tvCurrentPark.getPaint().measureText(tvCurrentPark.getText().toString());//文本宽度
if (w1 >= w0) {
// detail_charge_cost.setGravity(Gravity.LEFT);
tvCurrentPark.setAlign(AlignTextView.Align.ALIGN_LEFT);
}//需要换行就显示该控件
}
});
tvCurrentService.setText(pileData.getFee().getOther_cost());
tvCurrentCharging.setText(pileData.getFee().getCharge_cost());
if ("0".equals(pileData.getPower())) {
tvPilePower.setText("--");
} else {
tvPilePower.setText(pileData.getPower() + "kw");
}
if ("0".equals(pileData.getWattmeter())) {
tvPileVoltage.setText("--");
} else {
tvPileVoltage.setText(pileData.getWattmeter() + "V");
}
tvPileId.setText("桩编码:" + pileData.getPile_id());
//判断升降地锁和找地锁的View是否显示
if ("0".equals(pileData.getLock_num())) {
llOperateLock.setVisibility(View.GONE);
tvLockTime.setVisibility(View.GONE);
} else {
llOperateLock.setVisibility(View.VISIBLE);
}
//判断预约充电提示文字是否显示
if ("1".equals(pileData.getRes_status())) {
tvPileTips.setVisibility(View.VISIBLE);
} else {
tvPileTips.setVisibility(View.GONE);
}
//判断预约充电和开始充电按钮是否显示
if ("空闲中".equals(pileData.getStatus())) {
tvStartCharging.setVisibility(View.VISIBLE);
if ("1".equals(pileData.getRes_status())) {
tvOrderCharging.setVisibility(View.VISIBLE);
} else {
tvOrderCharging.setVisibility(View.GONE);
}
} else {
tvStartCharging.setVisibility(View.GONE);
tvOrderCharging.setVisibility(View.GONE);
}
}
private void showOrderDialog(PileData pileData) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("确认预约");
builder.setMessage("预约状态每分钟" + pileData.getRes_fee() + "元,时长超过" + pileData.getRes_timer() + "分钟系统将自动取消");
builder.setCancelable(false);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("取消", null);
builder.create().show();
}
/**
* 把毫秒转换成:1:20:30这样的形式
*
* @param timeMs
* @return
*/
public String stringForTime(int timeMs) {
StringBuilder mFormatBuilder;
Formatter mFormatter;
mFormatBuilder = new StringBuilder();
mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
int totalSeconds = timeMs / 1000;
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
mFormatBuilder.setLength(0);
if (hours > 0) {
return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
} else {
return mFormatter.format("%02d:%02d", minutes, seconds).toString();
}
}
private void initLocation() {
//声明mlocationClient对象
final AMapLocationClient mlocationClient;
//声明mLocationOption对象
AMapLocationClientOption mLocationOption = null;
mlocationClient = new AMapLocationClient(this);
//初始化定位参数
mLocationOption = new AMapLocationClientOption();
//设置定位监听
mlocationClient.setLocationListener(new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation amapLocation) {
if (amapLocation != null) {
if (amapLocation.getErrorCode() == 0) {
//定位成功回调信息,设置相关消息
amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
amapLocation.getLatitude();//获取纬度
amapLocation.getLongitude();//获取经度
amapLocation.getAccuracy();//获取精度信息
LogUtils.e(amapLocation.getLatitude() + "--" + amapLocation.getLongitude());
mlocationClient.stopLocation();
sendControlParkingDownLock(amapLocation, pileData);
} else {
//显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
Log.e("AmapError", "location Error, ErrCode:"
+ amapLocation.getErrorCode() + ", errInfo:"
+ amapLocation.getErrorInfo());
}
}
}
});
//设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//设置定位间隔,单位毫秒,默认为2000ms// mLocationOption.setInterval(2000);
//设置定位参数
mlocationClient.setLocationOption(mLocationOption);
// 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
// 注意设置合适的定位时间的间隔(最小间隔支持为1000ms),并且在合适时间调用stopLocation()方法来取消定位请求
// 在定位结束后,在合适的生命周期调用onDestroy()方法
// 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
//启动定位
mlocationClient.startLocation();
}
private void sendControlParkingDownLock(AMapLocation amapLocation, PileData pileData) {
String url = MainApplication.url + "/zhannew/basic/web/index.php/zhanlist/lock-down";
OkHttpUtils.get().url(url)
.addParams("lock_num", pileData.getLock_num())
.addParams("phone", MainApplication.userPhone)
.addParams("password", URLEncoder.encode(MainApplication.userPassword))
.addParams("poi_jing", String.valueOf(amapLocation.getLongitude()))
.addParams("poi_wei", String.valueOf(amapLocation.getLatitude()))
.build().execute(new StringCallback() {
@Override
public void onError(Call call, Exception e) {
}
@Override
public void onResponse(String response) {
LogUtils.e(response);
String rtnCode = JsonUtils.getKeyResult(response, "rtnCode");
if ("02".equals(rtnCode)) {
String rtnMsg = JsonUtils.getKeyResult(response, "rtnMsg");
ToastUtil.showToast(getApplicationContext(), rtnMsg, Toast.LENGTH_SHORT);
}
}
});
}
private void sendControlParkingUpLock(ZhuangStatus zhuangStatus) {
String url = MainApplication.url + "/zhannew/basic/web/index.php/zhanlist/lock-up";
OkHttpUtils.get().url(url)
.addParams("lock_num", zhuangStatus.getLock_num())
.addParams("phone", MainApplication.userPhone)
.addParams("password", URLEncoder.encode(MainApplication.userPassword))
.build().execute(new StringCallback() {
@Override
public void onError(Call call, Exception e) {
}
@Override
public void onResponse(String response) {
LogUtils.e(response);
String rtnCode = JsonUtils.getKeyResult(response, "rtnCode");
if ("02".equals(rtnCode)) {
String rtnMsg = JsonUtils.getKeyResult(response, "rtnMsg");
ToastUtil.showToast(getApplicationContext(), rtnMsg, Toast.LENGTH_SHORT);
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (null != timer) {
timer.cancel();
timer = null;
}
}
}
|