Java tutorial
/* * Copyright 2016 Kosrat D. Ahmed * * 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.kosratdahmad.location; import android.Manifest; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.content.res.Resources; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.ResultCallback; import com.google.android.gms.common.api.Status; import com.google.android.gms.location.ActivityRecognition; import com.google.android.gms.location.DetectedActivity; import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationServices; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Locale; public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener, ResultCallback<Status> { private final String TAG = MainActivity.class.getSimpleName(); private TextView mTextLatitude; private TextView mTextLongitude; private TextView mTextLatitudeOne; private TextView mTextLongitudeOne; private TextView mTextCountry; private TextView mTextState; private TextView mTextCity; private Location mLastLocation; private GoogleApiClient mGoogleApiClient; private LocationRequest mLocationRequest; protected ActivityDetectionBroadcastReceiver mBroadcastReceiver; protected GoogleApiClient mGoogleApiClientActivity; private TextView mStatusText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API) .addConnectionCallbacks(this).addOnConnectionFailedListener(this).build(); mTextLatitude = (TextView) findViewById(R.id.txt_latitude); mTextLongitude = (TextView) findViewById(R.id.txt_longitude); mTextLatitudeOne = (TextView) findViewById(R.id.txt_latitude_one); mTextLongitudeOne = (TextView) findViewById(R.id.txt_longitude_one); mStatusText = (TextView) findViewById(R.id.detectedActivities); mBroadcastReceiver = new ActivityDetectionBroadcastReceiver(); mTextCountry = (TextView) findViewById(R.id.txt_country); mTextState = (TextView) findViewById(R.id.txt_state); mTextCity = (TextView) findViewById(R.id.txt_city); buildGoogleApiClient(); } public void requestActivityUpdatesButtonHandler(View view) { if (!mGoogleApiClientActivity.isConnected()) { Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show(); return; } ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleApiClientActivity, Constants.DETECTION_INTERVAL_IN_MILLISECONDS, getActivityDetectionPendingIntent()) .setResultCallback(this); } public void removeActivityUpdatesButtonHandler(View view) { if (!mGoogleApiClientActivity.isConnected()) { Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show(); return; } // Remove all activity updates for the PendingIntent that was used to request activity // updates. ActivityRecognition.ActivityRecognitionApi .removeActivityUpdates(mGoogleApiClientActivity, getActivityDetectionPendingIntent()) .setResultCallback(this); } private PendingIntent getActivityDetectionPendingIntent() { Intent intent = new Intent(this, DetectedActivitiesIntentService.class); // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling // requestActivityUpdates() and removeActivityUpdates(). return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } protected synchronized void buildGoogleApiClient() { mGoogleApiClientActivity = new GoogleApiClient.Builder(this).addConnectionCallbacks(this) .addOnConnectionFailedListener(this).addApi(ActivityRecognition.API).build(); } public void onResult(Status status) { if (status.isSuccess()) { Log.e(TAG, "Successfully added activity detection."); } else { Log.e(TAG, "Error adding or removing activity detection: " + status.getStatusMessage()); } } @Override protected void onStart() { super.onStart(); mGoogleApiClient.connect(); mGoogleApiClientActivity.connect(); } @Override protected void onStop() { if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } if (mGoogleApiClientActivity.isConnected()) { mGoogleApiClientActivity.disconnect(); } super.onStop(); } @Override protected void onResume() { super.onResume(); // Register the broadcast receiver that informs this activity of the DetectedActivity // object broadcast sent by the intent service. LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, new IntentFilter(Constants.BROADCAST_ACTION)); } @Override protected void onPause() { // Unregister the broadcast receiver that was registered during onResume(). LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver); super.onPause(); } @Override public void onConnected(@Nullable Bundle bundle) { mLocationRequest = LocationRequest.create(); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); mLocationRequest.setInterval(1000); // Here, thisActivity is the current activity if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) { Toast.makeText(MainActivity.this, "The app needs permission to access you location.", Toast.LENGTH_SHORT).show(); // 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(this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, 0); // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an // app-defined int constant. The callback method gets the // result of the request. } } else { LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); } // get location one time and location information. mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if (mLastLocation != null) { String latitude = getString(R.string.latitude) + " " + mLastLocation.getLatitude(); String longitude = getString(R.string.longitude) + " " + mLastLocation.getLongitude(); mTextLatitudeOne.setText(latitude); mTextLongitudeOne.setText(longitude); } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case 0: { // 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. LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); } else { Toast.makeText(MainActivity.this, "Permission is denied.", Toast.LENGTH_SHORT).show(); // 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 onConnectionSuspended(int i) { Log.i(TAG, "onConnectionSuspended: "); } @Override public void onLocationChanged(Location location) { Log.i(TAG, "onLocationChanged: " + location.toString()); String latitude = getString(R.string.latitude) + " " + location.getLatitude(); String longitude = getString(R.string.longitude) + " " + location.getLongitude(); mTextLatitude.setText(latitude); mTextLongitude.setText(longitude); Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses = null; try { addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); String cityName = getString(R.string.city) + " " + addresses.get(0).getLocality(); String stateName = getString(R.string.state) + " " + addresses.get(0).getAdminArea(); String countryName = getString(R.string.country) + " " + addresses.get(0).getCountryName(); mTextCity.setText(cityName); mTextState.setText(stateName); mTextCountry.setText(countryName); } catch (IOException e) { e.printStackTrace(); } } @Override public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { Log.i(TAG, "onConnectionFailed: "); } /** * Returns a human readable String corresponding to a detected activity type. */ public String getActivityString(int detectedActivityType) { Resources resources = this.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); } } /** * Receiver for intents sent by DetectedActivitiesIntentService via a sendBroadcast(). * Receives a list of one or more DetectedActivity objects associated with the current state of * the device. */ public class ActivityDetectionBroadcastReceiver extends BroadcastReceiver { protected static final String TAG = "receiver"; @Override public void onReceive(Context context, Intent intent) { ArrayList<DetectedActivity> updatedActivities = intent .getParcelableArrayListExtra(Constants.ACTIVITY_EXTRA); String strStatus = ""; for (DetectedActivity thisActivity : updatedActivities) { strStatus += getActivityString(thisActivity.getType()) + thisActivity.getConfidence() + "%\n"; } mStatusText.setText(strStatus); } } }