@Test public void getMessages() { long t0 = 1586200500000L; for (int testCase = 3; testCase < 4; testCase++) { HashMap<String, List<Long>> scannedBleMap = new HashMap<>(); List<BleRecord> bleRecords = new ArrayList<>(); if (testCase == 1) { bleRecords.add(new BleRecord( "6f6d3cf7-ec31-7a3b-2563-2aab28ec37bb", t0 + (60000 * 12), 0, 0 )); bleRecords.add(new BleRecord( "ff268fd5-9ed9-46a4-556d-352936e9f064", t0 + (60000 * 17), 0, 0 )); } else if (testCase == 2) { bleRecords.add(new BleRecord( "6f6d3cf7-ec31-7a3b-2563-2aab28ec37bb", t0 + (60000 * 3), 0, 0 )); bleRecords.add(new BleRecord( "6f6d3cf7-ec31-7a3b-2563-2aab28ec37bb", t0 + (60000 * 8), 0, 0 )); bleRecords.add(new BleRecord( "6f6d3cf7-ec31-7a3b-2563-2aab28ec37bb", t0 + (60000 * 13), 0, 0 )); bleRecords.add(new BleRecord( "ff268fd5-9ed9-46a4-556d-352936e9f064", t0 + (60000 * 18), 0, 0 )); } else if (testCase == 3) { bleRecords.add(new BleRecord( "6f6d3cf7-ec31-7a3b-2563-2aab28ec37bb", t0 + (60000 * 3), 0, 0 )); bleRecords.add(new BleRecord( "6f6d3cf7-ec31-7a3b-2563-2aab28ec37bb", t0 + (60000 * 8), 0, 0 )); bleRecords.add(new BleRecord( "6f6d3cf7-ec31-7a3b-2563-2aab28ec37bb", t0 + (60000 * 12), 0, 0 )); bleRecords.add(new BleRecord( "ff268fd5-9ed9-46a4-556d-352936e9f064", t0 + (60000 * 18), 0, 0 )); bleRecords.add(new BleRecord( "ff268fd5-9ed9-46a4-556d-352936e9f064", t0 + (60000 * 20), 0, 0 )); } for (BleRecord bleRecord : bleRecords) { if (!scannedBleMap.containsKey(bleRecord.getUuid())) { scannedBleMap.put(bleRecord.getUuid(), new LinkedList<Long>()); } scannedBleMap.get(bleRecord.getUuid()).add(bleRecord.getTs()); } long[] times = PullFromServerTask.isExposed("00000000-0000-0000-0000-000000000000", t0, 0, scannedBleMap); if (testCase == 1) { assertNull(times); } else if (testCase == 2) { long start = (times[0] - t0) / 60000; long end = (times[1] - t0) / 60000; System.out.println("times " + start + "," + end); assertEquals(start, 3); assertEquals(end, 18); } else if (testCase == 3) { assertNull(times); } } }
public List<BluetoothMatch> getMessages(double lat, double longi, int precision, long lastQueryTime) { String messageListRequest = MessageListRequest.toHttpString(lat, longi, precision, lastQueryTime); Log.e("NET ","SEND MESSAGE LIST REQUEST "); JSONObject response = NetworkHelper.sendRequest(messageListRequest, Request.Method.GET,null); try { if (response == null || (response.has("statusCode") && response.getInt("statusCode") != 200)) { return null; } } catch(Exception e) { Log.e("err",e.getMessage()); return null; } MessageListResponse messageListResponse = null; try { messageListResponse = MessageListResponse.parse(response); } catch (Exception e) { Log.e("err",e.getMessage()); return null; } JSONObject messageRequestObj = null; try { messageRequestObj = MessageRequest.toJson(messageListResponse.messageInfo); } catch(Exception e) { Log.e("err",e.getMessage()); return null; } if (messageRequestObj == null) { return null; } String messageRequest = MessageRequest.toHttpString(); Log.e("NET ","MESSAGE REQUEST num of messages: "+messageListResponse.messageInfo.length); Log.e("NET ","MESSAGE REQUEST payload: "+messageRequestObj.toString()); response = NetworkHelper.sendRequest(messageRequest, Request.Method.POST, messageRequestObj); try { if (response == null || (response.has("statusCode") && response.getInt("statusCode") != 200)) { return null; } } catch(Exception e) { Log.e("err",e.getMessage()); return null; } MatchMessage[] matchMessages = null; try { JSONArray matchMessagesArr = response.getJSONArray("results"); matchMessages = new MatchMessage[matchMessagesArr.length()]; for (int i = 0; i < matchMessagesArr.length(); i++) { matchMessages[i] = MatchMessage.parse(matchMessagesArr.getJSONObject(i)); } } catch (Exception e) { Log.e("err",e.getMessage()); return null; } if (matchMessages == null || matchMessages.length == 0) { return null; } SharedPreferences prefs = context.getSharedPreferences(Constants.SHARED_PREFENCE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); if (messageListResponse.maxResponseTimestamp > 0) { editor.putLong(context.getString(R.string.time_of_last_query_pkey), messageListResponse.maxResponseTimestamp); editor.commit(); } List<String> narrowCastMessages = new ArrayList<String>(); List<Long> narrowCastMessageStartTimes = new ArrayList<>(); List<Long> narrowCastMessageEndTimes = new ArrayList<>(); for (int i = 0; i < matchMessages.length; i++) { if (matchMessages[i].areaMatches != null) { for (AreaMatch areaMatch : matchMessages[i].areaMatches) { Area[] areas = areaMatch.areas; for (Area area : areas) { if (intersect(area)) { Log.e("msg", "NARROWCAST USER MESSAGE " + areaMatch.userMessage); narrowCastMessages.add(areaMatch.userMessage); narrowCastMessageStartTimes.add(area.beginTime); narrowCastMessageEndTimes.add(area.endTime); break; } } } } } Log.e("msg","notify bulk narrowcast "+narrowCastMessages.size()); notifyBulk(Constants.MessageType.NarrowCast, narrowCastMessages,narrowCastMessageStartTimes,narrowCastMessageEndTimes); List<BluetoothMatch> bluetoothMatches = new ArrayList<>(); for (int i = 0; i < matchMessages.length; i++) { for (BluetoothMatch match : matchMessages[i].bluetoothMatches) { bluetoothMatches.add(match); } } return bluetoothMatches; }
PullFromServerTask extends AsyncTask<Void, Void, Void> { public List<BluetoothMatch> getMessages(double lat, double longi, int precision, long lastQueryTime) { String messageListRequest = MessageListRequest.toHttpString(lat, longi, precision, lastQueryTime); Log.e("NET ","SEND MESSAGE LIST REQUEST "); JSONObject response = NetworkHelper.sendRequest(messageListRequest, Request.Method.GET,null); try { if (response == null || (response.has("statusCode") && response.getInt("statusCode") != 200)) { return null; } } catch(Exception e) { Log.e("err",e.getMessage()); return null; } MessageListResponse messageListResponse = null; try { messageListResponse = MessageListResponse.parse(response); } catch (Exception e) { Log.e("err",e.getMessage()); return null; } JSONObject messageRequestObj = null; try { messageRequestObj = MessageRequest.toJson(messageListResponse.messageInfo); } catch(Exception e) { Log.e("err",e.getMessage()); return null; } if (messageRequestObj == null) { return null; } String messageRequest = MessageRequest.toHttpString(); Log.e("NET ","MESSAGE REQUEST num of messages: "+messageListResponse.messageInfo.length); Log.e("NET ","MESSAGE REQUEST payload: "+messageRequestObj.toString()); response = NetworkHelper.sendRequest(messageRequest, Request.Method.POST, messageRequestObj); try { if (response == null || (response.has("statusCode") && response.getInt("statusCode") != 200)) { return null; } } catch(Exception e) { Log.e("err",e.getMessage()); return null; } MatchMessage[] matchMessages = null; try { JSONArray matchMessagesArr = response.getJSONArray("results"); matchMessages = new MatchMessage[matchMessagesArr.length()]; for (int i = 0; i < matchMessagesArr.length(); i++) { matchMessages[i] = MatchMessage.parse(matchMessagesArr.getJSONObject(i)); } } catch (Exception e) { Log.e("err",e.getMessage()); return null; } if (matchMessages == null || matchMessages.length == 0) { return null; } SharedPreferences prefs = context.getSharedPreferences(Constants.SHARED_PREFENCE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); if (messageListResponse.maxResponseTimestamp > 0) { editor.putLong(context.getString(R.string.time_of_last_query_pkey), messageListResponse.maxResponseTimestamp); editor.commit(); } List<String> narrowCastMessages = new ArrayList<String>(); List<Long> narrowCastMessageStartTimes = new ArrayList<>(); List<Long> narrowCastMessageEndTimes = new ArrayList<>(); for (int i = 0; i < matchMessages.length; i++) { if (matchMessages[i].areaMatches != null) { for (AreaMatch areaMatch : matchMessages[i].areaMatches) { Area[] areas = areaMatch.areas; for (Area area : areas) { if (intersect(area)) { Log.e("msg", "NARROWCAST USER MESSAGE " + areaMatch.userMessage); narrowCastMessages.add(areaMatch.userMessage); narrowCastMessageStartTimes.add(area.beginTime); narrowCastMessageEndTimes.add(area.endTime); break; } } } } } Log.e("msg","notify bulk narrowcast "+narrowCastMessages.size()); notifyBulk(Constants.MessageType.NarrowCast, narrowCastMessages,narrowCastMessageStartTimes,narrowCastMessageEndTimes); List<BluetoothMatch> bluetoothMatches = new ArrayList<>(); for (int i = 0; i < matchMessages.length; i++) { for (BluetoothMatch match : matchMessages[i].bluetoothMatches) { bluetoothMatches.add(match); } } return bluetoothMatches; } }
PullFromServerTask extends AsyncTask<Void, Void, Void> { public List<BluetoothMatch> getMessages(double lat, double longi, int precision, long lastQueryTime) { String messageListRequest = MessageListRequest.toHttpString(lat, longi, precision, lastQueryTime); Log.e("NET ","SEND MESSAGE LIST REQUEST "); JSONObject response = NetworkHelper.sendRequest(messageListRequest, Request.Method.GET,null); try { if (response == null || (response.has("statusCode") && response.getInt("statusCode") != 200)) { return null; } } catch(Exception e) { Log.e("err",e.getMessage()); return null; } MessageListResponse messageListResponse = null; try { messageListResponse = MessageListResponse.parse(response); } catch (Exception e) { Log.e("err",e.getMessage()); return null; } JSONObject messageRequestObj = null; try { messageRequestObj = MessageRequest.toJson(messageListResponse.messageInfo); } catch(Exception e) { Log.e("err",e.getMessage()); return null; } if (messageRequestObj == null) { return null; } String messageRequest = MessageRequest.toHttpString(); Log.e("NET ","MESSAGE REQUEST num of messages: "+messageListResponse.messageInfo.length); Log.e("NET ","MESSAGE REQUEST payload: "+messageRequestObj.toString()); response = NetworkHelper.sendRequest(messageRequest, Request.Method.POST, messageRequestObj); try { if (response == null || (response.has("statusCode") && response.getInt("statusCode") != 200)) { return null; } } catch(Exception e) { Log.e("err",e.getMessage()); return null; } MatchMessage[] matchMessages = null; try { JSONArray matchMessagesArr = response.getJSONArray("results"); matchMessages = new MatchMessage[matchMessagesArr.length()]; for (int i = 0; i < matchMessagesArr.length(); i++) { matchMessages[i] = MatchMessage.parse(matchMessagesArr.getJSONObject(i)); } } catch (Exception e) { Log.e("err",e.getMessage()); return null; } if (matchMessages == null || matchMessages.length == 0) { return null; } SharedPreferences prefs = context.getSharedPreferences(Constants.SHARED_PREFENCE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); if (messageListResponse.maxResponseTimestamp > 0) { editor.putLong(context.getString(R.string.time_of_last_query_pkey), messageListResponse.maxResponseTimestamp); editor.commit(); } List<String> narrowCastMessages = new ArrayList<String>(); List<Long> narrowCastMessageStartTimes = new ArrayList<>(); List<Long> narrowCastMessageEndTimes = new ArrayList<>(); for (int i = 0; i < matchMessages.length; i++) { if (matchMessages[i].areaMatches != null) { for (AreaMatch areaMatch : matchMessages[i].areaMatches) { Area[] areas = areaMatch.areas; for (Area area : areas) { if (intersect(area)) { Log.e("msg", "NARROWCAST USER MESSAGE " + areaMatch.userMessage); narrowCastMessages.add(areaMatch.userMessage); narrowCastMessageStartTimes.add(area.beginTime); narrowCastMessageEndTimes.add(area.endTime); break; } } } } } Log.e("msg","notify bulk narrowcast "+narrowCastMessages.size()); notifyBulk(Constants.MessageType.NarrowCast, narrowCastMessages,narrowCastMessageStartTimes,narrowCastMessageEndTimes); List<BluetoothMatch> bluetoothMatches = new ArrayList<>(); for (int i = 0; i < matchMessages.length; i++) { for (BluetoothMatch match : matchMessages[i].bluetoothMatches) { bluetoothMatches.add(match); } } return bluetoothMatches; } PullFromServerTask(Context context, Activity av, View view); }
PullFromServerTask extends AsyncTask<Void, Void, Void> { public List<BluetoothMatch> getMessages(double lat, double longi, int precision, long lastQueryTime) { String messageListRequest = MessageListRequest.toHttpString(lat, longi, precision, lastQueryTime); Log.e("NET ","SEND MESSAGE LIST REQUEST "); JSONObject response = NetworkHelper.sendRequest(messageListRequest, Request.Method.GET,null); try { if (response == null || (response.has("statusCode") && response.getInt("statusCode") != 200)) { return null; } } catch(Exception e) { Log.e("err",e.getMessage()); return null; } MessageListResponse messageListResponse = null; try { messageListResponse = MessageListResponse.parse(response); } catch (Exception e) { Log.e("err",e.getMessage()); return null; } JSONObject messageRequestObj = null; try { messageRequestObj = MessageRequest.toJson(messageListResponse.messageInfo); } catch(Exception e) { Log.e("err",e.getMessage()); return null; } if (messageRequestObj == null) { return null; } String messageRequest = MessageRequest.toHttpString(); Log.e("NET ","MESSAGE REQUEST num of messages: "+messageListResponse.messageInfo.length); Log.e("NET ","MESSAGE REQUEST payload: "+messageRequestObj.toString()); response = NetworkHelper.sendRequest(messageRequest, Request.Method.POST, messageRequestObj); try { if (response == null || (response.has("statusCode") && response.getInt("statusCode") != 200)) { return null; } } catch(Exception e) { Log.e("err",e.getMessage()); return null; } MatchMessage[] matchMessages = null; try { JSONArray matchMessagesArr = response.getJSONArray("results"); matchMessages = new MatchMessage[matchMessagesArr.length()]; for (int i = 0; i < matchMessagesArr.length(); i++) { matchMessages[i] = MatchMessage.parse(matchMessagesArr.getJSONObject(i)); } } catch (Exception e) { Log.e("err",e.getMessage()); return null; } if (matchMessages == null || matchMessages.length == 0) { return null; } SharedPreferences prefs = context.getSharedPreferences(Constants.SHARED_PREFENCE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); if (messageListResponse.maxResponseTimestamp > 0) { editor.putLong(context.getString(R.string.time_of_last_query_pkey), messageListResponse.maxResponseTimestamp); editor.commit(); } List<String> narrowCastMessages = new ArrayList<String>(); List<Long> narrowCastMessageStartTimes = new ArrayList<>(); List<Long> narrowCastMessageEndTimes = new ArrayList<>(); for (int i = 0; i < matchMessages.length; i++) { if (matchMessages[i].areaMatches != null) { for (AreaMatch areaMatch : matchMessages[i].areaMatches) { Area[] areas = areaMatch.areas; for (Area area : areas) { if (intersect(area)) { Log.e("msg", "NARROWCAST USER MESSAGE " + areaMatch.userMessage); narrowCastMessages.add(areaMatch.userMessage); narrowCastMessageStartTimes.add(area.beginTime); narrowCastMessageEndTimes.add(area.endTime); break; } } } } } Log.e("msg","notify bulk narrowcast "+narrowCastMessages.size()); notifyBulk(Constants.MessageType.NarrowCast, narrowCastMessages,narrowCastMessageStartTimes,narrowCastMessageEndTimes); List<BluetoothMatch> bluetoothMatches = new ArrayList<>(); for (int i = 0; i < matchMessages.length; i++) { for (BluetoothMatch match : matchMessages[i].bluetoothMatches) { bluetoothMatches.add(match); } } return bluetoothMatches; } PullFromServerTask(Context context, Activity av, View view); int howBig(double lat, double longi, int precision, long ts); List<BluetoothMatch> getMessages(double lat, double longi, int precision, long lastQueryTime); boolean intersect(Area area); static long[] isExposed(String seed, long start_ts, long end_ts, HashMap<String,List<Long>> scannedBleMap); void notifyBulk(Constants.MessageType messageType,
                           List<String> msgs,
                           List<Long> contactTimesStart,
                           List<Long> contactTimesEnd); }
PullFromServerTask extends AsyncTask<Void, Void, Void> { public List<BluetoothMatch> getMessages(double lat, double longi, int precision, long lastQueryTime) { String messageListRequest = MessageListRequest.toHttpString(lat, longi, precision, lastQueryTime); Log.e("NET ","SEND MESSAGE LIST REQUEST "); JSONObject response = NetworkHelper.sendRequest(messageListRequest, Request.Method.GET,null); try { if (response == null || (response.has("statusCode") && response.getInt("statusCode") != 200)) { return null; } } catch(Exception e) { Log.e("err",e.getMessage()); return null; } MessageListResponse messageListResponse = null; try { messageListResponse = MessageListResponse.parse(response); } catch (Exception e) { Log.e("err",e.getMessage()); return null; } JSONObject messageRequestObj = null; try { messageRequestObj = MessageRequest.toJson(messageListResponse.messageInfo); } catch(Exception e) { Log.e("err",e.getMessage()); return null; } if (messageRequestObj == null) { return null; } String messageRequest = MessageRequest.toHttpString(); Log.e("NET ","MESSAGE REQUEST num of messages: "+messageListResponse.messageInfo.length); Log.e("NET ","MESSAGE REQUEST payload: "+messageRequestObj.toString()); response = NetworkHelper.sendRequest(messageRequest, Request.Method.POST, messageRequestObj); try { if (response == null || (response.has("statusCode") && response.getInt("statusCode") != 200)) { return null; } } catch(Exception e) { Log.e("err",e.getMessage()); return null; } MatchMessage[] matchMessages = null; try { JSONArray matchMessagesArr = response.getJSONArray("results"); matchMessages = new MatchMessage[matchMessagesArr.length()]; for (int i = 0; i < matchMessagesArr.length(); i++) { matchMessages[i] = MatchMessage.parse(matchMessagesArr.getJSONObject(i)); } } catch (Exception e) { Log.e("err",e.getMessage()); return null; } if (matchMessages == null || matchMessages.length == 0) { return null; } SharedPreferences prefs = context.getSharedPreferences(Constants.SHARED_PREFENCE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); if (messageListResponse.maxResponseTimestamp > 0) { editor.putLong(context.getString(R.string.time_of_last_query_pkey), messageListResponse.maxResponseTimestamp); editor.commit(); } List<String> narrowCastMessages = new ArrayList<String>(); List<Long> narrowCastMessageStartTimes = new ArrayList<>(); List<Long> narrowCastMessageEndTimes = new ArrayList<>(); for (int i = 0; i < matchMessages.length; i++) { if (matchMessages[i].areaMatches != null) { for (AreaMatch areaMatch : matchMessages[i].areaMatches) { Area[] areas = areaMatch.areas; for (Area area : areas) { if (intersect(area)) { Log.e("msg", "NARROWCAST USER MESSAGE " + areaMatch.userMessage); narrowCastMessages.add(areaMatch.userMessage); narrowCastMessageStartTimes.add(area.beginTime); narrowCastMessageEndTimes.add(area.endTime); break; } } } } } Log.e("msg","notify bulk narrowcast "+narrowCastMessages.size()); notifyBulk(Constants.MessageType.NarrowCast, narrowCastMessages,narrowCastMessageStartTimes,narrowCastMessageEndTimes); List<BluetoothMatch> bluetoothMatches = new ArrayList<>(); for (int i = 0; i < matchMessages.length; i++) { for (BluetoothMatch match : matchMessages[i].bluetoothMatches) { bluetoothMatches.add(match); } } return bluetoothMatches; } PullFromServerTask(Context context, Activity av, View view); int howBig(double lat, double longi, int precision, long ts); List<BluetoothMatch> getMessages(double lat, double longi, int precision, long lastQueryTime); boolean intersect(Area area); static long[] isExposed(String seed, long start_ts, long end_ts, HashMap<String,List<Long>> scannedBleMap); void notifyBulk(Constants.MessageType messageType,
                           List<String> msgs,
                           List<Long> contactTimesStart,
                           List<Long> contactTimesEnd);  }