package com.smartdot.cgt.activity; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import com.smartdot.cgt.R; import com.smartdot.cgt.request.ApiClent; import com.smartdot.cgt.util.BaseThread; import com.smartdot.cgt.util.HandlerStatus; import com.smartdot.cgt.util.Msg; import com.smartdot.cgt.view.TitleBar; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Timer; import java.util.TimerTask; public class FrmPersonOnline extends BaseActivity { private View view = null; private LinearLayout wr_areas; public int patrolclick = 770000; private TitleBar titlebar; private BaseThread thread; private HashMap<String, BaseThread> threadList = new HashMap<String, BaseThread>(5); JSONArray deptArray; List<Map<String,String>> mapList = new ArrayList<Map<String,String>>();; @Override protected void setLayout() { initActivity(); setContentView(R.layout.problemlist2); titlebar = (TitleBar) this.findViewById(R.id.titlebar); titlebar.setTitleText("在岗人员"); view = getLayoutInflater().inflate(R.layout.problemlist2, null); wr_areas=(LinearLayout) findViewById(R.id.wr_areas); } @Override protected void doInit() { refreshlist(); } public void refreshlist(){ deptArray = null; wr_areas.removeAllViews(); showlist(); try{ while (deptArray == null){ Thread.currentThread().sleep(100); } }catch (Exception e){ e.printStackTrace(); } addperson(); } public void addperson(){ thread = new BaseThread(baseHandler) { @Override public void runThread() { final Message msg = thread.obtainMessage(); if (msg != null) { msg.what = HandlerStatus.REQUEST_LIST; for(int i=0;i<deptArray.length();i++){ try{ final String id = deptArray.getJSONObject(i).getString("id"); final String name = deptArray.getJSONObject(i).getString("name"); ApiClent.userDeviceOnLine(id, new ApiClent.ClientCallback() { @Override public void onSuccess(Object data) { try { JSONObject jsonObject = new JSONObject(data.toString()); JSONArray jsonarr = jsonObject.getJSONArray("data"); Map<String,String> map = new HashMap<>(); map.put("name",name); map.put("jsonstr",jsonarr.toString()); mapList.add(map); msg.arg1 = HandlerStatus.HANDLE_OK; } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.i("list_error", e.getMessage()); msg.arg1 = HandlerStatus.HANDLE_FAIL; } } @Override public void onFailure(String message) { msg.arg1 = HandlerStatus.HANDLE_FAIL; } @Override public void onError(Exception e) { msg.arg1 = HandlerStatus.HANDLE_ERROR; } }); }catch (Exception e){ e.printStackTrace(); msg.arg1 = HandlerStatus.HANDLE_ERROR; } } msg.obj = mapList; this.sendMessage(msg); } } }; thread.start(); } @Override protected void addEventListener() { } private boolean checkIsNeedCloseProgress() { boolean canCloseProgress = true; for (BaseThread thread : threadList.values()) { if (thread != null && thread.getIsThreadGoing()) { canCloseProgress = false; } } if (canCloseProgress) { try { closeProgress(); } catch (Exception e) { e.printStackTrace(); } } return canCloseProgress; } @SuppressWarnings("unchecked") @Override protected void handleMessage(Message msg) { if(msg.what ==HandlerStatus.REQUEST_LIST){ if(msg.arg1 == HandlerStatus.HANDLE_OK){ try{ List<Map<String,String>> list= (List<Map<String,String>>) msg.obj; for(int j=0; j<list.size();j++){ Map<String,String> map = list.get(j); LinearLayout llWashingRoomItem = new LinearLayout(view.getContext()); llWashingRoomItem.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); llWashingRoomItem = (LinearLayout) getLayoutInflater().inflate(R.layout.table_view1, null); TextView txt = (TextView) llWashingRoomItem.findViewById(R.id.tv_wr_txt); txt.setText(map.get("name")); JSONArray jsonArray = new JSONArray(map.get("jsonstr")); View user_view = getLayoutInflater().inflate(R.layout.table_view1, null); final LinearLayout user_areas = (LinearLayout) llWashingRoomItem.findViewById(R.id.user_areas); llWashingRoomItem.setClickable(true); llWashingRoomItem.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ if(user_areas.getVisibility() == View.GONE){ user_areas.setVisibility(View.VISIBLE); }else if(user_areas.getVisibility() == View.VISIBLE){ user_areas.setVisibility(View.GONE); } } }); for (int i = 0; i < jsonArray.length(); i++) { JSONObject json = jsonArray.getJSONObject(i); LinearLayout linearLayoutItem = new LinearLayout(user_view.getContext()); linearLayoutItem.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); linearLayoutItem = (LinearLayout) getLayoutInflater().inflate(R.layout.table_view4, null); TextView name = (TextView) linearLayoutItem.findViewById(R.id.tv_name_txt); name.setText(json.getString("name")); TextView phone = (TextView) linearLayoutItem.findViewById(R.id.tv_phone_txt); phone.setText(json.getString("lastTimeFmt")); ImageView head = (ImageView) linearLayoutItem.findViewById(R.id.userhead); if(json.getBoolean("onLine")){ head.setBackgroundDrawable(getResources().getDrawable(R.drawable.online)); }else{ head.setBackgroundDrawable(getResources().getDrawable(R.drawable.offline)); } user_areas.addView(linearLayoutItem); } wr_areas.addView(llWashingRoomItem); } } catch (JSONException e){ e.printStackTrace(); } } else if (msg.arg1 == HandlerStatus.HANDLE_FAIL) { Msg.showInfo(FrmPersonOnline.this, "获取在岗人员失败!"); } else { Msg.showInfo(FrmPersonOnline.this, "获取在岗人员时出现错误!"); } checkIsNeedCloseProgress(); } } public void showlist(){ thread = new BaseThread(baseHandler) { @Override public void runThread() { final Message msg = thread.obtainMessage(); if (msg != null) { ApiClent.depttree(new ApiClent.ClientCallback() { @Override public void onSuccess(Object data) { try { JSONObject jsonObject = new JSONObject(data.toString()); deptArray = jsonObject.getJSONObject("data").getJSONArray("list"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onFailure(String message) { } @Override public void onError(Exception e) { } }); } } }; thread.start(); } private View.OnClickListener btnOnClick = new View.OnClickListener() { @Override public void onClick(View v){ switch (v.getId()) { default: break; } } }; @Override protected void onCancelProgress(DialogInterface arg0) { for (BaseThread thread : threadList.values()) { if (thread != null) { thread.interrupt(); } } threadList.clear(); } }