haijiang
2018-07-17 63b87b529196c056fb4d96a58eded3936d3445f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package com.moral.screen.activity;
 
import android.Manifest;
import android.content.Intent;
import android.graphics.Point;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
 
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.lzy.okgo.cache.CacheMode;
import com.moral.andbrickslib.utils.FastJsonTools;
import com.moral.screen.MainApp;
import com.moral.screen.common.API;
import com.moral.screen.httputils.HttpCallBack;
import com.moral.screen.model.DeviceBean;
import com.moral.screen.R;
import com.moral.screen.base.BaseActivity;
import com.moral.screen.update.UpdateManager;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
 
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
 
/**
 * Created by hjzhang on 2018/7/2.
 */
 
public class MapActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks{
    private TextView tv_left,tv_right;
    private MapView bmapView;
    private BaiduMap mBaiduMap;
    BitmapDescriptor bdNormal = BitmapDescriptorFactory
            .fromResource(R.mipmap.icon_normal);
    BitmapDescriptor bdA = BitmapDescriptorFactory
            .fromResource(R.mipmap.icon_level1);
    BitmapDescriptor bdB = BitmapDescriptorFactory
            .fromResource(R.mipmap.icon_level2);
    BitmapDescriptor bdC = BitmapDescriptorFactory
            .fromResource(R.mipmap.icon_level3);
    BitmapDescriptor bdOffline = BitmapDescriptorFactory
            .fromResource(R.mipmap.icon_offline);
    private ArrayList<Marker> mkList = new ArrayList<>();
    private ArrayList<DeviceBean> deviceList = new ArrayList<>();
    private View popView;
    private TextView tv_name,tv_address;
    private ImageView iv_close;
    private LinearLayout ll_pop;
    private DeviceBean curDeviceBean;
    private Timer timer;
    /**
     * 计时器
     */
    private boolean isFinish = false;
    private void showData() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if(!isFinish){
                    getData(false);
                }
            }
        },0,10*1000);
    }
    @Override
    protected void getBundleExtras(Bundle extras) {
 
    }
 
    @Override
    protected int getLayoutId() {
        return R.layout.ac_map_layout;
    }
 
    @Override
    protected void initViews() {
        tv_left = findView(R.id.tv_left);
        tv_right = findView(R.id.tv_right);
        bmapView = findView(R.id.bmapView);
        popView = LayoutInflater.from(this).inflate(R.layout.pop_layout,null);
        tv_name = popView.findViewById(R.id.tv_name);
        tv_address = popView.findViewById(R.id.tv_address);
        iv_close = popView.findViewById(R.id.iv_close);
        ll_pop = popView.findViewById(R.id.ll_pop);
        initMap();
    }
 
    @Override
    protected void initListener() {
        tv_left.setOnClickListener(this);
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBaiduMap.hideInfoWindow();
            }
        });
        ll_pop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if("4".equals(curDeviceBean.getState())){
                    mToatUtils.showSingletonToast("该设备正在维保中...");
                    return;
                }
                Intent intent = new Intent(MapActivity.this,DeviceDataActivity.class);
                intent.putExtra("deviceVersionId",curDeviceBean.getDeviceVersionId());
                intent.putExtra("mac",curDeviceBean.getMac());
                intent.putExtra("deviceId",curDeviceBean.getId());
                startActivity(intent);
            }
        });
        tv_right.setOnClickListener(this);
    }
 
    @Override
    protected void initData() {
        getData(true);
        showData();
        /**
         * 版本更新
         */
        if (Build.VERSION.SDK_INT >= 11) {
            new checkUpdateTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        } else {
            new checkUpdateTask().execute();
        }
    }
 
    @Override
    protected void processClick(View view) {
        switch (view.getId()){
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_right:
                MainApp.theApp.mSharedPreferencesUtil.exitLogin();
                Intent intent = new Intent(MapActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
                break;
        }
 
    }
 
    @Override
    protected void onErrorPageClick() {
 
    }
 
    private void initMap(){
        mBaiduMap = bmapView.getMap();
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
 
        mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            public boolean onMarkerClick(final Marker marker) {
                if (marker != null) {
                    curDeviceBean = deviceList.get(marker.getZIndex());
                    tv_address.setText("地址:"+curDeviceBean.getAddress());
                    tv_name.setText(curDeviceBean.getName());
                    final LatLng ll = marker.getPosition();
                    InfoWindow mInfoWindow;
                    Point p = mBaiduMap.getProjection().toScreenLocation(ll);
                    p.y -= 47;
                    LatLng llInfo = mBaiduMap.getProjection().fromScreenLocation(p);
                    //为弹出的InfoWindow添加点击事件
                    mInfoWindow = new InfoWindow(popView,llInfo,0);
                    mBaiduMap.showInfoWindow(mInfoWindow);
                }
                return true;
            }
        });
    }
 
    /**
     * 标注地图
     */
    private void showMapData(boolean show){
        mBaiduMap.clear();
        MarkerOptions mo = null;
        Marker mMarker = null;
        LatLng ll = null;
        for (int i = 0; i < deviceList.size(); i++) {
            DeviceBean bean = deviceList.get(i);
            double lat = bean.getLatitude();
            double lon = bean.getLongitude();
            ll = new LatLng(lat, lon);
//            0 正常
//            1 一级警报
//            2 二级警报
//            3 三级警报
//            4 维保
            if ("0".equals(bean.getState())) {
                mo = new MarkerOptions().position(ll).icon(bdNormal)
                        .zIndex(i);
            } else if ("1".equals(bean.getState())) {
                mo = new MarkerOptions().position(ll).icon(bdA)
                        .zIndex(i);
            } else if ("2".equals(bean.getState())) {
                mo = new MarkerOptions().position(ll).icon(bdB)
                        .zIndex(i);
            }else if ("3".equals(bean.getState())) {
                mo = new MarkerOptions().position(ll).icon(bdC)
                        .zIndex(i);
            }else if ("4".equals(bean.getState())) {
                mo = new MarkerOptions().position(ll).icon(bdOffline)
                        .zIndex(i);
            }
            // 掉下动画
            if(show)
               mo.animateType(MarkerOptions.MarkerAnimateType.grow);
            mMarker = (Marker) (mBaiduMap.addOverlay(mo));
            mkList.add(mMarker);
        }
    }
 
    @Override
    public void onResume() {
        super.onResume();
        bmapView.onResume();
    }
 
    @Override
    public void onPause() {
        super.onPause();
        bmapView.onPause();
    }
 
    @Override
    public void onDestroy() {
        bdNormal.recycle();
        bdOffline.recycle();
        bdA.recycle();
        bdB.recycle();
        bdC.recycle();
        super.onDestroy();
        bmapView.onDestroy();
        if(timer!=null){
            timer.cancel();
            timer =null;
        }
    }
 
    private void getData(final boolean show) {
        HashMap<String, String> params = new HashMap<>();
        params.put("organizationId", MainApp.theApp.orgId+"");
        mHttpUtils.doGet(API.DEVICES, params, CacheMode.DEFAULT, show, new HttpCallBack() {
            @Override
            public void onSuccess(String res, String msg) {
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
                ArrayList<DeviceBean> temp = (ArrayList<DeviceBean>) FastJsonTools.getArrayJson(res,DeviceBean.class);
                if(temp!=null){
                    deviceList.clear();
                    deviceList.addAll(temp);
                }
                if(show){
                    if(deviceList.size()>0){
                        DeviceBean bean = deviceList.get(0);
                        LatLng ll = new LatLng(bean.getLatitude(),
                                bean.getLongitude());
                        MapStatus.Builder builder = new MapStatus.Builder();
                        builder.target(ll).zoom(18.0f);
                        mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
                    }
                }
                showMapData(show);
            }
 
            @Override
            public void showLoadingDialog() {
                progressDialog.setTitleText("正在获取数据...");
                progressDialog.show();
            }
 
            @Override
            public void onFail(int errno, String s) {
                mToatUtils.showSingletonToast(s);
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
            }
        });
 
    }
 
 
    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        if (requestCode == WRITE_EXTERNAL_STORAGE) {
            manager.showNoticeDialog();
        }
    }
 
    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
 
    }
 
    private final static int WRITE_EXTERNAL_STORAGE = 0x01;
 
    @AfterPermissionGranted(WRITE_EXTERNAL_STORAGE)
    public void updateAPP() {
        String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (EasyPermissions.hasPermissions(this, perms)) {
            // Already have permission, do the thing
            manager.showNoticeDialog();
        } else {
            // Do not have permissions, request them now
            EasyPermissions.requestPermissions(this, "温馨提示,版本升级需要写存储权限!",
                    WRITE_EXTERNAL_STORAGE, perms);
        }
    }
 
    UpdateManager manager = new UpdateManager(MapActivity.this);
 
    private class checkUpdateTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            Looper.prepare();
            // 检查软件更新
            manager.checkUpdateJson();
            Looper.loop();
            return null;
        }
 
        @Override
        protected void onPostExecute(String result) {
        }
 
        @Override
        protected void onPreExecute() {
        }
 
        @Override
        protected void onProgressUpdate(Void... values) {
        }
    }
}