Java tutorial
/* * Copyright (C) 2016 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.edicon.activity.fit; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import com.edicon.activity.common.logger.Log; import com.edicon.activity.common.logger.LogView; import com.edicon.activity.common.logger.LogWrapper; import com.edicon.activity.common.logger.MessageOnlyLogFilter; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.fit.samples.basichistoryapi.FitUpdateActivity; import com.google.android.gms.fit.samples.basichistoryapi.R; import com.google.android.gms.fitness.data.DataSet; import java.util.Calendar; import static com.edicon.activity.common.Utils.REQUEST_OAUTH; import static com.edicon.activity.common.Utils.REQUEST_RESOLVE_ERROR; import static com.edicon.activity.common.Utils.STATE_RESOLVING_ERROR; import static com.edicon.activity.common.Utils.mResolvingError; /** * This sample demonstrates how to use the History API of the Google Fit platform to insert data, * query against existing data, and remove data. It also demonstrates how to authenticate * a user with Google Play Services and how to properly represent data in a {@link DataSet}. */ public class HistoryActivity extends AppCompatActivity { public static final String TAG = "BasicHistoryApi"; private static final String DATE_FORMAT = "yyyy.MM.dd HH:mm:ss"; /** * Track whether an authorization activity is stacking over the current activity, i.e. when * a known auth error is being resolved, such as showing the account chooser or presenting a * consent dialog. This avoids common duplications as might happen on screen rotations, etc. */ private static final String AUTH_PENDING = "auth_state_pending"; private static boolean authInProgress = false; private static AppCompatActivity thisActivity; public static GoogleApiClient mClient = null; public static FitHistory fitHistory; @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(); thisActivity = this; mResolvingError = savedInstanceState != null && savedInstanceState.getBoolean(STATE_RESOLVING_ERROR, false); if (savedInstanceState != null) { authInProgress = savedInstanceState.getBoolean(AUTH_PENDING); } if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (!(checkSelfPermission( android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)) { if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.ACCESS_FINE_LOCATION)) { // Show an expanation to the user *asynchronously* -- don't block this thread waiting for the user's response! After the user // sees the explanation, try again to request the permission. } else { // No explanation needed, we can request the permission. ActivityCompat.requestPermissions(thisActivity, new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, MY_ACCESS_FINE_LOCATION); // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an app-defined int constant. The callback method gets the result of the request. } } } // LocationService locationService = LocationService.getLocationService( this ); Calendar today = FitHistory.getTodayCalendar(); fitHistory = new FitHistory(thisActivity, null); // Activity/Map // Wait to connecting // fitHistory.startTrackingDataTask( today ); // buildSensorListener(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_history, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_location_data) { Calendar today = FitHistory.getTodayCalendar(); if (fitHistory == null) fitHistory = new FitHistory(thisActivity, null); // Activity/Map fitHistory.startTrackingDataTask(today); return true; } else if (id == R.id.action_delete_data) { fitHistory.deleteData(); return true; } else if (id == R.id.action_update_data) { Intent intent = new Intent(HistoryActivity.this, FitUpdateActivity.class); HistoryActivity.this.startActivity(intent); } return super.onOptionsItemSelected(item); } /** * Initialize 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."); } private final int MY_ACCESS_FINE_LOCATION = 100; @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_ACCESS_FINE_LOCATION: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // contacts-related task you need to do. } else { // permission denied, boo! Disable the // functionality that depends on this permission. } return; } // other 'case' lines to check for other // permissions this app might request } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_RESOLVE_ERROR || requestCode == REQUEST_OAUTH) { mResolvingError = false; if (resultCode == RESULT_OK) { // Make sure the app is not already connected or attempting to connect if (!mClient.isConnecting() && !mClient.isConnected()) { mClient.connect(); } } } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putBoolean(STATE_RESOLVING_ERROR, mResolvingError); } }