use of com.google.android.gms.location.DetectedActivity in project DataLogger by sussexwearlab.
the class HARecognizerApiIntentService method onHandleIntent.
@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();
// Broadcast the list of detected activities.
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(Constants.BROADCAST_ACTION).putExtra(Constants.DETECTED_ACTIVITIES_INTENT_KEY, detectedActivities));
}
use of com.google.android.gms.location.DetectedActivity in project xDrip by NightscoutFoundation.
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 android-play-location by googlesamples.
the class Utils method detectedActivitiesFromJson.
static ArrayList<DetectedActivity> detectedActivitiesFromJson(String jsonArray) {
Type listType = new TypeToken<ArrayList<DetectedActivity>>() {
}.getType();
ArrayList<DetectedActivity> detectedActivities = new Gson().fromJson(jsonArray, listType);
if (detectedActivities == null) {
detectedActivities = new ArrayList<>();
}
return detectedActivities;
}
use of com.google.android.gms.location.DetectedActivity in project android-play-location by googlesamples.
the class DetectedActivitiesAdapter method updateActivities.
/**
* Process list of recently detected activities and updates the list of {@code DetectedActivity}
* objects backing this adapter.
*
* @param detectedActivities the freshly detected activities
*/
void updateActivities(ArrayList<DetectedActivity> detectedActivities) {
HashMap<Integer, Integer> detectedActivitiesMap = new HashMap<>();
for (DetectedActivity activity : detectedActivities) {
detectedActivitiesMap.put(activity.getType(), activity.getConfidence());
}
// Every time we detect new activities, we want to reset the confidence level of ALL
// activities that we monitor. Since we cannot directly change the confidence
// of a DetectedActivity, we use a temporary list of DetectedActivity objects. If an
// activity was freshly detected, we use its confidence level. Otherwise, we set the
// confidence level to zero.
ArrayList<DetectedActivity> tempList = new ArrayList<>();
for (int i = 0; i < Constants.MONITORED_ACTIVITIES.length; i++) {
int confidence = detectedActivitiesMap.containsKey(Constants.MONITORED_ACTIVITIES[i]) ? detectedActivitiesMap.get(Constants.MONITORED_ACTIVITIES[i]) : 0;
tempList.add(new DetectedActivity(Constants.MONITORED_ACTIVITIES[i], confidence));
}
// Remove all items.
this.clear();
// changed and views reflecting the data should refresh.
for (DetectedActivity detectedActivity : tempList) {
this.add(detectedActivity);
}
}
use of com.google.android.gms.location.DetectedActivity in project xDrip-plus by jamorham.
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);
}
}
Aggregations