com.google.android.gms.fitness.data.Session Java Examples
The following examples show how to use
com.google.android.gms.fitness.data.Session.
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: GoogleFitSessionManagerTest.java From JayPS-AndroidApp with MIT License | 6 votes |
private DataSet getDataSet() { ArrayList<Session> sessions = new ArrayList<>(); sessions.add(_mockSession); ArgumentCaptor<ResultCallback> captor = ArgumentCaptor.forClass(ResultCallback.class); verify(_mockResult, times(1)).setResultCallback(captor.capture()); ArgumentCaptor<Session> sessionArgumentCaptor = ArgumentCaptor.forClass(Session.class); captor.getValue().onResult(new SessionStopResult(new Status(1), sessions)); ArgumentCaptor<DataSet> dataSetArgumentCaptor = ArgumentCaptor.forClass(DataSet.class); verify(_mockPlayServices,times(1)).newSessionInsertRequest(sessionArgumentCaptor.capture(), dataSetArgumentCaptor.capture()); assertEquals(_mockSession, sessionArgumentCaptor.getValue()); return dataSetArgumentCaptor.getValue(); }
Example #2
Source File: GoogleApiClientWrapper.java From android-play-games-in-motion with Apache License 2.0 | 6 votes |
/** * Starts a new session for Fit data. This will take care of registering all the sensors, * recording the sensor data, and registering the data set as a session to Google Fit. * @param dataTypeSettings Types of data to listen to, in an array. * @param sessionDescription The description of the session. * @param listener The OnDataPointListener to receive sensor events. */ public void startFitDataSession(FitDataTypeSetting[] dataTypeSettings, String sessionDescription, OnDataPointListener listener) { for (FitDataTypeSetting dataTypeSetting : dataTypeSettings) { registerFitDataListener(dataTypeSetting, listener); startRecordingFitData(dataTypeSetting); } Session session = new Session.Builder() .setName(SESSION_NAME) .setDescription(sessionDescription) .setActivity(FitnessActivities.RUNNING_JOGGING) .setStartTime(System.currentTimeMillis(), TimeUnit.MILLISECONDS) .build(); PendingResult<Status> pendingResult = Fitness.SessionsApi.startSession(mGoogleApiClient, session); pendingResult.setResultCallback(new FitResultCallback<Status>( this, FitResultCallback.RegisterType.SESSION, null /* dataType */, true /* subscribe */)); }
Example #3
Source File: DataQueries.java From GoogleFitExample with Apache License 2.0 | 6 votes |
/** * Sessions can include multiple data set types. * * @param startTime Start time for the activity in milliseconds * @param endTime End time for the activity in milliseconds * @param stepCountDelta Number of steps during the activity * @param activityName Name of the activity, must match FitnessActivities types * @param packageName Package name for the app * @return Resulting insert request */ public static SessionInsertRequest createSession(long startTime, long endTime, int stepCountDelta, String activityName, String packageName, Device device) { // Create a session with metadata about the activity. Session session = new Session.Builder() .setName(TAG) //.setDescription("Long run around Shoreline Park") .setIdentifier(packageName + ":" + startTime) .setActivity(FitnessActivities.WALKING) .setStartTime(startTime, TimeUnit.MILLISECONDS) .setEndTime(endTime, TimeUnit.MILLISECONDS) .build(); // Build a session insert request return new SessionInsertRequest.Builder() .setSession(session) .addDataSet(createStepDeltaDataSet(startTime, endTime, stepCountDelta, packageName, device)) .addDataSet(createActivityDataSet(startTime, endTime, activityName, packageName, device)) .build(); }
Example #4
Source File: GoogleFitSessionManagerTest.java From JayPS-AndroidApp with MIT License | 6 votes |
private void setupMocks() { _mockContext = mock(Context.class); when(_mockContext.getPackageName()).thenReturn("com.somepackage.or.other"); _mockGoogleApiClient = mock(GoogleApiClient.class); _mockSessionsApi = mock(SessionsApi.class); _mockSession = mock(Session.class); _mockSessionBuilder = mock(Session.Builder.class); when(_mockSessionBuilder.setName(anyString())).thenReturn(_mockSessionBuilder); when(_mockSessionBuilder.setIdentifier(anyString())).thenReturn(_mockSessionBuilder); when(_mockSessionBuilder.setStartTime(anyLong(),any(TimeUnit.class))).thenReturn(_mockSessionBuilder); when(_mockSessionBuilder.build()).thenReturn(_mockSession); _mockPlayServices = mock(IGooglePlayServices.class); when(_mockPlayServices.newSessionBuilder()).thenReturn(_mockSessionBuilder); _mockResult = mock(PendingResult.class); when(_mockSessionsApi.stopSession(any(GoogleApiClient.class),anyString())).thenReturn(_mockResult); }
Example #5
Source File: MainActivity.java From AndroidDemoProjects with Apache License 2.0 | 6 votes |
@OnClick(R.id.btn_start_session) public void startSession() { mSession = new Session.Builder() .setName(SESSION_NAME) .setIdentifier(getString(R.string.app_name) + " " + System.currentTimeMillis()) .setDescription("Yoga Session Description") .setStartTime(Calendar.getInstance().getTimeInMillis(), TimeUnit.MILLISECONDS) .setActivity(FitnessActivities.YOGA) .build(); PendingResult<Status> pendingResult = Fitness.SessionsApi.startSession(mGoogleApiClient, mSession); pendingResult.setResultCallback( new ResultCallback<Status>() { @Override public void onResult(Status status) { if (status.isSuccess()) { Log.i("Tuts+", "Successfully started session"); } else { Log.i("Tuts+", "Failed to start session: " + status.getStatusMessage()); } } } ); }
Example #6
Source File: GoogleFitSessionManager.java From JayPS-AndroidApp with MIT License | 6 votes |
private Session createSession(long startTime) { //String description = ""; String sessionName = _playServices.generateSessionName(); _sessionIdentifier = _playServices.generateSessionIdentifier(startTime); Session session = _playServices.newSessionBuilder() .setName(sessionName) .setIdentifier(_sessionIdentifier) //.setDescription(description) .setStartTime(startTime, TimeUnit.MILLISECONDS) // optional - if your app knows what activity: //.setActivity(FitnessActivities.RUNNING) .build(); _sessionsApi.startSession(_googleAPIClient, session); return session; }
Example #7
Source File: GoogleFitSessionManager.java From JayPS-AndroidApp with MIT License | 6 votes |
@Override public void saveActiveSession(final long endTime) { PendingResult<SessionStopResult> pendingResult = _sessionsApi.stopSession(_googleAPIClient, _sessionIdentifier); pendingResult.setResultCallback(new ResultCallback<SessionStopResult>() { @Override public void onResult(SessionStopResult sessionStopResult) { if (sessionStopResult.getSessions().size() > 0) { for (Session session : sessionStopResult.getSessions()) { buildDataPoints(session, endTime); insertDataPoints(session); } } _sessionDataList.clear(); } }); }
Example #8
Source File: GoogleFitSessionManager.java From JayPS-AndroidApp with MIT License | 5 votes |
private void buildDataPoints(Session session, long endTime) { if(_sessionDataList.size() < 1) { createUnknownDataPoint(session, endTime); } else { SessionData nextData = null; for(int n=0; n < _sessionDataList.size(); n++) { long calculatedEndTime = (_sessionDataList.size() == n + 1) ? endTime : _sessionDataList.get(n+1).getStartTime(); createActivityDataPoint(_sessionDataList.get(n), calculatedEndTime); } } }
Example #9
Source File: GooglePlayServices.java From JayPS-AndroidApp with MIT License | 5 votes |
@Override public SessionInsertRequest newSessionInsertRequest(Session session, DataSet dataSet) { return new SessionInsertRequest.Builder() .addDataSet(dataSet) .setSession(session) .build(); }
Example #10
Source File: SleepHistory.java From react-native-google-fit with MIT License | 5 votes |
private void processDataSet(DataSet dataSet, Session session, WritableArray map) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); dateFormat.setTimeZone(TimeZone.getDefault()); for (DataPoint dp : dataSet.getDataPoints()) { WritableMap sleepMap = Arguments.createMap(); sleepMap.putString("value", dp.getValue(Field.FIELD_ACTIVITY).asActivity()); sleepMap.putString("startDate", dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS))); sleepMap.putString("endDate", dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS))); map.pushMap(sleepMap); } }
Example #11
Source File: SleepHistory.java From react-native-google-fit with MIT License | 4 votes |
@RequiresApi(api = Build.VERSION_CODES.N) public void getSleepData(double startDate, double endDate, final Callback errorCallback, final Callback successCallback) { SessionReadRequest request = new SessionReadRequest.Builder() .readSessionsFromAllApps() .read(DataType.TYPE_ACTIVITY_SEGMENT) .setTimeInterval((long) startDate, (long) endDate, TimeUnit.MILLISECONDS) .build(); GoogleSignInAccount gsa = GoogleSignIn.getAccountForScopes(this.mReactContext, new Scope(Scopes.FITNESS_ACTIVITY_READ)); Fitness.getSessionsClient(this.mReactContext, gsa) .readSession(request) .addOnSuccessListener(new OnSuccessListener<SessionReadResponse>() { @Override public void onSuccess(SessionReadResponse response) { List<Object> sleepSessions = response.getSessions() .stream() .filter(new Predicate<Session>() { @Override public boolean test(Session s) { Log.i(TAG, "Activity found: " + s.getActivity()); return s.getActivity().equals(FitnessActivities.SLEEP); } }) .collect(Collectors.toList()); WritableArray sleep = Arguments.createArray(); for (Object session : sleepSessions) { List<DataSet> dataSets = response.getDataSet((Session) session); for (DataSet dataSet : dataSets) { processDataSet(dataSet, (Session) session, sleep); } } successCallback.invoke(sleep); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.i(TAG, "Failure: " + e.getMessage()); errorCallback.invoke(e.getMessage()); } }); }
Example #12
Source File: GoogleFitSessionManager.java From JayPS-AndroidApp with MIT License | 4 votes |
private void createUnknownDataPoint(Session session, long endTime) { DataPoint firstRunningDp = _activitySegments.createDataPoint() .setTimeInterval(session.getStartTime(TimeUnit.MILLISECONDS), endTime, TimeUnit.MILLISECONDS); firstRunningDp.getValue(Field.FIELD_ACTIVITY).setActivity(FitnessActivities.UNKNOWN); _activitySegments.add(firstRunningDp); }
Example #13
Source File: GoogleFitSessionManager.java From JayPS-AndroidApp with MIT License | 4 votes |
private void insertDataPoints(Session session) { _playServices.newSessionInsertRequest(session, _activitySegments); Log.d(TAG,"GoogleFit Session Saved"); }
Example #14
Source File: GooglePlayServices.java From JayPS-AndroidApp with MIT License | 4 votes |
@Override public Session.Builder newSessionBuilder() { return new Session.Builder(); }
Example #15
Source File: GoogleFit.java From OpenFit with MIT License | 4 votes |
protected Void doInBackground(Void... params) { Log.d(LOG_TAG, "readDataTask"); Calendar cal = Calendar.getInstance(); Date now = new Date(); cal.setTime(now); long endTime = cal.getTimeInMillis(); cal.add(Calendar.DAY_OF_MONTH, -3); long startTime = cal.getTimeInMillis(); lastPedometerSteps = 0; lastPedometerCals = 0; lastPedometerDist = 0; SessionReadRequest readRequest = new SessionReadRequest.Builder() .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS) .read(DataType.TYPE_ACTIVITY_SEGMENT) .build(); SessionReadResult sessionReadResult = Fitness.SessionsApi.readSession(mClient, readRequest).await(1, TimeUnit.MINUTES); Log.d(LOG_TAG, "Session read was successful. Number of returned sessions is: " + sessionReadResult.getSessions().size()); for(Session session : sessionReadResult.getSessions()) { Date start = new Date(session.getStartTime(TimeUnit.MILLISECONDS)); Date end = new Date(session.getEndTime(TimeUnit.MILLISECONDS)); Log.d(LOG_TAG, "Description: " + session.getDescription()); Log.d(LOG_TAG, "start: " + start + ", end: " + end); Log.d(LOG_TAG, "Activity" + session.getActivity()); List<DataSet> dataSets = sessionReadResult.getDataSet(session); for(DataSet dataSet : dataSets) { for(DataPoint dp : dataSet.getDataPoints()) { if(session.getActivity() == FitnessActivities.WALKING) { long sTime = dp.getStartTime(TimeUnit.MILLISECONDS); if(sTime % 600000 == 0) { lastPedometerStartTime = new Date(sTime); } lastPedometerSession = new Date(dp.getEndTime(TimeUnit.MILLISECONDS)); } else if(session.getActivity() == FitnessActivities.WALKING_FITNESS) { lastWalkingSession = new Date(dp.getEndTime(TimeUnit.MILLISECONDS)); } else if(session.getActivity() == FitnessActivities.RUNNING) { lastRunningSession = new Date(dp.getEndTime(TimeUnit.MILLISECONDS)); } else if(session.getActivity() == FitnessActivities.SLEEP) { lastSleepSession = new Date(dp.getStartTime(TimeUnit.MILLISECONDS)); } else if(session.getActivity() == FitnessActivities.BIKING) { lastCyclingSession = new Date(dp.getEndTime(TimeUnit.MILLISECONDS)); } else if(session.getActivity() == FitnessActivities.HIKING) { lastHikingSession = new Date(dp.getEndTime(TimeUnit.MILLISECONDS)); } } } } if(lastPedometerSession != null) { if(lastWalkingSession != null && lastPedometerSession.getTime() < lastWalkingSession.getTime()) { lastPedometerSession = lastWalkingSession; } if(lastRunningSession != null && lastPedometerSession.getTime() < lastRunningSession.getTime()) { lastPedometerSession = lastRunningSession; } if(lastCyclingSession != null && lastPedometerSession.getTime() < lastCyclingSession.getTime()) { lastPedometerSession = lastCyclingSession; } if(lastHikingSession != null && lastPedometerSession.getTime() < lastHikingSession.getTime()) { lastPedometerSession = lastHikingSession; } Calendar calendar = Calendar.getInstance(); calendar.setTime(lastPedometerSession); int minute = ((int) (calendar.get(Calendar.MINUTE) / 10.0) * 10); calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); lastPedometerStartTime = calendar.getTime(); if(lastPedometerStartTime.getTime() == lastPedometerSession.getTime()) { calendar.add(Calendar.MINUTE, -10); lastPedometerStartTime = calendar.getTime(); } } Log.d(LOG_TAG, "Last session updated start: " + lastPedometerStartTime + ", end: " + lastPedometerSession); lastPedometerSteps = prefs.getInt(prefsSteps, 0); lastPedometerCals = prefs.getFloat(prefsCals, 0); lastPedometerDist = prefs.getFloat(prefsDist, 0); Log.d(LOG_TAG, "Last pedometer session from: " + lastPedometerStartTime + " to: " + lastPedometerSession + " steps: " + lastPedometerSteps + " cals: " + lastPedometerCals + " dist: " + lastPedometerDist); Log.d(LOG_TAG, "Last walking session: " + lastWalkingSession); Log.d(LOG_TAG, "Last running session: " + lastRunningSession); Log.d(LOG_TAG, "Last cycling session: " + lastCyclingSession); Log.d(LOG_TAG, "Last hiking session: " + lastHikingSession); writeData(); return null; }
Example #16
Source File: GoogleFitSessionManagerTest.java From JayPS-AndroidApp with MIT License | 4 votes |
@SmallTest public void testStartSessionSetsSession() throws Exception { _sessionManager.startSession(1000, _mockGoogleApiClient); verify(_mockSessionsApi,timeout(2000).times(1)).startSession(any(GoogleApiClient.class),any(Session.class)); }
Example #17
Source File: IGooglePlayServices.java From JayPS-AndroidApp with MIT License | votes |
public SessionInsertRequest newSessionInsertRequest(Session session, DataSet dataSet);
Example #18
Source File: IGooglePlayServices.java From JayPS-AndroidApp with MIT License | votes |
public Session.Builder newSessionBuilder();