Skip to content

Commit 141d666

Browse files
committed
Added activitity recognition and google fit sensors api examples
1 parent ea746f5 commit 141d666

File tree

32 files changed

+492
-0
lines changed

32 files changed

+492
-0
lines changed

ActivityRecognition/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

ActivityRecognition/app/build.gradle

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 23
5+
buildToolsVersion "23.0.2"
6+
7+
defaultConfig {
8+
applicationId "com.tutsplus.activityrecognition"
9+
minSdkVersion 14
10+
targetSdkVersion 23
11+
versionCode 1
12+
versionName "1.0"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
testCompile 'junit:junit:4.12'
25+
compile 'com.android.support:appcompat-v7:23.+'
26+
27+
compile 'com.google.android.gms:play-services:8.4.0'
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.tutsplus.activityrecognition;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.tutsplus.activityrecognition">
4+
5+
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:supportsRtl="true"
12+
android:theme="@style/AppTheme">
13+
<activity android:name=".MainActivity">
14+
<intent-filter>
15+
<action android:name="android.intent.action.MAIN" />
16+
17+
<category android:name="android.intent.category.LAUNCHER" />
18+
</intent-filter>
19+
</activity>
20+
21+
<service android:name=".ActivityRecognizedService" />
22+
</application>
23+
24+
</manifest>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.tutsplus.activityrecognition;
2+
3+
import android.app.IntentService;
4+
import android.content.Intent;
5+
import android.support.v4.app.NotificationCompat;
6+
import android.support.v4.app.NotificationManagerCompat;
7+
import android.util.Log;
8+
9+
import com.google.android.gms.location.ActivityRecognitionResult;
10+
import com.google.android.gms.location.DetectedActivity;
11+
12+
import java.util.List;
13+
14+
/**
15+
* Created by Paul on 2/1/16.
16+
*/
17+
public class ActivityRecognizedService extends IntentService {
18+
19+
public ActivityRecognizedService() {
20+
super("ActivityRecognizedService");
21+
}
22+
23+
public ActivityRecognizedService(String name) {
24+
super(name);
25+
}
26+
27+
@Override
28+
protected void onHandleIntent(Intent intent) {
29+
if(ActivityRecognitionResult.hasResult(intent)) {
30+
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
31+
handleDetectedActivities( result.getProbableActivities() );
32+
}
33+
}
34+
35+
private void handleDetectedActivities(List<DetectedActivity> probableActivities) {
36+
for( DetectedActivity activity : probableActivities ) {
37+
switch( activity.getType() ) {
38+
case DetectedActivity.IN_VEHICLE: {
39+
Log.e( "ActivityRecogition", "In Vehicle: " + activity.getConfidence() );
40+
break;
41+
}
42+
case DetectedActivity.ON_BICYCLE: {
43+
Log.e( "ActivityRecogition", "On Bicycle: " + activity.getConfidence() );
44+
break;
45+
}
46+
case DetectedActivity.ON_FOOT: {
47+
Log.e( "ActivityRecogition", "On Foot: " + activity.getConfidence() );
48+
break;
49+
}
50+
case DetectedActivity.RUNNING: {
51+
Log.e( "ActivityRecogition", "Running: " + activity.getConfidence() );
52+
break;
53+
}
54+
case DetectedActivity.STILL: {
55+
Log.e( "ActivityRecogition", "Still: " + activity.getConfidence() );
56+
break;
57+
}
58+
case DetectedActivity.TILTING: {
59+
Log.e( "ActivityRecogition", "Tilting: " + activity.getConfidence() );
60+
break;
61+
}
62+
case DetectedActivity.WALKING: {
63+
Log.e( "ActivityRecogition", "Walking: " + activity.getConfidence() );
64+
if( activity.getConfidence() >= 75 ) {
65+
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
66+
builder.setContentText( "Are you walking?" );
67+
builder.setSmallIcon( R.mipmap.ic_launcher );
68+
builder.setContentTitle( getString( R.string.app_name ) );
69+
NotificationManagerCompat.from(this).notify(0, builder.build());
70+
}
71+
break;
72+
}
73+
case DetectedActivity.UNKNOWN: {
74+
Log.e( "ActivityRecogition", "Unknown: " + activity.getConfidence() );
75+
break;
76+
}
77+
}
78+
}
79+
}
80+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.tutsplus.activityrecognition;
2+
3+
import android.app.PendingIntent;
4+
import android.content.Intent;
5+
import android.support.annotation.NonNull;
6+
import android.support.annotation.Nullable;
7+
import android.support.v7.app.AppCompatActivity;
8+
import android.os.Bundle;
9+
10+
import com.google.android.gms.common.ConnectionResult;
11+
import com.google.android.gms.common.api.GoogleApiClient;
12+
import com.google.android.gms.location.ActivityRecognition;
13+
import com.google.android.gms.location.ActivityRecognitionApi;
14+
import com.google.android.gms.wallet.wobs.TimeInterval;
15+
16+
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
17+
18+
public GoogleApiClient mApiClient;
19+
20+
@Override
21+
protected void onCreate(Bundle savedInstanceState) {
22+
super.onCreate(savedInstanceState);
23+
setContentView(R.layout.activity_main);
24+
25+
mApiClient = new GoogleApiClient.Builder(this)
26+
.addApi(ActivityRecognition.API)
27+
.addConnectionCallbacks(this)
28+
.addOnConnectionFailedListener(this)
29+
.build();
30+
31+
mApiClient.connect();
32+
}
33+
34+
@Override
35+
public void onConnected(@Nullable Bundle bundle) {
36+
Intent intent = new Intent( this, ActivityRecognizedService.class );
37+
PendingIntent pendingIntent = PendingIntent.getService( this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT );
38+
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates( mApiClient, 3000, pendingIntent );
39+
}
40+
41+
@Override
42+
public void onConnectionSuspended(int i) {
43+
44+
}
45+
46+
@Override
47+
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
48+
49+
}
50+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:paddingBottom="@dimen/activity_vertical_margin"
7+
android:paddingLeft="@dimen/activity_horizontal_margin"
8+
android:paddingRight="@dimen/activity_horizontal_margin"
9+
android:paddingTop="@dimen/activity_vertical_margin"
10+
tools:context="com.tutsplus.activityrecognition.MainActivity">
11+
12+
<TextView
13+
android:layout_width="wrap_content"
14+
android:layout_height="wrap_content"
15+
android:text="Hello World!" />
16+
</RelativeLayout>
Loading
Loading
Loading
Loading
Loading
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<resources>
2+
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
3+
(such as screen margins) for screens with more than 820dp of available width. This
4+
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
5+
<dimen name="activity_horizontal_margin">64dp</dimen>
6+
</resources>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<color name="colorPrimary">#3F51B5</color>
4+
<color name="colorPrimaryDark">#303F9F</color>
5+
<color name="colorAccent">#FF4081</color>
6+
</resources>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<resources>
2+
<!-- Default screen margins, per the Android Design guidelines. -->
3+
<dimen name="activity_horizontal_margin">16dp</dimen>
4+
<dimen name="activity_vertical_margin">16dp</dimen>
5+
</resources>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">ActivityRecognition</string>
3+
</resources>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<resources>
2+
3+
<!-- Base application theme. -->
4+
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
5+
<!-- Customize your theme here. -->
6+
<item name="colorPrimary">@color/colorPrimary</item>
7+
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
8+
<item name="colorAccent">@color/colorAccent</item>
9+
</style>
10+
11+
</resources>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.tutsplus.activityrecognition;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
/**
8+
* To work on unit tests, switch the Test Artifact in the Build Variants view.
9+
*/
10+
public class ExampleUnitTest {
11+
@Test
12+
public void addition_isCorrect() throws Exception {
13+
assertEquals(4, 2 + 2);
14+
}
15+
}

GoogleFitSensorAPI/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.tutsplus.googlefit">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:supportsRtl="true"
10+
android:theme="@style/AppTheme">
11+
<activity android:name=".MainActivity">
12+
<intent-filter>
13+
<action android:name="android.intent.action.MAIN" />
14+
15+
<category android:name="android.intent.category.LAUNCHER" />
16+
</intent-filter>
17+
</activity>
18+
</application>
19+
20+
</manifest>

0 commit comments

Comments
 (0)