@Override public void searchEnd() { System.out.println("camera home activity search end call back"); ArrayList<Device> deviceList = onvifMgr.getOnvifData().getDeviceList(); handler.post( new Runnable() { @Override public void run() { progressBar.setVisibility(View.INVISIBLE); } }); if (deviceList.size() <= 0) { handler.post( new Runnable() { @Override public void run() { Toast toast = Toast.makeText( getApplicationContext(), "没有搜索到设备,请确认摄像头在局域网内", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } }); return; } if (searchDeviceFlag == 1) { /*点击button*/ searchDeviceFlag = 0; onResumeFlag = 1; Intent intent = new Intent(); intent.setClass(CamerasGridActivity.this, DeviceListActivity.class); CamerasGridActivity.this.startActivity(intent); } else { int appUsingCount = onvifMgr.getAppUsingCount(); System.out.println("appUsingCount ===" + appUsingCount); if (appUsingCount == 0) { onvifMgr.addAppUsingCount(); onResumeFlag = 1; Intent intent = new Intent(); intent.setClass(CamerasGridActivity.this, DeviceListActivity.class); CamerasGridActivity.this.startActivity(intent); } else { onvifMgr.addAppUsingCount(); handler.post( new Runnable() { @Override public void run() { autoMatchAfterDiscoverEnd(); } }); } } }
@Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.cameras_grid); onvifMgr = OnvifManager.getInstance(); onvifMgr.setContext(getApplicationContext()); onvifMgr.setListener(this); handler = new Handler(); displayView(); deviceSearch(); }
/*onresume 后检查数据*/ public void checkAfterOnResume() { ArrayList<GridsItemStatus> items = onvifMgr.getOnvifData().getGridsItemList(); for (int i = 0; i < items.size(); i++) { View view = cameraGrid.getChildAt(i); GridViewHolder holder = (GridViewHolder) view.getTag(); holder.vv.setBackgroundResource(Color.TRANSPARENT); if (items.get(i).getStatus() == 0) { holder.vv.setBackgroundResource(R.drawable.camera_default); } } }
@Override protected void onResume() { System.out.println("on resume====="); if (onResumeFlag == 1) { checkAfterOnResume(); } else if (onResumeFlag == 2) { if (onvifMgr.getOnvifData().getCurrentCameras().get(itemPosition).isAuth()) { /*认证,获取视频流OK*/ playByItemPosition(itemPosition); } else { Toast.makeText(this, "请先登陆", Toast.LENGTH_SHORT).show(); } } super.onResume(); }
/*不是第一次使用的时候,搜索完毕自动匹配存储好的密码进行链接*/ private void autoMatchAfterDiscoverEnd() { ArrayList<CameraData> cameraList = onvifMgr.getOnvifData().getCameras(); // 搜索到的所有设备 System.out.println("camera List size ===" + cameraList.size()); // 先清空一下所有的List onvifMgr.getOnvifData().initData(); for (CameraData camera : cameraList) { LocalCamera lCamera = onvifMgr.getLocalCameraByUUid(camera); if (lCamera != null) { camera.setUsername(lCamera.getUsername()); camera.setPassword(lCamera.getPassword()); onvifMgr.getOnvifData().getCurrentCameras().add(camera); } } System.out.println("current size=====" + onvifMgr.getOnvifData().getCurrentCameras().size()); /*本地没有保存用户名和密码*/ if (onvifMgr.getOnvifData().getCurrentCameras().size() <= 0) { onResumeFlag = 1; Intent intent = new Intent(); intent.setClass(CamerasGridActivity.this, DeviceListActivity.class); CamerasGridActivity.this.startActivity(intent); } else { /*本地保存用户名和密码,直接播放*/ for (int i = 0; i < onvifMgr.getOnvifData().getCurrentCameras().size(); i++) { onvifMgr.getOnvifData().getGridsItemList().get(i).setStatus(0); View view = cameraGrid.getChildAt(i); GridViewHolder holder = (GridViewHolder) view.getTag(); holder.vv.setBackgroundResource(R.drawable.camera_default); CameraData camera = onvifMgr.getOnvifData().getCurrentCameras().get(i); String username = camera.getUsername(); String password = camera.getPassword(); String deviceService = onvifMgr.getOnvifData().getCurrentCameras().get(i).getDevice().getDeviceService(); boolean isGetCapa = onvifMgr.getDeviceCapabilities(username, password, deviceService); boolean auth = onvifMgr.getMediaStreamUri(username, password, deviceService); if (isGetCapa && auth) { /*vv背景设置为透明*/ holder.vv.setBackgroundColor(R.color.transparency); onvifMgr.play(i, holder.vv); cameraGrid.requestFocus(); } } } }
/*播放*/ public void playByItemPosition(int position) { View view = cameraGrid.getChildAt(position); GridViewHolder holder = (GridViewHolder) view.getTag(); holder.vv.setBackgroundColor(R.color.transparency); onvifMgr.play(position, holder.vv); }