dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile files('libs/BaiduLBS_Android.jar') compile files('libs/pinyin4j-2.5.0.jar') compile 'com.squareup:otto:1.3.8' }
Dispatcher.java
package com.dji.sdk.Dispatcher;
import com.squareup.otto.Bus;
import com.squareup.otto.ThreadEnforcer;
/**
* Created by on 2017/12/4.
*/
public class Dispatcher {
private static Dispatcher dispatcher;
private final Bus bus = new Bus(ThreadEnforcer.ANY);
public static Dispatcher getInstance(){
if (dispatcher == null){
dispatcher = new Dispatcher();
}
return dispatcher;
}
private Dispatcher(){
}
public void register(Object obj){
bus.register(obj);
}
public void unregister(Object obj){
bus.unregister(obj);
}
public void post(Object envent){
bus.post(envent);
}
}
ActivityOfflineMap.java
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.baidu.mapapi.map.offline.MKOLSearchRecord;
import com.baidu.mapapi.map.offline.MKOLUpdateElement;
import com.baidu.mapapi.map.offline.MKOfflineMap;
import com.baidu.mapapi.map.offline.MKOfflineMapListener;
import com.dji.sdk.LocalMapInfo;
import com.dji.sdk.Dispatcher.Dispatcher;
import com.dji.sdk.R;
import com.dji.sdk.databinding.ItemListLocalmapBinding;
import com.squareup.otto.Subscribe;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import java.util.ArrayList;
import static com.dji.sdk.LocalMapInfo.UNDEFINED;
/**
* Created by admin on 2017/2/24.
*/
public class ActivityOfflineMap extends Activity implements MKOfflineMapListener {
private MKOfflineMap mOffline = null;
private TextView downView;
private EditText et_Serch;
private TextView tv_hottextview;
private TextView tv_alltextview;
private RecyclerView localMapListRecyclerView;
private RecyclerView downlocalMapListRecyclerView;
private RecyclerView hotMapListRecyclerView;
private RecyclerView serverMapListRecyclerView;
private Button btn_clButton;
private Button btn_localButton;
private TextView textView = null;
//private RelativeLayout relativeLayout_Main;
private ProgressBar progressBar_Wait;
private boolean bWait = true;
private TextView textView_wait;
private boolean bRefresh = false;
/**
* 已下载的离线地图信息列表
*/
private ArrayList localMapList = null;
private ArrayList localMapInfoList = null;
private ArrayList downlocalMapInfoList = null;
private LocalMapRecyclerAdapter localMapRecyclerAdapter = null;
private LocalMapRecyclerAdapter downlocalMapRecyclerAdapter = null;
private ArrayList serverMapInfoList = null;
private LocalMapRecyclerAdapter serverMapRecyclerAdapter = null;
private ArrayList hotMapInfoList = null;
private LocalMapRecyclerAdapter hotMapRecyclerAdapter = null;
// private LocalMapAdapter lAdapter = null;//废弃
private LinearLayout cl;
private LinearLayout lm;
private SharedPreferences sp;
ColorStateList list1;
ColorStateList list2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_offline);
mOffline = new MKOfflineMap();
mOffline.init(this);
sp = getSharedPreferences(Constant.MySp, 0);
initView();
}
private void initView() {
//relativeLayout_Main = (RelativeLayout)findViewById(R.id.relativeLayout_main);
progressBar_Wait = (ProgressBar)findViewById(R.id.progressBar_wait);
textView_wait = (TextView)findViewById(R.id.textView_wait);
//cidView = (TextView) findViewById(R.id.cityid);
//cityNameView = (EditText) findViewById(R.id.city);
//stateView = (TextView) findViewById(R.id.state);
downView = (TextView) findViewById(R.id.downtextview);
cl = (LinearLayout) findViewById(R.id.citylist_layout);
lm = (LinearLayout) findViewById(R.id.localmap_layout);
et_Serch = (EditText) findViewById(R.id.et_serch);
tv_hottextview = (TextView) findViewById(R.id.hottextview);
tv_alltextview = (TextView) findViewById(R.id.alltextview);
localMapListRecyclerView = (RecyclerView) findViewById(R.id.localmaplist);
downlocalMapListRecyclerView = (RecyclerView) findViewById(R.id.downlocalmaplist);
hotMapListRecyclerView = (RecyclerView) findViewById(R.id.hotcitylist);
serverMapListRecyclerView = (RecyclerView) findViewById(R.id.allcitylist);
btn_clButton = (Button) findViewById(R.id.clButton);
btn_localButton = (Button) findViewById(R.id.localButton);
list1 = ViewCompat.getBackgroundTintList(btn_clButton);
list2 = ViewCompat.getBackgroundTintList(btn_localButton);
et_Serch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
//变化
if (s.toString().equals("")) {
tv_hottextview.setVisibility(View.VISIBLE);
tv_alltextview.setVisibility(View.VISIBLE);
hotMapListRecyclerView.setVisibility(View.VISIBLE);
} else {
tv_hottextview.setVisibility(View.GONE);
tv_alltextview.setVisibility(View.GONE);
hotMapListRecyclerView.setVisibility(View.GONE);
}
serverMapRecyclerAdapter.setFilter(!getPingYin(s.toString()).equals(getPinYinHeadChar(s.toString())) ? getPingYin(s.toString()) : getPinYinHeadChar(s.toString()));
}
});
LinearLayout cl = (LinearLayout) findViewById(R.id.citylist_layout);
LinearLayout lm = (LinearLayout) findViewById(R.id.localmap_layout);
lm.setVisibility(View.GONE);
cl.setVisibility(View.VISIBLE);
Thread thread=new Thread(new Runnable()
{
@Override
public void run()
{
refreshData();
}
});
thread.start();
}
/**
* 切换至城市列表
*
* @param view
*/
public void clickCityListButton(View view) {
if (!bWait){
lm.setVisibility(View.GONE);
cl.setVisibility(View.VISIBLE);
hotMapRecyclerAdapter.notifyDataSetChanged();
serverMapRecyclerAdapter.notifyDataSetChanged();
btn_clButton.setTextColor(Color.WHITE);
ViewCompat.setBackgroundTintList(btn_clButton, list1);
btn_localButton.setTextColor(Color.BLACK);
ViewCompat.setBackgroundTintList(btn_localButton, list2);
updateView();
}
}
/**
* 切换至下载管理列表
*
* @param view
*/
public void clickLocalMapListButton(View view) {
if (!bWait){
lm.setVisibility(View.VISIBLE);
cl.setVisibility(View.GONE);
localMapRecyclerAdapter.notifyDataSetChanged();
downlocalMapRecyclerAdapter.notifyDataSetChanged();
btn_clButton.setTextColor(Color.BLACK);
ViewCompat.setBackgroundTintList(btn_clButton, list2);
btn_localButton.setTextColor(Color.WHITE);
ViewCompat.setBackgroundTintList(btn_localButton, list1);
updateView();
}
}
/**
* 更新状态显示
*/
public void updateView() {
localMapList = mOffline.getAllUpdateInfo();
ArrayList localMapInfoListTemp = new ArrayList();
ArrayList downlocalMapInfoListTemp = new ArrayList();
if (localMapList == null) {
localMapList = new ArrayList();
} else {
for (int iLoop = 0; iLoop < localMapList.size(); iLoop++) {
//已完成的
if (localMapList.get(iLoop).status == MKOLUpdateElement.FINISHED) {
LocalMapInfo localMapInfo = new LocalMapInfo(localMapList.get(iLoop));
localMapInfoListTemp.add(localMapInfo);
} else if (localMapList.get(iLoop).status == MKOLUpdateElement.DOWNLOADING ||
localMapList.get(iLoop).status == MKOLUpdateElement.WAITING ||
localMapList.get(iLoop).status == MKOLUpdateElement.SUSPENDED ||
localMapList.get(iLoop).status == MKOLUpdateElement.eOLDSWifiError) {
LocalMapInfo localMapInfo = new LocalMapInfo(localMapList.get(iLoop));
downlocalMapInfoListTemp.add(localMapInfo);
}
}
//和原始数据比较
for (int jLoop = 0; jLoop < downlocalMapInfoListTemp.size(); jLoop++) {
LocalMapInfo localMapInfoTemp = downlocalMapInfoListTemp.get(jLoop);
boolean b = false;
for (int zLoop = 0; zLoop < downlocalMapInfoList.size(); zLoop++) {
LocalMapInfo localMapInfo = downlocalMapInfoList.get(zLoop);
if (localMapInfo.cityID == localMapInfoTemp.cityID) {//存在就更新
downlocalMapInfoList.set(zLoop, localMapInfoTemp);
b = true;
break;
}
}
if (!b) {//不存在就添加
downlocalMapInfoList.add(localMapInfoTemp);
}
}
//比较完后如果原始数据以没有的,清除
boolean b = false;
for (int z = downlocalMapInfoList.size() - 1; z >= 0; z--) {
for (int j = 0; j < downlocalMapInfoListTemp.size(); j++) {
if (downlocalMapInfoList.get(z).cityID == downlocalMapInfoListTemp.get(j).cityID) {
b = true;
break;
}
}
if (!b)
downlocalMapInfoList.remove(z);
}
boolean bRefesh = false;
//和原始数据比较
for (int jLoop = 0; jLoop < localMapInfoListTemp.size(); jLoop++) {
LocalMapInfo localMapInfoTemp = localMapInfoListTemp.get(jLoop);
boolean c = false;
for (int zLoop = 0; zLoop < localMapInfoList.size(); zLoop++) {
LocalMapInfo localMapInfo = localMapInfoList.get(zLoop);
if (localMapInfo.cityID == localMapInfoTemp.cityID) {//存在就更新
localMapInfoList.set(jLoop, localMapInfoTemp);
c = true;
break;
}
}
if (!c) {//不存在就添加
localMapInfoList.add(localMapInfoTemp);
for (int k = 0; k < downlocalMapInfoList.size(); k++) {
if (downlocalMapInfoList.get(k).cityID == localMapInfoTemp.cityID) {
downlocalMapInfoList.remove(k);
break;
}
}
bRefesh = true;
}
}
//比较完后如果原始数据以没有的,清除
boolean c = false;
for (int z = localMapInfoList.size() - 1; z >= 0; z--) {
for (int j = 0; j < localMapInfoListTemp.size(); j++) {
if (localMapInfoList.get(z).cityID == localMapInfoListTemp.get(j).cityID) {
c = true;
break;
}
}
if (!c) {
localMapInfoList.remove(z);
bRefesh = true;
}
}
if (downlocalMapInfoList.size() > 0) {
downView.setVisibility(View.VISIBLE);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) downlocalMapListRecyclerView.getLayoutParams();
if (downlocalMapInfoList.size() > 5) {
params.height = dip2px(ActivityOfflineMap.this, 200);
downlocalMapListRecyclerView.setLayoutParams(params);
} else {
params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
downlocalMapListRecyclerView.setLayoutParams(params);
}
} else {
downView.setVisibility(View.GONE);
}
//更新数据
for (int iLoop = 0; iLoop < hotMapInfoList.size(); iLoop++) {
b = false;
for (int jLoop = 0; jLoop < downlocalMapInfoList.size(); jLoop++) {
if (hotMapInfoList.get(iLoop).cityID == downlocalMapInfoList.get(jLoop).cityID) {
hotMapInfoList.get(iLoop).ratio = downlocalMapInfoList.get(jLoop).ratio;
hotMapInfoList.get(iLoop).status = downlocalMapInfoList.get(jLoop).status;
hotMapInfoList.get(iLoop).geoPt = downlocalMapInfoList.get(jLoop).geoPt;
hotMapInfoList.get(iLoop).size = downlocalMapInfoList.get(jLoop).size;
hotMapInfoList.get(iLoop).serversize = downlocalMapInfoList.get(jLoop).serversize;
hotMapInfoList.get(iLoop).level = downlocalMapInfoList.get(jLoop).level;
hotMapInfoList.get(iLoop).update = downlocalMapInfoList.get(jLoop).update;
hotMapInfoList.get(iLoop).setnDownloadedSize(hotMapInfoList.get(iLoop).size);
hotMapInfoList.get(iLoop).updataColor();
b = true;
break;
}
}
if (!b) {
for (int jLoop = 0; jLoop < localMapInfoList.size(); jLoop++) {
if (hotMapInfoList.get(iLoop).cityID == localMapInfoList.get(jLoop).cityID) {
hotMapInfoList.get(iLoop).ratio = localMapInfoList.get(jLoop).ratio;
hotMapInfoList.get(iLoop).status = localMapInfoList.get(jLoop).status;
hotMapInfoList.get(iLoop).geoPt = localMapInfoList.get(jLoop).geoPt;
hotMapInfoList.get(iLoop).size = localMapInfoList.get(jLoop).size;
hotMapInfoList.get(iLoop).serversize = localMapInfoList.get(jLoop).serversize;
hotMapInfoList.get(iLoop).level = localMapInfoList.get(jLoop).level;
hotMapInfoList.get(iLoop).update = localMapInfoList.get(jLoop).update;
hotMapInfoList.get(iLoop).setnDownloadedSize(hotMapInfoList.get(iLoop).size);
hotMapInfoList.get(iLoop).updataColor();
b = true;
break;
}
}
}
if (!b && hotMapInfoList.get(iLoop).status != UNDEFINED) {//没找到
hotMapInfoList.get(iLoop).size = (long)0;
hotMapInfoList.get(iLoop).status = UNDEFINED;
hotMapInfoList.get(iLoop).setnDownloadedSize((long)0);
hotMapInfoList.get(iLoop).updataColor();
}
}
for (int iLoop = 0; iLoop < serverMapInfoList.size(); iLoop++) {
b = false;
for (int jLoop = 0; jLoop < downlocalMapInfoList.size(); jLoop++) {
if (serverMapInfoList.get(iLoop).cityID == downlocalMapInfoList.get(jLoop).cityID) {
serverMapInfoList.get(iLoop).ratio = downlocalMapInfoList.get(jLoop).ratio;
serverMapInfoList.get(iLoop).status = downlocalMapInfoList.get(jLoop).status;
serverMapInfoList.get(iLoop).geoPt = downlocalMapInfoList.get(jLoop).geoPt;
serverMapInfoList.get(iLoop).size = downlocalMapInfoList.get(jLoop).size;
serverMapInfoList.get(iLoop).serversize = downlocalMapInfoList.get(jLoop).serversize;
serverMapInfoList.get(iLoop).level = downlocalMapInfoList.get(jLoop).level;
serverMapInfoList.get(iLoop).update = downlocalMapInfoList.get(jLoop).update;
serverMapInfoList.get(iLoop).setnDownloadedSize(serverMapInfoList.get(iLoop).size);
serverMapInfoList.get(iLoop).updataColor();
b = true;
break;
}
}
if (!b) {
for (int jLoop = 0; jLoop < localMapInfoList.size(); jLoop++) {
if (serverMapInfoList.get(iLoop).cityID == localMapInfoList.get(jLoop).cityID) {
serverMapInfoList.get(iLoop).ratio = localMapInfoList.get(jLoop).ratio;
serverMapInfoList.get(iLoop).status = localMapInfoList.get(jLoop).status;
serverMapInfoList.get(iLoop).geoPt = localMapInfoList.get(jLoop).geoPt;
serverMapInfoList.get(iLoop).size = localMapInfoList.get(jLoop).size;
serverMapInfoList.get(iLoop).serversize = localMapInfoList.get(jLoop).serversize;
serverMapInfoList.get(iLoop).level = localMapInfoList.get(jLoop).level;
serverMapInfoList.get(iLoop).update = localMapInfoList.get(jLoop).update;
serverMapInfoList.get(iLoop).setnDownloadedSize(serverMapInfoList.get(iLoop).size);
serverMapInfoList.get(iLoop).updataColor();
b = true;
break;
}
}
}
if (!b && serverMapInfoList.get(iLoop).status != UNDEFINED) {//没找到
serverMapInfoList.get(iLoop).size = (long)0;
serverMapInfoList.get(iLoop).status = UNDEFINED;
serverMapInfoList.get(iLoop).setnDownloadedSize((long)0);
serverMapInfoList.get(iLoop).updataColor();
}
}
if (lm.getVisibility() == View.VISIBLE) {
if (bRefesh)
localMapRecyclerAdapter.notifyDataSetChanged();
downlocalMapRecyclerAdapter.notifyDataSetChanged();
} else {
hotMapRecyclerAdapter.notifyDataSetChanged();
serverMapRecyclerAdapter.notifyDataSetChanged();
}
}
}
@Override
protected void onPause() {
super.onPause();
Dispatcher.getInstance().unregister(this);
}
@Override
protected void onResume() {
super.onResume();
Dispatcher.getInstance().register(this);
}
@Subscribe
public void updata(String type) {
updateView();
}
@Override
protected void onDestroy() {
/**
* 退出时,销毁离线地图模块
*/
mOffline.destroy();
super.onDestroy();
}
@Override
public void onGetOfflineMapState(int type, int state) {
switch (type) {
case MKOfflineMap.TYPE_DOWNLOAD_UPDATE: {
MKOLUpdateElement update = mOffline.getUpdateInfo(state);
// 处理下载进度更新提示
if (update != null) {
//如果下载完成则下载另外一个
if (update.ratio == 100) {
for (int iLoop = 0; iLoop < downlocalMapInfoList.size(); iLoop++) {
if (update.cityID != downlocalMapInfoList.get(iLoop).cityID) {
mOffline.start(downlocalMapInfoList.get(iLoop).cityID);
break;
}
}
new Handler().postDelayed(new Runnable() {
public void run() {
updateView();
}
}, 500);
}
updateView();
}
}
break;
case MKOfflineMap.TYPE_NEW_OFFLINE:
// 有新离线地图安装
Log.d("OfflineDemo", String.format("add offlinemap num:%d", state));
break;
case MKOfflineMap.TYPE_VER_UPDATE:
// 版本更新提示
// MKOLUpdateElement e = mOffline.getUpdateInfo(state);
break;
default:
break;
}
}
/**
* Author:
* CreateTime: 2017/9/29 10:53
* Comment: 离线地图管理列表适配器
* Param:
*/
public class LocalMapRecyclerAdapter extends RecyclerView.Adapter {
private ArrayList localMapInfoList;
private ArrayList localMapInfoListTep;
private Activity activity;
private String csFilter = "";//列表过滤器
public LocalMapRecyclerAdapter(Activity activity, ArrayList localMapInfoList) {
this.activity = activity;
this.localMapInfoList = localMapInfoList;
}
public void refreshAll(ArrayList localMapInfoList) {
this.localMapInfoList = localMapInfoList;
notifyDataSetChanged();
}
@Override
public LocalMapViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ItemListLocalmapBinding binding = ItemListLocalmapBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
return new LocalMapViewHolder(binding.getRoot());
}
public void setFilter(String csFilter) {
this.csFilter = csFilter;
notifyDataSetChanged();
}
@Override
public void onBindViewHolder(LocalMapViewHolder holder, final int position) {
final LocalMapInfo localMapInfo;
if (!csFilter.equals(""))
localMapInfo = localMapInfoListTep.get(position);
else
localMapInfo = localMapInfoList.get(position);
holder.binding.itemTaskList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (localMapInfo.status == MKOLUpdateElement.DOWNLOADING ||
localMapInfo.status == MKOLUpdateElement.WAITING) {
mOffline.pause(localMapInfo.cityID);
localMapInfo.status = MKOLUpdateElement.SUSPENDED;
notifyDataSetChanged();
} else if (localMapInfo.status == MKOLUpdateElement.SUSPENDED) {
mOffline.start(localMapInfo.cityID);
} else if (localMapInfo.status == MKOLUpdateElement.eOLDSWifiError) {
mOffline.remove(localMapInfo.cityID);
mOffline.start(localMapInfo.cityID);
} else if (localMapInfo.status == MKOLUpdateElement.FINISHED) {
if (localMapInfo.update)
mOffline.update(localMapInfo.cityID);
} else if (localMapInfo.status == MKOLUpdateElement.UNDEFINED) {
mOffline.start(localMapInfo.cityID);
}
localMapInfo.updataColor();
Dispatcher.getInstance().post("updata");
}
});
holder.binding.itemTaskList.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(final View v) {
//if (localMapInfo.size > 0) {
new AlertDialog.Builder(ActivityOfflineMap.this).setTitle("是否删除<" + localMapInfo.cityName + ">离线包?")
.setIcon(android.R.drawable.ic_dialog_info)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mOffline.pause(localMapInfo.cityID);
mOffline.remove(localMapInfo.cityID);
if (localMapInfo.bServer) {
localMapInfo.size = (long)0;
localMapInfo.setnDownloadedSize((long)0);
localMapInfo.status = UNDEFINED;
localMapInfo.updataColor();
} else {
if (!csFilter.equals(""))
localMapInfoListTep.remove(position);
else
localMapInfoList.remove(position);
}
notifyDataSetChanged();
Dispatcher.getInstance().post("updata");
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点击“返回”后的操作,这里不设置没有任何操作
}
}).show();
// }
return false;
}
});
holder.binding.setLocalMapInfo(localMapInfo);
}
@Override
public int getItemCount() {
if (!csFilter.equals("")) {
localMapInfoListTep = new ArrayList();
for (int iLoop = 0; iLoop < localMapInfoList.size(); iLoop++) {
if (localMapInfoList.get(iLoop).csCityPY.contains(this.csFilter) ||
localMapInfoList.get(iLoop).csCityPYAll.contains(this.csFilter)) {
localMapInfoListTep.add(localMapInfoList.get(iLoop));
}
}
return localMapInfoListTep.size();
} else
return localMapInfoList.size();
}
@Override
public long getItemId(int position) {
return position - 1;
}
public class LocalMapViewHolder extends RecyclerView.ViewHolder {
ItemListLocalmapBinding binding;
public LocalMapViewHolder(View itemView) {
super(itemView);
binding = DataBindingUtil.bind(itemView);
}
}
}
/**
* 得到中文首字母
*
* @param str
* @return
*/
public static String getPinYinHeadChar(String str) {
String convert = "";
for (int j = 0; j < str.length(); j++) {
char word = str.charAt(j);
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
if (pinyinArray != null) {
convert += pinyinArray[0].charAt(0);
} else {
convert += word;
}
}
return convert;
}
/**
* 得到 全拼
*
* @param src
* @return
*/
public static String getPingYin(String src) {
char[] t1 = null;
t1 = src.toCharArray();
String[] t2 = new String[t1.length];
HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
t3.setVCharType(HanyuPinyinVCharType.WITH_V);
String t4 = "";
int t0 = t1.length;
try {
for (int i = 0; i < t0; i++) {
// 判断是否为汉字字符
if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
t4 += t2 == null ? t1[i] : t2[0];
} else {
t4 += Character.toString(t1[i]);
}
}
return t4;
} catch (BadHanyuPinyinOutputFormatCombination e1) {
e1.printStackTrace();
}
return t4.toLowerCase();
}
private void refreshData() {
bRefresh = true;
// 获取已下过的离线地图信息
localMapList = mOffline.getAllUpdateInfo();
localMapInfoList = new ArrayList();
downlocalMapInfoList = new ArrayList();
if (localMapList == null) {
localMapList = new ArrayList();
} else {
for (int iLoop = 0; iLoop < localMapList.size(); iLoop++) {
//已完成的
if (localMapList.get(iLoop).status == MKOLUpdateElement.FINISHED) {
LocalMapInfo localMapInfo = new LocalMapInfo(localMapList.get(iLoop));
localMapInfoList.add(localMapInfo);
} else if (localMapList.get(iLoop).status == MKOLUpdateElement.DOWNLOADING ||
localMapList.get(iLoop).status == MKOLUpdateElement.WAITING ||
localMapList.get(iLoop).status == MKOLUpdateElement.SUSPENDED ||
localMapList.get(iLoop).status == MKOLUpdateElement.eOLDSWifiError) {
LocalMapInfo localMapInfo = new LocalMapInfo(localMapList.get(iLoop));
downlocalMapInfoList.add(localMapInfo);
}
}
}
// 获取热门城市列表
ArrayList records1 = mOffline.getHotCityList();
hotMapInfoList = new ArrayList();
if (records1 != null) {
for (int iLoop = 0; iLoop < records1.size(); iLoop++) {
/*MKOLUpdateElement mKOLUpdateElement = new MKOLUpdateElement();
mKOLUpdateElement.cityID = records1.get(iLoop).cityID;
mKOLUpdateElement.cityName = records1.get(iLoop).cityName;
mKOLUpdateElement.serversize = (int)records1.get(iLoop).dataSize;*/
LocalMapInfo localMapInfo = new LocalMapInfo(records1.get(iLoop));
localMapInfo.setbHot(true);
localMapInfo.setbServer(true);
localMapInfo.updataColor();
//和本地数据比较
boolean b = false;
for (int zLoop = 0; zLoop < downlocalMapInfoList.size(); zLoop++) {
if (localMapInfo.cityID == downlocalMapInfoList.get(zLoop).cityID) {
try {
localMapInfo = (LocalMapInfo) downlocalMapInfoList.get(zLoop).clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
localMapInfo.setbServer(true);
localMapInfo.setbHot(true);
localMapInfo.updataColor();
hotMapInfoList.add(localMapInfo);
b = true;
break;
}
}
if (!b) {
for (int zLoop = 0; zLoop < localMapInfoList.size(); zLoop++) {
if (localMapInfo.cityID == localMapInfoList.get(zLoop).cityID) {
try {
localMapInfo = (LocalMapInfo) localMapInfoList.get(zLoop).clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
localMapInfo.setbServer(true);
localMapInfo.setbHot(true);
localMapInfo.updataColor();
hotMapInfoList.add(localMapInfo);
b = true;
break;
}
}
}
if (!b)
hotMapInfoList.add(localMapInfo);
}
}
// 获取所有支持离线地图的城市
serverMapInfoList = new ArrayList();
ArrayList records2 = mOffline.getOfflineCityList();
if (records2 != null) {
for (int iLoop = 0; iLoop < records2.size(); iLoop++) {
/*MKOLUpdateElement mKOLUpdateElement = new MKOLUpdateElement();
mKOLUpdateElement.cityID = records2.get(iLoop).cityID;
mKOLUpdateElement.cityName = records2.get(iLoop).cityName;
mKOLUpdateElement.serversize = (int)records2.get(iLoop).dataSize;*/
LocalMapInfo localMapInfo = new LocalMapInfo(records2.get(iLoop));
localMapInfo.setbServer(true);
localMapInfo.updataColor();
//和本地数据比较
boolean b = false;
for (int zLoop = 0; zLoop < downlocalMapInfoList.size(); zLoop++) {
if (localMapInfo.cityID == downlocalMapInfoList.get(zLoop).cityID) {
try {
localMapInfo = (LocalMapInfo) downlocalMapInfoList.get(zLoop).clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
localMapInfo.setbServer(true);
localMapInfo.updataColor();
serverMapInfoList.add(localMapInfo);
b = true;
break;
}
}
if (!b) {
for (int zLoop = 0; zLoop < localMapInfoList.size(); zLoop++) {
if (localMapInfo.cityID == localMapInfoList.get(zLoop).cityID) {
try {
localMapInfo = (LocalMapInfo) localMapInfoList.get(zLoop).clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
localMapInfo.setbServer(true);
localMapInfo.updataColor();
serverMapInfoList.add(localMapInfo);
b = true;
break;
}
}
}
if (!b)
serverMapInfoList.add(localMapInfo);
//如果是第一级,就是省循环城市
if (records2.get(iLoop).cityType == 1) {
for (int jLoop = 0; jLoop < records2.get(iLoop).childCities.size(); jLoop++) {
/*mKOLUpdateElement = new MKOLUpdateElement();
mKOLUpdateElement.cityID = records2.get(iLoop).childCities.get(jLoop).cityID;
mKOLUpdateElement.cityName = records2.get(iLoop).childCities.get(jLoop).cityName;
mKOLUpdateElement.serversize = (int) records2.get(iLoop).childCities.get(jLoop).dataSize;*/
localMapInfo = new LocalMapInfo(records2.get(iLoop).childCities.get(jLoop));
localMapInfo.setbServer(true);
localMapInfo.updataColor();
localMapInfo.setChildCity();
//和本地数据比较
b = false;
for (int zLoop = 0; zLoop < downlocalMapInfoList.size(); zLoop++) {
if (localMapInfo.cityID == downlocalMapInfoList.get(zLoop).cityID) {
try {
localMapInfo = (LocalMapInfo) downlocalMapInfoList.get(zLoop).clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
localMapInfo.setbServer(true);
localMapInfo.updataColor();
localMapInfo.setChildCity();
serverMapInfoList.add(localMapInfo);
b = true;
break;
}
}
if (!b) {
for (int zLoop = 0; zLoop < localMapInfoList.size(); zLoop++) {
if (localMapInfo.cityID == localMapInfoList.get(zLoop).cityID) {
try {
localMapInfo = (LocalMapInfo) localMapInfoList.get(zLoop).clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
localMapInfo.setbServer(true);
localMapInfo.updataColor();
localMapInfo.setChildCity();
serverMapInfoList.add(localMapInfo);
b = true;
break;
}
}
}
if (!b)
serverMapInfoList.add(localMapInfo);
}
}
}
}
//通知主线程去更改
Message message = new Message();
message.what = 1;
message.obj = null;
myHandler.sendMessage(message);
bRefresh = false;
}
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
private Handler myHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 1:
localMapListRecyclerView.setLayoutManager(new LinearLayoutManager(ActivityOfflineMap.this));
downlocalMapListRecyclerView.setLayoutManager(new LinearLayoutManager(ActivityOfflineMap.this));
hotMapListRecyclerView.setLayoutManager(new LinearLayoutManager(ActivityOfflineMap.this));
serverMapListRecyclerView.setLayoutManager(new LinearLayoutManager(ActivityOfflineMap.this));
localMapRecyclerAdapter = new LocalMapRecyclerAdapter(ActivityOfflineMap.this, localMapInfoList);
downlocalMapRecyclerAdapter = new LocalMapRecyclerAdapter(ActivityOfflineMap.this, downlocalMapInfoList);
hotMapRecyclerAdapter = new LocalMapRecyclerAdapter(ActivityOfflineMap.this, hotMapInfoList);
serverMapRecyclerAdapter = new LocalMapRecyclerAdapter(ActivityOfflineMap.this, serverMapInfoList);
localMapRecyclerAdapter.setHasStableIds(true);
localMapListRecyclerView.setAdapter(localMapRecyclerAdapter);
downlocalMapRecyclerAdapter.setHasStableIds(true);
downlocalMapListRecyclerView.setAdapter(downlocalMapRecyclerAdapter);
hotMapRecyclerAdapter.setHasStableIds(true);
hotMapListRecyclerView.setAdapter(hotMapRecyclerAdapter);
serverMapRecyclerAdapter.setHasStableIds(true);
serverMapListRecyclerView.setAdapter(serverMapRecyclerAdapter);
if (downlocalMapInfoList.size() > 0) {
downView.setVisibility(View.VISIBLE);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) downlocalMapListRecyclerView.getLayoutParams();
if (downlocalMapInfoList.size() > 5) {
params.height = dip2px(ActivityOfflineMap.this, 200);
downlocalMapListRecyclerView.setLayoutParams(params);
} else {
params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
downlocalMapListRecyclerView.setLayoutParams(params);
}
} else {
downView.setVisibility(View.GONE);
}
progressBar_Wait.setVisibility(View.GONE);
textView_wait.setVisibility(View.GONE);
bWait = false;
break;
}
}
};
//拦截返回键,等待刷新结束才能退出
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (bRefresh)
{
Toast.makeText(this, "正在获取地图数据,请稍候再返回", Toast.LENGTH_LONG).show();
return true;
}
break;
}
return super.onKeyUp(keyCode, event);
}
}
activity_offline.xml
LocalMapInfo.java
package com.dji.sdk.search.Bean;
import com.baidu.mapapi.map.offline.MKOLSearchRecord;
import com.baidu.mapapi.map.offline.MKOLUpdateElement;
import com.baidu.mapapi.model.LatLng;
import static com.dji.sdk.ActivityOfflineMap.getPinYinHeadChar;
import static com.dji.sdk.ActivityOfflineMap.getPingYin;
/**
* Created by on 2017/9/29.
*/
public class LocalMapInfo implements Cloneable {
public LocalMapInfo(MKOLUpdateElement mKOLUpdateElement) {
this.cityID = mKOLUpdateElement.cityID;
this.cityName = mKOLUpdateElement.cityName;
this.ratio = mKOLUpdateElement.ratio;
this.status = mKOLUpdateElement.status;
this.geoPt = mKOLUpdateElement.geoPt;
this.size = (long) mKOLUpdateElement.size;
this.serversize = (long) mKOLUpdateElement.serversize;
this.level = mKOLUpdateElement.level;
this.update = mKOLUpdateElement.update;
this.csCityName = mKOLUpdateElement.cityName;
this.nDownloadedSize = (long) mKOLUpdateElement.size;
bServer = false;
bHot = false;
csCityPY = getPinYinHeadChar(this.cityName);
csCityPYAll = getPingYin(this.cityName);
updataColor();
}
public LocalMapInfo(MKOLSearchRecord mKOLSearchRecord) {
this.cityID = mKOLSearchRecord.cityID;
this.cityName = mKOLSearchRecord.cityName;
this.serversize =mKOLSearchRecord.dataSize;
this.csCityName = mKOLSearchRecord.cityName;
bServer = false;
bHot = false;
csCityPY = getPinYinHeadChar(this.cityName);
csCityPYAll = getPingYin(this.cityName);
updataColor();
}
public static final int UNDEFINED = 0;
public static final int DOWNLOADING = 1;
public static final int WAITING = 2;
public static final int SUSPENDED = 3;
public static final int FINISHED = 4;
public static final int eOLDSMd5Error = 5;
public static final int eOLDSNetError = 6;
public static final int eOLDSIOError = 7;
public static final int eOLDSWifiError = 8;
public static final int eOLDSFormatError = 9;
public static final int eOLDSInstalling = 10;
public int cityID;
public String cityName="";
public int ratio = 0;
public int status = UNDEFINED;
public LatLng geoPt;
public Long size = (long)0;
public Long serversize=(long)0;
public int level;
public boolean update = false;
private String csCityName="";//城市名
private String csDownloadedSize="";//已下载
private String csUpdateSize="";//是否更新
private Long nDownloadedSize=(long)0;
private int nUpdateSize = 0;
public byte nTextColor;
public boolean bShowProgress = true;
public boolean bServer = false;
private boolean bHot = false;
public String csCityPY="";
public String csCityPYAll="";
public boolean bShowDownPng = false;
public int getnUpdateSize() {
return nUpdateSize;
}
public void setnUpdateSize(int nUpdateSize) {
this.nUpdateSize = nUpdateSize;
}
public Long getnDownloadedSize() {
return nDownloadedSize;
}
public void setnDownloadedSize(Long nDownloadedSize) {
this.nDownloadedSize = nDownloadedSize;
}
public String getCsUpdateSize() {
return csUpdateSize;
}
public void setCsUpdateSize(String csUpdateSize) {
this.csUpdateSize = csUpdateSize;
}
public String getCsDownloadedSize() {
return csDownloadedSize;
}
public void setCsDownloadedSize(String csDownloadedSize) {
this.csDownloadedSize = csDownloadedSize;
}
public String getCsCityName() {
return csCityName;
}
public void setCsCityName(String csCityName) {
this.csCityName = csCityName;
}
public void updataColor() {
bShowDownPng = false;
csDownloadedSize = String.format("%.1fM", (double) size / (1024 * 1024));
if (status == FINISHED) {
if (update) {
//csUpdateSize = "有更新,只需" + df.format((double)(serversize - nDownloadedSize) / (1024 * 1024)) + "M";
csUpdateSize = String.format("有更新,只需%.1fM", (double) (serversize - size) / (1024 * 1024));
bShowDownPng = bServer;
nTextColor = 1;
} else {
csUpdateSize = bServer ? "已下载" : "完成";
nTextColor = 0;
}
bShowProgress = false;
} else {
if (status == DOWNLOADING) {
if (update) {
//csUpdateSize = bServer ? "离线地图正在更新 " + df.format((double)(serversize - nDownloadedSize) / (1024 * 1024)) + "M" : "离线地图正在更新 " + ratio + "%";
csUpdateSize = bServer ? String.format("离线地图正在更新 %.1fM", (double) (serversize - size) / (1024 * 1024)) : "离线地图正在更新 " + ratio + "%";
nTextColor = 1;
} else {
//csUpdateSize = bServer ? "正在下载 " + df.format((double)(serversize - nDownloadedSize) / (1024 * 1024)) + "M" : "正在下载 " + ratio + "%";
csUpdateSize = bServer ? String.format("正在下载 %.1fM", (double) (serversize - size) / (1024 * 1024)) : "正在下载 " + ratio + "%";
nTextColor = 1;
}
} else if (status == SUSPENDED) {
//csUpdateSize = bServer ? "已暂停 " + df.format((double)(serversize - nDownloadedSize) / (1024 * 1024)) + "M" : "已暂停 " + ratio + "%";
csUpdateSize = bServer ? String.format("已暂停 %.1fM", (double) (serversize - size) / (1024 * 1024)) : "已暂停 " + ratio + "%";
nTextColor = 2;
} else if (status == eOLDSWifiError) {
csUpdateSize = "等待下载";
nTextColor = 1;
} else if (status == WAITING) {
//csUpdateSize = bServer ? "等待下载 " + df.format((double) (serversize - nDownloadedSize) / (1024 * 1024)) + "M" : "等待下载 " + ratio + "%";
csUpdateSize = bServer ? String.format("等待下载 %.1fM", (double) (serversize - size) / (1024 * 1024)) : "等待下载 " + ratio + "%";
nTextColor = 1;
} else if (status == UNDEFINED) {
//csUpdateSize = bServer ? df.format((double)serversize / (1024 * 1024)) + "M" : "";
csUpdateSize = bServer ? String.format("%.1fM", (double) serversize / (1024 * 1024)) : "";
bShowDownPng = bServer;
nTextColor = 1;
}
}
}
public void setbServer(boolean bServer) {
this.bServer = bServer;
if (bServer) {
bShowProgress = false;
}
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
public void setbHot(boolean bHot) {
this.bHot = bHot;
}
public void setChildCity() {
if (bServer && !this.bHot) {
this.csCityName = " " + this.cityName;
}
}
}
item_list_localmap.xml