Android Open Source - trip-chain-android Activity Receiver






From Project

Back to project page trip-chain-android.

License

The source code is released under:

MIT License

If you think the Android project trip-chain-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package fi.aalto.tripchain.receivers;
//www  .jav a 2 s .  c  om
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.ActivityRecognitionClient;
import com.google.android.gms.location.ActivityRecognitionResult;

import fi.aalto.tripchain.Configuration;

/**
 * Subscribes to Activity mode recognition service and calls
 * onActivityRecognitionResult on activity received.
 *
 */
public abstract class ActivityReceiver extends BroadcastReceiver implements 
    GooglePlayServicesClient.ConnectionCallbacks, 
    GooglePlayServicesClient.OnConnectionFailedListener,
    Receiver {
  private final static String TAG = ActivityReceiver.class.getSimpleName();
  
  private ActivityRecognitionClient activityRecognitionClient;
  
  protected Context context;
  
  private boolean starting = true;
  
  private Intent activityIntent;
    private PendingIntent callbackIntent;
  
  public ActivityReceiver(Context context) {
    this.context = context;
    
    this.activityIntent = new Intent(Configuration.ACTIVITY_INTENT);
    this.callbackIntent = PendingIntent.getBroadcast(context, 0, activityIntent,            
          PendingIntent.FLAG_UPDATE_CURRENT);
    this.activityRecognitionClient = new ActivityRecognitionClient(context, this, this);
  }
  
  public void start() {
    IntentFilter intentFilter = new IntentFilter(Configuration.ACTIVITY_INTENT);
    context.registerReceiver(this, intentFilter);
    
    activityRecognitionClient.connect();    
  }
  

  @Override
  public void onConnectionFailed(ConnectionResult result) {
    Log.d(TAG, "Connection failed");
  }

  @Override
  public void onConnected(Bundle connectionHint) {
    Log.d(TAG, "Connection succeeded");
    
      if (this.starting) {
        activityRecognitionClient.requestActivityUpdates(30000, callbackIntent);
        starting = false;
      } else {
        activityRecognitionClient.removeActivityUpdates(callbackIntent);
      }
      
      activityRecognitionClient.disconnect();
      Log.d(TAG, "Requested and disconnected.");
  }

  @Override
  public void onDisconnected() {
    Log.i(TAG, "Disconnected");
  }
  
  public void stop() {
    activityRecognitionClient.connect();
    context.unregisterReceiver(this);
  }
  
  @Override
  public void onReceive(Context context, Intent intent) {
    if (ActivityRecognitionResult.hasResult(intent)) {
      ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
      
      onActivityRecognitionResult(result);
    }    
  }
  
  public abstract void onActivityRecognitionResult(ActivityRecognitionResult activity);
}




Java Source Code List

fi.aalto.tripchain.BackgroundService.java
fi.aalto.tripchain.Configuration.java
fi.aalto.tripchain.LoginActivity.java
fi.aalto.tripchain.MainActivity.java
fi.aalto.tripchain.StartFragment.java
fi.aalto.tripchain.TripFragment.java
fi.aalto.tripchain.receivers.ActivityReceiver.java
fi.aalto.tripchain.receivers.EventDispatcher.java
fi.aalto.tripchain.receivers.EventListener.java
fi.aalto.tripchain.receivers.LocationReceiver.java
fi.aalto.tripchain.receivers.Receiver.java
fi.aalto.tripchain.route.ActivityListener.java
fi.aalto.tripchain.route.Activity.java
fi.aalto.tripchain.route.LocationListener.java
fi.aalto.tripchain.route.RoadSegment.java
fi.aalto.tripchain.route.Roads.java
fi.aalto.tripchain.route.RouteSegment.java
fi.aalto.tripchain.route.Route.java
fi.aalto.tripchain.route.TripRecorder.java
fi.aalto.tripchain.route.Trip.java