Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/MainScreenFragment.java
package it.reyboz.bustorino.fragments; | package it.reyboz.bustorino.fragments; | ||||
import android.Manifest; | import android.Manifest; | ||||
import android.content.Context; | import android.content.Context; | ||||
import android.content.pm.PackageManager; | import android.content.pm.PackageManager; | ||||
import android.location.Criteria; | import android.location.Criteria; | ||||
import android.location.Location; | import android.location.Location; | ||||
import android.location.LocationListener; | |||||
import android.location.LocationManager; | |||||
import android.location.LocationProvider; | |||||
import android.os.Build; | import android.os.Build; | ||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import androidx.activity.result.ActivityResultCallback; | |||||
import androidx.activity.result.ActivityResultLauncher; | |||||
import androidx.activity.result.contract.ActivityResultContracts; | |||||
import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
import androidx.annotation.Nullable; | import androidx.annotation.Nullable; | ||||
import androidx.appcompat.widget.AppCompatImageButton; | import androidx.appcompat.widget.AppCompatImageButton; | ||||
import androidx.core.app.ActivityCompat; | import androidx.core.app.ActivityCompat; | ||||
import androidx.fragment.app.Fragment; | import androidx.fragment.app.Fragment; | ||||
import androidx.fragment.app.FragmentManager; | import androidx.fragment.app.FragmentManager; | ||||
import androidx.fragment.app.FragmentTransaction; | import androidx.fragment.app.FragmentTransaction; | ||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; | import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; | ||||
Show All 11 Lines | |||||
import android.widget.ImageButton; | import android.widget.ImageButton; | ||||
import android.widget.ProgressBar; | import android.widget.ProgressBar; | ||||
import android.widget.TextView; | import android.widget.TextView; | ||||
import android.widget.Toast; | import android.widget.Toast; | ||||
import com.google.android.material.floatingactionbutton.FloatingActionButton; | import com.google.android.material.floatingactionbutton.FloatingActionButton; | ||||
import com.google.zxing.integration.android.IntentIntegrator; | import com.google.zxing.integration.android.IntentIntegrator; | ||||
import java.util.Map; | |||||
import it.reyboz.bustorino.R; | import it.reyboz.bustorino.R; | ||||
import it.reyboz.bustorino.backend.ArrivalsFetcher; | import it.reyboz.bustorino.backend.ArrivalsFetcher; | ||||
import it.reyboz.bustorino.backend.FiveTAPIFetcher; | import it.reyboz.bustorino.backend.FiveTAPIFetcher; | ||||
import it.reyboz.bustorino.backend.FiveTScraperFetcher; | import it.reyboz.bustorino.backend.FiveTScraperFetcher; | ||||
import it.reyboz.bustorino.backend.FiveTStopsFetcher; | import it.reyboz.bustorino.backend.FiveTStopsFetcher; | ||||
import it.reyboz.bustorino.backend.GTTJSONFetcher; | import it.reyboz.bustorino.backend.GTTJSONFetcher; | ||||
import it.reyboz.bustorino.backend.GTTStopsFetcher; | import it.reyboz.bustorino.backend.GTTStopsFetcher; | ||||
import it.reyboz.bustorino.backend.Palina; | |||||
import it.reyboz.bustorino.backend.StopsFinderByName; | import it.reyboz.bustorino.backend.StopsFinderByName; | ||||
import it.reyboz.bustorino.middleware.AppLocationManager; | |||||
import it.reyboz.bustorino.middleware.AsyncDataDownload; | import it.reyboz.bustorino.middleware.AsyncDataDownload; | ||||
import it.reyboz.bustorino.util.LocationCriteria; | |||||
import it.reyboz.bustorino.util.Permissions; | import it.reyboz.bustorino.util.Permissions; | ||||
import static android.content.Context.LOCATION_SERVICE; | import static it.reyboz.bustorino.util.Permissions.LOCATION_PERMISSIONS; | ||||
import static it.reyboz.bustorino.util.Permissions.LOCATION_PERMISSION_GIVEN; | import static it.reyboz.bustorino.util.Permissions.LOCATION_PERMISSION_GIVEN; | ||||
/** | /** | ||||
* A simple {@link Fragment} subclass. | * A simple {@link Fragment} subclass. | ||||
* Use the {@link MainScreenFragment#newInstance} factory method to | * Use the {@link MainScreenFragment#newInstance} factory method to | ||||
* create an instance of this fragment. | * create an instance of this fragment. | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | private final Runnable refreshStop = new Runnable() { | ||||
new AsyncDataDownload(fragmentHelper, fragment.getCurrentFetchersAsArray(), getContext()).execute(stopName); | new AsyncDataDownload(fragmentHelper, fragment.getCurrentFetchersAsArray(), getContext()).execute(stopName); | ||||
} | } | ||||
} else //we create a new fragment, which is WRONG | } else //we create a new fragment, which is WRONG | ||||
new AsyncDataDownload(fragmentHelper, arrivalsFetchers, getContext()).execute(); | new AsyncDataDownload(fragmentHelper, arrivalsFetchers, getContext()).execute(); | ||||
} | } | ||||
}; | }; | ||||
/// LOCATION STUFF /// | |||||
boolean pendingNearbyStopsRequest = false; | |||||
boolean locationPermissionGranted, locationPermissionAsked = false; | |||||
AppLocationManager locationManager; | |||||
private final LocationCriteria cr = new LocationCriteria(2000, 10000); | |||||
//Location | |||||
private AppLocationManager.LocationRequester requester = new AppLocationManager.LocationRequester() { | |||||
@Override | |||||
public void onLocationChanged(Location loc) { | |||||
/// LOCATION STUFF /// | } | ||||
boolean pendingNearbyStopsRequest = false; | |||||
LocationManager locmgr; | @Override | ||||
public void onLocationStatusChanged(int status) { | |||||
if(status == AppLocationManager.LOCATION_GPS_AVAILABLE && !isNearbyFragmentShown()){ | |||||
//request Stops | |||||
pendingNearbyStopsRequest = false; | |||||
mainHandler.post(new NearbyStopsRequester(getContext(), cr)); | |||||
} | |||||
} | |||||
@Override | |||||
public long getLastUpdateTimeMillis() { | |||||
return 50; | |||||
} | |||||
@Override | |||||
public LocationCriteria getLocationCriteria() { | |||||
return cr; | |||||
} | |||||
@Override | |||||
public void onLocationProviderAvailable() { | |||||
//Log.w(DEBUG_TAG, "pendingNearbyStopRequest: "+pendingNearbyStopsRequest); | |||||
if(!isNearbyFragmentShown()){ | |||||
pendingNearbyStopsRequest = false; | |||||
mainHandler.post(new NearbyStopsRequester(getContext(), cr)); | |||||
} | |||||
} | |||||
@Override | |||||
public void onLocationDisabled() { | |||||
} | |||||
}; | |||||
private final ActivityResultLauncher<String[]> requestPermissionLauncher = | |||||
registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() { | |||||
@Override | |||||
public void onActivityResult(Map<String, Boolean> result) { | |||||
if(result==null || result.get(Manifest.permission.ACCESS_COARSE_LOCATION) == null | |||||
||result.get(Manifest.permission.ACCESS_FINE_LOCATION) ) return; | |||||
if(result.get(Manifest.permission.ACCESS_COARSE_LOCATION) && result.get(Manifest.permission.ACCESS_FINE_LOCATION)){ | |||||
locationPermissionGranted = true; | |||||
Log.w(DEBUG_TAG, "Starting position"); | |||||
if (mListener!= null && getContext()!=null){ | |||||
if (locationManager==null) | |||||
locationManager = AppLocationManager.getInstance(getContext()); | |||||
locationManager.addLocationRequestFor(requester); | |||||
} | |||||
} | |||||
} | |||||
}); | |||||
private final Criteria cr = new Criteria(); | |||||
//// ACTIVITY ATTACHED (LISTENER /// | //// ACTIVITY ATTACHED (LISTENER /// | ||||
private CommonFragmentListener mListener; | private CommonFragmentListener mListener; | ||||
private String pendingStopID = null; | private String pendingStopID = null; | ||||
public MainScreenFragment() { | public MainScreenFragment() { | ||||
// Required empty public constructor | // Required empty public constructor | ||||
▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||||
cr.setAccuracy(Criteria.ACCURACY_FINE); | cr.setAccuracy(Criteria.ACCURACY_FINE); | ||||
cr.setAltitudeRequired(false); | cr.setAltitudeRequired(false); | ||||
cr.setBearingRequired(false); | cr.setBearingRequired(false); | ||||
cr.setCostAllowed(true); | cr.setCostAllowed(true); | ||||
cr.setPowerRequirement(Criteria.NO_REQUIREMENT); | cr.setPowerRequirement(Criteria.NO_REQUIREMENT); | ||||
locmgr = (LocationManager) getContext().getSystemService(LOCATION_SERVICE); | locationManager = AppLocationManager.getInstance(getContext()); | ||||
Log.d(DEBUG_TAG, "OnCreateView, savedInstanceState null: "+(savedInstanceState==null)); | Log.d(DEBUG_TAG, "OnCreateView, savedInstanceState null: "+(savedInstanceState==null)); | ||||
return root; | return root; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | public void onAttach(@NonNull Context context) { | ||||
mListener = (CommonFragmentListener) context; | mListener = (CommonFragmentListener) context; | ||||
} else { | } else { | ||||
throw new RuntimeException(context.toString() | throw new RuntimeException(context.toString() | ||||
+ " must implement CommonFragmentListener"); | + " must implement CommonFragmentListener"); | ||||
} | } | ||||
if (setupOnAttached) { | if (setupOnAttached) { | ||||
if (pendingStopID==null) | if (pendingStopID==null) | ||||
//We want the nearby bus stops! | //We want the nearby bus stops! | ||||
mainHandler.post(new NearbyStopsRequester(getContext(), cr, locListener)); | mainHandler.post(new NearbyStopsRequester(context, cr)); | ||||
else{ | else{ | ||||
///TODO: if there is a stop displayed, we need to hold the update | ///TODO: if there is a stop displayed, we need to hold the update | ||||
} | } | ||||
//If there are no providers available, then, wait for them | |||||
setupOnAttached = false; | setupOnAttached = false; | ||||
} else { | |||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
public void onDetach() { | public void onDetach() { | ||||
super.onDetach(); | super.onDetach(); | ||||
mListener = null; | mListener = null; | ||||
// setupOnAttached = true; | // setupOnAttached = true; | ||||
} | } | ||||
@Override | @Override | ||||
public void onResume() { | public void onResume() { | ||||
final Context con = getContext(); | final Context con = getContext(); | ||||
if (con != null) | Log.w(DEBUG_TAG, "OnResume called"); | ||||
locmgr = (LocationManager) getContext().getSystemService(LOCATION_SERVICE); | if (con != null) { | ||||
if(locationManager==null) | |||||
locationManager = AppLocationManager.getInstance(con); | |||||
if(Permissions.locationPermissionGranted(con)){ | |||||
Log.d(DEBUG_TAG, "Location permission OK"); | |||||
if(!locationManager.isRequesterRegistered(requester)) | |||||
locationManager.addLocationRequestFor(requester); | |||||
} else if(shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)){ | |||||
//we have already asked for the location, and we should show an explanation in order | |||||
// to ask again (TODO) | |||||
//do nothing | |||||
} else{ | |||||
//request permission | |||||
requestPermissionLauncher.launch(Permissions.LOCATION_PERMISSIONS); | |||||
} | |||||
} | |||||
else { | else { | ||||
Log.w(DEBUG_TAG, "Context is null at onResume"); | Log.w(DEBUG_TAG, "Context is null at onResume"); | ||||
} | } | ||||
super.onResume(); | super.onResume(); | ||||
// if we have a pending stopID request, do it | // if we have a pending stopID request, do it | ||||
Log.d(DEBUG_TAG, "Pending stop ID for arrivals: "+pendingStopID); | Log.d(DEBUG_TAG, "Pending stop ID for arrivals: "+pendingStopID); | ||||
//this is the second time we are attaching this fragment | //this is the second time we are attaching this fragment | ||||
Log.d(DEBUG_TAG, "Waiting for new stop request: "+ suppressArrivalsReload); | Log.d(DEBUG_TAG, "Waiting for new stop request: "+ suppressArrivalsReload); | ||||
Show All 11 Lines | public void onResume() { | ||||
pendingStopID = null; | pendingStopID = null; | ||||
} | } | ||||
mListener.readyGUIfor(FragmentKind.MAIN_SCREEN_FRAGMENT); | mListener.readyGUIfor(FragmentKind.MAIN_SCREEN_FRAGMENT); | ||||
} | } | ||||
@Override | @Override | ||||
public void onPause() { | public void onPause() { | ||||
//mainHandler = null; | //mainHandler = null; | ||||
locmgr = null; | locationManager.removeLocationRequestFor(requester); | ||||
super.onPause(); | super.onPause(); | ||||
} | } | ||||
/* | /* | ||||
GUI METHODS | GUI METHODS | ||||
*/ | */ | ||||
/** | /** | ||||
* QR scan button clicked | * QR scan button clicked | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | public class MainScreenFragment extends BaseFragment implements FragmentListenerMain{ | ||||
} | } | ||||
@Override | @Override | ||||
public void enableRefreshLayout(boolean yes) { | public void enableRefreshLayout(boolean yes) { | ||||
swipeRefreshLayout.setEnabled(yes); | swipeRefreshLayout.setEnabled(yes); | ||||
} | } | ||||
////////////////////////////////////// GUI HELPERS ///////////////////////////////////////////// | ////////////////////////////////////// GUI HELPERS ///////////////////////////////////////////// | ||||
public void showKeyboard() { | public void showKeyboard() { | ||||
if (getActivity() == null) | if(getActivity() == null) return; | ||||
return; | |||||
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); | InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); | ||||
View view = searchMode == SEARCH_BY_ID ? busStopSearchByIDEditText : busStopSearchByNameEditText; | View view = searchMode == SEARCH_BY_ID ? busStopSearchByIDEditText : busStopSearchByNameEditText; | ||||
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); | imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); | ||||
} | } | ||||
private void setSearchModeBusStopID() { | private void setSearchModeBusStopID() { | ||||
searchMode = SEARCH_BY_ID; | searchMode = SEARCH_BY_ID; | ||||
busStopSearchByNameEditText.setVisibility(View.GONE); | busStopSearchByNameEditText.setVisibility(View.GONE); | ||||
busStopSearchByNameEditText.setText(""); | busStopSearchByNameEditText.setText(""); | ||||
busStopSearchByIDEditText.setVisibility(View.VISIBLE); | busStopSearchByIDEditText.setVisibility(View.VISIBLE); | ||||
floatingActionButton.setImageResource(R.drawable.alphabetical); | floatingActionButton.setImageResource(R.drawable.alphabetical); | ||||
} | } | ||||
private void setSearchModeBusStopName() { | private void setSearchModeBusStopName() { | ||||
searchMode = SEARCH_BY_NAME; | searchMode = SEARCH_BY_NAME; | ||||
busStopSearchByIDEditText.setVisibility(View.GONE); | busStopSearchByIDEditText.setVisibility(View.GONE); | ||||
busStopSearchByIDEditText.setText(""); | busStopSearchByIDEditText.setText(""); | ||||
busStopSearchByNameEditText.setVisibility(View.VISIBLE); | busStopSearchByNameEditText.setVisibility(View.VISIBLE); | ||||
floatingActionButton.setImageResource(R.drawable.numeric); | floatingActionButton.setImageResource(R.drawable.numeric); | ||||
} | } | ||||
protected boolean isNearbyFragmentShown(){ | |||||
Fragment fragment = getChildFragmentManager().findFragmentByTag(NearbyStopsFragment.FRAGMENT_TAG); | |||||
return (fragment!= null && fragment.isVisible()); | |||||
} | |||||
/** | /** | ||||
* Having that cursor at the left of the edit text makes me cancer. | * Having that cursor at the left of the edit text makes me cancer. | ||||
* | * | ||||
* @param busStopID bus stop ID | * @param busStopID bus stop ID | ||||
*/ | */ | ||||
private void setBusStopSearchByIDEditText(String busStopID) { | private void setBusStopSearchByIDEditText(String busStopID) { | ||||
busStopSearchByIDEditText.setText(busStopID); | busStopSearchByIDEditText.setText(busStopID); | ||||
Show All 32 Lines | public class MainScreenFragment extends BaseFragment implements FragmentListenerMain{ | ||||
} | } | ||||
private void prepareGUIForBusStops() { | private void prepareGUIForBusStops() { | ||||
swipeRefreshLayout.setEnabled(false); | swipeRefreshLayout.setEnabled(false); | ||||
swipeRefreshLayout.setVisibility(View.VISIBLE); | swipeRefreshLayout.setVisibility(View.VISIBLE); | ||||
//actionHelpMenuItem.setVisible(false); | //actionHelpMenuItem.setVisible(false); | ||||
} | } | ||||
void showNearbyStopsFragment(){ | |||||
swipeRefreshLayout.setVisibility(View.VISIBLE); | |||||
NearbyStopsFragment fragment = NearbyStopsFragment.newInstance(NearbyStopsFragment.TYPE_STOPS); | |||||
Fragment oldFrag = fragMan.findFragmentById(R.id.resultFrame); | |||||
FragmentTransaction ft = fragMan.beginTransaction(); | |||||
if (oldFrag != null) | |||||
ft.remove(oldFrag); | |||||
ft.add(R.id.resultFrame, fragment, NearbyStopsFragment.FRAGMENT_TAG); | |||||
ft.commit(); | |||||
} | |||||
@Override | @Override | ||||
public void showFloatingActionButton(boolean yes) { | public void showFloatingActionButton(boolean yes) { | ||||
mListener.showFloatingActionButton(yes); | mListener.showFloatingActionButton(yes); | ||||
} | } | ||||
/** | /** | ||||
* This provides a temporary fix to make the transition | * This provides a temporary fix to make the transition | ||||
* to a single asynctask go smoother | * to a single asynctask go smoother | ||||
* | * | ||||
* @param fragmentType the type of fragment created | * @param fragmentType the type of fragment created | ||||
*/ | */ | ||||
@Override | @Override | ||||
public void readyGUIfor(FragmentKind fragmentType) { | public void readyGUIfor(FragmentKind fragmentType) { | ||||
hideKeyboard(); | hideKeyboard(); | ||||
//if we are getting results, already, stop waiting for nearbyStops | //if we are getting results, already, stop waiting for nearbyStops | ||||
if (pendingNearbyStopsRequest && (fragmentType == FragmentKind.ARRIVALS || fragmentType == FragmentKind.STOPS)) { | if (pendingNearbyStopsRequest && (fragmentType == FragmentKind.ARRIVALS || fragmentType == FragmentKind.STOPS)) { | ||||
locmgr.removeUpdates(locListener); | locationManager.removeLocationRequestFor(requester); | ||||
pendingNearbyStopsRequest = false; | pendingNearbyStopsRequest = false; | ||||
} | } | ||||
if (fragmentType == null) Log.e("ActivityMain", "Problem with fragmentType"); | if (fragmentType == null) Log.e("ActivityMain", "Problem with fragmentType"); | ||||
else | else | ||||
switch (fragmentType) { | switch (fragmentType) { | ||||
case ARRIVALS: | case ARRIVALS: | ||||
prepareGUIForBusLines(); | prepareGUIForBusLines(); | ||||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | public void requestArrivalsForStopID(String ID) { | ||||
} | } | ||||
} | } | ||||
else { | else { | ||||
new AsyncDataDownload(fragmentHelper,arrivalsFetchers, getContext()).execute(ID); | new AsyncDataDownload(fragmentHelper,arrivalsFetchers, getContext()).execute(ID); | ||||
Log.d(DEBUG_TAG, "Started search for arrivals of stop " + ID); | Log.d(DEBUG_TAG, "Started search for arrivals of stop " + ID); | ||||
} | } | ||||
} | } | ||||
/////////// LOCATION METHODS ////////// | /////////// LOCATION METHODS ////////// | ||||
final LocationListener locListener = new LocationListener() { | |||||
@Override | |||||
public void onLocationChanged(Location location) { | |||||
Log.d(DEBUG_TAG, "Location changed"); | |||||
} | |||||
@Override | |||||
public void onStatusChanged(String provider, int status, Bundle extras) { | |||||
Log.d(DEBUG_TAG, "Location provider status: " + status); | |||||
if (status == LocationProvider.AVAILABLE) { | |||||
resolveStopRequest(provider); | |||||
} | |||||
} | |||||
@Override | /* | ||||
public void onProviderEnabled(String provider) { | private void startStopRequest(String provider) { | ||||
resolveStopRequest(provider); | |||||
} | |||||
@Override | |||||
public void onProviderDisabled(String provider) { | |||||
} | |||||
}; | |||||
private void resolveStopRequest(String provider) { | |||||
Log.d(DEBUG_TAG, "Provider " + provider + " got enabled"); | Log.d(DEBUG_TAG, "Provider " + provider + " got enabled"); | ||||
if (locmgr != null && mainHandler != null && pendingNearbyStopsRequest && locmgr.getProvider(provider).meetsCriteria(cr)) { | if (locmgr != null && mainHandler != null && pendingNearbyStopsRequest && locmgr.getProvider(provider).meetsCriteria(cr)) { | ||||
pendingNearbyStopsRequest = false; | |||||
mainHandler.post(new NearbyStopsRequester(getContext(), cr, locListener)); | |||||
} | } | ||||
} | } | ||||
*/ | |||||
/** | /** | ||||
* Run location requests separately and asynchronously | * Run location requests separately and asynchronously | ||||
*/ | */ | ||||
class NearbyStopsRequester implements Runnable { | class NearbyStopsRequester implements Runnable { | ||||
Context appContext; | Context appContext; | ||||
Criteria cr; | Criteria cr; | ||||
LocationListener listener; | |||||
public NearbyStopsRequester(Context appContext, Criteria criteria, LocationListener listener) { | public NearbyStopsRequester(Context appContext, Criteria criteria) { | ||||
this.appContext = appContext.getApplicationContext(); | this.appContext = appContext.getApplicationContext(); | ||||
this.cr = criteria; | this.cr = criteria; | ||||
this.listener = listener; | |||||
} | } | ||||
@Override | @Override | ||||
public void run() { | public void run() { | ||||
final boolean canRunPosition = Build.VERSION.SDK_INT < Build.VERSION_CODES.M || getOption(LOCATION_PERMISSION_GIVEN, false); | if(isNearbyFragmentShown()) { | ||||
//nothing to do | |||||
Log.w(DEBUG_TAG, "launched nearby fragment request but we already are showing"); | |||||
return; | |||||
} | |||||
final boolean isOldVersion = Build.VERSION.SDK_INT < Build.VERSION_CODES.M; | |||||
final boolean noPermission = ActivityCompat.checkSelfPermission(appContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && | final boolean noPermission = ActivityCompat.checkSelfPermission(appContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && | ||||
ActivityCompat.checkSelfPermission(appContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED; | ActivityCompat.checkSelfPermission(appContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED; | ||||
//if we don't have the permission, we have to ask for it, if we haven't | //if we don't have the permission, we have to ask for it, if we haven't | ||||
// asked too many times before | // asked too many times before | ||||
if (noPermission) { | if (noPermission) { | ||||
if (!canRunPosition) { | if (!isOldVersion) { | ||||
pendingNearbyStopsRequest = true; | pendingNearbyStopsRequest = true; | ||||
Permissions.assertLocationPermissions(appContext,getActivity()); | //Permissions.assertLocationPermissions(appContext,getActivity()); | ||||
requestPermissionLauncher.launch(LOCATION_PERMISSIONS); | |||||
Log.w(DEBUG_TAG, "Cannot get position: Asking permission, noPositionFromSys: " + noPermission); | Log.w(DEBUG_TAG, "Cannot get position: Asking permission, noPositionFromSys: " + noPermission); | ||||
return; | return; | ||||
} else { | } else { | ||||
Toast.makeText(appContext, "Asked for permission position too many times", Toast.LENGTH_LONG).show(); | Toast.makeText(appContext, "Asked for permission position too many times", Toast.LENGTH_LONG).show(); | ||||
} | } | ||||
} else setOption(LOCATION_PERMISSION_GIVEN, true); | } else setOption(LOCATION_PERMISSION_GIVEN, true); | ||||
LocationManager locManager = (LocationManager) appContext.getSystemService(LOCATION_SERVICE); | AppLocationManager appLocationManager = AppLocationManager.getInstance(appContext); | ||||
if (locManager == null) { | final boolean haveProviders = appLocationManager.anyLocationProviderMatchesCriteria(cr); | ||||
Log.e(DEBUG_TAG, "location manager is nihil, cannot create NearbyStopsFragment"); | if (haveProviders | ||||
return; | |||||
} | |||||
if (Permissions.anyLocationProviderMatchesCriteria(locManager, cr, true) | |||||
&& fragmentHelper.getLastSuccessfullySearchedBusStop() == null | && fragmentHelper.getLastSuccessfullySearchedBusStop() == null | ||||
&& !fragMan.isDestroyed()) { | && !fragMan.isDestroyed()) { | ||||
//Go ahead with the request | //Go ahead with the request | ||||
Log.d("mainActivity", "Recreating stop fragment"); | Log.d("mainActivity", "Recreating stop fragment"); | ||||
swipeRefreshLayout.setVisibility(View.VISIBLE); | showNearbyStopsFragment(); | ||||
NearbyStopsFragment fragment = NearbyStopsFragment.newInstance(NearbyStopsFragment.TYPE_STOPS); | |||||
Fragment oldFrag = fragMan.findFragmentById(R.id.resultFrame); | |||||
FragmentTransaction ft = fragMan.beginTransaction(); | |||||
if (oldFrag != null) | |||||
ft.remove(oldFrag); | |||||
ft.add(R.id.resultFrame, fragment, "nearbyStop_correct"); | |||||
ft.commit(); | |||||
//fragMan.executePendingTransactions(); | |||||
pendingNearbyStopsRequest = false; | pendingNearbyStopsRequest = false; | ||||
} else if (!Permissions.anyLocationProviderMatchesCriteria(locManager, cr, true)) { | } else if(!haveProviders){ | ||||
//Wait for the providers | Log.e(DEBUG_TAG, "NO PROVIDERS FOR POSITION"); | ||||
Log.d(DEBUG_TAG, "Queuing position request"); | |||||
pendingNearbyStopsRequest = true; | |||||
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 0.1f, listener); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
No newline at end of file | No newline at end of file |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register