/** * @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); } } }