CONCLUSION: This App is Very simple to install and use. The

Walk n’ Play
Spring 2012
Group - 8
Project Report
Spring 2012 – CSCI 6838
Walk n’ Play
A calorie-calculating App for Android Mobiles
By
Team Members:
Murali Krishna Goli
ViswanathPatimalla
Sivarama Krishna Polamaraju
Instructor:
Dr. Said Bettayeb
Mentor:
Dr. PradeepBuddharaju
1
Walk n’ Play
Spring 2012
ACKNOWLEDGEMENT
This project would have not been successful without the help and guidance
of several people who directly or indirectly helped us.
Our utmost gratitude to our dear instructor Dr. Said Bettayeb, Professor of
Computer Science and Computer Information Systems at University of HoustonClear Lake who not only helped and guided us throughout the entire semester,
but also pointed out the mistakes and taught us how to be successful in this
project and career.
Our special thanks to our dear mentor Dr. PradeepBuddharaju, who was
with us from the beginning to the end of this semester to help us in overcoming
each and every hurdle. We feel proud to tell that his encouragement and
guidance helped us in not only doing this project, but also in getting experience of
real world programming and technology.
2
Walk n’ Play
Spring 2012
Abstract
Walk n’ Play is a mobile application built for all devices having Android
operating system and Accelerometer sensor. This application provides accurate
calories count that are burnt against the physical activity based on cutting edge
calorie calculation technology, which uses the accelerometer in the device. This
application needs the user to sign up, enter information like height, weight, and
age and select an avatar. It provides accurate calorie count wherever you attach
the device (waist, arm, pockets).
Using this application, the user can track the total number of calories that
are spent during the physical activity. User can compete with the intelligent
simulator in the system and can achieve recommended activity levels. This
application uses an algorithm that will calculate the total amount of calories using
accelerometer in the device.
The main features of this application include
1. Tracking amount of calories burnt.
2. Compete with the intelligent simulator.
3. Achieving recommended activity level.
4. Play walking game.
UH researchers (Computational Physiology Lab) developed this algorithm
that gives an equation to find the total number of calories that are burnt for
physical activity using the accelerometer in the device. This algorithm is being
used in this application. This algorithm considers the position of the device on the
body, height, weight and gender of the user in calculating the calories spent.
3
Walk n’ Play
Spring 2012
CONTENTS:
INTRODUCTION: .................................................................................................................................... 5
Scope:............................................................................................................................................................ 5
Purpose: ........................................................................................................................................................ 5
Objective: ...................................................................................................................................................... 5
Android: ........................................................................................................................................................ 5
SPECIFICATIONS: ................................................................................................................................... 6
Software Requirements: ............................................................................................................................... 6
Hardware Requirements: .............................................................................................................................. 6
DESIGN PHASE: ....................................................................................................................................... 6
UML Class Diagram: ...................................................................................................................................... 6
UI Design: ...................................................................................................................................................... 7
IMPLEMENTATION: .............................................................................................................................. 7
System Details: .............................................................................................................................................. 7
Framework: ................................................................................................................................................... 8
Activity Life Cycle: ......................................................................................................................................... 9
Module Specification .................................................................................................................................. 10
TECHNICAL DETAILS: ......................................................................................................................... 10
Eclipse: ........................................................................................................................................................ 10
Android SDK: ............................................................................................................................................... 11
Android Development Tools Plugin: ........................................................................................................... 11
Swarm: ........................................................................................................................................................ 11
PROJECT OVERVIEW: ......................................................................................................................... 11
Algorithm: ................................................................................................................................................... 13
Avatars: ....................................................................................................................................................... 15
Male: ....................................................................................................................................................... 15
Female:.................................................................................................................................................... 18
CONCLUSION: ........................................................................................................................................ 27
FUTURE WORK: .................................................................................................................................... 27
References: ............................................................................................................................................ 28
Appendix: ............................................................................................................................................... 29
Sample Code: ......................................................................................................................................... 30
4
Walk n’ Play
Spring 2012
INTRODUCTION:
Scope:
Walk n’ Play is a mobile application built for all devices having Android
operating system and Accelerometer sensor. This application provides accurate
calories count burnt against the physical activity based on cutting edge calorie
calculation technology, which uses the accelerometer in the device. This
application is going to be posted in Market Place – place for Android Apps. Any
user with Android based mobile phone, can download this App at no charge.
Note: Physical Activities had some conditions.
Purpose:
This App helps any user to monitor the total amount of calories that are
burnt during his walking and running sessions. He can keep a track of his calories
that are burnt against the mentioned activities and can improve himself for a
better performance.
Objective:
The main objective behind taking this project is to help an individual keep
track of the amount of calories that he is burning towards activities that he
performs which are included in this App. By keeping a track on this number, he
can manage his performance levels and can do lot better to keep himself/herself
fit and healthy.
Android:
Android is one of the latest and fastest growing mobile platforms.Apart
from great benefits of the Android Operating System, it gives its users a wide
choice of hardware across a wide range of manufacturers. Android mobiles are
available in many types of phones like touchscreen, QWERTY, Slider, Bar type
mobiles. When it comes to the budget [1], with the entry of Android OS into the
market, smart phones became very much reachable to many individuals who
cannot afford for expensive smartphones in the market.
5
Walk n’ Play
Spring 2012
SPECIFICATIONS:
Software Requirements:
Android SDK.
Eclipse IDE.
ADT Plugin.
Android Emulator.
Hardware Requirements:
Any smart phone with Android OS with Internet connectivity.
DESIGN PHASE:
UML Class Diagram:
6
Walk n’ Play
Spring 2012
UI Design:
IMPLEMENTATION:
System Details:
This Application was developed in Android platform. The architecture of an
Android application can be explained with the following figure.
7
Walk n’ Play
Spring 2012
(Ref:http://developer.android.com/reference/android)
Framework:
Android offers developers the ability to build extremely rich and innovative
applications. Developers can use the device hardware, location information,
background services and many more [2]. The architecture is designed to simplify
the reuse of components. If one application publishes its capabilities and any
other application may use those capabilities [1]. This same mechanism allows
components to be replaced by the user.
Every application is a set of services and systems, including:
1. A rich set of Views that can be used to build an application.
2. Content Providers helps applications to access data from other applications
or to share their own data.
3. Resource Manager helps in accessing the non-code resources such as
localized strings, graphics and layout files.
4. Notification Manager helps all applications to display custom alerts
8
Walk n’ Play
Spring 2012
5. Activity Manager helps in managing the lifecycle of applications and in
providing a common navigation backstack.
Activity Life Cycle:
The below figure shows the life cycle of a game from the time we start and exit it.
Activity Life Cycle
(Ref: http://developer.android.com/reference/android/app/Activity.html)
oncreate(): Called when the activity is created.
Onpause():Called when the system is about to start resuming a previous
activity.
9
Walk n’ Play
Spring 2012
Onresume():Called when the activity will start interacting with the user.
Onrestart():Called after your activity has been stopped, prior to it being started
again.
Onstop():Called when the activity is no longer visible to the user, because
another activity has been resumed and is covering this one.
Ondestroy():The final call you receive before your activity is destroyed.
Module Specification
Modules mentioned below are the mandatory modules that we used for
developing this App.
Window Management: Responsible for pausing and resuming the App
Input: Keeps track of user input e.g., touch events, keystrokes etc.
File I/O: Get bytes of all assets into our program from disk.
Graphics: Responsible for loading and drawing graphics on screen.
Audio: Responsible for loading and playing sounds in the App.
Framework: This ties all the above modules together.
TECHNICAL DETAILS:
The main technologies used in developing this Application are Eclipse and
Android SDK.
Eclipse:
IBM created the Eclipse project. For Java developers, Eclipse is one of the
most useful IDE. Android applications can be easily developed using Eclipse with
10
Walk n’ Play
Spring 2012
the help of ADT Plugin (Android Development Tools Plugin) [4]. Eclipse provides a
complete set of tools for developers to create Android applications.
Android SDK:
Android Software Development Kit provides set of tools, libraries,
documentation, sample code, debugger and emulator. Applications can be
developed using Eclipse. To install Android SDK, we have few steps to be
followed.
1. Checking the system if it has got the minimum requirements.
2. Downloading and Installing the SDK Package
3. Downloading and Installing the ADT Plugin for Eclipse.
4. Integrating Android platforms and other components to your SDK.
Android Development Tools Plugin:
ADT Plugin is an eclipse plugin. ADT is designed to give powerful, integrated
environment to build Android applications [4]. ADT extends the capabilities of
Eclipse in setting up new Android projects, in creating user interfaces, in adding
packages based on the android framework, debugging the applications and also
to export the application.
Swarm:
Leaderboards can track scores in different formats such as time, integers
etc. Users can easily find their own scores or check out the top of the charts.
Users need not necessarily login to use this service. They can login using their
Facebook login credentials [1]. Scores can be displayed and sorted by day, week,
and month or all-time. To use this service, simply configure the Leaderboards via
Swarm’s Admin and add a few lines of code to your game or app. There are many
APS’s available to customize the Leaderboards.
PROJECT OVERVIEW:
11
Walk n’ Play
Spring 2012
Installing this application is very clear and simple process. This Application
can be downloaded from the Market Place. This can be installed in the device
with Android Operating System and Accelerometer sensor. There is a three-step
process for using this Application.
Step 1: Installing the App
Step 2: Registration Process
Step 3: Using the App
In the registration process the app will be asking some details about the
user to be entered in. This app will consider all these details entered in providing
the accurate calculations. The main details include:
1. Username
2. Weight
3. Gender
This App got strong validations in all the fields in the registration process.
Once the user enters all the information in the registration page and submits the
date, App takes an Avatar based on the gender provided on the registration page.
This Avatar will show us the physical state of the user, which can be classified
under different categories as Idle, Walking, Running and Tired state.
It will also ask for the position on your body where you are having the
device on. The options given in the App are:
1. Waist
2. Breast Pocket
3. Jacket Side Pocket
4. Hand
5. Back Pocket
6. Pants Pocket
7. Hand Bag
8. Arm
Once the user finishes the registration process, the App will be directed to
Swarm Leaderboard login screen. Swarm Leaderboard is used in this application.
This Leaderboard service is integrated to let the user to track and compare his
12
Walk n’ Play
Spring 2012
performance with the other users. User can login into Swarm Leaderboard either
by registering with Swarm or as a guest or by using the Facebook login
credentials. Once the user selects the mode of login and logs into Swarm, the
application main page will be displayed. In this page, the user can choose the
position of the device on the body and also can edit his information.
There is an On/Off button on the main page of this application. When user
clicks on this button to ON, the application will change into power saver mode. In
power saver mode, the application will not calculate any calories. When it is
changed to OFF, the application will come out from power saver mode.
Algorithm:
University of Houston – Main Campus Researchers from Computational
Physiology Lab developed an algorithm to calculate the total number of calories
that are burnt during walking and running activities of the user. This cutting edge
algorithm was used in this application.
SamplesPerSecond = 20; TotalSamples = 200; SampleFrequency = 10;
A_FrequencyCalibration = -­­0.0024;
B_FrequencyCalibration = 1.2117;
A_CalorieCalibration = 1.357e-­­05;
B_CalorieCalibration = 0.000374;
MINUTE_FOR_MEATBOLIC_CALC = 0.16667;
13
Walk n’ Play
Pocket
Handbag
Hand
Arm
Waist
Backpack
Breast Pocket
Jacket Side Pocket
Spring 2012
A_PositionalCalibration
B_PositionalCalibration
0.54245
71.9421
1.4671
7.4758
2.3772
-­­13.3029
1.7625
37.0678
1
0
1.5198
81.7146
1.605
30.2151
1.1534
14.3466
AccelerometerEnergy = Σ1<i<200(|xi-xi-1|+|yi-yi-1|+|zi-zi-1|); tmp = 3* AccelerometerEnergy;
tmp = (SampleFrequency * A_FrequencyCalibration + B_FrequencyCalibration) *
tmp;
tmp = A_PositionalCalibration * tmp + B_PositionalCalibration;
14
Walk n’ Play
Spring 2012
tmp = A_CalorieCalibration * tmp + B_CalorieCalibration;
If(tmp< 0)
{
kcal = 0;
}
else
{
kcal = tmp * weight * MINUTE_FOR_MEATBOLIC_CALC * 5.0;
}
Avatars:
We used cool avatars, which show the state of the user. Depending on the gender
of the user given when the user is registering with the App, the App will take the
default avatar for the user and will show the avatar defined for that particular
state of the user.
We have two Avatars.
1. Male
2. Female
Male:
The user states are categorized into 3 states.
1. Run
2. Tired
3. Walk
15
Walk n’ Play
Spring 2012
Run: When the user is running, this avatar will be displayed on the activity screen.
Tired: When the user is tired, this avatar will be displayed on the activity screen.
16
Walk n’ Play
Spring 2012
Walk: When the user is walking, this avatar will be displayed on the activity
screen.
17
Walk n’ Play
Spring 2012
Female:
The user states are categorizes into 3 states.
1. Run
2. Tired
3. Walk
Run: When the user is running, this avatar will be displayed on the activity screen.
18
Walk n’ Play
Spring 2012
Tired: When the user is tired, this avatar will be displayed on the activity screen.
Walk: When the user is walking, this avatar will be displayed on the activity
screen.
19
Walk n’ Play
Spring 2012
The various stages of the App are:
20
Walk n’ Play
Spring 2012
This is the start page in the application. When the application was
launched, this screen will pop up. The user should tap on the screen to begin the
registration.
21
Walk n’ Play
Spring 2012
When the user taps on the home screen, then the registration page will be
displayed. The user should input the user name, weight and the gender. Once all
the details are filled in, the user can click on Play button.
22
Walk n’ Play
Spring 2012
When the user clicks on Play, the above screen will be displayed. The user
should select one of the three options to Create Account or to Use Existing
Account or to Play as Guest to login to Swarm Leaderboard. The user can also
login to this application using Facebook login credentials.
23
Walk n’ Play
Spring 2012
When the user login into the application into the Swarm, this page will be
displayed. In this page, there are four features. When the user clicks on the
Options, the user can edit his information. When the user clicks on Set Position,
the user can select the position of the device on the body. When the user clicks
on Leaderboard, the scores stored in Leaderboard will be displayed. User can
know about his performance and can compare their own performance with other
users registered in the application.
24
Walk n’ Play
Spring 2012
When the user clicks on the Set Position on the main page, this window will
be appeared. Here the user can select the position of the device on the body.
There are eight options. User can choose one from Waist, Back pocket, Breast
pocket, Pants pocket, Jacket side pocket, Handbag, Hand, Arm.
25
Walk n’ Play
Spring 2012
When the user clicks on the Leaderboard button on the main page, this
screen will be displayed. The user can look at many details on this page which
include the personal score, friends scores, top scores, the user ranking when
compared to all the other users.
26
Walk n’ Play
Spring 2012
CONCLUSION:
This App is Very simple to install and use. The registration process is simple
and the user interface is very clear. The calculations of this App are very
accurate.Designed in the way that it utilizes the device resources efficiently.
FUTURE WORK:
Future work of this project includes making an arrangement to include a
facility where a user can use this App not only in his device, but can use it in any
device by logging into that device with the login credentials provided. When the
user log into other devices, all his previous records and account information will
be retrieved by this App into the current device.This can be achieved by saving all
the information in a Cloud.
27
Walk n’ Play
Spring 2012
References:
[1] Lars Vogel. (July. 07, 2009).Android development tutorial.
Available: http://www.vogella.de/articles/Android/article.html#first_code
[2] IDC Corporate USA.( Apr. 29, 2012). A Study by IDC (International
Data Corporation.
Available:
http://www.idc.com/about/viewpressrelease.jsp?containerId=prUS22617910,
2011
[3] Android developers. Available: http://developer.Android.com/index.html
[4] Bucky Roberts. (Apr. 16, 2012). Android Application Development.
Available: http://thenewboston.org/list.php?cat=6
28
Walk n’ Play
Spring 2012
Appendix:
Team member information:
1. Murali Krishna Goli
Major: Computer Science
Email: [email protected]
Role: Programmer/Reports Documenter
2. ViswanathPatimalla
Major: Computer Science
Email: [email protected]
Role: Team Leader/ Programmer/Reports Documenter
3. Shivaram Krishna Polamaraju
Major: Computer Science
Email: [email protected]
Role: Programmer/Webmaster
29
Walk n’ Play
Spring 2012
Sample Code:
/***
Copyright (c) 2012 Group Project-8 Walk 'n' Play
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.
*/
packagecom.authorwjf;
importcom.swarmconnect.Swarm;
importcom.swarmconnect.SwarmActiveUser;
importcom.swarmconnect.SwarmActivity;
importcom.swarmconnect.SwarmLeaderboard;
importcom.swarmconnect.SwarmLeaderboard.GotLeaderboardCB;
importcom.swarmconnect.SwarmStore;
importcom.swarmconnect.SwarmUser;
importcom.swarmconnect.SwarmUser.GotUserCB;
importcom.swarmconnect.SwarmUserInventory;
importcom.swarmconnect.delegates.SwarmLoginListener;
importandroid.app.Activity;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.content.SharedPreferences;
importandroid.graphics.Typeface;
importandroid.graphics.drawable.AnimationDrawable;
importandroid.hardware.Sensor;
importandroid.hardware.SensorEvent;
importandroid.hardware.SensorEventListener;
importandroid.hardware.SensorManager;
importandroid.os.Bundle;
30
Walk n’ Play
Spring 2012
importandroid.os.SystemClock;
importandroid.preference.PreferenceManager;
importandroid.util.Log;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.ImageView;
importandroid.widget.TextView;
importandroid.widget.Toast;
importandroid.widget.ToggleButton;
public class Male extends SwarmActivity implements SensorEventListener {
private float mLastX, mLastY, mLastZ;
privatebooleanmInitialized;
privateSensorManagermSensorManager;
private Sensor mAccelerometer,mgrAccelerometer;
private final float NOISE = (float) 2.0;
private long lastUpdate = -1;
private float x, y, z;
private float last_x, last_y, last_z;
private static final int SHAKE_THRESHOLD = 800;
TextViewtvX;
TextViewtvY;
TextViewtvZ;
TextViewnameview;
TextViewweightview;
TextViewgenderview;
TextView calo,calo2,posi,welcome,swarmwelcome;
TextView check;
ImageViewiv;
ToggleButtontglSample;
doubleA_FrequencyCalibration=-0.0024;
doubleB_FrequencyCalibration=1.2117;
doubleA_CalorieCalibration=1.357e-05;
doubleB_CalorieCalibration=0.000374;
double MINUTE_FOR_MEATBOLIC_CALC=0.16667;
doubleA_PositionalCalibration=0.54245;
doubleB_PositionalCalibration=71.9421;
31
Walk n’ Play
Spring 2012
int weight=100;
longstartTime;
private long lastShakeTimestamp=0;
intSampleFrequency=10;
private long gap=400;
double r=0;
double threshold=1.05d;
privateSensorManagermgr=null;
double kcal=0;
public static String filename="Mysharedcalorie";
SharedPreferencescaloriepreferences;
doubletmp;
floatAccelerometerEnergy=0;
longNowTime = -1;
doublek,servek;
Button ldbrd,showl,positionshow,options;
ImageView splashImage1,splashImage2,splashImagef,splashImage3;
AnimationDrawable
splashAnimation1,splashAnimation2,splashAnimationf,splashAnimation3;
public static SwarmLeaderboard lead;
int j=0;
String n,resumewelcome;
public static String filenames="namefile";
public static String fileweight="weightfile";
public static String filegender="genderfile";
SharedPreferencesMalecaloriepreferences;
SharedPreferencesMalenamepreferences;
SharedPreferencesMaleweightpreferences;
SharedPreferencesMalegenderpreferences;
String Malecaloriestore;
String Malenamestore;
String Maleweightstore;
String Malegenderstore;
public static String servefilenames="servefile";
SharedPreferencesservepreferences;
String servestore;
String posname;
32
Walk n’ Play
Spring 2012
SharedPreferencesposoptions;
SharedPreferences opt;
Intent fileScannerService;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.male);
mInitialized = false;
Malenamepreferences=getSharedPreferences(filenames,0);
n=Malenamepreferences.getString("Countname","0");
welcome=(TextView)findViewById(R.id.editText1);
welcome.setTypeface(null,Typeface.BOLD);
posi= (TextView)findViewById(R.id.textView);
Swarm.init(this, 482,
"0d8cbfe99a1ed0bdbe95dd90a4094dc4",mySwarmLoginListener);
//if (Swarm.isLoggedIn())
//{
//posi.setText(Swarm.user.username);
//}
SwarmLeaderboard.getLeaderboardById(508, callback);
caloriepreferences=getSharedPreferences(filename,0);
//if (Swarm.isLoggedIn())
//{
//posi.setText(Swarm.user.username);
//}
SharedPreferencespositionpreferences =
PreferenceManager.getDefaultSharedPreferences(this);
// Set the values of the UI
tglSample = (ToggleButton) findViewById(R.id.toggleButton1);
tvX= (TextView)findViewById(R.id.x_axis);
tvY= (TextView)findViewById(R.id.y_axis);
tvZ= (TextView)findViewById(R.id.z_axis);
swarmwelcome=(TextView)findViewById(R.id.editTextswarm);
//nameview= (TextView)findViewById(R.id.name);
//weightview= (TextView)findViewById(R.id.weight);
//genderview= (TextView)findViewById(R.id.gender);
check=(TextView)findViewById(R.id.textView3);
33
Walk n’ Play
Spring 2012
showl=(Button)findViewById(R.id.showleader);
options=(Button)findViewById(R.id.options);
positionshow=(Button)findViewById(R.id.button1);
//calo= (TextView)findViewById(R.id.textView1);
calo2= (TextView)findViewById(R.id.textView2);
startTime = System.currentTimeMillis();
mSensorManager = (SensorManager)
getSystemService(Context.SENSOR_SERVICE);
mAccelerometer =
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer ,
SensorManager.SENSOR_DELAY_NORMAL);
threshold=threshold*SensorManager.GRAVITY_EARTH*SensorManager.GRAVITY_
EARTH;
mgr=(SensorManager) getSystemService(Context.SENSOR_SERVICE);
mgrAccelerometer = mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mgr.registerListener(listener,
mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
splashImage1 = (ImageView) findViewById(R.id.animationImage1);
splashImage2 = (ImageView) findViewById(R.id.animationImage1);
splashImage3 = (ImageView) findViewById(R.id.animationImage1);
splashImage1.setBackgroundResource(R.drawable.run);
splashAnimation1 = (AnimationDrawable) splashImage1.getBackground();
splashImage2.setBackgroundResource(R.drawable.mtrun);
splashAnimation2 = (AnimationDrawable) splashImage2.getBackground();
splashImage3.setBackgroundResource(R.drawable.rest);
splashAnimation3 = (AnimationDrawable) splashImage3.getBackground();
Maleweightpreferences=getSharedPreferences(fileweight,0);
String w=Maleweightpreferences.getString("Countweight","0");
Malegenderpreferences=getSharedPreferences(filegender,0);
String g=Malegenderpreferences.getString("Countgender","0");
//posi.setText(posname);
opt = getSharedPreferences("settingschange", 0);
String prefName = opt.getString("name", "nothing");
String wallPaper = opt.getString("weight", "null");
tglSample.setOnClickListener(new View.OnClickListener() {
34
Walk n’ Play
Spring 2012
@Override
public void onClick(View v) {
if(((ToggleButton) v).isChecked()){
Toast.makeText(getBaseContext(), "Power saver mode is 'On'
calories would not counted after app is closed", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "Power saver mode is 'Off'
calories will be counted after app is closed", Toast.LENGTH_SHORT).show();
}
}
});
//weightview.setText(w);
fileScannerService = new Intent(this, calorieserviece.class);
showl.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (lead != null) {
if (lead != null) {
lead.showLeaderboard();
}
}
}
});
positionshow.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent1=new Intent();
intent1.setClass(getApplication(), Position.class);
startActivity(intent1);
}
});
options.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent2=new Intent();
intent2.setClass(getApplication(), options.class);
startActivity(intent2);
}
});
35
Walk n’ Play
Spring 2012
//posi.setText(( text));
/*
posoptions= getSharedPreferences("positionschange", 0);
posname =posoptions.getString("pname", "nothing");
posi.setText(posname);
if(posname==":in waist")
{
A_PositionalCalibration=1;
B_PositionalCalibration=0;
posi.setText(posname+Double.toString(A_PositionalCalibration));
}
if(posname==":in hand")
{
A_PositionalCalibration=2.3772;
B_PositionalCalibration=-13.3029;
posi.setText(posname+Double.toString(A_PositionalCalibration));
}
if(posname==":in arm")
{
A_PositionalCalibration=1.7625;
B_PositionalCalibration=37.0678;
posi.setText(posname+Double.toString(A_PositionalCalibration));
}
if(posname==":in backpocket")
{
A_PositionalCalibration=1.5198;
B_PositionalCalibration=81.7146;
posi.setText(posname+Double.toString(A_PositionalCalibration));
}
if(posname==":in breastpocket")
{
A_PositionalCalibration=1.605;
B_PositionalCalibration=30.2151;
posi.setText(posname+Double.toString(A_PositionalCalibration));
}
if(posname==":in handbag")
{
36
Walk n’ Play
Spring 2012
A_PositionalCalibration=1.4671;
B_PositionalCalibration=7.4758;
posi.setText(posname+Double.toString(A_PositionalCalibration));
}
if(posname==":in jacksidepocket")
{
A_PositionalCalibration=1.1534;
B_PositionalCalibration=14.3466;
posi.setText(posname+Double.toString(A_PositionalCalibration));
}
if(posname==":in pantpocket")
{
A_PositionalCalibration=0.54245;
B_PositionalCalibration=71.9421;
posi.setText(posname+Double.toString(A_PositionalCalibration));
}
if(posname=="nothing")
{
A_PositionalCalibration=-0.0024;
B_PositionalCalibration=1.2117;
posi.setText(posname+Double.toString(A_PositionalCalibration));
}
*/
}
protected void onResume() {
super.onResume();
caloriepreferences=getSharedPreferences(filename,0);
String l=caloriepreferences.getString("CountCalorie","0");
String sid=caloriepreferences.getString("swarmid", resumewelcome);
//String t=caloriepreferences.getString("toggle","0");
// k=Double.parseDouble(l);
//r=0;
//calo2.setText(l);
//kcal=0;
Swarm.init(this, 482,
"0d8cbfe99a1ed0bdbe95dd90a4094dc4",mySwarmLoginListener);
swarmwelcome.setText("Your Swarm User Name is:"+sid);
AccelerometerEnergy=0;
37
Walk n’ Play
Spring 2012
//tmp=0;
servepreferences=getSharedPreferences(servefilenames,0);
//genderview.setText(ser);
String chk=caloriepreferences.getString("toggle","0");
if(chk=="notcheck")
{
stopService(fileScannerService);
String ser=servepreferences.getString("serveCalorie","0");
servek=Double.parseDouble(ser);
k=Double.parseDouble(l)+(Math.round(servek*1000000)/1000000);
tglSample.setChecked(false);
}
if(chk=="check")
{
tglSample.setChecked(true);
String ser=servepreferences.getString("serveCalorie","0");
servek=Double.parseDouble(ser);
k=Double.parseDouble(l)+servek;
}
//r=0;
l=String.valueOf(k);
calo2.setText(l);
//posi.setText(String.valueOf(servek));
//posi.setText(ser);
//AccelerometerEnergy=Float.parseFloat(calo.getText().toString());
//Set the values of the UI
//String h=preferences.getString("Name", null);
//AccelerometerEnergy=Float.parseFloat(h);
}
protected void onPause() {
super.onPause();
SharedPreferences.Editor editor = caloriepreferences.edit();
//String caloriestore=calo2.getText().toString();
//String caloriestore=String.valueOf(r);
String caloriestore=calo2.getText().toString();
//Put the values from the UI
editor.putString("CountCalorie", caloriestore); // value to store
editor.putString("swarmid", resumewelcome);
38
Walk n’ Play
Spring 2012
//editor.commit();
// Commit to storage
//mSensorManager.unregisterListener(this);
// mSensorManager.unregisterListener(this);
r=0;
k=0;
servepreferences=getSharedPreferences(servefilenames,0);
SharedPreferences.Editorserveeditor = servepreferences.edit();
servestore=String.valueOf(0);
//Put the values from the UI
serveeditor.putString("serveCalorie", servestore); // value to store
// Commit to storage
serveeditor.commit();
if(tglSample.isChecked()==false){
startService(fileScannerService);
editor.putString("toggle","notcheck");
editor.commit();
}
if(tglSample.isChecked()==true){
editor.putString("toggle","check");
editor.commit();
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// can be safely ignored for this demo
}
public void onSensorChanged(SensorEvent event) {
//double netForce=event.values[0]*event.values[0];
//netForce+=event.values[1]*event.values[1];
//netForce+=event.values[2]*event.values[2];
// if (threshold<netForce) {
//long now=SystemClock.uptimeMillis();
//if (lastShakeTimestamp==0) {
//lastShakeTimestamp=now;
//Log.d("ShakerDemo", "Shaking started!");
//splashAnimation2.stop();
//splashImage1.setBackgroundResource(R.drawable.run);
//splashAnimation1 = (AnimationDrawable)
39
Walk n’ Play
Spring 2012
splashImage1.getBackground();
//splashAnimation1.start();
//}
//else {
//long now=SystemClock.uptimeMillis();
// if (lastShakeTimestamp>0) {
// if (now-lastShakeTimestamp>gap) {
//lastShakeTimestamp=0;
//Log.d("ShakerDemo", "Shaking stopped!");
//splashAnimation1.stop();
//splashImage2.setBackgroundResource(R.drawable.mtrun);
//splashAnimation2 = (AnimationDrawable)
splashImage2.getBackground();
//splashAnimation2.start();
//}
//}
//}
if((startTime+(NowTime-startTime))<(10000+startTime))
{
longcurTime = System.currentTimeMillis();
// only allow one update every 100ms.
if ((curTime - lastUpdate) > 50) {
longdiffTime = (curTime - lastUpdate);
lastUpdate = curTime;
x = event.values[SensorManager.DATA_X];
y = event.values[SensorManager.DATA_Y];
z = event.values[SensorManager.DATA_Z];
//tvY.setText(String.valueOf(NowTime+"----------"+startTime));
//tvX.setText(String.valueOf(AccelerometerEnergy));
//tvX.setText(String.valueOf(AccelerometerEnergy));
//tvY.setText(String.valueOf(y));
//tvZ.setText(String.valueOf(z));
float speed = Math.abs(x+y+z - last_x - last_y - last_z)
/ diffTime * 10000;
double netForce1=x*x;
netForce1+=y*y;
40
Walk n’ Play
Spring 2012
netForce1+=z*z;
if (netForce1 > threshold) {
// yes, this is a shake action! Do something about
it!AccelerometerEnergy=AccelerometerEnergy+(Math.abs(x-last_x)+Math.abs(ylast_y)+Math.abs(y-last_y));
}
last_x = x;
last_y = y;
last_z = z;
NowTime = System.currentTimeMillis();
}
}
if((startTime+(NowTime-startTime))>(10000+startTime))
{
tmp=3*AccelerometerEnergy;
tmp=(SampleFrequency*A_FrequencyCalibration+B_FrequencyCalibration)
*tmp;
tmp=A_PositionalCalibration*tmp+B_PositionalCalibration;
tmp=A_CalorieCalibration*tmp+B_CalorieCalibration;
//check.setText(String.valueOf(AccelerometerEnergy));
AccelerometerEnergy=0;
if(tmp<0)
{
kcal=0;
//calo.setText(String.valueOf(kcal));
}
if(tmp>0)
{
//AccelerometerEnergy!=0 &&
kcal=tmp*weight*MINUTE_FOR_MEATBOLIC_CALC*5.0;
//posi.setText(String.valueOf(kcal));
if(kcal>(.11252344184049501))
{
//calo.setText(String.valueOf((kcal+Double.parseDouble(k))));
//calo.setText(String.valueOf((kcal.11252344184049501)+r));
r = (kcal-.11252344184049501)+r+k;
41
Walk n’ Play
Spring 2012
r=(Math.round(r*1000000)/1000000);
calo2.setText(String.valueOf(r));
k=0;
servek=0;
if (lead != null) {
// Then submit the score
floatnumBalloonsPopped=new
Float(calo2.getText().toString());
lead.submitScore(numBalloonsPopped);
}
}
else{
//calo.setText(String.valueOf(0));
}
}
/*if(AccelerometerEnergy==0)
{
calo.setText(String.valueOf((kcal+Double.parseDouble(k))));
}*/
startTime = System.currentTimeMillis();
//pty.setText("reached");
}
}
privateSensorEventListener listener=new SensorEventListener() {
public void onSensorChanged(SensorEvent e) {
if (e.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
doublenetForce=e.values[0]*e.values[0];
netForce+=e.values[1]*e.values[1];
netForce+=e.values[2]*e.values[2];
if (threshold<netForce) {
long now=SystemClock.uptimeMillis();
if (lastShakeTimestamp==0) {
lastShakeTimestamp=now;
//Log.d("ShakerDemo", "Shaking started!");
splashAnimation2.stop();
splashAnimation3.stop();
42
Walk n’ Play
Spring 2012
splashImage1.setBackgroundResource(R.drawable.run);
splashAnimation1 = (AnimationDrawable)
splashImage1.getBackground();
splashAnimation1.start();
j=0;
}
else {
lastShakeTimestamp=now;
}
}
/*
if(netForce==0){
splashAnimation2.stop();
splashAnimation1.stop();
splashImage3.setBackgroundResource(R.drawable.rest);
splashAnimation3 = (AnimationDrawable)
splashImage3.getBackground();
splashAnimation3.start();
}*/
else {
j++;
if(j>100){
splashAnimation2.stop();
splashAnimation1.stop();
splashImage3.setBackgroundResource(R.drawable.rest);
splashAnimation3 = (AnimationDrawable)
splashImage3.getBackground();
splashAnimation3.start();
}
long now=SystemClock.uptimeMillis();
if (lastShakeTimestamp>0) {
if (now-lastShakeTimestamp>gap) {
lastShakeTimestamp=0;
//Log.d("ShakerDemo", "Shaking stopped!");
splashAnimation1.stop();
splashAnimation3.stop();
splashImage2.setBackgroundResource(R.drawable.mtrun);
splashAnimation2 = (AnimationDrawable)
43
Walk n’ Play
Spring 2012
splashImage2.getBackground();
splashAnimation2.start();
}
}
}
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// unused
}
};
privateSwarmLoginListenermySwarmLoginListener = new SwarmLoginListener() {
// This method is called when the login process has started
// (when a login dialog is displayed to the user).
public void loginStarted() {
//posi.setText(Swarm.user.username);
}
// This method is called if the user cancels the login process.
public void loginCanceled() {
}
// This method is called when the user has successfully logged
in.
public void userLoggedIn(SwarmActiveUser user) {
SwarmLeaderboard.getLeaderboardById(508, callback);
//posi.setText(Swarm.user.username);
}
// This method is called when the user logs out.
public void userLoggedOut() {
}
};
GotLeaderboardCB callback = new GotLeaderboardCB() {
public void gotLeaderboard(SwarmLeaderboard leaderboard) {
if (leaderboard != null) {
// Save the leaderboard for later use
lead = leaderboard;
if (Swarm.isLoggedIn())
{
44
Walk n’ Play
Spring 2012
welcome.setText("Welcome:"+n);
resumewelcome=Swarm.user.username;
swarmwelcome.setText("Your Swarm User Name
is:"+Swarm.user.username);
}
}
}
};
@Override
public void onBackPressed() {
// do nothing.
}
}
45