Search in sources :

Example 11 with DetectedActivity

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));
}
Also used : DetectedActivity(com.google.android.gms.location.DetectedActivity) ArrayList(java.util.ArrayList) Intent(android.content.Intent) ActivityRecognitionResult(com.google.android.gms.location.ActivityRecognitionResult)

Example 12 with DetectedActivity

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;
}
Also used : DetectedActivity(com.google.android.gms.location.DetectedActivity)

Example 13 with DetectedActivity

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;
}
Also used : DetectedActivity(com.google.android.gms.location.DetectedActivity) Type(java.lang.reflect.Type) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson)

Example 14 with DetectedActivity

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);
    }
}
Also used : DetectedActivity(com.google.android.gms.location.DetectedActivity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 15 with 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);
    }
}
Also used : PowerManager(android.os.PowerManager) DetectedActivity(com.google.android.gms.location.DetectedActivity) ArrayList(java.util.ArrayList) ActivityRecognitionResult(com.google.android.gms.location.ActivityRecognitionResult)

Aggregations

DetectedActivity (com.google.android.gms.location.DetectedActivity)15 ArrayList (java.util.ArrayList)7 ActivityRecognitionResult (com.google.android.gms.location.ActivityRecognitionResult)6 Intent (android.content.Intent)2 PowerManager (android.os.PowerManager)2 Bus (cl.smartcities.isci.transportinspector.backend.Bus)2 BusStop (cl.smartcities.isci.transportinspector.backend.BusStop)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 NonNull (android.support.annotation.NonNull)1 ListView (android.widget.ListView)1 ProgressBar (android.widget.ProgressBar)1 TextView (android.widget.TextView)1 ActivityRecognitionClient (com.google.android.gms.location.ActivityRecognitionClient)1 Gson (com.google.gson.Gson)1 Type (java.lang.reflect.Type)1 Date (java.util.Date)1