Java Code Examples for com.google.android.gms.location.ActivityRecognitionResult#extractResult()

The following examples show how to use com.google.android.gms.location.ActivityRecognitionResult#extractResult() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ActivityRecognitionLocationProvider.java    From background-geolocation-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
    ArrayList<DetectedActivity> detectedActivities = (ArrayList) result.getProbableActivities();

    //Find the activity with the highest percentage
    lastActivity = getProbableActivity(detectedActivities);

    logger.debug("Detected activity={} confidence={}", BackgroundActivity.getActivityString(lastActivity.getType()), lastActivity.getConfidence());

    handleActivity(lastActivity);

    if (lastActivity.getType() == DetectedActivity.STILL) {
        showDebugToast("Detected STILL Activity");
        // stopTracking();
        // we will delay stop tracking after position is found
    } else {
        showDebugToast("Detected ACTIVE Activity");
        startTracking();
    }
    //else do nothing
}
 
Example 2
Source File: BackgroundLocationUpdateService.java    From cordova-background-geolocation-services with Apache License 2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
  ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
  ArrayList<DetectedActivity> detectedActivities = (ArrayList) result.getProbableActivities();

  //Find the activity with the highest percentage
  lastActivity = Constants.getProbableActivity(detectedActivities);

  Log.w(TAG, "MOST LIKELY ACTIVITY: " + Constants.getActivityString(lastActivity.getType()) + " " + lastActivity.getConfidence());

  Intent mIntent = new Intent(Constants.CALLBACK_ACTIVITY_UPDATE);
  mIntent.putExtra(Constants.ACTIVITY_EXTRA, detectedActivities);
  getApplicationContext().sendBroadcast(mIntent);
  Log.w(TAG, "Activity is recording" + isRecording);

  if(lastActivity.getType() == DetectedActivity.STILL && isRecording) {
      showDebugToast(context, "Detected Activity was STILL, Stop recording");
      stopRecording();
  } else if(lastActivity.getType() != DetectedActivity.STILL && !isRecording) {
      showDebugToast(context, "Detected Activity was ACTIVE, Start Recording");
      startRecording();
  }
  //else do nothing
}
 
Example 3
Source File: DetectedActivitiesIntentService.java    From location-samples with Apache License 2.0 6 votes vote down vote up
/**
 * 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() + "%"
        );
    }
}
 
Example 4
Source File: SKActivityRecognitionIntentService.java    From SensingKit-Android with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {

    // If the intent contains an update
    if (ActivityRecognitionResult.hasResult(intent)) {

        // Get the update
        ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);

        Intent i = new Intent(BROADCAST_UPDATE);
        i.putExtra(RECOGNITION_RESULT, result);
        LocalBroadcastManager manager = LocalBroadcastManager.getInstance(this);
        manager.sendBroadcast(i);
    }

}
 
Example 5
Source File: ActivityRecognitionIntentService.java    From android-notification-log with MIT License 5 votes vote down vote up
@Override
protected void onHandleIntent(@Nullable Intent intent) {
	if(intent == null) {
		return;
	}

	ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);

	ArrayList<DetectedActivity> detectedActivities = (ArrayList<DetectedActivity>) result.getProbableActivities();

	ArrayList<String> activities = new ArrayList<>();
	ArrayList<Integer> confidences = new ArrayList<>();

	for(DetectedActivity activity : detectedActivities) {
		activities.add(getActivityString(activity.getType()));
		confidences.add(activity.getConfidence());
	}

	JSONObject json = new JSONObject();
	String str = null;
	try {
		long now = System.currentTimeMillis();
		json.put("time", now);
		json.put("offset", TimeZone.getDefault().getOffset(now));
		json.put("activities", new JSONArray(activities));
		json.put("confidences", new JSONArray(confidences));
		str = json.toString();
	} catch (JSONException e) {
		if(Const.DEBUG) e.printStackTrace();
	}

	SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
	sp.edit().putString(Const.PREF_LAST_ACTIVITY, str).apply();
}
 
Example 6
Source File: HARecognizerApiIntentService.java    From DataLogger with MIT License 5 votes vote down vote up
@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)
    );

}
 
Example 7
Source File: DetectionService.java    From react-native-activity-recognition with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
    ArrayList<DetectedActivity> detectedActivities = (ArrayList) result.getProbableActivities();

    Log.d(TAG, "Detected activities:");
    for (DetectedActivity da: detectedActivities) {
        Log.d(TAG, getActivityString(da.getType()) + " (" + da.getConfidence() + "%)");
    }

    Intent localIntent = new Intent(BROADCAST_ACTION);
    localIntent.putExtra(ACTIVITY_EXTRA, detectedActivities);
    LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
}
 
Example 8
Source File: ActivityRecognizedService.java    From AndroidDemoProjects with Apache License 2.0 5 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    if(ActivityRecognitionResult.hasResult(intent)) {
        ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
        handleDetectedActivities( result.getProbableActivities() );
    }
}
 
Example 9
Source File: AndroidActivityRecognitionWrapper.java    From gsn with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void runOnce() {
	if (!isRunning()) {
		disconnectGoogleAPI();
	} else {
		if (mGoogleApiClient == null) {
			connectGoogleAPI();
		}
		updateWrapperInfo();

		if (ActivityRecognitionResult.hasResult(ActivityRecognitionService.getIntent())) {
			// Get the update
			ActivityRecognitionResult result =
					ActivityRecognitionResult.extractResult(ActivityRecognitionService.getIntent());

			DetectedActivity mostProbableActivity
					= result.getMostProbableActivity();

			// Get the confidence % (probability)
			double confidence = mostProbableActivity.getConfidence();

			// Get the type
			double activityType = mostProbableActivity.getType();
	   /* types:
	    * DetectedActivity.IN_VEHICLE
           * DetectedActivity.ON_BICYCLE
           * DetectedActivity.ON_FOOT
           * DetectedActivity.STILL
           * DetectedActivity.UNKNOWN
           * DetectedActivity.TILTING
           */
			StreamElement streamElement = new StreamElement(FIELD_NAMES, FIELD_TYPES,
					                                               new Serializable[]{activityType, confidence});
			postStreamElement(streamElement);
		} else {
			Log.d(TAG, "No results for AndroidActivityRecognition");
		}
	}
}
 
Example 10
Source File: ActivityRecognitionIntentService.java    From JayPS-AndroidApp with MIT License 5 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    ((PebbleBikeApplication)getApplication()).inject(this);

    if (ActivityRecognitionResult.hasResult(intent)) {
        ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);

        switch(result.getMostProbableActivity().getType()) {

            case DetectedActivity.ON_BICYCLE:
                Log.d(TAG, "ON_BICYCLE");
                sendReply(result.getMostProbableActivity().getType());
                break;
            case DetectedActivity.WALKING:
                Log.d(TAG, "WALKING");
                sendReply(result.getMostProbableActivity().getType());
                break;
            case DetectedActivity.RUNNING:
                Log.d(TAG, "RUNNING");
                sendReply(result.getMostProbableActivity().getType());
                break;
            case DetectedActivity.ON_FOOT:
                Log.d(TAG, "ON_FOOT");
                sendReply(result.getMostProbableActivity().getType());
                break;
            case DetectedActivity.TILTING:
                Log.d(TAG, "TILTING");
                break;
            case DetectedActivity.STILL:
                Log.d(TAG, "STILL");
                sendReply(result.getMostProbableActivity().getType());
                break;
            default:
                logActivity(result);
        }
    }
}
 
Example 11
Source File: MotionIntentService.java    From Saiy-PS with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Store the most recent user activity for use elsewhere in the application.
 *
 * @param intent which should contain an {@link ActivityRecognitionResult}
 * @return a {@link Motion} object
 */
private Motion extractMotion(final Intent intent) {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "extractMotion");
    }

    final ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);

    if (result != null) {

        final DetectedActivity detectedActivity = result.getMostProbableActivity();

        if (detectedActivity != null) {
            if (DEBUG) {
                logActivity(detectedActivity);
            }

            final int confidence = detectedActivity.getConfidence();

            if (confidence > Motion.CONFIDENCE_THRESHOLD) {

                switch (detectedActivity.getType()) {

                    case DetectedActivity.WALKING:
                    case DetectedActivity.IN_VEHICLE:
                    case DetectedActivity.ON_BICYCLE:
                    case DetectedActivity.ON_FOOT:
                    case DetectedActivity.RUNNING:
                    case DetectedActivity.STILL:
                        return new Motion(detectedActivity.getType(), confidence, result.getTime());
                    case DetectedActivity.TILTING:
                    case DetectedActivity.UNKNOWN:
                    default:
                        break;
                }

            } else {
                if (DEBUG) {
                    MyLog.v(CLS_NAME, "detectedActivity: ignoring low confidence");
                }
            }
        } else {
            if (DEBUG) {
                MyLog.i(CLS_NAME, "detectedActivity: null");
            }
        }
    } else {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "detectedActivity: ActivityRecognitionResult: null");
        }
    }

    return null;
}
 
Example 12
Source File: ActivityRecognitionIntentService.java    From mytracks with Apache License 2.0 4 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
  if (!ActivityRecognitionResult.hasResult(intent)) {
    return;
  }
  ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
  DetectedActivity detectedActivity = result.getMostProbableActivity();
  if (detectedActivity == null) {
    return;
  }
  if (detectedActivity.getConfidence() < REQUIRED_CONFIDENCE) {
    return;
  }

  long recordingTrackId = PreferencesUtils.getLong(this, R.string.recording_track_id_key);
  if (recordingTrackId == PreferencesUtils.RECORDING_TRACK_ID_DEFAULT) {
    return;
  }
  boolean recordingTrackPaused = PreferencesUtils.getBoolean(
      this, R.string.recording_track_paused_key, PreferencesUtils.RECORDING_TRACK_PAUSED_DEFAULT);
  if (recordingTrackPaused) {
    return;
  }

  int currentType = PreferencesUtils.getInt(this, R.string.activity_recognition_type_key,
      PreferencesUtils.ACTIVITY_RECOGNITION_TYPE_DEFAULT);

  switch (detectedActivity.getType()) {
    case DetectedActivity.IN_VEHICLE:
      if (currentType != DetectedActivity.IN_VEHICLE) {
        PreferencesUtils.setInt(
            this, R.string.activity_recognition_type_key, DetectedActivity.IN_VEHICLE);
      }
      break;
    case DetectedActivity.ON_BICYCLE:
      if (currentType != DetectedActivity.IN_VEHICLE
          && currentType != DetectedActivity.ON_BICYCLE) {
        PreferencesUtils.setInt(
            this, R.string.activity_recognition_type_key, DetectedActivity.ON_BICYCLE);
      }
      break;
    case DetectedActivity.ON_FOOT:
      if (currentType != DetectedActivity.IN_VEHICLE && currentType != DetectedActivity.ON_BICYCLE
          && currentType != DetectedActivity.ON_FOOT) {
        PreferencesUtils.setInt(
            this, R.string.activity_recognition_type_key, DetectedActivity.ON_FOOT);
      }
      break;
    default:
      break;
  }
}