/** * Cancel any future callbacks and disconnect the client. */ public void destroy() { if (activityClient != null && pendingIntent != null) { try { ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(activityClient, pendingIntent); activityClient.disconnect(); } catch (final Exception e) { if (DEBUG) { MyLog.i(CLS_NAME, "destroy: Exception"); e.printStackTrace(); } } } }
private void attachRecorder() { if (googleApiClient == null) { connectToPlayAPI(); } else if (googleApiClient.isConnected()) { if (isWatchingActivity) { return; } startTracking(); if (config.getStopOnStillActivity()) { ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates( googleApiClient, config.getActivitiesInterval(), detectedActivitiesPI ); isWatchingActivity = true; } } else { googleApiClient.connect(); } }
public synchronized void start(boolean no_rate_limit) { if (Pref.getBoolean("use_remote_motion", false)) { Log.d(TAG, "Not starting as we are expecting remote instead of local motion"); return; } if ((no_rate_limit) || (JoH.ratelimit("recognizer-start", 60))) { release_wl_start(); wl_start = JoH.getWakeLock("recognizer-start", 60000); UserError.Log.e(TAG, "Restarting API"); mApiClient = new GoogleApiClient.Builder(this) .addApi(ActivityRecognition.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); mApiClient.connect(); } else { UserError.Log.e(TAG, "Couldn't restart API due to ratelimit"); } }
@Override public void stopSensor() { if (mGoogleApiClient != null) { try { if (mGoogleApiClient.isConnected()) { ActivityRecognition.ActivityRecognitionApi .removeActivityUpdates( mGoogleApiClient, mActivityRecognitionPendingIntent); mGoogleApiClient.disconnect(); } } catch (IllegalStateException e) { Log.e(TAG, "Cannot disconnect from Google Api!", e); } finally { setRunning(false); mGoogleApiClient = null; } } }
public ActivityRecognizer(ReactApplicationContext reactContext) { mGoogleApiAvailability = GoogleApiAvailability.getInstance(); mContext = reactContext.getApplicationContext(); mReactContext = reactContext; connected = false; started = false; if (checkPlayServices()) { mBroadcastReceiver = new ActivityDetectionBroadcastReceiver(); mGoogleApiClient = new GoogleApiClient.Builder(mContext) .addApi(ActivityRecognition.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); } }
public void start(long detectionIntervalMillis) { if (mGoogleApiClient == null) { throw new Error("No Google API client. Your device likely doesn't have Google Play Services."); } interval = detectionIntervalMillis; if (!connected) { mGoogleApiClient.connect(); LocalBroadcastManager.getInstance(mContext).registerReceiver(mBroadcastReceiver, new IntentFilter(DetectionService.BROADCAST_ACTION)); } else if (!started) { ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates( mGoogleApiClient, detectionIntervalMillis, getActivityDetectionPendingIntent() ).setResultCallback(this); started = true; } }
public void stop() { if (mGoogleApiClient == null) { throw new Error("No Google API client. Your device likely doesn't have Google Play Services."); } if (started) { ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates( mGoogleApiClient, getActivityDetectionPendingIntent() ).setResultCallback(this); started = false; } if (connected) { LocalBroadcastManager.getInstance(mContext).unregisterReceiver(mBroadcastReceiver); mGoogleApiClient.disconnect(); connected = false; } }
public LocationManager(Context context) { _locationCache = new ArrayList<>(); _detectedActivitiesCache = new ArrayList<>(); _googleApiClient = new GoogleApiClient.Builder(context) .addApi(LocationServices.API) .addApi(ActivityRecognition.API) .addConnectionCallbacks(new GoogleApiConnectionCallbacks()) .build(); _fusedLocationListener = new FusedLocationListener(); Intent intent = new Intent(context, DetectedActivitiesIntentService.class); // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling // requestActivityUpdates() and removeActivityUpdates(). _activityPendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); connect(); }
private static void startActivityRecognition(final Context context) { if (Util.hasPlayServices(context)) { GoogleApiClient gac = new GoogleApiClient.Builder(context).addApi(ActivityRecognition.API).build(); if (gac.blockingConnect().isSuccess()) { Log.i(TAG, "GoogleApiClient connected"); Intent activityIntent = new Intent(context, BackgroundService.class); activityIntent.setAction(BackgroundService.ACTION_ACTIVITY); PendingIntent pi = PendingIntent.getService(context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean still = (prefs.getInt(SettingsFragment.PREF_LAST_ACTIVITY, DetectedActivity.STILL) == DetectedActivity.STILL); String setting = (still ? SettingsFragment.PREF_RECOGNITION_INTERVAL_STILL : SettingsFragment.PREF_RECOGNITION_INTERVAL_MOVING); String standard = (still ? SettingsFragment.DEFAULT_RECOGNITION_INTERVAL_STILL : SettingsFragment.DEFAULT_RECOGNITION_INTERVAL_MOVING); int interval = Integer.parseInt(prefs.getString(setting, standard)); ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(gac, interval * 1000, pi); Log.i(TAG, "Activity updates frequency=" + interval + "s"); } } }
private static void stopActivityRecognition(final Context context) { if (Util.hasPlayServices(context)) { GoogleApiClient gac = new GoogleApiClient.Builder(context).addApi(ActivityRecognition.API).build(); if (gac.blockingConnect().isSuccess()) { Log.i(TAG, "GoogleApiClient connected"); Intent activityIntent = new Intent(context, BackgroundService.class); activityIntent.setAction(BackgroundService.ACTION_ACTIVITY); PendingIntent pi = PendingIntent.getService(context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT); ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(gac, pi); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor editor = prefs.edit(); editor.remove(SettingsFragment.PREF_LAST_ACTIVITY); editor.remove(SettingsFragment.PREF_LAST_CONFIDENCE); editor.apply(); Log.i(TAG, "Canceled activity updates"); } } }
private void attachDARecorder() { if (detectedActivitiesAPI == null) { buildDAClient(); } else if (detectedActivitiesAPI.isConnected()) { ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates( detectedActivitiesAPI, this.activitiesInterval, detectedActivitiesPI ); if(isDebugging) { Log.d(TAG, "- DA RECORDER attached - start recording location updates"); } } else { Log.i(TAG, "NOT CONNECTED, CONNECT"); detectedActivitiesAPI.connect(); } }
public void registerActivityUpdates() { if (mRecognitionPendingIntent == null) { mRecognitionPendingIntent = PendingIntent.getService(this, 0, new Intent(this, RecognitionIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT); } ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mClient, ACTIVITY_INTERVAL, mRecognitionPendingIntent).setResultCallback(new ResultCallback<Status>() { @Override public void onResult(Status status) { if(status.isSuccess()) { Log.d(TAG, "Activities coming!"); } else { Log.e(TAG, "No activity coming!"); } } }); }
@Override protected void onGoogleApiClientReady(GoogleApiClient apiClient, Observer<? super ActivityRecognitionResult> observer) { receiver = new ActivityUpdatesBroadcastReceiver(observer); context.registerReceiver(receiver, new IntentFilter(ACTION_ACTIVITY_DETECTED)); PendingIntent receiverIntent = getReceiverPendingIntent(); ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(apiClient, detectionIntervalMilliseconds, receiverIntent); }
@Override protected void onUnsubscribed(GoogleApiClient apiClient) { if (apiClient.isConnected()) { ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(apiClient, getReceiverPendingIntent()); } context.unregisterReceiver(receiver); receiver = null; }
protected RxLocationBaseOnSubscribe(@NonNull RxLocation rxLocation, Long timeout, TimeUnit timeUnit) { this.ctx = rxLocation.ctx; this.services = new Api[]{ LocationServices.API, ActivityRecognition.API }; this.scopes = null; if (timeout != null && timeUnit != null) { this.timeoutTime = timeout; this.timeoutUnit = timeUnit; } else { this.timeoutTime = rxLocation.timeoutTime; this.timeoutUnit = rxLocation.timeoutUnit; } }
@Override protected void onGoogleApiClientReady(GoogleApiClient apiClient, SingleEmitter<Status> emitter) { //noinspection MissingPermission setupLocationPendingResult( ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(apiClient, detectionIntervalMillis, pendingIntent), SingleResultCallBack.get(emitter) ); }
@Override protected void onGoogleApiClientReady(GoogleApiClient apiClient, SingleEmitter<Status> emitter) { //noinspection MissingPermission setupLocationPendingResult( ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(apiClient, pendingIntent), SingleResultCallBack.get(emitter) ); }
@Override public void onConnected(@Nullable final Bundle bundle) { if (DEBUG) { MyLog.i(CLS_NAME, "onConnected"); } if (activityClient != null && pendingIntent != null) { ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(activityClient, MotionIntentService.UPDATE_INTERVAL, pendingIntent).setResultCallback(this); } }
private void connectToPlayAPI() { Log.d(TAG, "connecting to Google Play Services"); googleApiClient = new GoogleApiClient.Builder(context) .addApi(LocationServices.API) .addApi(ActivityRecognition.API) .addConnectionCallbacks(this) //.addOnConnectionFailedListener(this) .build(); googleApiClient.connect(); }
private void detachRecorder() { if (isWatchingActivity) { Log.d(TAG, "detachRecorder"); ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(googleApiClient, detectedActivitiesPI); isWatchingActivity = false; } }
private synchronized void requestUpdates(int frequency) { try { received = 0; // reset wl_global = JoH.getWakeLock("motion-wait", frequency * 5); // released later if (d) Log.d(TAG, "requestUpdates called: " + frequency); incrementInternalPrefsLong(REQUESTED); interpretRatio(this); ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mApiClient, frequency, get_pending_intent()); } catch (Exception e) { UserError.Log.wtf(TAG, "Got exception starting activity recognition: " + e.toString()); } }
private void stopUpdates() { try { if (d) Log.d(TAG, "stopUpdates called"); ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(mApiClient, get_pending_intent()); if (wl_global != null) { if (d) Log.d(TAG, "release wl_global"); JoH.releaseWakeLock(wl_global); wl_global = null; } } catch (Exception e) { UserError.Log.wtf(TAG, "Got exception stopping activity recognition: " + e.toString()); } }
protected synchronized void buildGoogleApiClient(Context context) { mGoogleApiClient = new GoogleApiClient.Builder(context) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .addApi(ActivityRecognition.API) .build(); mGoogleApiClient.connect(); }
/** * Builds a GoogleApiClient. Uses the {@code #addApi} method to request the * ActivityRecognition API. */ protected synchronized void buildGoogleApiClient() { mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(ActivityRecognition.API) .build(); }
/** * Registers for activity recognition updates using * {@link com.google.android.gms.location.ActivityRecognitionApi#requestActivityUpdates} which * returns a {@link com.google.android.gms.common.api.PendingResult}. Since this activity * implements the PendingResult interface, the activity itself receives the callback, and the * code within {@code onResult} executes. Note: once {@code requestActivityUpdates()} completes * successfully, the {@code DetectedActivitiesIntentService} starts receiving callbacks when * activities are detected. */ public void requestActivityUpdatesButtonHandler(View view) { if (!mGoogleApiClient.isConnected()) { Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show(); return; } ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates( mGoogleApiClient, Constants.DETECTION_INTERVAL_IN_MILLISECONDS, getActivityDetectionPendingIntent() ).setResultCallback(this); }
/** * Removes activity recognition updates using * {@link com.google.android.gms.location.ActivityRecognitionApi#removeActivityUpdates} which * returns a {@link com.google.android.gms.common.api.PendingResult}. Since this activity * implements the PendingResult interface, the activity itself receives the callback, and the * code within {@code onResult} executes. Note: once {@code removeActivityUpdates()} completes * successfully, the {@code DetectedActivitiesIntentService} stops receiving callbacks about * detected activities. */ public void removeActivityUpdatesButtonHandler(View view) { if (!mGoogleApiClient.isConnected()) { Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show(); return; } // Remove all activity updates for the PendingIntent that was used to request activity // updates. ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates( mGoogleApiClient, getActivityDetectionPendingIntent() ).setResultCallback(this); }
@NonNull public GoogleApiClient getGoogleApiClient() { return new Builder(context) .addApi(ActivityRecognition.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); }
private synchronized void buildGoogleApiClient() { mGoogleApiClient = new GoogleApiClient.Builder(this) .enableAutoManage(this, this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(ActivityRecognition.API) .build(); }
@Override public void onConnected(Bundle connectionHint) { if (Log.isLoggable(TAG, Log.INFO)) { Log.i(TAG, "Connected to GoogleApiClient"); } ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates( mGoogleApiClient, 0, createActivityDetectionPendingIntent()); }
public ActivityRecognizerImpl(Context context) { this.context = context; googleApiClient = new GoogleApiClient.Builder(context) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(ActivityRecognition.API) .build(); }
@Override public void stopToRecognizeActivities() { if (googleApiClient.isConnected()) { ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates( googleApiClient, PendingIntent.getService(context, 0, new Intent(context, ActivityRecognitionIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT) ); //Stopping to recognize activities. googleApiClient.disconnect(); //Closing connection with Google APIs } }
/** * After calling connect(), this method will be invoked asynchronously when the connect request has successfully completed. * After this callback, the application can make requests on other methods provided by the client and expect that no user intervention * is required to call methods that use account and scopes provided to the client constructor. * * @param bundle */ @Override public void onConnected(Bundle bundle) { //Google API connection has been done successfully, now we can work with it ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates( googleApiClient, ACTIVITY_RECOGNITION_INTERVAL, PendingIntent.getService(context, 0, new Intent(context, ActivityRecognitionIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT) ); }
protected synchronized void buildGoogleApiClient() { Timber.d("Building the Google Api Client"); mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(ActivityRecognition.API) .build(); }
@Override public void onDestroy() { Timber.d("Service destroyed, stopping the google client"); ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates( mGoogleApiClient, getActivityDetectionPendingIntent() ).setResultCallback(this); mGoogleApiClient.disconnect(); }
@Override public void onConnected(Bundle connectionHint) { Timber.d("Google Client connected"); ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates( mGoogleApiClient, Constants.DETECTION_INTERVAL_IN_MILLISECONDS, getActivityDetectionPendingIntent() ).setResultCallback(this); }
private void detatchDARecorder() { if (detectedActivitiesAPI == null) { buildDAClient(); } else if (detectedActivitiesAPI.isConnected()) { ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(detectedActivitiesAPI, detectedActivitiesPI); if(isDebugging) { Log.d(TAG, "- Recorder detached - stop recording activity updates"); } } else { detectedActivitiesAPI.connect(); } }
@Override public void onConnected(Bundle bundle) { Log.w(TAG, "Activity Client Connected"); ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates( detectedActivitiesAPI, activitiesInterval, detectedActivitiesPI ); }