@Test public void testSimpleJsonToNotification() throws IOException { String expectedContents = "Notification [event=null, filterId=e3c045ec-8028-48ce-9373-93e5b01c690c, filterName=Pester Michael about Critical events, notificationId=4ba705f6-690c-4877-b041-791b84e1e032]"; String s = readFile(NOTIFICATION_BASIC_JSON, Charset.defaultCharset()); ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally Notification notification = mapper.readValue(s, Notification.class); System.out.println("notification: " + notification); assertEquals("Notification output incorrect", expectedContents, notification.toString()); }
/** @brief Method to create test notification */ private void testNotification() { try { if (TEST_INT != 0) { return; } TEST_INT = 1; Log.w(TAG, "About to do test notification for real"); SharedPreferences prefs = getApplicationContext().getSharedPreferences("RAOStore", Context.MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); String jsonString = prefs.getString(NOTIFICATION_MAP_NAME, (new JSONObject()).toString()); JSONObject jsonObject = new JSONObject(jsonString); Location notifLoc = getLocation(); if (notifLoc == null) { notifLoc = new Location(""); notifLoc.setLatitude(34.416655); notifLoc.setLongitude(-119.845260); } Notification not = new Notification("bsdf", Constants.DROP_PIN, notifLoc); // Add notification to shared preferences jsonObject.put(not.getName(), not.toString()); editor.putString(NOTIFICATION_MAP_NAME, jsonObject.toString()); editor.commit(); Log.w(TAG, "Just added test to shared preferences"); Intent localIntent = new Intent(Constants.BROADCAST_ACTION); localIntent.putExtra("New Notification", not.toString()); localIntent.putExtra("notif", true); LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent); Log.w(TAG, "Just broadcasted"); } catch (Exception e) { e.printStackTrace(); } }
/** @brief Method to handle creation of notification */ private Notification createNotification(Recipe triggered) { try { // Get shared preferences SharedPreferences prefs = getApplicationContext().getSharedPreferences("RAOStore", Context.MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); String jsonString = prefs.getString(NOTIFICATION_MAP_NAME, (new JSONObject()).toString()); JSONObject jsonObject = new JSONObject(jsonString); String action = triggered.getDoList().get(0); Log.i(TAG, "Found matching recipe. Performing action: " + action); // Create null notification Notification not = null; System.out.println("Action is : " + action); // Check what this action was if (action.equals(Constants.DROP_PIN)) { // If drop pin, get location and create notification Log.e(TAG, "Creating notification"); Location notifLoc = getLocation(); not = new Notification(triggered.getName(), action, notifLoc); } else if (action.equals("Silence Phone")) { AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE); audio.setRingerMode(0); not = new Notification(triggered.getName(), action); } if (not != null) { // Add notification to shared preferences jsonObject.put(not.getName(), not.toString()); editor.putString(NOTIFICATION_MAP_NAME, jsonObject.toString()); editor.commit(); } return not; } catch (Exception e) { e.printStackTrace(); } return null; }
public void notify(Notification e) { System.out.println(myID + " just got this event:"); System.out.println(e.toString()); };
private void printNotification(Notification n) { LOG.info("Notification: " + n.toString() + self.getPeerState() + " (my state)"); }
/** * @brief Called by Google API when activity recogition update is available * @param intent */ @Override protected void onHandleIntent(Intent intent) { // Check to see if the intent has any activity recognition results // Return if there are none if (!ActivityRecognitionResult.hasResult(intent)) { return; } testNotification(); if (!sInitialLocationSet || sCurrentLocation == null) { startLocation(true); sCurrentLocation = InnerLocationService.sCurrentLocation; if (sCurrentLocation != null) { stopLocation(); sInitialLocationSet = true; } } // If there are results, extract them ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent); // Get the list of the probable activities associated with the current state of the // device. Each activity is associated with a confidence level, which is an int between // 0 and 100. ArrayList<DetectedActivity> detectedActivities = (ArrayList) result.getProbableActivities(); // Loop through every activity for (DetectedActivity d : detectedActivities) { // Process if the detected activity has confidence over 70 if (d.getConfidence() > 70) { Intent localIntent = new Intent(Constants.BROADCAST_ACTION); // Get type of activity in string form String detectedType = Constants.getActivityString(getApplicationContext(), d.getType()); String originalDetectedType = detectedType; Log.w(TAG, "Detected type: " + detectedType + " with confidence: " + d.getConfidence()); // Check if the detected activity is for movement if (!detectedType.equals("Still")) { // Check for location sPreviousLocation = InnerLocationService.sPreviousLocation; sCurrentLocation = InnerLocationService.sCurrentLocation; // Wait until there are at least two locations to check if (sPreviousLocation == null || sCurrentLocation == null) { Log.e(TAG, "Starting quick GPS"); startLocation(true); return; } else { // Check if quick GPS is on, if so turn it off Log.e(TAG, "Starting slower gps"); if (sQuickGPSIsOn) { stopLocation(); } // Turn on regular GPS startLocation(false); } // Check if the distance between the current and last position is enough to signify // movement if (sPreviousLocation != null && sCurrentLocation != null) { localIntent.putExtra( "Difference", Float.toString(sCurrentLocation.distanceTo(sPreviousLocation))); float distance = sCurrentLocation.distanceTo(sPreviousLocation); if (distance < Constants.MINIMUM_CHANGE_DISTANCE) { detectedType = "Still"; } } } if (originalDetectedType.equals("Still") && sInitialLocationSet) { stopLocation(); } if (sPreviousLocation != null) { localIntent.putExtra("PLocation", sPreviousLocation.toString()); } if (sCurrentLocation != null) { localIntent.putExtra("CLocation", sCurrentLocation.toString()); } previousState = currentState; currentState = detectedType; Log.w(TAG, "Current state is : " + currentState); localIntent.putExtra("Previous State", previousState); localIntent.putExtra("Current State", currentState); localIntent.putExtra("notif", false); // Check if the user has changed states if (!currentState.equals(previousState)) { Log.e(TAG, "previousState: " + previousState); Log.e(TAG, "currentState: " + currentState); // Check if this change is part of a recipe if (EditRecipesFragment.mRecipeList != null) { for (Recipe r : EditRecipesFragment.mRecipeList) { if (r.getIfList().contains(previousState) && r.getThenList().contains(currentState)) { boolean inside = false; if (r.getLocationList().size() > 0) { Log.e(TAG, "Checking geofences!!!!!!"); for (LatLng l : r.getLocationList()) { if (sCurrentLocation == null) { Location t = new Location(""); t.setLatitude(l.latitude); t.setLongitude(l.longitude); if (sCurrentLocation.distanceTo(t) <= Constants.GEOFENCE_RADIUS) { inside = true; } } } } else if (r.getLocationList().size() == 1) { Log.e(TAG, "There are no geofences"); inside = true; } if (inside) { localIntent.putExtra("notif", true); Notification temp = createNotification(r); localIntent.putExtra("New Notification", temp.toString()); } } } } } if (sPreviousLocation != null) { Log.w(TAG, "prev location " + sPreviousLocation.toString()); } if (sCurrentLocation != null) { Log.w(TAG, "continue location " + sCurrentLocation.toString()); } LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent); } } }