Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/MainScreenFragment.java
Show All 28 Lines | |||||
import android.view.LayoutInflater; | import android.view.LayoutInflater; | ||||
import android.view.MenuItem; | import android.view.MenuItem; | ||||
import android.view.View; | import android.view.View; | ||||
import android.view.ViewGroup; | import android.view.ViewGroup; | ||||
import android.view.inputmethod.EditorInfo; | import android.view.inputmethod.EditorInfo; | ||||
import android.view.inputmethod.InputMethodManager; | import android.view.inputmethod.InputMethodManager; | ||||
import android.widget.Button; | import android.widget.Button; | ||||
import android.widget.EditText; | import android.widget.EditText; | ||||
import android.widget.FrameLayout; | |||||
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 java.util.List; | import java.util.List; | ||||
import java.util.Map; | import java.util.Map; | ||||
import it.reyboz.bustorino.R; | import it.reyboz.bustorino.R; | ||||
import it.reyboz.bustorino.backend.*; | import it.reyboz.bustorino.backend.*; | ||||
import it.reyboz.bustorino.middleware.AppLocationManager; | import it.reyboz.bustorino.middleware.AppLocationManager; | ||||
import it.reyboz.bustorino.middleware.AsyncArrivalsSearcher; | import it.reyboz.bustorino.middleware.AsyncArrivalsSearcher; | ||||
import it.reyboz.bustorino.middleware.AsyncStopsSearcher; | import it.reyboz.bustorino.middleware.AsyncStopsSearcher; | ||||
import it.reyboz.bustorino.middleware.BarcodeScanContract; | import it.reyboz.bustorino.middleware.BarcodeScanContract; | ||||
import it.reyboz.bustorino.middleware.BarcodeScanOptions; | import it.reyboz.bustorino.middleware.BarcodeScanOptions; | ||||
import it.reyboz.bustorino.middleware.BarcodeScanUtils; | import it.reyboz.bustorino.middleware.BarcodeScanUtils; | ||||
import it.reyboz.bustorino.util.LocationCriteria; | import it.reyboz.bustorino.util.LocationCriteria; | ||||
import it.reyboz.bustorino.util.Permissions; | import it.reyboz.bustorino.util.Permissions; | ||||
import static it.reyboz.bustorino.backend.utils.getBusStopIDFromUri; | 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_PERMISSIONS; | ||||
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. | ||||
*/ | */ | ||||
public class MainScreenFragment extends ScreenBaseFragment implements FragmentListenerMain{ | public class MainScreenFragment extends ScreenBaseFragment implements FragmentListenerMain{ | ||||
Show All 14 Lines | public class MainScreenFragment extends ScreenBaseFragment implements FragmentListenerMain{ | ||||
private SwipeRefreshLayout swipeRefreshLayout; | private SwipeRefreshLayout swipeRefreshLayout; | ||||
private EditText busStopSearchByIDEditText; | private EditText busStopSearchByIDEditText; | ||||
private EditText busStopSearchByNameEditText; | private EditText busStopSearchByNameEditText; | ||||
private ProgressBar progressBar; | private ProgressBar progressBar; | ||||
private TextView howDoesItWorkTextView; | private TextView howDoesItWorkTextView; | ||||
private Button hideHintButton; | private Button hideHintButton; | ||||
private MenuItem actionHelpMenuItem; | private MenuItem actionHelpMenuItem; | ||||
private FloatingActionButton floatingActionButton; | private FloatingActionButton floatingActionButton; | ||||
private FrameLayout resultFrameLayout; | |||||
private boolean setupOnResume = true; | private boolean setupOnStart = true; | ||||
private boolean suppressArrivalsReload = false; | private boolean suppressArrivalsReload = false; | ||||
private boolean instanceStateSaved = false; | private boolean instanceStateSaved = false; | ||||
//private Snackbar snackbar; | //private Snackbar snackbar; | ||||
/* | /* | ||||
* Search mode | * Search mode | ||||
*/ | */ | ||||
private static final int SEARCH_BY_NAME = 0; | private static final int SEARCH_BY_NAME = 0; | ||||
private static final int SEARCH_BY_ID = 1; | private static final int SEARCH_BY_ID = 1; | ||||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | private final ActivityResultLauncher<BarcodeScanOptions> barcodeLauncher = registerForActivityResult(new BarcodeScanContract(), | ||||
} | } | ||||
}); | }); | ||||
/// LOCATION STUFF /// | /// LOCATION STUFF /// | ||||
boolean pendingNearbyStopsRequest = false; | boolean pendingNearbyStopsRequest = false; | ||||
boolean locationPermissionGranted, locationPermissionAsked = false; | boolean locationPermissionGranted, locationPermissionAsked = false; | ||||
AppLocationManager locationManager; | AppLocationManager locationManager; | ||||
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) == 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); | private final LocationCriteria cr = new LocationCriteria(2000, 10000); | ||||
//Location | //Location | ||||
private AppLocationManager.LocationRequester requester = new AppLocationManager.LocationRequester() { | private AppLocationManager.LocationRequester requester = new AppLocationManager.LocationRequester() { | ||||
@Override | @Override | ||||
public void onLocationChanged(Location loc) { | public void onLocationChanged(Location loc) { | ||||
} | } | ||||
@Override | @Override | ||||
public void onLocationStatusChanged(int status) { | public void onLocationStatusChanged(int status) { | ||||
if(status == AppLocationManager.LOCATION_GPS_AVAILABLE && !isNearbyFragmentShown()){ | if(status == AppLocationManager.LOCATION_GPS_AVAILABLE && !isNearbyFragmentShown() && checkLocationPermission()){ | ||||
//request Stops | //request Stops | ||||
pendingNearbyStopsRequest = false; | //pendingNearbyStopsRequest = false; | ||||
if (getContext()!= null) | if (getContext()!= null && !isNearbyFragmentShown()) | ||||
mainHandler.post(new NearbyStopsRequester(getContext(), cr)); | //mainHandler.post(new NearbyStopsRequester(getContext(), cr)); | ||||
showNearbyFragmentIfNeeded(cr); | |||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
public long getLastUpdateTimeMillis() { | public long getLastUpdateTimeMillis() { | ||||
return 50; | return 50; | ||||
} | } | ||||
@Override | @Override | ||||
public LocationCriteria getLocationCriteria() { | public LocationCriteria getLocationCriteria() { | ||||
return cr; | return cr; | ||||
} | } | ||||
@Override | @Override | ||||
public void onLocationProviderAvailable() { | public void onLocationProviderAvailable() { | ||||
//Log.w(DEBUG_TAG, "pendingNearbyStopRequest: "+pendingNearbyStopsRequest); | //Log.w(DEBUG_TAG, "pendingNearbyStopRequest: "+pendingNearbyStopsRequest); | ||||
if(!isNearbyFragmentShown() && getContext()!=null){ | if(!isNearbyFragmentShown() && getContext()!=null){ | ||||
pendingNearbyStopsRequest = false; | // we should have the location permission | ||||
mainHandler.post(new NearbyStopsRequester(getContext(), cr)); | 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); | |||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
public void onLocationDisabled() { | 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) == 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 /// | //// ACTIVITY ATTACHED (LISTENER /// | ||||
private CommonFragmentListener mListener; | private CommonFragmentListener mListener; | ||||
private String pendingStopID = null; | private String pendingStopID = null; | ||||
private CoordinatorLayout coordLayout; | private CoordinatorLayout coordLayout; | ||||
public MainScreenFragment() { | public MainScreenFragment() { | ||||
// Required empty public constructor | // Required empty public constructor | ||||
} | } | ||||
public static MainScreenFragment newInstance() { | public static MainScreenFragment newInstance() { | ||||
MainScreenFragment fragment = new MainScreenFragment(); | return new MainScreenFragment(); | ||||
Bundle args = new Bundle(); | |||||
//args.putString(ARG_PARAM1, param1); | |||||
//args.putString(ARG_PARAM2, param2); | |||||
fragment.setArguments(args); | |||||
return fragment; | |||||
} | } | ||||
@Override | @Override | ||||
public void onCreate(Bundle savedInstanceState) { | public void onCreate(Bundle savedInstanceState) { | ||||
super.onCreate(savedInstanceState); | super.onCreate(savedInstanceState); | ||||
if (getArguments() != null) { | if (getArguments() != null) { | ||||
//do nothing | //do nothing | ||||
Log.d(DEBUG_TAG, "ARGS ARE NOT NULL: "+getArguments()); | Log.d(DEBUG_TAG, "ARGS ARE NOT NULL: "+getArguments()); | ||||
Show All 10 Lines | public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||||
addToFavorites = root.findViewById(R.id.addToFavorites); | addToFavorites = root.findViewById(R.id.addToFavorites); | ||||
busStopSearchByIDEditText = root.findViewById(R.id.busStopSearchByIDEditText); | busStopSearchByIDEditText = root.findViewById(R.id.busStopSearchByIDEditText); | ||||
busStopSearchByNameEditText = root.findViewById(R.id.busStopSearchByNameEditText); | busStopSearchByNameEditText = root.findViewById(R.id.busStopSearchByNameEditText); | ||||
progressBar = root.findViewById(R.id.progressBar); | progressBar = root.findViewById(R.id.progressBar); | ||||
howDoesItWorkTextView = root.findViewById(R.id.howDoesItWorkTextView); | howDoesItWorkTextView = root.findViewById(R.id.howDoesItWorkTextView); | ||||
hideHintButton = root.findViewById(R.id.hideHintButton); | hideHintButton = root.findViewById(R.id.hideHintButton); | ||||
swipeRefreshLayout = root.findViewById(R.id.listRefreshLayout); | swipeRefreshLayout = root.findViewById(R.id.listRefreshLayout); | ||||
floatingActionButton = root.findViewById(R.id.floatingActionButton); | floatingActionButton = root.findViewById(R.id.floatingActionButton); | ||||
resultFrameLayout = root.findViewById(R.id.resultFrame); | |||||
busStopSearchByIDEditText.setSelectAllOnFocus(true); | busStopSearchByIDEditText.setSelectAllOnFocus(true); | ||||
busStopSearchByIDEditText | busStopSearchByIDEditText | ||||
.setOnEditorActionListener((v, actionId, event) -> { | .setOnEditorActionListener((v, actionId, event) -> { | ||||
// IME_ACTION_SEARCH alphabetical option | // IME_ACTION_SEARCH alphabetical option | ||||
if (actionId == EditorInfo.IME_ACTION_SEARCH) { | if (actionId == EditorInfo.IME_ACTION_SEARCH) { | ||||
onSearchClick(v); | onSearchClick(v); | ||||
return true; | return true; | ||||
} | } | ||||
Show All 27 Lines | public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||||
// Fragment stuff | // Fragment stuff | ||||
fragMan = getChildFragmentManager(); | fragMan = getChildFragmentManager(); | ||||
fragMan.addOnBackStackChangedListener(() -> Log.d("BusTO Main Fragment", "BACK STACK CHANGED")); | fragMan.addOnBackStackChangedListener(() -> Log.d("BusTO Main Fragment", "BACK STACK CHANGED")); | ||||
fragmentHelper = new FragmentHelper(this, getChildFragmentManager(), getContext(), R.id.resultFrame); | fragmentHelper = new FragmentHelper(this, getChildFragmentManager(), getContext(), R.id.resultFrame); | ||||
setSearchModeBusStopID(); | setSearchModeBusStopID(); | ||||
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); | ||||
locationManager = AppLocationManager.getInstance(getContext()); | 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; | ||||
} | } | ||||
@Override | @Override | ||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { | ||||
super.onViewCreated(view, savedInstanceState); | super.onViewCreated(view, savedInstanceState); | ||||
Log.d(DEBUG_TAG, "onViewCreated, SwipeRefreshLayout visible: "+(swipeRefreshLayout.getVisibility()==View.VISIBLE)); | 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 | //Restore instance state | ||||
if (savedInstanceState!=null){ | /*if (savedInstanceState!=null){ | ||||
Fragment fragment = getChildFragmentManager().getFragment(savedInstanceState, SAVED_FRAGMENT); | Fragment fragment = getChildFragmentManager().getFragment(savedInstanceState, SAVED_FRAGMENT); | ||||
if (fragment!=null){ | if (fragment!=null){ | ||||
getChildFragmentManager().beginTransaction().add(R.id.resultFrame, fragment).commit(); | getChildFragmentManager().beginTransaction().add(R.id.resultFrame, fragment).commit(); | ||||
setupOnResume = false; | setupOnStart = false; | ||||
} | } | ||||
} | } | ||||
*/ | |||||
if (getChildFragmentManager().findFragmentById(R.id.resultFrame)!= null){ | if (getChildFragmentManager().findFragmentById(R.id.resultFrame)!= null){ | ||||
swipeRefreshLayout.setVisibility(View.VISIBLE); | swipeRefreshLayout.setVisibility(View.VISIBLE); | ||||
} | } | ||||
instanceStateSaved = false; | instanceStateSaved = false; | ||||
} | } | ||||
@Override | @Override | ||||
public void onSaveInstanceState(@NonNull Bundle outState) { | public void onSaveInstanceState(@NonNull Bundle outState) { | ||||
super.onSaveInstanceState(outState); | super.onSaveInstanceState(outState); | ||||
Log.d(DEBUG_TAG, "Saving instance state"); | |||||
Fragment fragment = getChildFragmentManager().findFragmentById(R.id.resultFrame); | Fragment fragment = getChildFragmentManager().findFragmentById(R.id.resultFrame); | ||||
if (fragment!=null) | if (fragment!=null) | ||||
getChildFragmentManager().putFragment(outState, SAVED_FRAGMENT, fragment); | getChildFragmentManager().putFragment(outState, SAVED_FRAGMENT, fragment); | ||||
fragmentHelper.setBlockAllActivities(true); | if (fragmentHelper!=null) fragmentHelper.setBlockAllActivities(true); | ||||
instanceStateSaved = true; | instanceStateSaved = true; | ||||
} | } | ||||
public void setSuppressArrivalsReload(boolean value){ | public void setSuppressArrivalsReload(boolean value){ | ||||
suppressArrivalsReload = value; | suppressArrivalsReload = value; | ||||
// we have to suppress the reloading of the (possible) ArrivalsFragment | // we have to suppress the reloading of the (possible) ArrivalsFragment | ||||
/*if(value) { | /*if(value) { | ||||
Show All 20 Lines | public void cancelReloadArrivalsIfNeeded(){ | ||||
toggleSpinner(false); | toggleSpinner(false); | ||||
} | } | ||||
@Override | @Override | ||||
public void onAttach(@NonNull Context context) { | public void onAttach(@NonNull Context context) { | ||||
super.onAttach(context); | super.onAttach(context); | ||||
Log.d(DEBUG_TAG, "OnAttach called, setupOnAttach: "+ setupOnResume); | Log.d(DEBUG_TAG, "OnAttach called, setupOnAttach: "+ setupOnStart); | ||||
mainHandler = new Handler(); | mainHandler = new Handler(); | ||||
if (context instanceof CommonFragmentListener) { | if (context instanceof CommonFragmentListener) { | ||||
mListener = (CommonFragmentListener) context; | mListener = (CommonFragmentListener) context; | ||||
} else { | } else { | ||||
throw new RuntimeException(context | throw new RuntimeException(context | ||||
+ " must implement CommonFragmentListener"); | + " must implement CommonFragmentListener"); | ||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
public void onDetach() { | public void onDetach() { | ||||
super.onDetach(); | super.onDetach(); | ||||
mListener = null; | mListener = null; | ||||
// setupOnAttached = true; | // 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 | @Override | ||||
public void onResume() { | public void onResume() { | ||||
final Context con = getContext(); | final Context con = getContext(); | ||||
Log.w(DEBUG_TAG, "OnResume called"); | Log.w(DEBUG_TAG, "OnResume called, setupOnStart: "+ setupOnStart); | ||||
if (con != null) { | if (con != null) { | ||||
if(locationManager==null) | if(locationManager==null) | ||||
locationManager = AppLocationManager.getInstance(con); | locationManager = AppLocationManager.getInstance(con); | ||||
if(Permissions.locationPermissionGranted(con)){ | if(Permissions.locationPermissionGranted(con)){ | ||||
Log.d(DEBUG_TAG, "Location permission OK"); | Log.d(DEBUG_TAG, "Location permission OK"); | ||||
if(!locationManager.isRequesterRegistered(requester)) | if(!locationManager.isRequesterRegistered(requester)) | ||||
locationManager.addLocationRequestFor(requester); | locationManager.addLocationRequestFor(requester); | ||||
} else if(shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)){ | } //don't request permission | ||||
//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); | ||||
if (suppressArrivalsReload){ | if (suppressArrivalsReload){ | ||||
// we have to suppress the reloading of the (possible) ArrivalsFragment | // we have to suppress the reloading of the (possible) ArrivalsFragment | ||||
Fragment fragment = getChildFragmentManager().findFragmentById(R.id.resultFrame); | Fragment fragment = getChildFragmentManager().findFragmentById(R.id.resultFrame); | ||||
if (fragment instanceof ArrivalsFragment){ | if (fragment instanceof ArrivalsFragment){ | ||||
ArrivalsFragment frag = (ArrivalsFragment) fragment; | ArrivalsFragment frag = (ArrivalsFragment) fragment; | ||||
frag.setReloadOnResume(false); | frag.setReloadOnResume(false); | ||||
} | } | ||||
suppressArrivalsReload = false; | 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){ | if(pendingStopID!=null){ | ||||
Log.d(DEBUG_TAG, "Re-requesting arrivals for pending stop "+pendingStopID); | Log.d(DEBUG_TAG, "Re-requesting arrivals for pending stop "+pendingStopID); | ||||
requestArrivalsForStopID(pendingStopID); | requestArrivalsForStopID(pendingStopID); | ||||
pendingStopID = null; | pendingStopID = null; | ||||
} | } | ||||
mListener.readyGUIfor(FragmentKind.MAIN_SCREEN_FRAGMENT); | mListener.readyGUIfor(FragmentKind.MAIN_SCREEN_FRAGMENT); | ||||
fragmentHelper.setBlockAllActivities(false); | fragmentHelper.setBlockAllActivities(false); | ||||
} | } | ||||
@Override | @Override | ||||
public void onPause() { | public void onPause() { | ||||
//mainHandler = null; | //mainHandler = null; | ||||
locationManager.removeLocationRequestFor(requester); | locationManager.removeLocationRequestFor(requester); | ||||
super.onPause(); | super.onPause(); | ||||
fragmentHelper.setBlockAllActivities(true); | fragmentHelper.setBlockAllActivities(true); | ||||
fragmentHelper.stopLastRequestIfNeeded(true); | fragmentHelper.stopLastRequestIfNeeded(true); | ||||
} | } | ||||
/* | /* | ||||
GUI METHODS | GUI METHODS | ||||
*/ | */ | ||||
/** | /** | ||||
* QR scan button clicked | * QR scan button clicked | ||||
* | * | ||||
* @param v View QRButton clicked | * @param v View QRButton clicked | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 137 Lines • ▼ Show 20 Lines | public class MainScreenFragment extends ScreenBaseFragment 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(){ | private void actuallyShowNearbyStopsFragment(){ | ||||
swipeRefreshLayout.setVisibility(View.VISIBLE); | swipeRefreshLayout.setVisibility(View.VISIBLE); | ||||
final Fragment existingFrag = fragMan.findFragmentById(R.id.resultFrame); | final Fragment existingFrag = fragMan.findFragmentById(R.id.resultFrame); | ||||
NearbyStopsFragment fragment; | // fragment; | ||||
if (!(existingFrag instanceof NearbyStopsFragment)){ | if (!(existingFrag instanceof NearbyStopsFragment)){ | ||||
Log.d(DEBUG_TAG, "actually showing Nearby Stops Fragment"); | |||||
//there is no fragment showing | //there is no fragment showing | ||||
fragment = NearbyStopsFragment.newInstance(NearbyStopsFragment.TYPE_STOPS); | final NearbyStopsFragment fragment = NearbyStopsFragment.newInstance(NearbyStopsFragment.TYPE_STOPS); | ||||
FragmentTransaction ft = fragMan.beginTransaction(); | FragmentTransaction ft = fragMan.beginTransaction(); | ||||
ft.replace(R.id.resultFrame, fragment, NearbyStopsFragment.FRAGMENT_TAG); | ft.replace(R.id.resultFrame, fragment, NearbyStopsFragment.FRAGMENT_TAG); | ||||
if (getActivity()!=null && !getActivity().isFinishing() &&!instanceStateSaved) | if (getActivity()!=null && !getActivity().isFinishing() &&!instanceStateSaved) | ||||
ft.commit(); | ft.commit(); | ||||
else Log.e(DEBUG_TAG, "Not showing nearby fragment because we saved instanceState"); | else Log.e(DEBUG_TAG, "Not showing nearby fragment because we saved instanceState"); | ||||
} | } | ||||
Show All 9 Lines | public class MainScreenFragment extends ScreenBaseFragment implements FragmentListenerMain{ | ||||
* 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(); | |||||
//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 (fragmentType == FragmentKind.ARRIVALS || fragmentType == FragmentKind.STOPS) { | ||||
hideKeyboard(); | |||||
if (pendingNearbyStopsRequest) { | |||||
locationManager.removeLocationRequestFor(requester); | 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(); | ||||
if (getOption(OPTION_SHOW_LEGEND, true)) { | if (getOption(OPTION_SHOW_LEGEND, true)) { | ||||
showHints(); | showHints(); | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | public void requestArrivalsForStopID(String ID) { | ||||
new AsyncArrivalsSearcher(fragmentHelper, fetchers, getContext()).execute(ID); | new AsyncArrivalsSearcher(fragmentHelper, fetchers, getContext()).execute(ID); | ||||
} | } | ||||
} | } | ||||
else { | else { | ||||
new AsyncArrivalsSearcher(fragmentHelper,fetchers, getContext()).execute(ID); | new AsyncArrivalsSearcher(fragmentHelper,fetchers, 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); | ||||
} | } | ||||
} | } | ||||
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 ////////// | /////////// LOCATION METHODS ////////// | ||||
/* | /* | ||||
private void startStopRequest(String provider) { | private void startStopRequest(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)) { | ||||
} | } | ||||
} | } | ||||
*/ | */ | ||||
/* | |||||
/** | |||||
* 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; | ||||
public NearbyStopsRequester(Context appContext, Criteria criteria) { | public NearbyStopsRequester(Context appContext, Criteria criteria) { | ||||
this.appContext = appContext.getApplicationContext(); | this.appContext = appContext.getApplicationContext(); | ||||
this.cr = criteria; | this.cr = criteria; | ||||
} | } | ||||
Show All 35 Lines | class NearbyStopsRequester implements Runnable { | ||||
pendingNearbyStopsRequest = false; | pendingNearbyStopsRequest = false; | ||||
} else if(!haveProviders){ | } else if(!haveProviders){ | ||||
Log.e(DEBUG_TAG, "NO PROVIDERS FOR POSITION"); | Log.e(DEBUG_TAG, "NO PROVIDERS FOR POSITION"); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
*/ | |||||
} | } | ||||
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