use of com.google.android.gms.location.DetectedActivity in project DataLogger by sussexwearlab.
the class HARecognizerApiLogger method logDetectedActivities.
public void logDetectedActivities(ArrayList<DetectedActivity> detectedActivities) {
HashMap<Integer, Integer> detectedActivitiesMap = new HashMap<>();
for (DetectedActivity activity : detectedActivities) {
detectedActivitiesMap.put(activity.getType(), activity.getConfidence());
}
// Timestamp in system nanoseconds since boot, including time spent in sleep.
long nanoTime = SystemClock.elapsedRealtimeNanos() + mNanosOffset;
// System local time in millis
long currentMillis = (new Date()).getTime();
String message = String.format("%s", currentMillis) + ";" + String.format("%s", nanoTime) + ";" + String.format("%s", mNanosOffset);
for (int i = 0; i < Constants.API_ACTIVITY_RECOGNIZER_LIST.length; i++) {
message += ";" + Integer.toString(detectedActivitiesMap.containsKey(Constants.API_ACTIVITY_RECOGNIZER_LIST[i]) ? detectedActivitiesMap.get(Constants.API_ACTIVITY_RECOGNIZER_LIST[i]) : 0);
}
log(message);
log(System.lineSeparator());
}
use of com.google.android.gms.location.DetectedActivity in project xDrip by NightscoutFoundation.
the class ActivityRecognizedService method onHandleIntent.
@Override
protected void onHandleIntent(Intent intent) {
final PowerManager.WakeLock wl = JoH.getWakeLock(TAG, 60000);
try {
if (intent.getStringExtra(START_ACTIVITY_ACTION) != null) {
start(true);
} else if (intent.getStringExtra(RESTART_ACTIVITY_ACTION) != null) {
restart(FREQUENCY);
checkVehicleRepeatNotification();
} else if (intent.getStringExtra(STOP_ACTIVITY_ACTION) != null) {
UserError.Log.uel(TAG, "Stopping service");
stop();
} else if (intent.getStringExtra(RECHECK_VEHICLE_MODE) != null) {
checkVehicleRepeatNotification();
} else if (ActivityRecognitionResult.hasResult(intent)) {
if ((Pref.getBooleanDefaultFalse("motion_tracking_enabled"))) {
if (mApiClient == null)
start();
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
final int topConfidence = handleDetectedActivities(result.getProbableActivities(), true, 0);
last_data = JoH.ts();
received++;
if (d)
Log.d(TAG, JoH.hourMinuteString() + " :: Packets received: " + received + " Top confidence: " + topConfidence);
if ((received > MAX_RECEIVED) || (topConfidence > 90))
// one hit only
stopUpdates();
} else {
// / DEEEBUG
UserError.Log.wtf(TAG, "Received ActivityRecognition we were not expecting!");
}
} else {
// spoof from intent
final String payload = intent.getStringExtra(INCOMING_ACTIVITY_ACTION);
if ((payload != null) && (Pref.getBoolean("use_remote_motion", false))) {
try {
final String[] amup = payload.split("\\^");
final long timestamp = Long.parseLong(amup[0]);
final int activity = Integer.parseInt(amup[1]);
final List<DetectedActivity> incoming_list = new ArrayList<>();
incoming_list.add(new DetectedActivity(activity, 101));
handleDetectedActivities(incoming_list, false, timestamp);
} catch (Exception e) {
Log.wtf(TAG, "Exception processing incoming motion: " + e.toString());
}
}
}
} finally {
JoH.releaseWakeLock(wl);
}
}
use of com.google.android.gms.location.DetectedActivity in project xDrip-plus by jamorham.
the class ActivityRecognizedService method handleDetectedActivities.
private int handleDetectedActivities(List<DetectedActivity> probableActivities, boolean from_local, long timestamp) {
DetectedActivity topActivity = null;
int topConfidence = 0;
if (timestamp == 0)
timestamp = JoH.tsl();
incrementInternalPrefsLong(RECEIVED);
for (DetectedActivity activity : probableActivities) {
if ((activity.getType() != DetectedActivity.TILTING) && (activity.getType() != DetectedActivity.UNKNOWN)) {
if (activity.getConfidence() > topConfidence) {
topActivity = activity;
topConfidence = activity.getConfidence();
}
}
}
if (topActivity != null) {
if (d)
UserError.Log.uel(TAG, "Top activity: " + topActivity.toString() + "req: " + getInternalPrefsLong(REQUESTED) + " rec: " + getInternalPrefsLong(RECEIVED));
if ((topActivity.getType() != DetectedActivity.UNKNOWN) && (topActivity.getType() != DetectedActivity.TILTING)) {
if (activityState == null)
activityState = getLastStoredDetectedActivity();
if (((topActivity.getConfidence() > 89) || ((lastactivity != null) && (topActivity.getType() == lastactivity.getType()) && ((lastactivity.getConfidence() + topActivity.getConfidence()) > 150))) && ((activityState == null) || (activityState.getType() != topActivity.getType()))) {
if (Pref.getBoolean("motion_tracking_enabled", false)) {
UserError.Log.ueh(TAG, "Changed activity state from " + ((activityState == null) ? "null" : activityState.toString()) + " to: " + topActivity.toString());
activityState = topActivity;
if (Pref.getBoolean("plot_motion", true))
saveUpdatedActivityState(timestamp);
switch(topActivity.getType()) {
case DetectedActivity.IN_VEHICLE:
{
UserError.Log.e(TAG, "Vehicle: " + topActivity.getConfidence());
// confidence condition above overrides this for non consolidated entries
if (topActivity.getConfidence() >= 75) {
if (!is_in_vehicle_mode())
set_vehicle_mode(true);
// also checks if vehicle mode enabled on this handset if get != set
if (is_in_vehicle_mode()) {
raise_vehicle_notification("In Vehicle Mode: " + JoH.dateTimeText(JoH.tsl()));
}
}
break;
}
default:
if (is_in_vehicle_mode()) {
set_vehicle_mode(false);
cancel_vehicle_notification();
}
break;
}
if ((from_local) && Pref.getBoolean("motion_tracking_enabled", false) && (Pref.getBoolean("act_as_motion_master", false))) {
if (d)
Log.d(TAG, "Sending update: " + activityState.getType());
GcmActivity.sendMotionUpdate(JoH.tsl(), activityState.getType());
}
} else {
UserError.Log.e(TAG, "Shutting down");
stop();
}
} else {
if (JoH.ratelimit("check-vehicle-repeat", 60))
checkVehicleRepeatNotification();
if (d)
UserError.Log.uel(TAG, "Last: " + ((lastactivity == null) ? "null" : lastactivity.toString()) + " Current: " + topActivity.toString());
}
lastactivity = topActivity;
}
}
return topConfidence;
}
use of com.google.android.gms.location.DetectedActivity in project stillStanding by katsik.
the class ActivityRecognizedService method onHandleIntent.
@Override
protected void onHandleIntent(Intent intent) {
if (ActivityRecognitionResult.hasResult(intent)) {
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
// handleDetectedActivities(result.getProbableActivities());
DetectedActivity mostProbableActivity = result.getMostProbableActivity();
int confidence = mostProbableActivity.getConfidence();
int activityType = mostProbableActivity.getType();
/*types:
* DetectedActivity.IN_VEHICLE
* DetectedActivity.ON_BICYCLE
* DetectedActivity.ON_FOOT
* DetectedActivity.STILL
* DetectedActivity.UNKNOWN
* DetectedActivity.TILTING
*/
handleDetectedActivities(mostProbableActivity);
}
}
use of com.google.android.gms.location.DetectedActivity in project android-play-location by googlesamples.
the class DetectedActivitiesIntentService method onHandleIntent.
/**
* Handles incoming intents.
* @param intent The Intent is provided (inside a PendingIntent) when requestActivityUpdates()
* is called.
*/
@SuppressWarnings("unchecked")
@Override
protected void onHandleIntent(Intent intent) {
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();
PreferenceManager.getDefaultSharedPreferences(this).edit().putString(Constants.KEY_DETECTED_ACTIVITIES, Utils.detectedActivitiesToJson(detectedActivities)).apply();
// Log each activity.
Log.i(TAG, "activities detected");
for (DetectedActivity da : detectedActivities) {
Log.i(TAG, Utils.getActivityString(getApplicationContext(), da.getType()) + " " + da.getConfidence() + "%");
}
}
Aggregations