package simonov.pk.myapplication;

import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

import java.util.Calendar;

public class MainActivity  extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {


    private final String LOG_TAG = "Location Services Lesson 2-1";
    private TextView locationView;
    private TextView latidudeView;
    private TextView longitudeView;
    private TextView timeView;
    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;
    private Location mLastLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        locationView = (TextView) findViewById(R.id.location_view);
//        latidudeView = (TextView) findViewById(R.id.latitude_view);
//        longitudeView = (TextView) findViewById(R.id.longitude_view);
//        timeView = (TextView) findViewById(R.id.time_view);
//
//        buildGoogleApiClient();
    }

    protected synchronized void buildGoogleApiClient() {
//        mGoogleApiClient = new GoogleApiClient.Builder(this)
//                .addApi(LocationServices.API)
//                .addConnectionCallbacks(this)
//                .addOnConnectionFailedListener(this)
//                .build();
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Connect the client.
//        mGoogleApiClient.connect();
    }

    @Override
    protected void onStop() {
        // Disconnecting the client invalidates it.
//        if (mGoogleApiClient.isConnected()){
//            mGoogleApiClient.disconnect();
//        }
        super.onStop();
    }

    @Override
    public void onConnected(Bundle bundle) {
//
//        mLocationRequest = LocationRequest.create();
//        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
//        mLocationRequest.setInterval(1000); // Update location every second
//
//        LocationServices.FusedLocationApi.requestLocationUpdates(
//                mGoogleApiClient,
//                mLocationRequest,
//                this);
    }

    @Override
    public void onConnectionSuspended(int i) {
//        Log.i(LOG_TAG, "GoogleApiClient connection has been suspend");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
//        Log.i(LOG_TAG, "GoogleApiClient connection has failed");
    }

    private void fillViews(Location location) {
        Log.i(LOG_TAG, location.toString());
        locationView.setText(location.toString());
        latidudeView.setText(String.valueOf(location.getLatitude()));
        longitudeView.setText(String.valueOf(location.getLongitude()));
        int hour = Calendar.getInstance().get(Calendar.HOUR);
        int minute = Calendar.getInstance().get(Calendar.MINUTE);
        int second = Calendar.getInstance().get(Calendar.SECOND);
        timeView.setText(hour + ":" + minute + ":" + second);
    }

    @Override
    public void onLocationChanged(Location location) {
//        fillViews(location);

//        locationView.setText(Double.toString(location.getLatitude()));
    }

    @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_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
@@ -218,16 +218,46 @@
 nuItem;%0A
+import android.widget.Button;%0A
 import a
@@ -975,24 +975,139 @@
 w timeView;%0A
+    private TextView mStatusText;%0A    private Button requestUpdatesButton;%0A    private Button removeUpdatesButton;%0A
     private 
@@ -1661,21 +1661,272 @@
 _view);%0A
-//%0A//
+%0A        mStatusText = (TextView) findViewById(R.id.detectedActivities);%0A        requestUpdatesButton = (Button) findViewById(R.id.request_activity_updates_button);%0A        removeUpdatesButton = (Button) findViewById(R.id.remove_activity_updates_button);%0A%0A
         
@@ -2009,26 +2009,24 @@
 iClient() %7B%0A
-//
         mGoo
@@ -2066,34 +2066,32 @@
 t.Builder(this)%0A
-//
                 
@@ -2120,18 +2120,16 @@
 es.API)%0A
-//
         
@@ -2162,26 +2162,24 @@
 backs(this)%0A
-//
             
@@ -2219,18 +2219,16 @@
 r(this)%0A
-//
         

update the MainActivity Class for new example