|
package com.electric.chargingpile.activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.blankj.utilcode.util.LogUtils;
import com.electric.chargingpile.BuildConfig;
import com.electric.chargingpile.R;
import com.electric.chargingpile.application.MainApplication;
import com.electric.chargingpile.data.PileData;
import com.electric.chargingpile.data.RecommendZhan;
import com.electric.chargingpile.util.BarColorUtil;
import com.electric.chargingpile.util.JsonUtils;
import com.electric.chargingpile.util.LoadingDialog;
import com.electric.chargingpile.util.ToastUtil;
import com.electric.chargingpile.util.Util;
import com.umeng.analytics.MobclickAgent;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import okhttp3.Call;
import static com.electric.chargingpile.activity.MainMapActivity.getMD5;
public class RecommendChargingStationActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "RecommendChargingStationActivity";
List<RecommendZhan> recommendZhanList;
List<RecommendZhan> zhanList;
private LoadingDialog loadingDialog;
private View popupWindowView;
private PopupWindow popupWindow;
private Button cancleButton;
private TextView tvOne, tvTwo, tvThree;
private Double bd_lon, bd_lat, bd_jing, bd_wei;
private String center_jing, center_wei, gd_jing, gd_wei, bd_jing1, bd_wei1;
private LatLng center;
private RecommendChargingStationActivity activity;
private LinearLayout first_condition;
private TextView first_condition_text;
private ImageView first_condition_image;
private int firstCondition = 3;
private TextView first_can_charging;
private TextView first_free_parking;
private TextView first_quick_charging;
private TextView first_slow_charging;
private Boolean firstCanCharging = false;
private Boolean firstFreeParking = false;
private Boolean firstQuickCharging = false;
private Boolean firstSlowCharging = false;
private ConstraintLayout condition_second_bar;
private Button second_1;
private Button second_2;
private Button second_3;
private Button second_4;
private Button second_5;
private RecyclerView recyclerView;
private RecommendChargingStationAdapter recommendChargingStationAdapter;
private TextView no_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recommend_charging_station);
BarColorUtil.initStatusBarColor(RecommendChargingStationActivity.this);
initView();
MobclickAgent.onEvent(getApplicationContext(), "1136");
if (zhanList.size() == 0) {
loadingDialog.show();
ete("{\"fun\":\"timer\"}");
} else {
filterChargingStation();
}
}
private void initView() {
recommendZhanList = new ArrayList<>();
zhanList = new ArrayList<>();
activity = this;
loadingDialog = new LoadingDialog(this);
loadingDialog.setCanceledOnTouchOutside(false);
no_result = findViewById(R.id.no_result);
center_jing = getIntent().getStringExtra("center_jing");
center_wei = getIntent().getStringExtra("center_wei");
center = new LatLng(Double.parseDouble(center_wei), Double.parseDouble(center_jing));
findViewById(R.id.iv_back).setOnClickListener(this);
first_condition = findViewById(R.id.first_condition);
first_condition.setOnClickListener(this);
first_condition_text = findViewById(R.id.first_condition_text);
first_condition_image = findViewById(R.id.first_condition_image);
first_can_charging = findViewById(R.id.first_can_charging);
first_can_charging.setOnClickListener(this);
first_free_parking = findViewById(R.id.first_free_parking);
first_free_parking.setOnClickListener(this);
first_quick_charging = findViewById(R.id.first_quick_charging);
first_quick_charging.setOnClickListener(this);
first_slow_charging = findViewById(R.id.first_slow_charging);
first_slow_charging.setOnClickListener(this);
condition_second_bar = findViewById(R.id.condition_second_bar);
condition_second_bar.setOnClickListener(this);
second_1 = findViewById(R.id.second_1);
second_2 = findViewById(R.id.second_2);
second_3 = findViewById(R.id.second_3);
second_4 = findViewById(R.id.second_4);
second_5 = findViewById(R.id.second_5);
second_1.setOnClickListener(this);
second_2.setOnClickListener(this);
second_3.setOnClickListener(this);
second_4.setOnClickListener(this);
second_5.setOnClickListener(this);
recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recommendChargingStationAdapter = new RecommendChargingStationAdapter();
recyclerView.setAdapter(recommendChargingStationAdapter);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_back:
finish();
break;
case R.id.first_condition:
if (condition_second_bar.getVisibility() == View.VISIBLE) {
foldSecondCondition();
} else {
unfoldSecondCondition();
}
break;
case R.id.first_can_charging:
firstCanCharging = !firstCanCharging;
first_can_charging.setTextColor(firstCanCharging ? Color.parseColor("#2FC45B") : Color.parseColor("#222222"));
foldSecondCondition();
filterChargingStation();
if (firstCanCharging) {
MobclickAgent.onEvent(getApplicationContext(), "1141");
}
break;
case R.id.first_free_parking:
firstFreeParking = !firstFreeParking;
first_free_parking.setTextColor(firstFreeParking ? Color.parseColor("#2FC45B") : Color.parseColor("#222222"));
foldSecondCondition();
filterChargingStation();
if (firstFreeParking) {
MobclickAgent.onEvent(getApplicationContext(), "1143");
}
break;
case R.id.first_quick_charging:
firstQuickCharging = !firstQuickCharging;
first_quick_charging.setTextColor(firstQuickCharging ? Color.parseColor("#2FC45B") : Color.parseColor("#222222"));
foldSecondCondition();
filterChargingStation();
if (firstQuickCharging) {
MobclickAgent.onEvent(getApplicationContext(), "1142");
}
break;
case R.id.first_slow_charging:
firstSlowCharging = !firstSlowCharging;
first_slow_charging.setTextColor(firstSlowCharging ? Color.parseColor("#2FC45B") : Color.parseColor("#222222"));
foldSecondCondition();
filterChargingStation();
if (firstSlowCharging) {
MobclickAgent.onEvent(getApplicationContext(), "1144");
}
break;
case R.id.second_1:
case R.id.second_2:
case R.id.second_3:
case R.id.second_4:
case R.id.second_5:
chooseSecondCondition(v.getId());
break;
case R.id.condition_second_bar:
foldSecondCondition();
break;
}
}
// 折叠
private void foldSecondCondition() {
condition_second_bar.setVisibility(View.GONE);
first_condition_image.setImageDrawable(getResources().getDrawable(R.drawable.recommend_fold));
}
// 打开
private void unfoldSecondCondition() {
condition_second_bar.setVisibility(View.VISIBLE);
first_condition_image.setImageDrawable(getResources().getDrawable(R.drawable.recommend_unfold));
}
private void chooseSecondCondition(int id) {
resetFirstCondition();
foldSecondCondition();
second_1.setBackground(getResources().getDrawable(R.drawable.recommend_btn_normal));
second_2.setBackground(getResources().getDrawable(R.drawable.recommend_btn_normal));
second_3.setBackground(getResources().getDrawable(R.drawable.recommend_btn_normal));
second_4.setBackground(getResources().getDrawable(R.drawable.recommend_btn_normal));
second_5.setBackground(getResources().getDrawable(R.drawable.recommend_btn_normal));
second_1.setTextColor(Color.parseColor("#2FC45B"));
second_2.setTextColor(Color.parseColor("#2FC45B"));
second_3.setTextColor(Color.parseColor("#2FC45B"));
second_4.setTextColor(Color.parseColor("#2FC45B"));
second_5.setTextColor(Color.parseColor("#2FC45B"));
switch (id) {
case R.id.second_1:
firstCondition = 1;
second_1.setBackground(getResources().getDrawable(R.drawable.recommend_btn_selected));
second_1.setTextColor(Color.parseColor("#FFFFFF"));
first_condition_text.setText("智能推荐");
if (recommendZhanList.size() == 0) {
recommendChargingStationFunc();
} else {
filterChargingStation();
}
MobclickAgent.onEvent(getApplicationContext(), "1136");
break;
case R.id.second_2:
firstCondition = 2;
second_2.setBackground(getResources().getDrawable(R.drawable.recommend_btn_selected));
second_2.setTextColor(Color.parseColor("#FFFFFF"));
first_condition_text.setText("价格最低");
if (zhanList.size() == 0 && socket != null && socket.isClosed()) {
loadingDialog.show();
ete("{\"fun\":\"timer\"}");
} else {
filterChargingStation();
}
MobclickAgent.onEvent(getApplicationContext(), "1138");
break;
case R.id.second_3:
firstCondition = 3;
second_3.setBackground(getResources().getDrawable(R.drawable.recommend_btn_selected));
second_3.setTextColor(Color.parseColor("#FFFFFF"));
first_condition_text.setText("距离最近");
if (zhanList.size() == 0 && socket != null && socket.isClosed()) {
loadingDialog.show();
ete("{\"fun\":\"timer\"}");
} else {
filterChargingStation();
}
MobclickAgent.onEvent(getApplicationContext(), "1139");
break;
case R.id.second_4:
firstCondition = 4;
second_4.setBackground(getResources().getDrawable(R.drawable.recommend_btn_selected));
second_4.setTextColor(Color.parseColor("#FFFFFF"));
first_condition_text.setText("空闲最多");
if (zhanList.size() == 0 && socket != null && socket.isClosed()) {
loadingDialog.show();
ete("{\"fun\":\"timer\"}");
} else {
filterChargingStation();
}
MobclickAgent.onEvent(getApplicationContext(), "1140");
break;
case R.id.second_5:
firstCondition = 5;
second_5.setBackground(getResources().getDrawable(R.drawable.recommend_btn_selected));
second_5.setTextColor(Color.parseColor("#FFFFFF"));
first_condition_text.setText("评分最高");
if (zhanList.size() == 0 && socket != null && socket.isClosed()) {
loadingDialog.show();
ete("{\"fun\":\"timer\"}");
} else {
filterChargingStation();
}
MobclickAgent.onEvent(getApplicationContext(), "1137");
break;
}
}
private void resetFirstCondition() {
firstCanCharging = false;
first_can_charging.setTextColor(Color.parseColor("#222222"));
firstFreeParking = false;
first_free_parking.setTextColor(Color.parseColor("#222222"));
firstQuickCharging = false;
first_quick_charging.setTextColor(Color.parseColor("#222222"));
firstSlowCharging = false;
first_slow_charging.setTextColor(Color.parseColor("#222222"));
}
private void recommendChargingStationFunc() {
loadingDialog.show();
no_result.setVisibility(View.GONE);
String url = MainApplication.url + "/zhannew/basic/web/index.php/zhanlist/recommend?longitude=" + center_jing + "&latitude=" + center_wei + "&version_name=" + BuildConfig.VERSION_NAME + "&version_code=" + BuildConfig.VERSION_CODE + "&pg=30";
OkHttpUtils.get().url(url).build().readTimeOut(10000).connTimeOut(10000).execute(new StringCallback() {
@Override
public void onError(Call call, Exception e) {
if (activity != null && !activity.isFinishing()) {
loadingDialog.dismiss();
}
ToastUtil.showToast(getApplicationContext(), e.getLocalizedMessage(), Toast.LENGTH_SHORT);
}
@Override
public void onResponse(String response) {
if (activity != null && !activity.isFinishing()) {
loadingDialog.dismiss();
} else {
return;
}
String rtnCode = JsonUtils.getKeyResult(response, "rtnCode");
if (rtnCode.equals("01")) {
String data = JsonUtils.getKeyResult(response, "data");
List<RecommendZhan> list = JsonUtils.parseToObjectList(data, RecommendZhan.class);
if (list != null) {
for (int i = 0; i < list.size(); i++) {
String wei = list.get(i).getPoi_wei().trim();
String jing = list.get(i).getPoi_jing().trim();
int position_jing = jing.length() - jing.indexOf(".") - 1;
int position_wei = wei.length() - wei.indexOf(".") - 1;
LatLng ll;
if (position_jing > 13 || position_wei > 13) {
ll = new LatLng(Double.parseDouble(wei), Double.parseDouble(jing));
} else {
double dwei = Double.parseDouble(wei);
double djing = Double.parseDouble(jing);
getGaode(djing, dwei);
ll = new LatLng(Double.parseDouble(gd_wei), Double.parseDouble(gd_jing));
}
double d = AMapUtils.calculateLineDistance(center, ll);
list.get(i).setDistanceNum(d);
list.get(i).setDistance(Util.getDistance(d));
list.get(i).setZhan_id(list.get(i).getId()); // 呵呵...
}
recommendZhanList = list;
filterChargingStation();
}
}
ete("{\"fun\":\"timer\"}");
}
});
}
public class RecommendChargingStationAdapter extends RecyclerView.Adapter<RecommendChargingStationAdapter.ViewHolder> {
private List<RecommendZhan> mList;
class ViewHolder extends RecyclerView.ViewHolder {
final LinearLayout station_item;
final TextView zhan_name;
final TextView distance;
final ImageView red_pager_bag_icon;
final Button recommend_nav;
final ImageView score_img;
final TextView score_text;
final TextView charge_record_cnt;
final TextView comment_cnt;
final View times_line;
final LinearLayout zongjia_container;
final TextView zongjia;
final TextView iv_label_public;
final TextView iv_label_ground;
final TextView iv_label_free_park;
final TextView payment_method;
final TextView window_tv_fast_free_label;
final TextView window_tv_fast_free_num;
final TextView window_tv_fast_total_num;
final TextView window_tv_slow_free_num;
final TextView window_tv_slow_free_label;
final TextView window_tv_slow_total_num;
final TextView fenshi_info_textview;
public ViewHolder(View itemView) {
super(itemView);
station_item = itemView.findViewById(R.id.station_item);
zhan_name = itemView.findViewById(R.id.zhan_name);
distance = itemView.findViewById(R.id.distance);
red_pager_bag_icon = itemView.findViewById(R.id.red_paper_bag);
recommend_nav = itemView.findViewById(R.id.recommend_nav);
score_img = itemView.findViewById(R.id.score_img);
score_text = itemView.findViewById(R.id.score_text);
charge_record_cnt = itemView.findViewById(R.id.charge_record_cnt);
comment_cnt = itemView.findViewById(R.id.comment_cnt);
times_line = itemView.findViewById(R.id.times_line);
zongjia_container = itemView.findViewById(R.id.zongjia_container);
zongjia = itemView.findViewById(R.id.zongjia);
iv_label_public = itemView.findViewById(R.id.iv_label_public);
iv_label_ground = itemView.findViewById(R.id.iv_label_ground);
iv_label_free_park = itemView.findViewById(R.id.iv_label_free_park);
payment_method = itemView.findViewById(R.id.payment_method);
window_tv_fast_free_label = itemView.findViewById(R.id.window_tv_fast_free_label);
window_tv_fast_free_num = itemView.findViewById(R.id.window_tv_fast_free_num);
window_tv_fast_total_num = itemView.findViewById(R.id.window_tv_fast_total_num);
window_tv_slow_free_label = itemView.findViewById(R.id.window_tv_slow_free_label);
window_tv_slow_free_num = itemView.findViewById(R.id.window_tv_slow_free_num);
window_tv_slow_total_num = itemView.findViewById(R.id.window_tv_slow_total_num);
fenshi_info_textview = itemView.findViewById(R.id.fenshi_info_textview);
}
}
public RecommendChargingStationAdapter() {
mList = new ArrayList<RecommendZhan>();
}
public void setData(List<RecommendZhan> list) {
mList = list;
notifyDataSetChanged();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recommend_charging_station_item, parent, false);
ViewHolder viewHolder = new ViewHolder(itemView);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
RecommendZhan recommendZhan = mList.get(position);
holder.station_item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplication(), NewZhanDetailsActivity.class);
intent.putExtra("zhan_id", recommendZhan.getZhan_id());
startActivity(intent);
}
});
holder.zhan_name.setText(recommendZhan.getZhan_name());
if ("0m".equals(recommendZhan.getDistance())) {
holder.distance.setText("未知");
} else {
holder.distance.setText(recommendZhan.getDistance());
}
if ("1".equals(recommendZhan.getOwn_pay())) {
holder.red_pager_bag_icon.setVisibility(View.VISIBLE);
} else {
holder.red_pager_bag_icon.setVisibility(View.GONE);
}
holder.red_pager_bag_icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent_example = new Intent(getApplicationContext(), ExampleActivity.class);
intent_example.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent_example);
overridePendingTransition(0, 0);
}
});
holder.recommend_nav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recommendNav(recommendZhan);
}
});
showStarInfoView(holder, recommendZhan);
if (recommendZhan.getCharge_record_cnt() == 0) {
holder.charge_record_cnt.setVisibility(View.GONE);
} else {
holder.charge_record_cnt.setVisibility(View.VISIBLE);
if (recommendZhan.getCharge_record_cnt() < 1000) {
holder.charge_record_cnt.setText(recommendZhan.getCharge_record_cnt() + "次充电成功");
} else if (recommendZhan.getCharge_record_cnt() < 10000) {
holder.charge_record_cnt.setText(recommendZhan.getCharge_record_cnt() / 1000 + "k+次充电成功");
} else {
holder.charge_record_cnt.setText(recommendZhan.getCharge_record_cnt() / 10000 + "w+次充电成功");
}
}
if (recommendZhan.getComment_cnt() == 0) {
holder.comment_cnt.setVisibility(View.GONE);
} else {
holder.comment_cnt.setVisibility(View.VISIBLE);
if (recommendZhan.getComment_cnt() < 1000) {
holder.comment_cnt.setText(recommendZhan.getComment_cnt() + "次评论");
} else if (recommendZhan.getComment_cnt() < 10000) {
holder.comment_cnt.setText(recommendZhan.getComment_cnt() / 1000 + "k+次评论");
} else {
holder.comment_cnt.setText(recommendZhan.getComment_cnt() / 10000 + "w+次评论");
}
}
if (recommendZhan.getComment_cnt() == 0 || recommendZhan.getCharge_record_cnt() == 0 && recommendZhan.getComment_cnt() == 0) {
holder.times_line.setVisibility(View.GONE);
} else {
holder.times_line.setVisibility(View.VISIBLE);
}
if (recommendZhan.getZongjia() == 0) {
holder.zongjia_container.setVisibility(View.GONE);
} else {
holder.zongjia_container.setVisibility(View.VISIBLE);
holder.zongjia.setText(String.format("%.2f", recommendZhan.getZongjia()));
}
holder.iv_label_public.setText(recommendZhan.getBelong_attribute());
holder.iv_label_ground.setText("0".equals(recommendZhan.getPark_location()) ? "地下" : "地上");
holder.iv_label_free_park.setVisibility(recommendZhan.getStop_cost().contains("免费") ? View.VISIBLE : View.GONE);
if ("1".equals(recommendZhan.getOwn_pay())) {
holder.payment_method.setText("可使用本APP扫码支付");
} else {
if ("".equals(recommendZhan.getCharge_cost_way2())) {
holder.payment_method.setText("暂无");
} else {
holder.payment_method.setText(recommendZhan.getCharge_cost_way2());
}
}
if (recommendZhan.getFast_able_num() == null && recommendZhan.getSlow_able_num() == null) {
holder.window_tv_fast_free_label.setVisibility(View.GONE);
holder.window_tv_fast_total_num.setVisibility(View.GONE);
holder.window_tv_slow_free_label.setVisibility(View.GONE);
holder.window_tv_slow_total_num.setVisibility(View.GONE);
holder.window_tv_fast_free_num.setText("".equals(recommendZhan.getFast_num()) ? "0" : recommendZhan.getFast_num());
holder.window_tv_slow_free_num.setText("".equals(recommendZhan.getSlow_num()) ? "0" : recommendZhan.getSlow_num());
} else {
holder.window_tv_fast_free_label.setVisibility(View.VISIBLE);
holder.window_tv_fast_total_num.setVisibility(View.VISIBLE);
holder.window_tv_slow_free_label.setVisibility(View.VISIBLE);
holder.window_tv_slow_total_num.setVisibility(View.VISIBLE);
holder.window_tv_fast_free_num.setText(recommendZhan.getFast_able_num());
holder.window_tv_slow_free_num.setText(recommendZhan.getSlow_able_num());
holder.window_tv_fast_total_num.setText("".equals(recommendZhan.getFast_num()) ? "0" : "/" + recommendZhan.getFast_num());
holder.window_tv_slow_total_num.setText("".equals(recommendZhan.getSlow_num()) ? "0" : "/" + recommendZhan.getSlow_num());
}
holder.fenshi_info_textview.setText("");
if ("1".equals(recommendZhan.getFenshi_is())) {
ArrayList<PileData.FenshiInfoBean> fenshiList = (ArrayList<PileData.FenshiInfoBean>) JsonUtils.parseToObjectList(recommendZhan.getFenshi_info(), PileData.FenshiInfoBean.class);
if (fenshiList.size() == 1) {
holder.fenshi_info_textview.setText("全天价格统一");
} else {
Calendar calendar = Calendar.getInstance();
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
int totalMinutes = hours * 60 + minutes;
for (PileData.FenshiInfoBean bean : fenshiList) {
if (bean.getStartTotalMinutes() > totalMinutes) {
double service_free = 0;
double charge_free = 0;
try {
service_free = Double.valueOf(bean.getService_free());
charge_free = Double.valueOf(bean.getCharge_free());
} catch (Exception e) {
e.printStackTrace();
}
holder.fenshi_info_textview.setText(String.format("%s开始 %.2f元/度", bean.getStart(), service_free + charge_free));
break;
}
}
}
} else {
if (recommendZhan.getZongjia() != 0) {
holder.fenshi_info_textview.setText("全天价格统一");
}
}
}
@Override
public int getItemCount() {
return mList.size();
}
private void showStarInfoView(ViewHolder holder, RecommendZhan recommendZhan) {
String starStr = recommendZhan.getStar();
double star = 0.0;
try {
star = Double.parseDouble(starStr);
} catch (NumberFormatException e) {
e.printStackTrace();
}
holder.score_text.setText(String.format("%.1f分", star));
if (star == 0) {
holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_0xing));
holder.score_text.setText("暂无评分");
} else if (star > 0 && star < 1) {
holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_05xing));
} else if (star >= 1 && star < 1.5) {
holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_1xing));
} else if (star >= 1.5 && star < 2) {
holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_15xing));
} else if (star >= 2 && star < 2.5) {
holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_2xing));
} else if (star >= 2.5 && star < 3) {
holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_25xing));
} else if (star >= 3 && star < 3.5) {
holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_3xing));
} else if (star >= 3.5 && star < 4) {
holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_35xing));
} else if (star >= 4 && star < 4.5) {
holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_4xing));
} else if (star >= 4.5 && star < 5) {
holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_45xing));
} else {
holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_5xing));
}
}
private void recommendNav(RecommendZhan rz) {
String wei = rz.getPoi_wei().trim();
String jing = rz.getPoi_jing().trim();
int position_jing = jing.length() - jing.indexOf(".") - 1;
int position_wei = wei.length() - wei.indexOf(".") - 1;
final LatLng ll;
if (position_jing > 13 || position_wei > 13) {
ll = new LatLng(Double.parseDouble(wei), Double.parseDouble(jing));
} else {
double dwei = Double.parseDouble(wei);
double djing = Double.parseDouble(jing);
getGaode(djing, dwei);
ll = new LatLng(Double.parseDouble(gd_wei), Double.parseDouble(gd_jing));
}
if (!isAvilible(getApplicationContext(), "com.baidu.BaiduMap") &&
!isAvilible(getApplicationContext(), "com.autonavi.minimap")) {
Intent intent = new Intent(getApplication(), BasicNaviActivity.class);
intent.putExtra("start_jing", center.longitude + "");
intent.putExtra("start_wei", center.latitude + "");
intent.putExtra("stop_jing", ll.longitude + "");
intent.putExtra("stop_wei", ll.latitude + "");
startActivity(intent);
} else {
if (center != null) {
LayoutInflater inflater1 = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popupWindowView = inflater1.inflate(R.layout.item_selectmap, null);
popupWindow = new PopupWindow(popupWindowView,
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT,
true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
// 设置PopupWindow的弹出和消失效果
popupWindow.setAnimationStyle(R.style.popupAnimation);
LinearLayout ll_tvTwo = (LinearLayout) popupWindowView.findViewById(R.id.ll_tvTwo);
if (!isAvilible(getApplicationContext(), "com.autonavi.minimap")) {
ll_tvTwo.setVisibility(View.GONE);
}
LinearLayout ll_tvOne = (LinearLayout) popupWindowView.findViewById(R.id.ll_tvOne);
if (!isAvilible(getApplicationContext(), "com.baidu.BaiduMap")) {
ll_tvOne.setVisibility(View.GONE);
}
cancleButton = (Button) popupWindowView
.findViewById(R.id.cancleButton);
cancleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
tvThree = (TextView) popupWindowView.findViewById(R.id.tvThree);
tvThree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplication(), BasicNaviActivity.class);
intent.putExtra("start_jing", center.longitude + "");
intent.putExtra("start_wei", center.latitude + "");
intent.putExtra("stop_jing", ll.longitude + "");
intent.putExtra("stop_wei", ll.latitude + "");
startActivity(intent);
popupWindow.dismiss();
}
});
tvOne = (TextView) popupWindowView.findViewById(R.id.tvOne);
tvOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = null;
int position_jing = rz.getPoi_jing().length() - rz.getPoi_jing().indexOf(".") - 1;
int position_wei = rz.getPoi_wei().length() - rz.getPoi_wei().indexOf(".") - 1;
if (position_jing > 13 || position_wei > 13) {
getBaidu1(Double.parseDouble(rz.getPoi_jing()), Double.parseDouble(rz.getPoi_wei()));
} else {
bd_jing = Double.parseDouble(rz.getPoi_jing());
bd_wei = Double.parseDouble(rz.getPoi_wei());
}
getBaidu(center.longitude, center.latitude);
try {
String s = "intent://map/direction?" +
"origin=" + bd_lat + "," + bd_lon + "&" +
"destination=" + bd_wei + "," + bd_jing +
"&mode=driving&" +
"src=充电桩#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end";
intent = Intent.getIntent(s);
} catch (URISyntaxException e) {
e.printStackTrace();
}
startActivity(intent);
popupWindow.dismiss();
}
});
tvTwo = (TextView) popupWindowView.findViewById(R.id.tvTwo);
tvTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
Intent intent4 = new Intent("android.intent.action.VIEW",
android.net.Uri.parse("androidamap://route?sourceApplication=amap&slat=" + center.latitude + "&slon=" + center.longitude + "&sname=我的位置&dlat=" + ll.latitude + "&dlon=" + ll.longitude + "&dname=" + rz.getZhan_name() + "&dev=0&m=0&t=2"));
intent4.setPackage("com.autonavi.minimap");
startActivity(intent4);
popupWindow.dismiss();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplication(), "请您确认是否安装高德地图APP", Toast.LENGTH_SHORT).show();
}
}
});
popupWindow.showAtLocation(cancleButton, Gravity.CENTER, 0, 0);
} else {
Toast.makeText(getApplication(),
"没有定位到您的当前位置", Toast.LENGTH_SHORT)
.show();
}
}
}
private boolean isAvilible(Context context, String packageName) {
//获取packagemanager
final PackageManager packageManager = context.getPackageManager();
//获取所有已安装程序的包信息
List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
//用于存储所有已安装程序的包名
List<String> packageNames = new ArrayList<String>();
//从pinfo中将包名字逐一取出,压入pName list中
if (packageInfos != null) {
for (int i = 0; i < packageInfos.size(); i++) {
String packName = packageInfos.get(i).packageName;
packageNames.add(packName);
}
}
//判断packageNames中是否有目标程序的包名,有TRUE,没有FALSE
return packageNames.contains(packageName);
}
private void getBaidu(Double jing, Double wei) {
double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
double x = jing;
double y = wei;
double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
bd_lon = z * Math.cos(theta) + 0.0065;
bd_lat = z * Math.sin(theta) + 0.006;
}
private void getBaidu1(Double jing, Double wei) {
double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
double x = jing;
double y = wei;
double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
bd_jing = z * Math.cos(theta) + 0.0065;
bd_wei = z * Math.sin(theta) + 0.006;
}
}
private void filterChargingStation() {
no_result.setVisibility(View.GONE);
List<RecommendZhan> list;
if (firstCondition == 1) {
list = recommendZhanList;
} else {
list = zhanList;
}
if (firstCondition == 2) { // 价格最低
List<RecommendZhan> priceList1 = new ArrayList<>();
List<RecommendZhan> priceList2 = new ArrayList<>();
for (RecommendZhan zhan : list) {
if (zhan.getZongjia() > 0) {
priceList1.add(zhan);
} else {
priceList2.add(zhan);
}
}
list = priceList1;
Collections.sort(list, new Comparator<RecommendZhan>() {
@Override
public int compare(RecommendZhan o1, RecommendZhan o2) {
double diff = o1.getZongjia() - o2.getZongjia();
if (o1.getZongjia() == 0) {
diff = -1;
}
if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
}
return 0;
}
});
list.addAll(priceList2);
}
if (firstCondition == 3) { // 距离最近
Collections.sort(list, new Comparator<RecommendZhan>() {
@Override
public int compare(RecommendZhan o1, RecommendZhan o2) {
double diff = o1.getDistanceNum() - o2.getDistanceNum();
if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
}
return 0;
}
});
}
if (firstCondition == 4) { // 空闲最多
Collections.sort(list, new Comparator<RecommendZhan>() {
@Override
public int compare(RecommendZhan o1, RecommendZhan o2) {
int fast_able_num1;
int fast_able_num2;
int slow_able_num1;
int slow_able_num2;
int total_num1;
int total_num2;
if (o1.getFast_able_num() == null || "".equals(o1.getFast_able_num())) {
fast_able_num1 = 0;
} else {
fast_able_num1 = Integer.parseInt(o1.getFast_able_num());
}
if (o1.getSlow_able_num() == null || "".equals(o1.getSlow_able_num())) {
slow_able_num1 = 0;
} else {
slow_able_num1 = Integer.parseInt(o1.getSlow_able_num());
}
if (o2.getFast_able_num() == null || "".equals(o2.getFast_able_num())) {
fast_able_num2 = 0;
} else {
fast_able_num2 = Integer.parseInt(o2.getFast_able_num());
}
if (o2.getSlow_able_num() == null || "".equals(o2.getSlow_able_num())) {
slow_able_num2 = 0;
} else {
slow_able_num2 = Integer.parseInt(o2.getSlow_able_num());
}
total_num1 = fast_able_num1 + slow_able_num1;
total_num2 = fast_able_num2 + slow_able_num2;
int diff = total_num1 - total_num2;
if (diff > 0) {
return -1;
} else if (diff < 0) {
return 1;
}
return 0;
}
});
}
if (firstCondition == 5) { // 评分最高
Collections.sort(list, new Comparator<RecommendZhan>() {
@Override
public int compare(RecommendZhan o1, RecommendZhan o2) {
double star1;
double star2;
if (o1.getStar() == null || "".equals(o1.getStar())) {
star1 = 0;
} else {
star1 = Double.parseDouble(o1.getStar());
}
if (o2.getStar() == null || "".equals(o2.getStar())) {
star2 = 0;
} else {
star2 = Double.parseDouble(o2.getStar());
}
double diff = star1 - star2;
if (diff > 0) {
return -1;
} else if (diff < 0) {
return 1;
}
return 0;
}
});
}
if (list.size() > 30) {
list = list.subList(0, 30);
}
List<RecommendZhan> list1 = new ArrayList<>();
if (firstCanCharging) {
for (RecommendZhan item : list) {
if ("1".equals(item.getOwn_pay())) {
list1.add(item);
}
}
} else {
for (RecommendZhan item : list) {
list1.add(item);
}
}
List<RecommendZhan> list2 = new ArrayList<>();
if (firstFreeParking) {
for (RecommendZhan item : list1) {
if (item.getStop_cost().contains("免费")) {
list2.add(item);
}
}
} else {
for (RecommendZhan item : list1) {
list2.add(item);
}
}
List<RecommendZhan> list3 = new ArrayList<>();
if (firstQuickCharging) {
for (RecommendZhan item : list2) {
int fastNum;
if ("".equals(item.getFast_num())) {
fastNum = 0;
} else {
fastNum = Integer.parseInt(item.getFast_num());
}
if (fastNum > 0) {
list3.add(item);
}
}
} else {
for (RecommendZhan item : list2) {
list3.add(item);
}
}
List<RecommendZhan> list4 = new ArrayList<>();
if (firstSlowCharging) {
for (RecommendZhan item : list3) {
int slowNum;
if ("".equals(item.getSlow_num())) {
slowNum = 0;
} else {
slowNum = Integer.parseInt(item.getSlow_num());
}
if (slowNum > 0) {
list4.add(item);
}
}
} else {
for (RecommendZhan item : list3) {
list4.add(item);
}
}
recommendChargingStationAdapter.setData(list4);
if (list4.size() == 0) {
if (socket != null && !socket.isClosed()) {
no_result.setText("数据加载中,请稍后...");
} else {
no_result.setText("周边没有符合筛选条件的站点,请检查您的筛选项");
}
no_result.setVisibility(View.VISIBLE);
} else {
no_result.setVisibility(View.GONE);
}
}
private void getGaode(Double jing, Double wei) {
double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
double gg_lon;
double gg_lat;
double x = jing - 0.0065, y = wei - 0.006;
double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
gg_lon = z * Math.cos(theta);
gd_jing = String.valueOf(gg_lon);
gg_lat = z * Math.sin(theta);
gd_wei = String.valueOf(gg_lat);
}
private Socket socket;
private Handler hand = new Handler() {
public void handleMessage(android.os.Message msg) {
if (activity != null && !activity.isFinishing()) {
loadingDialog.dismiss();
}
switch (msg.what) {
case 3:
try {
String rtnCode = JsonUtils.getKeyResult(msg.obj.toString(), "rtnCode");
if ("0".equals(rtnCode)) {
try {
String data = JsonUtils.getKeyResult(msg.obj.toString(), "data");
List<RecommendZhan> list = JsonUtils.parseToObjectList(data, RecommendZhan.class);
if (list != null) {
int count = list.size();
for (int i = 0; i < count; i++) {
String wei = list.get(i).getPoi_wei().trim();
String jing = list.get(i).getPoi_jing().trim();
int position_jing = jing.length() - jing.indexOf(".") - 1;
int position_wei = wei.length() - wei.indexOf(".") - 1;
LatLng ll;
if (position_jing > 13 || position_wei > 13) {
ll = new LatLng(Double.parseDouble(wei), Double.parseDouble(jing));
} else {
double dwei = Double.parseDouble(wei);
double djing = Double.parseDouble(jing);
getGaode(djing, dwei);
ll = new LatLng(Double.parseDouble(gd_wei), Double.parseDouble(gd_jing));
}
double d = AMapUtils.calculateLineDistance(center, ll);
list.get(i).setDistanceNum(d);
list.get(i).setDistance(Util.getDistance(d));
}
zhanList = list;
filterChargingStation();
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
break;
default:
break;
}
}
};
//获取站点列表
private void ete(final String params) {
new Thread(new Runnable() {
@Override
public void run() {
try {
// 创建Socket
socket = new Socket("cdz.evcharge.cc", 9503); // IP:59.110.68.162,端口54321 cdz.evcharge.cc
// 向服务器发送消息
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
out.println(params);
// 接收来自服务器的消息
BufferedReader br = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String msg = br.readLine();
int time = (int) (System.currentTimeMillis() / 1000);
int cha = time - Integer.parseInt(msg);
String Token = getMD5(String.valueOf((System.currentTimeMillis() / 1000 - cha) / 10) + "qwerty");
String llon, llat, rlon, rlat;
llon = String.valueOf(Double.parseDouble(center_jing) - 0.047414454728858857);
llat = String.valueOf(Double.parseDouble(center_wei) + 0.047414454728858857);
rlon = String.valueOf(Double.parseDouble(center_jing) + 0.047414454728858857);
rlat = String.valueOf(Double.parseDouble(center_wei) - 0.041842459916509256);
HashMap<String, String> map = new HashMap<String, String>();
map.put("fun", "zhangetlist");
map.put("poi_lux", llon);
map.put("poi_luy", llat);
map.put("poi_rdx", rlon);
map.put("poi_rdy", rlat);
map.put("timer", Token);
String socket_string = JsonUtils.mapToJson(map);
ete1(socket_string);
} catch (Exception e) {
e.printStackTrace();
Message mes = new Message();
mes.obj = "error";
mes.what = 100;
hand.sendMessage(mes);
}
}
}).start();
}
private void ete1(final String params) {
new Thread(new Runnable() {
@Override
public void run() {
try {
socket = new Socket("cdz.evcharge.cc", 9503); // IP:59.110.68.162,端口54321 cdz.evcharge.cc
socket.setReceiveBufferSize(99999); // 接收缓冲大小
socket.setSendBufferSize(99999);
// 向服务器发送消息
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
out.println(params);
// 接收来自服务器的消息
BufferedReader br = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String msg = br.readLine();
if (msg != null) {
Message mes = new Message();
mes.obj = msg;
mes.what = 3;
hand.sendMessage(mes);
} else {
LogUtils.e("msg=null");
}
out.close();
br.close();
// 关闭Socket
socket.close();
} catch (Exception e) {
e.printStackTrace();
Message mes = new Message();
mes.obj = "error";
mes.what = 100;
hand.sendMessage(mes);
}
}
}).start();
}
}
|