Created
June 11, 2016 04:43
-
-
Save smoak/409732af62c6021c82aa21e4306b6953 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.reststopperpro.android.activity; | |
import android.Manifest; | |
import android.content.pm.PackageManager; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.ActivityCompat; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.api.GoogleApiClient; | |
import com.google.android.gms.location.LocationServices; | |
import com.reststopperpro.android.R; | |
public class MainActivity extends AppCompatActivity implements | |
ActivityCompat.OnRequestPermissionsResultCallback, | |
GoogleApiClient.ConnectionCallbacks, | |
GoogleApiClient.OnConnectionFailedListener { | |
private static final int LOCATION_PERMISSION_REQUEST = 100; | |
private GoogleApiClient googleApiClient; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
setUpGoogleApiClient(); | |
} | |
private void setUpGoogleApiClient() { | |
googleApiClient = new GoogleApiClient.Builder(this) | |
.enableAutoManage(this, this) | |
.addConnectionCallbacks(this) | |
.addApi(LocationServices.API) | |
.build(); | |
} | |
// the view has one button, this is its "onClick" | |
public void onNearbyClicked(View view) { | |
if (locationPermissionGranted()) { | |
showNearbyActivity(); | |
} else { | |
requestLocationPermissionForNearbyList(); | |
} | |
} | |
private void showNearbyActivity() { | |
// TODO: only show the next activity if we have a google api client | |
// if we don't do we wait and keep checking? | |
if (!googleApiClient.isConnected()) { | |
// TODO: what should we do? sleep and try again? | |
} else { | |
// go ahead and show the nearby list | |
} | |
} | |
private void requestLocationPermissionForNearbyList() { | |
ActivityCompat.requestPermissions(this, | |
new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, | |
LOCATION_PERMISSION_REQUEST); | |
} | |
private boolean locationPermissionGranted() { | |
return ActivityCompat.checkSelfPermission(this, | |
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED; | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, String permissions[], | |
int[] grantResults) { | |
switch (requestCode) { | |
case LOCATION_PERMISSION_REQUEST: | |
if (permissionGranted(grantResults)) { | |
showNearbyActivity(); | |
} | |
return; | |
} | |
} | |
private boolean permissionGranted(int[] grantResults) { | |
return grantResults.length > 0 && | |
grantResults[0] == PackageManager.PERMISSION_GRANTED; | |
} | |
@Override | |
public void onConnected(@Nullable Bundle bundle) { | |
// TODO: figure out what to do here | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
// TODO: figure out what to do here | |
} | |
@Override | |
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { | |
// TODO: figure out what to do here | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment