LocatrActivity.java
package com.bignerdranch.android.locatr;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
public class LocatrActivity extends SingleFragmentActivity {
private static final int REQUEST_ERROR = 0;
@Override
protected Fragment createFragment() {
return LocatrFragment.newInstance();
}
@Override
protected void onResume() {
super.onResume();
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int errorCode = apiAvailability.isGooglePlayServicesAvailable(this);
if(errorCode != ConnectionResult.SUCCESS) {
Dialog errorDialog = apiAvailability.getErrorDialog(this,errorCode,REQUEST_ERROR,new DialogInterface.OnCancelListener(){
@Override
public void onCancel(DialogInterface dialog) {
// Leave if services are unavailable.
finish();
}
});
errorDialog.show();
}
}
}
LocatrFragment.java
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
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;
/**
* Created by Lavoro on 27/05/2018.
*/
public class LocatrFragment extends Fragment {
private ImageView mImageView;
private GoogleApiClient mClient;
private static final String TAG = "LocatrFragment";
private static final String[] LOCATION_PERMISSIONS = new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
};
private static final int REQUEST_LOCATION_PERMISSIONS = 0;
public static LocatrFragment newInstance() {
return new LocatrFragment();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mClient = new GoogleApiClient.Builder(getActivity()).addApi(LocationServices.API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
getActivity().invalidateOptionsMenu();
}
@Override
public void onConnectionSuspended(int i) {
}
}).build();
}
@Override
public void onStart() {
super.onStart();
getActivity().invalidateOptionsMenu();
mClient.connect();
}
@Override
public void onStop() {
super.onStop();
mClient.disconnect();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_locatr,container,false);
mImageView = (ImageView) view.findViewById(R.id.image);
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_locatr,menu);
MenuItem searchItem = menu.findItem(R.id.action_locate);
searchItem.setEnabled(mClient.isConnected());
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_locate:
//Check if we have permission for the location
if(hasLocationPermission()) {
findImage();
} else {
requestPermissions(LOCATION_PERMISSIONS,REQUEST_LOCATION_PERMISSIONS);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode) {
case REQUEST_LOCATION_PERMISSIONS:
if(hasLocationPermission()) {
findImage();
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private boolean hasLocationPermission() {
int result = ContextCompat.checkSelfPermission(getActivity(),LOCATION_PERMISSIONS[0]);
return result == PackageManager.PERMISSION_GRANTED;
}
private void findImage() {
LocationRequest request = LocationRequest.create();
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
request.setNumUpdates(1);
request.setInterval(0);
LocationServices.FusedLocationApi
.requestLocationUpdates(mClient, request, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, "Got a fix: " + location);
}
});
}
}
SingleFragmentActivity
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
/**
* Created by Lavoro on 27/05/2018.
*/
public abstract class SingleFragmentActivity extends AppCompatActivity {
protected abstract Fragment createFragment();
@LayoutRes
private int layoutId() {
return R.layout.activity_fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
setContentView(layoutId());
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if(fragment == null) {
fragment = createFragment();
fm.beginTransaction().add(R.id.fragment_container,fragment).commit();
}
}
}
activity_fragment.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_container">
</FrameLayout>
fragment_locatr.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="@+id/image"
android:background="@android:color/white"
android:layout_centerInParent="true" />
</RelativeLayout>
Menu fragment_locatr.xml
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_locate"
android:title="@string/search"
android:enabled="false"
app:showAsAction="ifRoom"
android:icon="@android:drawable/ic_menu_compass"/>
</menu>