Skip to content

Commit

Permalink
Activity detector work
Browse files Browse the repository at this point in the history
  • Loading branch information
audaciouscode committed Oct 22, 2016
1 parent 2b13afe commit a684783
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 10 deletions.
1 change: 1 addition & 0 deletions AndroidManifest.xml
@@ -1,5 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.audacious_software.pdk.passivedatakit">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
<activity android:name="com.audacious_software.passive_data_kit.activities.generators.MicrosoftBandAuthActivity" android:label="@string/app_name" />
<activity android:name="com.audacious_software.passive_data_kit.activities.generators.RequestPermissionActivity" android:label="@string/app_name" />
Expand Down
21 changes: 12 additions & 9 deletions build.gradle
Expand Up @@ -6,7 +6,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.android.tools.build:gradle:2.1.2'
}
}

Expand All @@ -15,15 +15,15 @@ repositories {
}

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
compileSdkVersion 24
buildToolsVersion "24.0.0"

useLibrary 'org.apache.http.legacy'

defaultConfig {
// applicationId "com.audacious_software.pdk.passivedatakit"
minSdkVersion 14
targetSdkVersion 23
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
Expand All @@ -45,11 +45,14 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:recyclerview-v7:23.3.0'
compile 'com.android.support:cardview-v7:23.3.0'
compile 'com.google.android.gms:play-services-location:8.4.0'
compile 'com.google.android.gms:play-services-maps:8.4.0'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:recyclerview-v7:24.0.0'
compile 'com.android.support:cardview-v7:24.0.0'
// compile 'com.google.android.gms:play-services-contextmanager:9.2.0'
compile 'com.google.android.gms:play-services-location:9.2.0'
compile 'com.google.android.gms:play-services-nearby:9.2.0'
compile 'com.google.android.gms:play-services-places:9.2.0'
compile 'com.google.android.gms:play-services-contextmanager:9.2.0'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'commons-io:commons-io:2.4'
compile 'org.apache.commons:commons-lang3:3.4'
Expand Down
2 changes: 1 addition & 1 deletion res/values/diagnostics.xml
Expand Up @@ -3,5 +3,5 @@
<string name="diagnostic_missing_msft_band_auth">Please grant the app sensor access on the Microsoft Band.</string>
<string name="diagnostic_missing_location_permission">Please grant the app permission to access location services on this device.</string>


<string name="diagnostic_missing_activity_recognition_permission">Please grant the app permission to recognize your activity using data from this device.</string>
</resources>
1 change: 1 addition & 0 deletions res/values/generators.xml
Expand Up @@ -2,6 +2,7 @@
<string-array name="pdk_available_generators">
<item>com.audacious_software.passive_data_kit.generators.device.Location</item>
<item>com.audacious_software.passive_data_kit.generators.wearables.MicrosoftBand</item>
<item>com.audacious_software.passive_data_kit.generators.services.GoogleAwareness</item>
</string-array>

<string-array name="pdk_app_generators"></string-array>
Expand Down
Expand Up @@ -51,6 +51,8 @@ public void start() {
for (String className : this.mGenerators)
{
try {
Log.e("PDK", "TRYING " + className);

Class<Generator> probeClass = (Class<Generator>) Class.forName(className);

Method isEnabled = probeClass.getDeclaredMethod("isEnabled", Context.class);
Expand All @@ -64,12 +66,16 @@ public void start() {
this.stopGenerator(className);
}
} catch (ClassNotFoundException e) {
Log.e("PDK", "ClassNotFoundException " + className);
Logger.getInstance(this.mContext).logThrowable(e);
} catch (NoSuchMethodException e) {
Log.e("PDK", "NoSuchMethodException " + className);
Logger.getInstance(this.mContext).logThrowable(e);
} catch (InvocationTargetException e) {
Log.e("PDK", "InvocationTargetException " + className);
Logger.getInstance(this.mContext).logThrowable(e);
} catch (IllegalAccessException e) {
Log.e("PDK", "IllegalAccessException " + className);
Logger.getInstance(this.mContext).logThrowable(e);
}
}
Expand Down
@@ -0,0 +1,221 @@
package com.audacious_software.passive_data_kit.generators.services;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.Log;

import com.audacious_software.passive_data_kit.activities.generators.RequestPermissionActivity;
import com.audacious_software.passive_data_kit.diagnostics.DiagnosticAction;
import com.audacious_software.passive_data_kit.generators.Generator;
import com.audacious_software.passive_data_kit.generators.Generators;
import com.audacious_software.pdk.passivedatakit.R;
import com.google.android.gms.awareness.Awareness;
import com.google.android.gms.awareness.snapshot.DetectedActivityResult;
import com.google.android.gms.awareness.snapshot.HeadphoneStateResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;

import java.util.ArrayList;

/**
* Created by cjkarr on 6/28/2016.
*/
public class GoogleAwareness extends Generator implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private static final String GENERATOR_IDENTIFIER = "google-awareness";

private static final String ENABLED = "com.audacious_software.passive_data_kit.generators.services.GoogleAwareness.ENABLED";
private static final boolean ENABLED_DEFAULT = true;
private static final long SENSING_INTERVAL = 60 * 1000;

private static GoogleAwareness sInstance = null;
private GoogleApiClient mGoogleApiClient = null;
private Handler mSensingHandler = null;

public static GoogleAwareness getInstance(Context context) {
if (GoogleAwareness.sInstance == null) {
GoogleAwareness.sInstance = new GoogleAwareness(context.getApplicationContext());
}

return GoogleAwareness.sInstance;
}

public GoogleAwareness(Context context) {
super(context);
}

public static void start(final Context context) {
GoogleAwareness.getInstance(context).startGenerator();
}

private void startGenerator() {
final GoogleAwareness me = this;

Runnable r = new Runnable()
{

@Override
public void run() {
Log.e("PDK", "GA MAKE CLIENT");
if (me.mGoogleApiClient == null) {
GoogleApiClient.Builder builder = new GoogleApiClient.Builder(me.mContext);
builder.addConnectionCallbacks(me);
builder.addOnConnectionFailedListener(me);
builder.addApi(Awareness.API);

me.mGoogleApiClient = builder.build();
me.mGoogleApiClient.connect();
Log.e("PDK", "GA MADE CLIENT");
}
}
};

Thread t = new Thread(r);
t.start();

Generators.getInstance(this.mContext).registerCustomViewClass(GoogleAwareness.GENERATOR_IDENTIFIER, GoogleAwareness.class);
}

public static ArrayList<DiagnosticAction> diagnostics(Context context)
{
return GoogleAwareness.getInstance(context).runDiagostics();
}

private ArrayList<DiagnosticAction> runDiagostics() {
ArrayList<DiagnosticAction> actions = new ArrayList<>();

final GoogleAwareness me = this;

final Handler handler = new Handler(Looper.getMainLooper());

Log.e("PDK", "1");
if (GoogleAwareness.isEnabled(this.mContext)) {
Log.e("PDK", "2");
int permissionCheck = ContextCompat.checkSelfPermission(this.mContext, Manifest.permission.ACCESS_FINE_LOCATION);

if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
actions.add(new DiagnosticAction(me.mContext.getString(R.string.diagnostic_missing_location_permission), new Runnable() {
@Override
public void run() {
handler.post(new Runnable() {

@Override
public void run() {
Intent intent = new Intent(me.mContext, RequestPermissionActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(RequestPermissionActivity.PERMISSION, Manifest.permission.ACCESS_FINE_LOCATION);

me.mContext.startActivity(intent);
}
});
}
}));
}

Log.e("PDK", "3");

permissionCheck = ContextCompat.checkSelfPermission(this.mContext, "com.google.android.gms.permission.ACTIVITY_RECOGNITION");

if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
Log.e("PDK", "3.3");
actions.add(new DiagnosticAction(me.mContext.getString(R.string.diagnostic_missing_activity_recognition_permission), new Runnable() {
@Override
public void run() {
handler.post(new Runnable() {

@Override
public void run() {
Intent intent = new Intent(me.mContext, RequestPermissionActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(RequestPermissionActivity.PERMISSION, "com.google.android.gms.permission.ACTIVITY_RECOGNITION");

me.mContext.startActivity(intent);
}
});
}
}));
Log.e("PDK", "3.7");
}


Log.e("PDK", "4");
}

Log.e("PDK", "5 " + actions.size());

return actions;
}

public static boolean isRunning(Context context) {
if (GoogleAwareness.sInstance == null) {
return false;
}

return (GoogleAwareness.sInstance.mGoogleApiClient != null);
}

public static boolean isEnabled(Context context) {
SharedPreferences prefs = Generators.getInstance(context).getSharedPreferences(context);

return prefs.getBoolean(GoogleAwareness.ENABLED, GoogleAwareness.ENABLED_DEFAULT);
}

@Override
public void onConnected(@Nullable Bundle bundle) {
Log.e("PDK", "GA onConnected");
final GoogleAwareness me = this;

Runnable r = new Runnable() {

@Override
public void run() {
Log.e("PDK", "GA GET ACTIVITY");
Awareness.SnapshotApi.getHeadphoneState(mGoogleApiClient).setResultCallback(new ResultCallback<HeadphoneStateResult>() {
@Override
public void onResult(@NonNull HeadphoneStateResult headphoneStateResult) {
if (!headphoneStateResult.getStatus().isSuccess()) {
Log.e("PDK", "Could not get the current headphone state.");
return;
}

Log.i("PDK", "HEAD: " + headphoneStateResult.getHeadphoneState());
}
});

if (me.mSensingHandler != null) {
me.mSensingHandler.postDelayed(this, GoogleAwareness.SENSING_INTERVAL);
}
}
};

this.mSensingHandler = new Handler();
this.mSensingHandler.post(r);
}

@Override
public void onConnectionSuspended(int i) {
Log.e("PDK", "GA onConnectionSuspended");
if (this.mGoogleApiClient != null && this.mGoogleApiClient.isConnected()) {
this.mGoogleApiClient.disconnect();
}

this.mSensingHandler = null;
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e("PDK", "GA onConnectionFailed");
this.mGoogleApiClient = null;
}
}

0 comments on commit a684783

Please sign in to comment.