This repository has been archived by the owner on Sep 19, 2019. It is now read-only.
/
MainActivity.java
203 lines (184 loc) · 8.33 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright (C) 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.
*/
package com.google.android.gms.fit.samples.basicrecordingapi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.fit.samples.common.logger.Log;
import com.google.android.gms.fit.samples.common.logger.LogView;
import com.google.android.gms.fit.samples.common.logger.LogWrapper;
import com.google.android.gms.fit.samples.common.logger.MessageOnlyLogFilter;
import com.google.android.gms.fitness.Fitness;
import com.google.android.gms.fitness.FitnessOptions;
import com.google.android.gms.fitness.data.DataType;
import com.google.android.gms.fitness.data.Subscription;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import java.util.List;
/**
* This sample demonstrates how to use the Recording API of the Google Fit platform to subscribe
* to data sources, query against existing subscriptions, and remove subscriptions. It also
* demonstrates how to authenticate a user with Google Play Services.
*/
public class MainActivity extends AppCompatActivity {
public static final String TAG = "BasicRecordingApi";
private static final int REQUEST_OAUTH_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// This method sets up our custom logger, which will print all log messages to the device
// screen, as well as to adb logcat.
initializeLogging();
FitnessOptions fitnessOptions =
FitnessOptions.builder().addDataType(DataType.TYPE_ACTIVITY_SAMPLES).build();
// Check if the user has permissions to talk to Fitness APIs, otherwise authenticate the
// user and request required permissions.
if (!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(this), fitnessOptions)) {
GoogleSignIn.requestPermissions(
this,
REQUEST_OAUTH_REQUEST_CODE,
GoogleSignIn.getLastSignedInAccount(this),
fitnessOptions);
} else {
subscribe();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_OAUTH_REQUEST_CODE) {
subscribe();
}
}
}
/**
* Subscribes to an available {@link DataType}. Subscriptions can exist across application
* instances (so data is recorded even after the application closes down). When creating
* a new subscription, it may already exist from a previous invocation of this app. If
* the subscription already exists, the method is a no-op. However, you can check this with
* a special success code.
*/
public void subscribe() {
// To create a subscription, invoke the Recording API. As soon as the subscription is
// active, fitness data will start recording.
// [START subscribe_to_datatype]
Fitness.getRecordingClient(this, GoogleSignIn.getLastSignedInAccount(this))
.subscribe(DataType.TYPE_ACTIVITY_SAMPLES)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.i(TAG, "Successfully subscribed!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i(TAG, "There was a problem subscribing.");
}
});
// [END subscribe_to_datatype]
}
/**
* Fetches a list of all active subscriptions and log it. Since the logger for this sample
* also prints to the screen, we can see what is happening in this way.
*/
private void dumpSubscriptionsList() {
// [START list_current_subscriptions]
Fitness.getRecordingClient(this, GoogleSignIn.getLastSignedInAccount(this))
.listSubscriptions(DataType.TYPE_ACTIVITY_SAMPLES)
.addOnSuccessListener(new OnSuccessListener<List<Subscription>>() {
@Override
public void onSuccess(List<Subscription> subscriptions) {
for (Subscription sc : subscriptions) {
DataType dt = sc.getDataType();
Log.i(TAG, "Active subscription for data type: " + dt.getName());
}
}
});
// [END list_current_subscriptions]
}
/**
* Cancels the ACTIVITY_SAMPLE subscription by calling unsubscribe on that {@link DataType}.
*/
private void cancelSubscription() {
final String dataTypeStr = DataType.TYPE_ACTIVITY_SAMPLES.toString();
Log.i(TAG, "Unsubscribing from data type: " + dataTypeStr);
// Invoke the Recording API to unsubscribe from the data type and specify a callback that
// will check the result.
// [START unsubscribe_from_datatype]
Fitness.getRecordingClient(this, GoogleSignIn.getLastSignedInAccount(this))
.unsubscribe(DataType.TYPE_ACTIVITY_SAMPLES)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.i(TAG, "Successfully unsubscribed for data type: " + dataTypeStr);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Subscription not removed
Log.i(TAG, "Failed to unsubscribe for data type: " + dataTypeStr);
}
});
// [END unsubscribe_from_datatype]
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_cancel_subs) {
cancelSubscription();
return true;
} else if (id == R.id.action_dump_subs) {
dumpSubscriptionsList();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Initializes a custom log class that outputs both to in-app targets and logcat.
*/
private void initializeLogging() {
// Wraps Android's native log framework.
LogWrapper logWrapper = new LogWrapper();
// Using Log, front-end to the logging chain, emulates android.util.log method signatures.
Log.setLogNode(logWrapper);
// Filter strips out everything except the message text.
MessageOnlyLogFilter msgFilter = new MessageOnlyLogFilter();
logWrapper.setNext(msgFilter);
// On screen logging via a customized TextView.
LogView logView = (LogView) findViewById(R.id.sample_logview);
// Fixing this lint error adds logic without benefit.
//noinspection AndroidLintDeprecation
logView.setTextAppearance(this, R.style.Log);
logView.setBackgroundColor(Color.WHITE);
msgFilter.setNext(logView);
Log.i(TAG, "Ready");
}
}