Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
Created activity recognition sample
Browse files Browse the repository at this point in the history
Change-Id: I316bc3a1f71b175c78939694d4bce26664111a2b
  • Loading branch information
shailen committed Jan 2, 2015
1 parent 0a2dc15 commit a72024c
Show file tree
Hide file tree
Showing 35 changed files with 1,262 additions and 63 deletions.
6 changes: 6 additions & 0 deletions ActivityRecognition/.gitignore
@@ -0,0 +1,6 @@
build/
.idea
.gradle
*.iml
local.properties
.DS_Store
73 changes: 73 additions & 0 deletions ActivityRecognition/README.md
@@ -0,0 +1,73 @@
Recognizing the User's Current Activity
=======================================

Demonstrates use of the [ActivityRecognitionApi][1] to recognize a user's current
activity, such as walking, driving, or standing still.

Introduction
============

Demonstrates use of the [ActivityRecognitionApi][1] to recognize a user's current
activity, such as walking, driving, or standing still.

Users can request activity updates by pressing the "Request Updates" button,
and stop receiving updates using the "Remove Updates" button.

The sample uses an `IntentService` to process detected activity changes, which
are sent using [ActivityRecognitionResult][2] objects. The IntentService gets a
list of probable detected activities and broadcasts them through a BroadcastReceiver. See the
[DetectedActivity[3] class for a list of DetectedActivity types. Each
`DetectedActivity` is associdated with a confidence level, which is an int
between 0 and 100.

To run this sample, **location must be enabled**.

[1]: https://developer.android.com/reference/com/google/android/gms/location/ActivityRecognitionApi.html

[2]: https://developer.android.com/reference/com/google/android/gms/location/ActivityRecognitionResult.html

[3]: https://developer.android.com/reference/com/google/android/gms/location/DetectedActivity.html

Prerequisites
--------------

- Android API Level >v9
- Android Build Tools >v21
- Google (Support) Repository

Getting Started
---------------

This sample uses the Gradle build system. To build this project, use the
"gradlew build" command or use "Import Project" in Android Studio.

Support
-------

- Stack Overflow: http://stackoverflow.com/questions/tagged/google-play-services

If you've found an error in this sample, please file an issue:
https://github.com/googlesamples/android-play-location/issues

Patches are encouraged, and may be submitted according to the instructions in
CONTRIBUTING.md.

License
-------

Copyright 2014 Google, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements. See the NOTICE file distributed with this work for
additional information regarding copyright ownership. The ASF licenses this
file to you under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
20 changes: 20 additions & 0 deletions ActivityRecognition/app/build.gradle
@@ -0,0 +1,20 @@
apply plugin: 'android'

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
applicationId "com.google.android.gms.location.sample.activityrecognition"
minSdkVersion 9
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
}
48 changes: 48 additions & 0 deletions ActivityRecognition/app/src/main/AndroidManifest.xml
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2014 Google, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.gms.location.sample.activityrecognition">

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Base">
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<!-- Service that provides activity recognition data. Setting the android:exported attribute
to "false" stops other apps from starting this service, even when using an explicit
intent. -->
<service
android:name=".DetectedActivitiesIntentService"
android:exported="false" />
</application>

<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
</manifest>
@@ -0,0 +1,93 @@
/**
* Copyright 2014 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.gms.location.sample.activityrecognition;

import android.content.Context;
import android.content.res.Resources;

import com.google.android.gms.location.DetectedActivity;

/**
* Constants used in this sample.
*/
public final class Constants {

private Constants() {
}

public static final String PACKAGE_NAME = "com.google.android.gms.location.activityrecognition";

public static final String BROADCAST_ACTION = PACKAGE_NAME + ".BROADCAST_ACTION";

public static final String ACTIVITY_EXTRA = PACKAGE_NAME + ".ACTIVITY_EXTRA";

public static final String SHARED_PREFERENCES_NAME = PACKAGE_NAME + ".SHARED_PREFERENCES";

public static final String ACTIVITY_UPDATES_REQUESTED_KEY = PACKAGE_NAME +
".ACTIVITY_UPDATES_REQUESTED";

public static final String DETECTED_ACTIVITIES = PACKAGE_NAME + ".DETECTED_ACTIVITIES";

/**
* The desired time between activity detections. Larger values result in fewer activity
* detections while improving battery life. A value of 0 results in activity detections at the
* fastest possible rate. Getting frequent updates negatively impact battery life and a real
* app may prefer to request less frequent updates.
*/
public static final long DETECTION_INTERVAL_IN_MILLISECONDS = 0;

/**
* List of DetectedActivity types that we monitor in this sample.
*/
protected static final int[] MONITORED_ACTIVITIES = {
DetectedActivity.STILL,
DetectedActivity.ON_FOOT,
DetectedActivity.WALKING,
DetectedActivity.RUNNING,
DetectedActivity.ON_BICYCLE,
DetectedActivity.IN_VEHICLE,
DetectedActivity.TILTING,
DetectedActivity.UNKNOWN
};

/**
* Returns a human readable String corresponding to a detected activity type.
*/
public static String getActivityString(Context context, int detectedActivityType) {
Resources resources = context.getResources();
switch(detectedActivityType) {
case DetectedActivity.IN_VEHICLE:
return resources.getString(R.string.in_vehicle);
case DetectedActivity.ON_BICYCLE:
return resources.getString(R.string.on_bicycle);
case DetectedActivity.ON_FOOT:
return resources.getString(R.string.on_foot);
case DetectedActivity.RUNNING:
return resources.getString(R.string.running);
case DetectedActivity.STILL:
return resources.getString(R.string.still);
case DetectedActivity.TILTING:
return resources.getString(R.string.tilting);
case DetectedActivity.UNKNOWN:
return resources.getString(R.string.unknown);
case DetectedActivity.WALKING:
return resources.getString(R.string.walking);
default:
return resources.getString(R.string.unidentifiable_activity, detectedActivityType);
}
}
}
@@ -0,0 +1,97 @@
/**
* Copyright 2014 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.gms.location.sample.activityrecognition;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.android.gms.location.DetectedActivity;

import java.util.ArrayList;
import java.util.HashMap;

/**
* Adapter that is backed by an array of {@code DetectedActivity} objects. Finds UI elements in the
* detected_activity layout and populates each element with data from a DetectedActivity
* object.
*/
public class DetectedActivitiesAdapter extends ArrayAdapter<DetectedActivity> {

public DetectedActivitiesAdapter(Context context,
ArrayList<DetectedActivity> detectedActivities) {
super(context, 0, detectedActivities);
}

@Override
public View getView(int position, View view, ViewGroup parent) {
DetectedActivity detectedActivity = getItem(position);
if (view == null) {
view = LayoutInflater.from(getContext()).inflate(
R.layout.detected_activity, parent, false);
}

// Find the UI widgets.
TextView activityName = (TextView) view.findViewById(R.id.detected_activity_name);
TextView activityConfidenceLevel = (TextView) view.findViewById(R.id.detected_activity_confidence_level);
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.detected_activity_progress_bar);

// Populate widgets with values.
activityName.setText(Constants.getActivityString(getContext(),
detectedActivity.getType()));
activityConfidenceLevel.setText(detectedActivity.getConfidence() + "%");
progressBar.setProgress(detectedActivity.getConfidence());
return view;
}

/**
* Process list of recently detected activities and updates the list of {@code DetectedActivity}
* objects backing this adapter.
*
* @param detectedActivities the freshly detected activities
*/
protected void updateActivities(ArrayList<DetectedActivity> detectedActivities) {
HashMap<Integer, Integer> detectedActivitiesMap = new HashMap<>();
for (DetectedActivity activity : detectedActivities) {
detectedActivitiesMap.put(activity.getType(), activity.getConfidence());
}
// Every time we detect new activities, we want to reset the confidence level of ALL
// activities that we monitor. Since we cannot directly change the confidence
// of a DetectedActivity, we use a temporary list of DetectedActivity objects. If an
// activity was freshly detected, we use its confidence level. Otherwise, we set the
// confidence level to zero.
ArrayList<DetectedActivity> tempList = new ArrayList<DetectedActivity>();
for (int i = 0; i < Constants.MONITORED_ACTIVITIES.length; i++) {
int confidence = detectedActivitiesMap.containsKey(Constants.MONITORED_ACTIVITIES[i]) ?
detectedActivitiesMap.get(Constants.MONITORED_ACTIVITIES[i]) : 0;

tempList.add(new DetectedActivity(Constants.MONITORED_ACTIVITIES[i],
confidence));
}

// Remove all items.
this.clear();

// Adding the new list items using {@link Adapter#addAll} notifies attached observers that
// the underlying data has changed and views reflecting the data should refresh.
this.addAll(tempList);
}
}

0 comments on commit a72024c

Please sign in to comment.