Skip to content

Commit

Permalink
Grand stability upgrade #3 (#323)
Browse files Browse the repository at this point in the history
* Prevent multiple NotificationService instances

* Added null check for getView() prevents crash
Amount of steps to search read from prefs
Zoom in map animation only for first time

* Prevent  java.lang.ArrayIndexOutOfBoundsException

* Removed unnecessary call; there is an Activity in the back

* Sleep timer calculated properly now (missed * 1000) so actually did only wait milliseconds. Uff.
Added initial sleep timer to prevent UpdateRunnable calling getCatchablePokemon() directly after invoke (get shortly invoked and shuts down then when resuming from Settings for example) -- using this initial sleep and proper calculated timing we might fix the EventBus issues as well.

* - Map suggestion remembers that it was closed. So it doesn't show up again after restart.
- After returning from SettingsActivity the map does not zoom like crazy. It just returns to the last known location (that why its static now)
- Initial search after login still zooms in. Now the initial search uses the  step range configured in the settings!
  • Loading branch information
kyr0 authored and rohanjain1 committed Jul 29, 2016
1 parent 0b8ad2c commit f15c7a0
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ public interface PokemapAppPreferences {
Set<PokemonIdOuterClass.PokemonId> getShowablePokemonIDs();

void setShowablePokemonIDs(Set<PokemonIdOuterClass.PokemonId> pokemonIDs);

void setShowMapSuggestion(boolean showMapSuggestion);

boolean getShowMapSuggestion();
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public final class PokemapSharedPreferences implements PokemapAppPreferences {
private static final String SERVICE_REFRESH_KEY = "service_refresh_rate";
private static final String POKEMONS_TO_SHOW = "pokemons_to_show";
private static final String STEPS = "search_steps";
private static final String SHOW_MAP_SUGGESTION = "show_map_suggestion";

private static final String INFO_TOKEN = "token=";
private static final String INFO_REFRESH = "refresh=";
Expand Down Expand Up @@ -175,7 +176,7 @@ public boolean getShowGyms() {

@Override
public boolean getShowLuredPokemon() {
return sharedPreferences.getBoolean(SHOW_LURED, false);
return sharedPreferences.getBoolean(SHOW_LURED, true);
}

public Set<PokemonIdOuterClass.PokemonId> getShowablePokemonIDs() {
Expand Down Expand Up @@ -204,6 +205,16 @@ public void setShowablePokemonIDs(Set<PokemonIdOuterClass.PokemonId> ids) {
sharedPreferences.edit().putStringSet(POKEMONS_TO_SHOW, showablePokemonStringIDs).apply();
}

@Override
public void setShowMapSuggestion(boolean showMapSuggestion) {
sharedPreferences.edit().putBoolean(SHOW_MAP_SUGGESTION, showMapSuggestion).apply();
}

@Override
public boolean getShowMapSuggestion() {
return sharedPreferences.getBoolean(SHOW_MAP_SUGGESTION, true);
}

@Override
public void setServiceState(@NonNull boolean isEnabled) {
sharedPreferences.edit().putBoolean(SERVICE_KEY, isEnabled).apply();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private void loginPTC(final String username, final String password, NianticServi
@Override
public void onResponse(Call<NianticService.LoginResponse> call, Response<NianticService.LoginResponse> response) {
String location = response.headers().get("location");
if (location != null) {
if (location != null && location.split("ticket=").length > 0) {
String ticket = location.split("ticket=")[1];
requestToken(ticket, loginListener);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class PokemonNotificationService extends Service{
private static final int notificationId = 2423235;
private static final String ACTION_STOP_SELF = "com.omkarmoghe.pokemap.STOP_SERVICE";

public static boolean isRunning = false;

private UpdateRunnable updateRunnable;
private Thread workThread;
private LocationManager locationManager;
Expand Down Expand Up @@ -66,13 +68,14 @@ public void onCreate() {
locationManager = LocationManager.getInstance(this);
nianticManager = NianticManager.getInstance();

updateRunnable = new UpdateRunnable(preffs.getServiceRefreshRate());
updateRunnable = new UpdateRunnable(preffs.getServiceRefreshRate() * 1000);
workThread = new Thread(updateRunnable);

initBroadcastReciever();
workThread.start();
locationManager.onResume();

isRunning = true;
}

/**
Expand All @@ -90,6 +93,7 @@ public void onDestroy() {
updateRunnable.stop();
EventBus.getDefault().unregister(this);
unregisterReceiver(mBroadcastReciever);
isRunning = false;
}

@Override
Expand Down Expand Up @@ -175,22 +179,30 @@ public UpdateRunnable(int refreshRate){

@Override
public void run() {
while(isRunning){
try{
LatLng currentLocation = locationManager.getLocation();

if(currentLocation != null){
nianticManager.getCatchablePokemon(currentLocation.latitude,currentLocation.longitude,0);
}else {
locationManager = LocationManager.getInstance(PokemonNotificationService.this);
}

try {

// initial wait (fFor a reason! Do NOT remove because of cyclic sleep!)
Thread.sleep(refreshRate);

while (isRunning) {

LatLng currentLocation = locationManager.getLocation();

if (currentLocation != null){
nianticManager.getCatchablePokemon(currentLocation.latitude,currentLocation.longitude,0);
} else {
locationManager = LocationManager.getInstance(PokemonNotificationService.this);
}

// cyclic sleep
Thread.sleep(refreshRate);

}
} catch (InterruptedException | NullPointerException e) {
e.printStackTrace();
Log.e(TAG, "Failed updating. UpdateRunnable.run() raised: " + e.getMessage());
}
}
}

public void stop(){
Expand All @@ -205,4 +217,8 @@ public void onReceive(Context context, Intent intent) {
locationManager.onPause();
}
};

public static boolean isRunning() {
return isRunning;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}

private void startNotificationService(){

// check for if the service is already running
if (PokemonNotificationService.isRunning()) {
stopNotificationService();
}

Intent intent = new Intent(this, PokemonNotificationService.class);
startService(intent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ public class MapWrapperFragment extends Fragment implements OnMapReadyCallback,
private View mView;
private SupportMapFragment mSupportMapFragment;
private GoogleMap mGoogleMap;
private Location mLocation = null;
private static Location mLocation = null;
private PokemonMarkerExtended mSelectedMarker;
private Location currentCenter = new Location("0,0");
private Map<String, GymMarkerExtended> gymsList = new HashMap<>();
Map<Integer, String> gymTeamImageUrls = new HashMap<>();
String lurePokeStopImageUrl = "http://i.imgur.com/2BI3Cqv.png";
Expand Down Expand Up @@ -161,7 +160,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
public void onLocationChanged(Location location) {
if (mLocation == null) {
mLocation = location;
initMap();
initMap(true, true);
} else {
mLocation = location;
}
Expand Down Expand Up @@ -195,7 +194,7 @@ public void onLocationFetchFailed(@Nullable ConnectionResult connectionResult) {
@Override
public void onClick(View view) {
if (mLocation != null && mGoogleMap != null) {
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(mLocation.getLatitude(), mLocation.getLongitude()), 15));
} else {
showLocationFetchFailed();
Expand All @@ -206,42 +205,71 @@ public void onClick(View view) {
mView.findViewById(R.id.closeSuggestions).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mView.findViewById(R.id.layoutSuggestions).setVisibility(View.GONE);

hideMapSuggestion();
}
});

if (!mPref.getShowMapSuggestion()) {
hideMapSuggestion();
}

return mView;
}

private void initMap() {
pokeSnackbar = Snackbar.make(getView(), "", Snackbar.LENGTH_LONG);
if (mLocation != null && mGoogleMap != null) {
if (ContextCompat.checkSelfPermission(mView.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(mView.getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

new AlertDialog.Builder(getActivity())
.setTitle(getString(R.string.enable_location_permission_title))
.setMessage(getString(R.string.enable_location_permission_message))
.setPositiveButton(getString(R.string.button_ok), null)
.show();
return;
}
mGoogleMap.setMyLocationEnabled(true);
private void hideMapSuggestion() {

LatLng currentLatLngLocation = new LatLng(mLocation.getLatitude(), mLocation.getLongitude());
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
currentLatLngLocation, 15));
mPref.setShowMapSuggestion(false);

//Run the initial scan at the current location reusing the long click function
SearchInPosition sip = new SearchInPosition();
sip.setPosition(currentLatLngLocation);
sip.setSteps(1);
EventBus.getDefault().post(sip);
} else {
showLocationFetchFailed();
if (mView != null) {
mView.findViewById(R.id.layoutSuggestions).setVisibility(View.GONE);
}
}

private void initMap(boolean animateZoomIn, boolean searchInPlace) {

if (getView() != null) {
pokeSnackbar = Snackbar.make(getView(), "", Snackbar.LENGTH_LONG);
if (mLocation != null && mGoogleMap != null) {
if (ContextCompat.checkSelfPermission(mView.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(mView.getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

new AlertDialog.Builder(getActivity())
.setTitle(getString(R.string.enable_location_permission_title))
.setMessage(getString(R.string.enable_location_permission_message))
.setPositiveButton(getString(R.string.button_ok), null)
.show();
return;
}
mGoogleMap.setMyLocationEnabled(true);

LatLng currentLatLngLocation = new LatLng(mLocation.getLatitude(), mLocation.getLongitude());

if (animateZoomIn) {
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLatLngLocation, 15));
} else {
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLngLocation, 15));
}

if (searchInPlace) {
searchInPlace(currentLatLngLocation);
}

} else {
showLocationFetchFailed();
}
}
}

private void searchInPlace(LatLng latLngLocation) {

//Run the initial scan at the current location reusing the long click function
SearchInPosition sip = new SearchInPosition();
sip.setPosition(latLngLocation);
sip.setSteps(mPref.getSteps());
EventBus.getDefault().post(sip);
}

private void clearMarkers() {
if (mGoogleMap != null) {
if (markerList != null && !markerList.isEmpty()) {
Expand Down Expand Up @@ -722,6 +750,8 @@ public void onMapReady(GoogleMap googleMap) {
mGoogleMap.setOnMarkerClickListener(this);
//Disable for now coz is under FAB
settings.setMapToolbarEnabled(false);

initMap(false, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,4 @@ protected void onResume() {
recreate();
}
}

@Override
public void onBackPressed() {
super.onBackPressed();
NavUtils.navigateUpFromSameTask(this);
}
}

0 comments on commit f15c7a0

Please sign in to comment.