hxb
2022-11-22 5d0b8109b4e56463343ad23f75d1a1c1153ce5f9
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
package com.lechange.demo.ui;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
 
import com.alibaba.android.arouter.facade.annotation.Route;
import com.common.openapi.ClassInstanceManager;
import com.common.openapi.DeviceListService;
import com.common.openapi.DeviceSubAccountListService;
import com.common.openapi.IGetDeviceInfoCallBack;
import com.common.openapi.MethodConst;
import com.common.openapi.entity.DeviceDetailListData;
import com.common.openapi.entity.DeviceListData;
import com.hdl.HdlToLc;
import com.lechange.demo.R;
import com.lechange.demo.adapter.DeviceListAdapter;
import com.lechange.demo.view.LcPullToRefreshRecyclerView;
import com.lechange.pulltorefreshlistview.Mode;
import com.lechange.pulltorefreshlistview.PullToRefreshBase;
import com.mm.android.deviceaddmodule.LCDeviceEngine;
import com.mm.android.deviceaddmodule.mobilecommon.utils.LogUtil;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
 
public class DeviceListActivity extends Activity implements IGetDeviceInfoCallBack.ISubAccountDevice< DeviceDetailListData.Response>, PullToRefreshBase.OnRefreshListener2, View.OnClickListener {
    private static final String TAG = DeviceListActivity.class.getSimpleName();
    private LcPullToRefreshRecyclerView deviceList;
    private RecyclerView mRecyclerView;
    private List<DeviceDetailListData.ResponseData.DeviceListBean> datas = new ArrayList<>();
    private DeviceListAdapter deviceListAdapter;
    private LinearLayout llAdd;
    private LinearLayout llBack;
    private RelativeLayout rlNoDevice;
    //乐橙分页index
    public long baseBindId = -1;
    //开放平台分页index
    public long openBindId = -1;
    int pageNum = 1;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_list);
        initView();
        HdlToLc.requestPermission();
    }
 
    private void initView() {
        llAdd = findViewById(R.id.ll_add);
        llBack = findViewById(R.id.ll_back);
        rlNoDevice = findViewById(R.id.rl_no_device);
        deviceList = findViewById(R.id.device_list);
        deviceList.setOnRefreshListener(this);
        llAdd.setOnClickListener(this);
        llBack.setOnClickListener(this);
        refreshState(false);
        mRecyclerView = deviceList.getRefreshableView();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(DeviceListActivity.this));
        deviceListAdapter = new DeviceListAdapter(DeviceListActivity.this, datas);
        mRecyclerView.setAdapter(deviceListAdapter);
        deviceListAdapter.setOnItemClickListener(new DeviceListAdapter.OnItemClickListener() {
            @Override
            public void onSettingClick(int position) {
                if (datas.size() == 0) {
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putSerializable(MethodConst.ParamConst.deviceDetail, datas.get(position));
                bundle.putString(MethodConst.ParamConst.fromList, MethodConst.ParamConst.fromList);
                Intent intent = new Intent(DeviceListActivity.this, DeviceDetailActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
 
            @Override
            public void onDetailClick(int position) {
                if (datas.size() == 0) {
                    return;
                }
                if (!datas.get(position).status.equals("online")) {
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putSerializable(MethodConst.ParamConst.deviceDetail, datas.get(position));
                Intent intent = new Intent(DeviceListActivity.this, DeviceOnlineMediaPlayActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
 
            @Override
            public void onChannelClick(int outPosition, int innerPosition) {
                if (datas.size() == 0) {
                    return;
                }
                if (!datas.get(outPosition).channels.get(innerPosition).status.equals("online")) {
                    return;
                }
                Bundle bundle = new Bundle();
                DeviceDetailListData.ResponseData.DeviceListBean deviceListBean = datas.get(outPosition);
                deviceListBean.checkedChannel = innerPosition;
                bundle.putSerializable(MethodConst.ParamConst.deviceDetail, deviceListBean);
                Intent intent = new Intent(DeviceListActivity.this, DeviceOnlineMediaPlayActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshMode(Mode.PULL_FROM_START);
                refreshState(true);
            }
        }, 200);
    }
 
 
   /* public void deviceList(DeviceDetailListData.Response responseData) {
        if (isDestroyed()) {
            return;
        }
        refreshState(false);
        if (responseData.baseBindId != -1) {
            baseBindId = responseData.baseBindId;
        }
        if (responseData.openBindId != -1) {
            openBindId = responseData.openBindId;
        }
        if (responseData.data != null && responseData.data.deviceList != null && responseData.data.deviceList.size() != 0) {
            Iterator<DeviceDetailListData.ResponseData.DeviceListBean> iterator =  responseData.data.deviceList.iterator();
            while(iterator.hasNext()){
                DeviceDetailListData.ResponseData.DeviceListBean next = iterator.next();
                if (next.channels.size() == 0 && !next.catalog.contains("NVR")) {
                    // 使用迭代器中的remove()方法,可以删除元素.
                    iterator.remove();
                }
            }
        }
        //没有数据
        if ((responseData.data == null || responseData.data.deviceList == null || responseData.data.deviceList.size() == 0) && datas.size() == 0) {
            //本次未拉到数据且上次也没有数据
            rlNoDevice.setVisibility(View.VISIBLE);
            deviceList.setVisibility(View.GONE);
        } else {
            if ((responseData.data == null || responseData.data.deviceList == null || responseData.data.deviceList.size() == 0)) {
                return;
            }
            rlNoDevice.setVisibility(View.GONE);
            deviceList.setVisibility(View.VISIBLE);
            datas.addAll(responseData.data.deviceList);
            deviceListAdapter.notifyDataSetChanged();
            if (datas.size() >= DeviceListService.pageSize) {
                refreshMode(Mode.BOTH);
            } else {
                refreshMode(Mode.PULL_FROM_START);
            }
        }
    }*/
 
 
    @Override
    public void DeviceList(DeviceDetailListData.Response responseData) {
        if (isDestroyed()) {
            return;
        }
        refreshState(false);
        if (responseData.baseBindId != -1) {
            baseBindId = responseData.baseBindId;
        }
        if (responseData.openBindId != -1) {
            openBindId = responseData.openBindId;
        }
        if (responseData.data != null && responseData.data.deviceList != null && responseData.data.deviceList.size() != 0) {
            Iterator<DeviceDetailListData.ResponseData.DeviceListBean> iterator =  responseData.data.deviceList.iterator();
            while(iterator.hasNext()){
                DeviceDetailListData.ResponseData.DeviceListBean next = iterator.next();
                if (next.channels!=null&&next.channels.size() == 0 && !next.catalog.contains("NVR")) {
                    // 使用迭代器中的remove()方法,可以删除元素.
                    iterator.remove();
                }
            }
        }
        //没有数据
        if ((responseData.data == null || responseData.data.deviceList == null || responseData.data.deviceList.size() == 0) && datas.size() == 0) {
            //本次未拉到数据且上次也没有数据
            rlNoDevice.setVisibility(View.VISIBLE);
            deviceList.setVisibility(View.GONE);
        } else {
            if ((responseData.data == null || responseData.data.deviceList == null || responseData.data.deviceList.size() == 0)) {
                return;
            }
            rlNoDevice.setVisibility(View.GONE);
            deviceList.setVisibility(View.VISIBLE);
            datas.addAll(responseData.data.deviceList);
            deviceListAdapter.notifyDataSetChanged();
            if (datas.size() >= DeviceListService.pageSize) {
                refreshMode(Mode.BOTH);
            } else {
                refreshMode(Mode.PULL_FROM_START);
            }
        }
    }
 
    @Override
    public void onError(Throwable throwable) {
        if (isDestroyed()) {
            return;
        }
        refreshState(false);
        LogUtil.errorLog(TAG, "error", throwable);
        Toast.makeText(DeviceListActivity.this, throwable.getMessage(), Toast.LENGTH_SHORT).show();
    }
 
    @Override
    public void onPullDownToRefresh(PullToRefreshBase pullToRefreshBase) {
        pageNum = 1;
        getDeviceList(false);
    }
 
    @Override
    public void onPullUpToRefresh(PullToRefreshBase pullToRefreshBase) {
        pageNum = pageNum+1;
        getDeviceList(true);
    }
 
    private void getDeviceList(boolean isLoadMore) {
        if (!isLoadMore) {
            baseBindId = -1;
            openBindId = -1;
            datas.clear();
        }
        DeviceSubAccountListService deviceSubAccountListService = ClassInstanceManager.newInstance().getDeviceSubAccountListService();
        deviceSubAccountListService.getSubAccountDeviceList(pageNum,this);
 
      /*  DeviceListService deviceVideoService = ClassInstanceManager.newInstance().getDeviceListService();
        DeviceListData deviceListData = new DeviceListData();
        deviceListData.data.openBindId = this.openBindId;
        deviceListData.data.baseBindId = this.baseBindId;
        deviceVideoService.deviceBaseList(deviceListData, DeviceListActivity.this);*/
    }
 
    private void refreshState(boolean refresh) {
        if (refresh) {
            deviceList.setRefreshing(true);
        } else {
            deviceList.onRefreshComplete();
        }
    }
 
    private void refreshMode(Mode mode) {
        deviceList.setMode(mode);
    }
 
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_back) {
            finish();
        } else if (id == R.id.ll_add) {
            try {
                LCDeviceEngine.newInstance().addDevice(this);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}