From a6847839cd2f9ed1798f352d3ba3a68de66e7b6d Mon Sep 17 00:00:00 2001 From: "Chris J. Karr" Date: Fri, 21 Oct 2016 23:16:29 -0500 Subject: [PATCH 1/4] Activity detector work --- AndroidManifest.xml | 1 + build.gradle | 21 +- res/values/diagnostics.xml | 2 +- res/values/generators.xml | 1 + .../generators/Generators.java | 6 + .../generators/services/GoogleAwareness.java | 221 ++++++++++++++++++ 6 files changed, 242 insertions(+), 10 deletions(-) create mode 100755 src/com/audacious_software/passive_data_kit/generators/services/GoogleAwareness.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 25e3843..a376559 100755 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,5 +1,6 @@ + diff --git a/build.gradle b/build.gradle index d5d42cd..ffeb000 100755 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:2.0.0' + classpath 'com.android.tools.build:gradle:2.1.2' } } @@ -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" } @@ -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' diff --git a/res/values/diagnostics.xml b/res/values/diagnostics.xml index 7922b62..67b5383 100755 --- a/res/values/diagnostics.xml +++ b/res/values/diagnostics.xml @@ -3,5 +3,5 @@ Please grant the app sensor access on the Microsoft Band. Please grant the app permission to access location services on this device. - + Please grant the app permission to recognize your activity using data from this device. diff --git a/res/values/generators.xml b/res/values/generators.xml index 0b960d2..dd94af9 100755 --- a/res/values/generators.xml +++ b/res/values/generators.xml @@ -2,6 +2,7 @@ com.audacious_software.passive_data_kit.generators.device.Location com.audacious_software.passive_data_kit.generators.wearables.MicrosoftBand + com.audacious_software.passive_data_kit.generators.services.GoogleAwareness diff --git a/src/com/audacious_software/passive_data_kit/generators/Generators.java b/src/com/audacious_software/passive_data_kit/generators/Generators.java index bc5ee09..732a952 100755 --- a/src/com/audacious_software/passive_data_kit/generators/Generators.java +++ b/src/com/audacious_software/passive_data_kit/generators/Generators.java @@ -51,6 +51,8 @@ public void start() { for (String className : this.mGenerators) { try { + Log.e("PDK", "TRYING " + className); + Class probeClass = (Class) Class.forName(className); Method isEnabled = probeClass.getDeclaredMethod("isEnabled", Context.class); @@ -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); } } diff --git a/src/com/audacious_software/passive_data_kit/generators/services/GoogleAwareness.java b/src/com/audacious_software/passive_data_kit/generators/services/GoogleAwareness.java new file mode 100755 index 0000000..423912b --- /dev/null +++ b/src/com/audacious_software/passive_data_kit/generators/services/GoogleAwareness.java @@ -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 diagnostics(Context context) + { + return GoogleAwareness.getInstance(context).runDiagostics(); + } + + private ArrayList runDiagostics() { + ArrayList 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() { + @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; + } +} From f65fddfb7c605f5f40e541ae87732d826893e131 Mon Sep 17 00:00:00 2001 From: "Chris J. Karr" Date: Sun, 30 Oct 2016 19:16:10 -0500 Subject: [PATCH 2/4] Updating build requirements --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index b0998b5..f57ce7d 100755 --- a/build.gradle +++ b/build.gradle @@ -48,8 +48,8 @@ android { compile 'com.android.support:appcompat-v7:25.0.0' compile 'com.android.support:recyclerview-v7:25.0.0' compile 'com.android.support:cardview-v7:25.0.0' - compile 'com.google.android.gms:play-services-location:9.8.00' - compile 'com.google.android.gms:play-services-maps:9.8.00' + compile 'com.google.android.gms:play-services-location:9.8.0' + compile 'com.google.android.gms:play-services-maps:9.8.0' compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'commons-io:commons-io:2.4' compile 'commons-codec:commons-codec:1.10' From 5f91207f911b7c0c3f0b32b371f5dfd968521416 Mon Sep 17 00:00:00 2001 From: "Chris J. Karr" Date: Sun, 6 Nov 2016 14:17:37 -0600 Subject: [PATCH 3/4] Updating pointers to Google Awareness API --- build.gradle | 2 +- .../passive_data_kit/generators/services/GoogleAwareness.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 3488d16..26f51f5 100755 --- a/build.gradle +++ b/build.gradle @@ -52,7 +52,7 @@ android { compile 'com.google.android.gms:play-services-maps:9.8.0' compile 'com.google.android.gms:play-services-nearby:9.8.0' compile 'com.google.android.gms:play-services-places:9.8.0' -// compile 'com.google.android.gms:play-services-contextmanager:9.4.0' + compile 'com.google.android.gms:play-services-awareness:9.8.0' compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'commons-io:commons-io:2.4' compile 'commons-codec:commons-codec:1.10' diff --git a/src/com/audacious_software/passive_data_kit/generators/services/GoogleAwareness.java b/src/com/audacious_software/passive_data_kit/generators/services/GoogleAwareness.java index 423912b..796b1c2 100755 --- a/src/com/audacious_software/passive_data_kit/generators/services/GoogleAwareness.java +++ b/src/com/audacious_software/passive_data_kit/generators/services/GoogleAwareness.java @@ -174,6 +174,7 @@ public static boolean isEnabled(Context context) { @Override public void onConnected(@Nullable Bundle bundle) { Log.e("PDK", "GA onConnected"); + final GoogleAwareness me = this; Runnable r = new Runnable() { From df9b6521ececf4bfa054c77359d10d11ce6effb9 Mon Sep 17 00:00:00 2001 From: "Chris J. Karr" Date: Mon, 14 Nov 2016 21:06:09 -0600 Subject: [PATCH 4/4] Updating build dependencies --- build.gradle | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 26f51f5..d66c442 100755 --- a/build.gradle +++ b/build.gradle @@ -45,9 +45,9 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) // testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:25.0.0' - compile 'com.android.support:recyclerview-v7:25.0.0' - compile 'com.android.support:cardview-v7:25.0.0' + compile 'com.android.support:appcompat-v7:25.0.1' + compile 'com.android.support:recyclerview-v7:25.0.1' + compile 'com.android.support:cardview-v7:25.0.1' compile 'com.google.android.gms:play-services-location:9.8.0' compile 'com.google.android.gms:play-services-maps:9.8.0' compile 'com.google.android.gms:play-services-nearby:9.8.0'