diff --git a/src/it/reyboz/bustorino/ActivityPrincipal.java b/src/it/reyboz/bustorino/ActivityPrincipal.java --- a/src/it/reyboz/bustorino/ActivityPrincipal.java +++ b/src/it/reyboz/bustorino/ActivityPrincipal.java @@ -79,6 +79,7 @@ @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); + Log.d(DEBUG_TAG, "onCreate, savedInstanceState is: "+savedInstanceState); setContentView(R.layout.activity_principal); final SharedPreferences theShPr = getMainSharedPreferences(); boolean showingArrivalsFromIntent = false; @@ -241,15 +242,17 @@ Log.d(DEBUG_TAG, "The default screen to open is: "+vl); if (showingArrivalsFromIntent){ //do nothing but exclude a case - } - else if (vl.equals("map")){ - requestMapFragment(false); - } else if(vl.equals("favorites")){ - checkAndShowFavoritesFragment(getSupportFragmentManager(), false); - } else if(vl.equals("lines")){ - showLinesFragment(getSupportFragmentManager(), false, null); - } else { - showMainFragment(false); + }else if (savedInstanceState==null) { + //we are not restarting the activity from nothing + if (vl.equals("map")) { + requestMapFragment(false); + } else if (vl.equals("favorites")) { + checkAndShowFavoritesFragment(getSupportFragmentManager(), false); + } else if (vl.equals("lines")) { + showLinesFragment(getSupportFragmentManager(), false, null); + } else { + showMainFragment(false); + } } onCreateComplete = true; @@ -323,6 +326,7 @@ drawerToggle.syncState(); } + @Override public void onConfigurationChanged(@NonNull Configuration newConfig) { super.onConfigurationChanged(newConfig); @@ -449,7 +453,7 @@ * @param fraMan the fragmentManager * @param arguments args for the fragment */ - private static void createShowMainFragment(FragmentManager fraMan, Bundle arguments, boolean addToBackStack){ + private static void createShowMainFragment(FragmentManager fraMan,@Nullable Bundle arguments, boolean addToBackStack){ FragmentTransaction ft = fraMan.beginTransaction() .replace(R.id.mainActContentFrame, MainScreenFragment.class, arguments, MainScreenFragment.FRAGMENT_TAG) .setReorderingAllowed(false) @@ -514,14 +518,12 @@ .commit(); } - private MainScreenFragment showMainFragment(boolean addToBackStack){ + private void showMainFragment(boolean addToBackStack){ FragmentManager fraMan = getSupportFragmentManager(); Fragment fragment = fraMan.findFragmentByTag(MainScreenFragment.FRAGMENT_TAG); final MainScreenFragment mainScreenFragment; if (fragment==null | !(fragment instanceof MainScreenFragment)){ - mainScreenFragment = MainScreenFragment.newInstance(); - //mainScreenFragment = createAndShowMainFragment(); - showMainFragment(fraMan, mainScreenFragment, addToBackStack); + createShowMainFragment(fraMan, null, addToBackStack); } else if(!fragment.isVisible()){ @@ -532,7 +534,7 @@ } else{ mainScreenFragment = (MainScreenFragment) fragment; } - return mainScreenFragment; + //return mainScreenFragment; } @Nullable private MainScreenFragment getMainFragmentIfVisible(){ @@ -565,9 +567,9 @@ @Override public void readyGUIfor(FragmentKind fragmentType) { - MainScreenFragment probableFragment = getMainFragmentIfVisible(); - if (probableFragment!=null){ - probableFragment.readyGUIfor(fragmentType); + MainScreenFragment mainFragmentIfVisible = getMainFragmentIfVisible(); + if (mainFragmentIfVisible!=null){ + mainFragmentIfVisible.readyGUIfor(fragmentType); } int titleResId; switch (fragmentType){ @@ -622,14 +624,12 @@ showMainFragment(fraMan, probableFragment, true); probableFragment.requestArrivalsForStopID(ID); } else { - //createAndShowMainFragment // we have no fragment final Bundle args = new Bundle(); args.putString(MainScreenFragment.PENDING_STOP_SEARCH, ID); //if onCreate is complete, then we are not asking for the first showing fragment boolean addtobackstack = onCreateComplete; createShowMainFragment(fraMan, args ,addtobackstack); - //probableFragment = createAndShowMainFragment(); } } else { //the MainScreeFragment is shown, nothing to do @@ -655,6 +655,7 @@ } } + @Override public void showMapCenteredOnStop(Stop stop) { createAndShowMapFragment(stop, true); diff --git a/src/it/reyboz/bustorino/adapters/PalinaAdapter.java b/src/it/reyboz/bustorino/adapters/PalinaAdapter.java --- a/src/it/reyboz/bustorino/adapters/PalinaAdapter.java +++ b/src/it/reyboz/bustorino/adapters/PalinaAdapter.java @@ -215,6 +215,7 @@ } List passaggi = route.passaggi; + //TODO: Sort the passaggi with realtime first if source is GTTJSONFetcher if(passaggi.size() == 0) { vh.rowRouteTimetable.setText(R.string.no_passages); diff --git a/src/it/reyboz/bustorino/fragments/ArrivalsFragment.java b/src/it/reyboz/bustorino/fragments/ArrivalsFragment.java --- a/src/it/reyboz/bustorino/fragments/ArrivalsFragment.java +++ b/src/it/reyboz/bustorino/fragments/ArrivalsFragment.java @@ -375,6 +375,7 @@ count++; } return count < 200; + } protected boolean adjustFetchersToSource(){ if (lastUpdatedPalina == null) return false; diff --git a/src/it/reyboz/bustorino/fragments/LinesFragment.kt b/src/it/reyboz/bustorino/fragments/LinesFragment.kt --- a/src/it/reyboz/bustorino/fragments/LinesFragment.kt +++ b/src/it/reyboz/bustorino/fragments/LinesFragment.kt @@ -58,8 +58,7 @@ private lateinit var patternsSpinner: Spinner private lateinit var currentRoutes: List - private lateinit var currentPatterns: List - private lateinit var currentPatternStops: List + private lateinit var selectedPatterns: List private lateinit var routeDescriptionTextView: TextView private lateinit var stopsRecyclerView: RecyclerView @@ -103,6 +102,7 @@ override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) + Log.d(DEBUG_TAG, "saveInstanceState bundle: $outState") } @@ -113,6 +113,7 @@ linesSpinner = rootView.findViewById(R.id.linesSpinner) patternsSpinner = rootView.findViewById(R.id.patternsSpinner) + routeDescriptionTextView = rootView.findViewById(R.id.routeDescriptionTextView) stopsRecyclerView = rootView.findViewById(R.id.patternStopsRecyclerView) @@ -134,20 +135,23 @@ viewModel.patternsWithStopsByRouteLiveData.observe(viewLifecycleOwner){ patterns -> run { - currentPatterns = patterns.sortedBy { p-> p.pattern.code } + selectedPatterns = patterns.sortedBy { p-> p.pattern.code } //patterns. //sortedBy {-1*it.stopsIndices.size}// "${p.pattern.directionId} - ${p.pattern.headsign}" } patternsAdapter?.let { it.clear() - it.addAll(currentPatterns.map { p->"${p.pattern.directionId} - ${p.pattern.headsign}" }) + it.addAll(selectedPatterns.map { p->"${p.pattern.directionId} - ${p.pattern.headsign}" }) it.notifyDataSetChanged() } + viewModel.selectedPatternLiveData.value?.let { + setSelectedPattern(it) + } val pos = patternsSpinner.selectedItemPosition //might be possible that the selectedItem is different (larger than list size) - if(pos!= INVALID_POSITION && pos >= 0 && (pos < currentPatterns.size)){ - val p = currentPatterns[pos] + if(pos!= INVALID_POSITION && pos >= 0 && (pos < selectedPatterns.size)){ + val p = selectedPatterns[pos] Log.d(DEBUG_TAG, "Setting patterns with pos $pos and p gtfsID ${p.pattern.code}") - setPatternAndReqStops(currentPatterns[pos]) + setPatternAndReqStops(selectedPatterns[pos]) } } @@ -190,9 +194,10 @@ patternsSpinner.onItemSelectedListener = object : OnItemSelectedListener{ override fun onItemSelected(p0: AdapterView<*>?, p1: View?, position: Int, p3: Long) { - val patternWithStops = currentPatterns.get(position) + val patternWithStops = selectedPatterns.get(position) // setPatternAndReqStops(patternWithStops) + //viewModel.currentPositionInPatterns.value = position } @@ -232,6 +237,22 @@ return null } + private fun setSelectedPattern(patternWs: MatoPatternWithStops){ + Log.d(DEBUG_TAG, "Finding pattern to show: ${patternWs.pattern.code}") + var pos = -2 + val code = patternWs.pattern.code.trim() + for(k in selectedPatterns.indices){ + if(selectedPatterns[k].pattern.code.trim() == code){ + pos = k + break + } + } + Log.d(DEBUG_TAG, "Found pattern $code in position: $pos") + if(pos>=0){ + patternsSpinner.setSelection(pos) + } + } + private fun setRoutes(routes: List){ Log.d(DEBUG_TAG, "Resetting routes") currentRoutes = routes.sortedWith(linesComparator) @@ -239,10 +260,11 @@ if (linesAdapter!=null){ var selGtfsRoute = viewModel.getRouteIDQueried() - var selPatternIndex = 0 + var selRouteIdx = 0 if(selGtfsRoute == null){ selGtfsRoute ="" } + Log.d(DEBUG_TAG, "Setting routes, selected route gtfsID: $selGtfsRoute") val adapter = linesAdapter!! if (adapter.isEmpty) { @@ -254,14 +276,14 @@ } adapter.addAll(currentRoutes.map { r -> r.shortName }) adapter.notifyDataSetChanged() - for(j in 0 until currentRoutes.size){ + for(j in currentRoutes.indices){ val route = currentRoutes[j] if (route.gtfsId == selGtfsRoute) { - selPatternIndex = j + selRouteIdx = j Log.d(DEBUG_TAG, "Route $selGtfsRoute has index $j") } } - linesSpinner.setSelection(selPatternIndex) + linesSpinner.setSelection(selRouteIdx) // } /* @@ -273,7 +295,8 @@ private fun setCurrentStops(stops: List){ - val orderBy = currentPatternStops.withIndex().associate{it.value.stopGtfsId to it.index} + Log.d(DEBUG_TAG, "Setting stops from: "+viewModel.currentPatternStops.value) + val orderBy = viewModel.currentPatternStops.value!!.withIndex().associate{it.value.stopGtfsId to it.index} val stopsSorted = stops.sortedBy { s -> orderBy[s.gtfsID] } val numStops = stopsSorted.size Log.d(DEBUG_TAG, "RecyclerView adapter is: ${stopsRecyclerView.adapter}") @@ -302,12 +325,14 @@ } + } private fun setPatternAndReqStops(patternWithStops: MatoPatternWithStops){ Log.d(DEBUG_TAG, "Requesting stops for pattern ${patternWithStops.pattern.code}") - currentPatternStops = patternWithStops.stopsIndices.sortedBy { i-> i.order } - + //currentPatternStops = patternWithStops.stopsIndices.sortedBy { i-> i.order } + viewModel.currentPatternStops.value = patternWithStops.stopsIndices.sortedBy { i-> i.order } + viewModel.selectedPatternLiveData.value = patternWithStops viewModel.requestStopsForPatternWithStops(patternWithStops) } diff --git a/src/it/reyboz/bustorino/fragments/LinesViewModel.kt b/src/it/reyboz/bustorino/fragments/LinesViewModel.kt --- a/src/it/reyboz/bustorino/fragments/LinesViewModel.kt +++ b/src/it/reyboz/bustorino/fragments/LinesViewModel.kt @@ -10,6 +10,7 @@ import it.reyboz.bustorino.data.gtfs.GtfsDatabase import it.reyboz.bustorino.data.gtfs.GtfsRoute import it.reyboz.bustorino.data.gtfs.MatoPatternWithStops +import it.reyboz.bustorino.data.gtfs.PatternStop import java.util.concurrent.Executors class LinesViewModel(application: Application) : AndroidViewModel(application) { @@ -21,6 +22,9 @@ private val routeIDToSearch = MutableLiveData() private var lastShownPatternStops = ArrayList() + val currentPatternStops = MutableLiveData>() + val selectedPatternLiveData = MutableLiveData() + val stopsForPatternLiveData = MutableLiveData>() private val executor = Executors.newFixedThreadPool(2) diff --git a/src/it/reyboz/bustorino/fragments/MainScreenFragment.java b/src/it/reyboz/bustorino/fragments/MainScreenFragment.java --- a/src/it/reyboz/bustorino/fragments/MainScreenFragment.java +++ b/src/it/reyboz/bustorino/fragments/MainScreenFragment.java @@ -34,6 +34,7 @@ import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; +import android.widget.FrameLayout; import android.widget.ImageButton; import android.widget.ProgressBar; import android.widget.TextView; @@ -57,7 +58,6 @@ import static it.reyboz.bustorino.backend.utils.getBusStopIDFromUri; import static it.reyboz.bustorino.util.Permissions.LOCATION_PERMISSIONS; -import static it.reyboz.bustorino.util.Permissions.LOCATION_PERMISSION_GIVEN; /** @@ -88,8 +88,9 @@ private Button hideHintButton; private MenuItem actionHelpMenuItem; private FloatingActionButton floatingActionButton; + private FrameLayout resultFrameLayout; - private boolean setupOnResume = true; + private boolean setupOnStart = true; private boolean suppressArrivalsReload = false; private boolean instanceStateSaved = false; //private Snackbar snackbar; @@ -159,6 +160,38 @@ boolean pendingNearbyStopsRequest = false; boolean locationPermissionGranted, locationPermissionAsked = false; AppLocationManager locationManager; + private final ActivityResultLauncher requestPermissionLauncher = + registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback>() { + @Override + public void onActivityResult(Map result) { + if(result==null || result.get(Manifest.permission.ACCESS_COARSE_LOCATION) == null + ||result.get(Manifest.permission.ACCESS_FINE_LOCATION) == null) return; + + if(result.get(Manifest.permission.ACCESS_COARSE_LOCATION) == null || + result.get(Manifest.permission.ACCESS_FINE_LOCATION) == null) + return; + boolean resCoarse = result.get(Manifest.permission.ACCESS_COARSE_LOCATION); + boolean resFine = result.get(Manifest.permission.ACCESS_FINE_LOCATION); + Log.d(DEBUG_TAG, "Permissions for location are: "+result); + 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); + } + // show nearby fragment + //showNearbyStopsFragment(); + Log.d(DEBUG_TAG, "We have location permission"); + if(pendingNearbyStopsRequest){ + showNearbyFragmentIfNeeded(cr); + pendingNearbyStopsRequest = false; + } + } + if(pendingNearbyStopsRequest) pendingNearbyStopsRequest=false; + } + }); private final LocationCriteria cr = new LocationCriteria(2000, 10000); @@ -172,11 +205,12 @@ @Override public void onLocationStatusChanged(int status) { - if(status == AppLocationManager.LOCATION_GPS_AVAILABLE && !isNearbyFragmentShown()){ + if(status == AppLocationManager.LOCATION_GPS_AVAILABLE && !isNearbyFragmentShown() && checkLocationPermission()){ //request Stops - pendingNearbyStopsRequest = false; - if (getContext()!= null) - mainHandler.post(new NearbyStopsRequester(getContext(), cr)); + //pendingNearbyStopsRequest = false; + if (getContext()!= null && !isNearbyFragmentShown()) + //mainHandler.post(new NearbyStopsRequester(getContext(), cr)); + showNearbyFragmentIfNeeded(cr); } } @@ -194,8 +228,13 @@ public void onLocationProviderAvailable() { //Log.w(DEBUG_TAG, "pendingNearbyStopRequest: "+pendingNearbyStopsRequest); if(!isNearbyFragmentShown() && getContext()!=null){ - pendingNearbyStopsRequest = false; - mainHandler.post(new NearbyStopsRequester(getContext(), cr)); + // we should have the location permission + if(!checkLocationPermission()) + Log.e(DEBUG_TAG, "Asking to show nearbystopfragment when " + + "we have no location permission"); + pendingNearbyStopsRequest = true; + //mainHandler.post(new NearbyStopsRequester(getContext(), cr)); + showNearbyFragmentIfNeeded(cr); } } @@ -204,28 +243,7 @@ } }; - private final ActivityResultLauncher requestPermissionLauncher = - registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback>() { - @Override - public void onActivityResult(Map 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) == null || - result.get(Manifest.permission.ACCESS_FINE_LOCATION) == null) - 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); - } - } - } - }); //// ACTIVITY ATTACHED (LISTENER /// @@ -240,12 +258,7 @@ public static MainScreenFragment newInstance() { - MainScreenFragment fragment = new MainScreenFragment(); - Bundle args = new Bundle(); - //args.putString(ARG_PARAM1, param1); - //args.putString(ARG_PARAM2, param2); - fragment.setArguments(args); - return fragment; + return new MainScreenFragment(); } @Override @@ -272,6 +285,7 @@ hideHintButton = root.findViewById(R.id.hideHintButton); swipeRefreshLayout = root.findViewById(R.id.listRefreshLayout); floatingActionButton = root.findViewById(R.id.floatingActionButton); + resultFrameLayout = root.findViewById(R.id.resultFrame); busStopSearchByIDEditText.setSelectAllOnFocus(true); busStopSearchByIDEditText .setOnEditorActionListener((v, actionId, event) -> { @@ -315,6 +329,7 @@ setSearchModeBusStopID(); + cr.setAccuracy(Criteria.ACCURACY_FINE); cr.setAltitudeRequired(false); cr.setBearingRequired(false); @@ -334,15 +349,17 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); Log.d(DEBUG_TAG, "onViewCreated, SwipeRefreshLayout visible: "+(swipeRefreshLayout.getVisibility()==View.VISIBLE)); - Log.d(DEBUG_TAG, "Setup on attached: "+ setupOnResume); + Log.d(DEBUG_TAG, "Saved instance state is: "+savedInstanceState); //Restore instance state - if (savedInstanceState!=null){ + /*if (savedInstanceState!=null){ Fragment fragment = getChildFragmentManager().getFragment(savedInstanceState, SAVED_FRAGMENT); if (fragment!=null){ getChildFragmentManager().beginTransaction().add(R.id.resultFrame, fragment).commit(); - setupOnResume = false; + setupOnStart = false; } } + + */ if (getChildFragmentManager().findFragmentById(R.id.resultFrame)!= null){ swipeRefreshLayout.setVisibility(View.VISIBLE); } @@ -353,10 +370,11 @@ @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); + Log.d(DEBUG_TAG, "Saving instance state"); Fragment fragment = getChildFragmentManager().findFragmentById(R.id.resultFrame); if (fragment!=null) getChildFragmentManager().putFragment(outState, SAVED_FRAGMENT, fragment); - fragmentHelper.setBlockAllActivities(true); + if (fragmentHelper!=null) fragmentHelper.setBlockAllActivities(true); instanceStateSaved = true; } @@ -393,7 +411,7 @@ public void onAttach(@NonNull Context context) { super.onAttach(context); - Log.d(DEBUG_TAG, "OnAttach called, setupOnAttach: "+ setupOnResume); + Log.d(DEBUG_TAG, "OnAttach called, setupOnAttach: "+ setupOnStart); mainHandler = new Handler(); if (context instanceof CommonFragmentListener) { mListener = (CommonFragmentListener) context; @@ -410,12 +428,36 @@ // setupOnAttached = true; } + @Override + public void onStart() { + super.onStart(); + Log.d(DEBUG_TAG, "onStart called, setupOnStart: "+setupOnStart); + if (setupOnStart) { + if (pendingStopID==null){ + //We want the nearby bus stops! + //mainHandler.post(new NearbyStopsRequester(getContext(), cr)); + Log.d(DEBUG_TAG, "Showing nearby stops"); + if(!checkLocationPermission()){ + requestLocationPermission(); + pendingNearbyStopsRequest = true; + } + else { + showNearbyFragmentIfNeeded(cr); + } + } + else{ + ///TODO: if there is a stop displayed, we need to hold the update + } + + setupOnStart = false; + } + } @Override public void onResume() { final Context con = getContext(); - Log.w(DEBUG_TAG, "OnResume called"); + Log.w(DEBUG_TAG, "OnResume called, setupOnStart: "+ setupOnStart); if (con != null) { if(locationManager==null) locationManager = AppLocationManager.getInstance(con); @@ -424,14 +466,7 @@ 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); - } + } //don't request permission } else { Log.w(DEBUG_TAG, "Context is null at onResume"); @@ -450,16 +485,7 @@ } suppressArrivalsReload = false; } - if (setupOnResume) { - if (pendingStopID==null) - //We want the nearby bus stops! - mainHandler.post(new NearbyStopsRequester(getContext(), cr)); - else{ - ///TODO: if there is a stop displayed, we need to hold the update - } - setupOnResume = false; - } if(pendingStopID!=null){ Log.d(DEBUG_TAG, "Re-requesting arrivals for pending stop "+pendingStopID); @@ -482,6 +508,8 @@ } + + /* GUI METHODS */ @@ -635,13 +663,14 @@ //actionHelpMenuItem.setVisible(false); } - void showNearbyStopsFragment(){ + private void actuallyShowNearbyStopsFragment(){ swipeRefreshLayout.setVisibility(View.VISIBLE); final Fragment existingFrag = fragMan.findFragmentById(R.id.resultFrame); - NearbyStopsFragment fragment; + // fragment; if (!(existingFrag instanceof NearbyStopsFragment)){ + Log.d(DEBUG_TAG, "actually showing Nearby Stops Fragment"); //there is no fragment showing - fragment = NearbyStopsFragment.newInstance(NearbyStopsFragment.TYPE_STOPS); + final NearbyStopsFragment fragment = NearbyStopsFragment.newInstance(NearbyStopsFragment.TYPE_STOPS); FragmentTransaction ft = fragMan.beginTransaction(); @@ -667,12 +696,15 @@ @Override public void readyGUIfor(FragmentKind fragmentType) { - hideKeyboard(); //if we are getting results, already, stop waiting for nearbyStops - if (pendingNearbyStopsRequest && (fragmentType == FragmentKind.ARRIVALS || fragmentType == FragmentKind.STOPS)) { - locationManager.removeLocationRequestFor(requester); - pendingNearbyStopsRequest = false; + if (fragmentType == FragmentKind.ARRIVALS || fragmentType == FragmentKind.STOPS) { + hideKeyboard(); + + if (pendingNearbyStopsRequest) { + locationManager.removeLocationRequestFor(requester); + pendingNearbyStopsRequest = false; + } } if (fragmentType == null) Log.e("ActivityMain", "Problem with fragmentType"); @@ -739,6 +771,46 @@ Log.d(DEBUG_TAG, "Started search for arrivals of stop " + ID); } } + + private boolean checkLocationPermission(){ + final Context context = getContext(); + if(context==null) return false; + + final boolean isOldVersion = Build.VERSION.SDK_INT < Build.VERSION_CODES.M; + final boolean noPermission = ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && + ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED; + + return isOldVersion || !noPermission; + + } + private void requestLocationPermission(){ + requestPermissionLauncher.launch(LOCATION_PERMISSIONS); + } + + private void showNearbyFragmentIfNeeded(Criteria cr){ + if(isNearbyFragmentShown()) { + //nothing to do + Log.w(DEBUG_TAG, "launched nearby fragment request but we already are showing"); + return; + } + if(getContext()==null){ + Log.e(DEBUG_TAG, "Wanting to show nearby fragment but context is null"); + return; + } + + AppLocationManager appLocationManager = AppLocationManager.getInstance(getContext()); + final boolean haveProviders = appLocationManager.anyLocationProviderMatchesCriteria(cr); + if (haveProviders + && fragmentHelper.getLastSuccessfullySearchedBusStop() == null + && !fragMan.isDestroyed()) { + //Go ahead with the request + Log.d("mainActivity", "Recreating stop fragment"); + actuallyShowNearbyStopsFragment(); + pendingNearbyStopsRequest = false; + } else if(!haveProviders){ + Log.e(DEBUG_TAG, "NO PROVIDERS FOR POSITION"); + } + } /////////// LOCATION METHODS ////////// /* @@ -750,10 +822,10 @@ } */ + /* - /** * Run location requests separately and asynchronously - */ + class NearbyStopsRequester implements Runnable { Context appContext; Criteria cr; @@ -805,4 +877,6 @@ } } + */ + } \ No newline at end of file diff --git a/src/it/reyboz/bustorino/fragments/NearbyStopsFragment.java b/src/it/reyboz/bustorino/fragments/NearbyStopsFragment.java --- a/src/it/reyboz/bustorino/fragments/NearbyStopsFragment.java +++ b/src/it/reyboz/bustorino/fragments/NearbyStopsFragment.java @@ -582,7 +582,7 @@ public void onLocationChanged(Location location) { //set adapter float accuracy = location.getAccuracy(); - if(accuracy<60 && !dbUpdateRunning) { + if(accuracy<100 && !dbUpdateRunning) { distance = 20; final Bundle msgBundle = new Bundle(); msgBundle.putParcelable(BUNDLE_LOCATION,location);