package com.electric.chargingpile.util; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Build; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.view.WindowManager; import com.amap.api.maps.AMap; import com.amap.api.maps.model.CustomMapStyleOptions; import com.blankj.utilcode.util.StringUtils; import com.electric.chargingpile.activity.MainMapActivity; import com.electric.chargingpile.data.Zhan; import com.electric.chargingpile.data.Zhuang; import com.electric.chargingpile.manager.ProfileManager; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; public class Util { private static final String PHONE_PATTERN = "[1]{1}[3,4,5,6,7,8,9]{1}[0-9]{9}"; public static String hao = "1号"; public static int fenzhong = 0; // public static boolean isValidPhone(String phone) { // if (TextUtils.isEmpty(phone)) // return false; // return phone.matches(PHONE_PATTERN); // } public static String handlePhone(String phone) { if (!TextUtils.isEmpty(phone) && phone.length() == 11) { return phone.substring(0, 3) + "****" + phone.substring(7); } return "请点击头像登录"; } public static String handlePhoneComment(String phone) { if (!TextUtils.isEmpty(phone) && phone.length() == 11) { return phone.substring(0, 3) + "****" + phone.substring(7); } return "app用户"; } public static String convertMap2Query(Map map) { StringBuffer sb = new StringBuffer(); if (map.size() > 0) { for (String key : map.keySet()) { sb.append(key + "="); String value = map.get(key); if (StringUtils.isEmpty(value)) { sb.append("&"); } else { try { value = URLEncoder.encode(value, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } sb.append(value + "&"); } } } return sb.toString(); } public static String getRandom(int length) { String val = ""; Random random = new Random(); for (int i = 0; i < length; i++) { val += String.valueOf(random.nextInt(10)); } return val; } public static String getPhone(Context context) { return ProfileManager.getInstance().getUsername(context); } public static String formatString(String s, int size) { if (s.length() <= size) { return s; } else if (s.length() > size && s.length() <= 2 * size) { return s.substring(0, size) + "\n" + s.substring(size); } else { return s.substring(0, size) + "\n" + s.substring(size, 2 * size - 1) + "..."; } } public static String getCityName(String address) { String city = ""; if (address.length() > 3) { city = address; } if (city.contains("省")) { city = address.substring(address.indexOf("省") + 1); } if (city.contains("自治区")) { city = city.substring(city.indexOf("自治区") + 3); } return city.split("市")[0]; } public static String getDistance(double d) { int a = (int) d; if (a < 1000) { return a + "m"; } else { double b = a / 1000.0; DecimalFormat df = new DecimalFormat(); String style = "0.00"; df.applyPattern(style); return df.format(b) + "km"; } } public static String getStringFromMap(Map map) { // StringBuffer sb = new StringBuffer(); // try { // sb.append(WSConfig.BASE_URL); // if (map != null && !map.isEmpty()) { // sb.append("?"); // // final ArrayList keyList = new ArrayList( // map.keySet()); // final int keyListLength = keyList.size(); // // for (int i = 0; i < keyListLength; i++) { // final String key = keyList.get(i); // sb.append(URLEncoder.encode(key, "UTF-8")); // sb.append("="); // sb.append(URLEncoder.encode(map.get(key), "UTF-8")); // sb.append("&"); // } // } // // } catch (UnsupportedEncodingException e) { // e.printStackTrace(); // } return ""; } public static List> getMapList(List list) { List> l = new ArrayList>(); Map map; for (int i = 0; i < list.size(); i++) { if (list.get(i).getStatus() != null && list.get(i).getStatus().equals("1")) { map = new HashMap(); map.put("name", i + 1 + "号"); int a = Integer.valueOf(list.get(i).getLeft_time()); map.put("bai", (int) (1.0 * (150 - a) / 150 * 100) + "%"); l.add(map); if (fenzhong == 0 || fenzhong > a) { hao = i + 1 + "号"; fenzhong = a; } } else { map = new HashMap(); map.put("name", i + 1 + "号"); map.put("bai", "100%"); l.add(map); } } return l; } public static List getSortList(List list) { List list2 = new ArrayList(); if (list.size() > 0 && "0m".equals(list.get(0).getDistance())) { return list; } for (int i = 0; i < list.size(); i++) { // Log.e("i===",i+""); if (i == 0) { list2.add(list.get(i)); } else { for (int j = 0; j < list2.size(); j++) { int jd = 0, id = 0; String zj = list2.get(j).getDistance(); String zi = list.get(i).getDistance(); if (!zj.equals("")) { if (zj.endsWith("km")) { jd = (int) (1000 * Double.valueOf(zj.substring(0, zj.length() - 2))); } else { jd = Integer.valueOf(zj.substring(0, zj.length() - 1)); } } if (! zi.equals("")) { if (zi.endsWith("km")) { id = (int) (1000 * Double.valueOf(zi.substring(0, zi.length() - 2))); } else { id = Integer.valueOf(zi.substring(0, zi.length() - 1)); } } if (jd > id) { list2.add(j, list.get(i)); break; } if (j == list2.size() - 1) { list2.add(list.get(i)); break; } } } } return list2; } public static List getSortPFList(List list) { List list2 = new ArrayList(); for (int i = 0; i < list.size(); i++) { if (i == 0) { list2.add(list.get(i)); } else { for (int j = 0; j < list2.size(); j++) { float jd = 0, id = 0; String zj = list2.get(j).getStar_level(); String zi = list.get(i).getStar_level(); if (zj.equals("")) { jd = Float.valueOf("0"); } else { jd = Float.valueOf(zj); } Log.e("jd==", jd + ""); if (zi.equals("")) { id = Float.valueOf("0"); } else { id = Float.valueOf(zi); } Log.e("id==", id + ""); if (jd < id) { list2.add(j, list.get(i)); break; } if (j == list2.size() - 1) { list2.add(list.get(i)); break; } } } } MainMapActivity.list2.addAll(list2); return list2; } public static List getSortFTList(List list) { List list2 = new ArrayList(); for (int i = 0; i < list.size(); i++) { if (i == 0) { list2.add(list.get(i)); } else { for (int j = 0; j < list2.size(); j++) { int jd = 0, id = 0; String zj = list2.get(j).getAcableNum(); String zjj = list2.get(j).getDcableNum(); String zi = list.get(i).getAcableNum(); String zii = list.get(i).getDcableNum(); if (zj.equals("")) { zj = "0"; } if (zjj.equals("")) { zjj = "0"; } jd = Integer.valueOf(zjj) + Integer.valueOf(zj); Log.e("jd==", jd + ""); if (zi.equals("")) { zi = "0"; } if (zii.equals("")) { zii = "0"; } id = Integer.valueOf(zii) + Integer.valueOf(zi); Log.e("id==", id + ""); if (jd < id) { list2.add(j, list.get(i)); break; } if (j == list2.size() - 1) { list2.add(list.get(i)); break; } } } } MainMapActivity.list3.addAll(list2); return list2; } /** * 显示隐藏状态栏,全屏不变,只在有全屏时有效 * * @param enable */ public static void setStatusBarVisibility(Activity activity, boolean enable) { WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); if (enable) { lp.flags |= WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN; } else { lp.flags &= (~WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); } activity.getWindow().setAttributes(lp); activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); } /** * 设置是否全屏 * * @param enable */ public static void setFullScreen(Activity activity, boolean enable) { WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); if (enable) { lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; } else { lp.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN); } activity.getWindow().setAttributes(lp); activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); } /** * 设定高德地图离线样式文件 * * @param aMap * @param context */ public static void setMapCustomStyleFile(AMap aMap, Context context) { byte[] buffer1 = null; byte[] buffer2 = null; InputStream is1 = null; InputStream is2 = null; try { is1 = context.getAssets().open("amap_custom_style_2019082001.data"); int lenght1 = is1.available(); buffer1 = new byte[lenght1]; is1.read(buffer1); is2 = context.getAssets().open("amap_custom_style_extra_2019082001.data"); int lenght2 = is2.available(); buffer2 = new byte[lenght2]; is2.read(buffer2); } catch (IOException e) { e.printStackTrace(); } finally { try { if (is1 != null) is1.close(); if (is2 != null) is2.close(); } catch (IOException e) { e.printStackTrace(); } } CustomMapStyleOptions customMapStyleOptions = new CustomMapStyleOptions(); customMapStyleOptions.setStyleData(buffer1); customMapStyleOptions.setStyleExtraData(buffer2); aMap.setCustomMapStyle(customMapStyleOptions); } }