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.LocationListener; | ||||
import android.location.LocationManager; | import android.location.LocationManager; | ||||
import android.location.LocationProvider; | import android.location.LocationProvider; | ||||
import android.os.Build; | import android.os.Build; | ||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
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; | ||||
import android.os.Handler; | import android.os.Handler; | ||||
Show All 9 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 it.reyboz.bustorino.ActivityMain; | |||||
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.Stop; | |||||
import it.reyboz.bustorino.backend.StopsFinderByName; | import it.reyboz.bustorino.backend.StopsFinderByName; | ||||
import it.reyboz.bustorino.data.UserDB; | |||||
import it.reyboz.bustorino.middleware.AsyncDataDownload; | import it.reyboz.bustorino.middleware.AsyncDataDownload; | ||||
import it.reyboz.bustorino.util.Permissions; | import it.reyboz.bustorino.util.Permissions; | ||||
import static android.content.Context.LOCATION_SERVICE; | import static android.content.Context.LOCATION_SERVICE; | ||||
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. | ||||
*/ | */ | ||||
public class MainScreenFragment extends BaseFragment implements FragmentListenerMain{ | public class MainScreenFragment extends BaseFragment implements FragmentListenerMain{ | ||||
private final String OPTION_SHOW_LEGEND = "show_legend"; | private static final String OPTION_SHOW_LEGEND = "show_legend"; | ||||
private static final String SAVED_FRAGMENT="saved_fragment"; | |||||
private static final String DEBUG_TAG = "BusTO - MainFragment"; | private static final String DEBUG_TAG = "BusTO - MainFragment"; | ||||
public final static String FRAGMENT_TAG = "MainScreenFragment"; | public final static String FRAGMENT_TAG = "MainScreenFragment"; | ||||
/// UI ELEMENTS // | /// UI ELEMENTS // | ||||
private ImageButton addToFavorites; | private ImageButton addToFavorites; | ||||
private FragmentHelper fragmentHelper; | private FragmentHelper fragmentHelper; | ||||
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 boolean setupOnAttached = true; | private boolean setupOnAttached = true; | ||||
private boolean suppressArrivalsReload = 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; | ||||
private static final int SEARCH_BY_ROUTE = 2; // TODO: implement this -- https://gitpull.it/T12 | private static final int SEARCH_BY_ROUTE = 2; // TODO: implement this -- https://gitpull.it/T12 | ||||
private int searchMode; | private int searchMode; | ||||
Show All 26 Lines | public class MainScreenFragment extends BaseFragment implements FragmentListenerMain{ | ||||
boolean pendingNearbyStopsRequest = false; | boolean pendingNearbyStopsRequest = false; | ||||
LocationManager locmgr; | LocationManager locmgr; | ||||
private final Criteria cr = new Criteria(); | private final Criteria cr = new Criteria(); | ||||
//// ACTIVITY ATTACHED (LISTENER /// | //// ACTIVITY ATTACHED (LISTENER /// | ||||
private CommonFragmentListener mListener; | private CommonFragmentListener mListener; | ||||
private String pendingStopID = null; | |||||
public MainScreenFragment() { | public MainScreenFragment() { | ||||
// Required empty public constructor | // Required empty public constructor | ||||
} | } | ||||
public static MainScreenFragment newInstance() { | public static MainScreenFragment newInstance() { | ||||
MainScreenFragment fragment = new MainScreenFragment(); | MainScreenFragment fragment = new MainScreenFragment(); | ||||
Bundle args = new Bundle(); | Bundle args = new Bundle(); | ||||
▲ Show 20 Lines • Show All 67 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); | locmgr = (LocationManager) getContext().getSystemService(LOCATION_SERVICE); | ||||
Log.d(DEBUG_TAG, "OnCreateView, savedInstanceState null: "+(savedInstanceState==null)); | |||||
return root; | return root; | ||||
} | } | ||||
@Override | |||||
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: "+setupOnAttached); | |||||
//Restore instance state | |||||
if (savedInstanceState!=null){ | |||||
Fragment fragment = getChildFragmentManager().getFragment(savedInstanceState, SAVED_FRAGMENT); | |||||
if (fragment!=null){ | |||||
getChildFragmentManager().beginTransaction().add(R.id.resultFrame, fragment).commit(); | |||||
setupOnAttached = false; | |||||
} | |||||
} | |||||
if (getChildFragmentManager().findFragmentById(R.id.resultFrame)!= null){ | |||||
swipeRefreshLayout.setVisibility(View.VISIBLE); | |||||
} | |||||
} | |||||
@Override | |||||
public void onSaveInstanceState(@NonNull Bundle outState) { | |||||
super.onSaveInstanceState(outState); | |||||
Fragment fragment = getChildFragmentManager().findFragmentById(R.id.resultFrame); | |||||
if (fragment!=null) | |||||
getChildFragmentManager().putFragment(outState, SAVED_FRAGMENT, fragment); | |||||
} | |||||
public void setSuppressArrivalsReload(boolean value){ | |||||
suppressArrivalsReload = value; | |||||
// we have to suppress the reloading of the (possible) ArrivalsFragment | |||||
/*if(value) { | |||||
Fragment fragment = getChildFragmentManager().findFragmentById(R.id.resultFrame); | |||||
if (fragment instanceof ArrivalsFragment) { | |||||
ArrivalsFragment frag = (ArrivalsFragment) fragment; | |||||
frag.setReloadOnResume(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: "+setupOnAttached); | |||||
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.toString() | throw new RuntimeException(context.toString() | ||||
+ " must implement CommonFragmentListener"); | + " must implement CommonFragmentListener"); | ||||
} | } | ||||
if (setupOnAttached){ | if (setupOnAttached) { | ||||
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(getContext(), cr, locListener)); | ||||
else{ | |||||
///TODO: if there is a stop displayed, we need to hold the update | |||||
} | |||||
//If there are no providers available, then, wait for them | //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; | |||||
} | } | ||||
@Override | @Override | ||||
public void onResume() { | public void onResume() { | ||||
final Context con = getContext(); | final Context con = getContext(); | ||||
if (con != null) | if (con != null) | ||||
locmgr = (LocationManager) getContext().getSystemService(LOCATION_SERVICE); | locmgr = (LocationManager) getContext().getSystemService(LOCATION_SERVICE); | ||||
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 | |||||
Log.d(DEBUG_TAG, "Pending stop ID for arrivals: "+pendingStopID); | |||||
//this is the second time we are attaching this fragment | |||||
Log.d(DEBUG_TAG, "Waiting for new stop request: "+ suppressArrivalsReload); | |||||
if (suppressArrivalsReload){ | |||||
// we have to suppress the reloading of the (possible) ArrivalsFragment | |||||
Fragment fragment = getChildFragmentManager().findFragmentById(R.id.resultFrame); | |||||
if (fragment instanceof ArrivalsFragment){ | |||||
ArrivalsFragment frag = (ArrivalsFragment) fragment; | |||||
frag.setReloadOnResume(false); | |||||
} | |||||
suppressArrivalsReload = false; | |||||
} | |||||
if(pendingStopID!=null){ | |||||
requestArrivalsForStopID(pendingStopID); | |||||
pendingStopID = null; | |||||
} | |||||
} | } | ||||
@Override | @Override | ||||
public void onPause() { | public void onPause() { | ||||
//mainHandler = null; | //mainHandler = null; | ||||
locmgr = null; | locmgr = null; | ||||
super.onPause(); | super.onPause(); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 50 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) | |||||
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); | ||||
▲ Show 20 Lines • Show All 96 Lines • ▼ Show 20 Lines | public void readyGUIfor(FragmentKind fragmentType) { | ||||
Log.e("BusTO Activity", "Called readyGUI with unsupported type of Fragment"); | Log.e("BusTO Activity", "Called readyGUI with unsupported type of Fragment"); | ||||
return; | return; | ||||
} | } | ||||
// Shows hints | // Shows hints | ||||
} | } | ||||
/** | |||||
* Main method for stops requests | |||||
* @param ID the Stop ID | |||||
*/ | |||||
@Override | @Override | ||||
public void requestArrivalsForStopID(String ID) { | public void requestArrivalsForStopID(String ID) { | ||||
if (!isResumed()){ | |||||
//defer request | |||||
pendingStopID = ID; | |||||
Log.d(DEBUG_TAG, "Deferring update for stop "+ID); | |||||
return; | |||||
} | |||||
final boolean delayedRequest = !(pendingStopID==null); | |||||
final FragmentManager framan = getChildFragmentManager(); | final FragmentManager framan = getChildFragmentManager(); | ||||
if (getContext()==null){ | |||||
Log.e(DEBUG_TAG, "Asked for arrivals with null context"); | |||||
return; | |||||
} | |||||
if (ID == null || ID.length() <= 0) { | if (ID == null || ID.length() <= 0) { | ||||
// we're still in UI thread, no need to mess with Progress | // we're still in UI thread, no need to mess with Progress | ||||
showToastMessage(R.string.insert_bus_stop_number_error, true); | showToastMessage(R.string.insert_bus_stop_number_error, true); | ||||
toggleSpinner(false); | toggleSpinner(false); | ||||
} else if (framan.findFragmentById(R.id.resultFrame) instanceof ArrivalsFragment) { | } else if (framan.findFragmentById(R.id.resultFrame) instanceof ArrivalsFragment) { | ||||
ArrivalsFragment fragment = (ArrivalsFragment) framan.findFragmentById(R.id.resultFrame); | ArrivalsFragment fragment = (ArrivalsFragment) framan.findFragmentById(R.id.resultFrame); | ||||
if (fragment != null && fragment.getStopID() != null && fragment.getStopID().equals(ID)){ | if (fragment != null && fragment.getStopID() != null && fragment.getStopID().equals(ID)){ | ||||
// Run with previous fetchers | // Run with previous fetchers | ||||
//fragment.getCurrentFetchers().toArray() | //fragment.getCurrentFetchers().toArray() | ||||
new AsyncDataDownload(fragmentHelper,fragment.getCurrentFetchersAsArray(), getContext()).execute(ID); | new AsyncDataDownload(fragmentHelper,fragment.getCurrentFetchersAsArray(), getContext()).execute(ID); | ||||
} else{ | } else{ | ||||
new AsyncDataDownload(fragmentHelper, arrivalsFetchers, getContext()).execute(ID); | new AsyncDataDownload(fragmentHelper, arrivalsFetchers, getContext()).execute(ID); | ||||
} | } | ||||
} | } | ||||
else { | else { | ||||
new AsyncDataDownload(fragmentHelper,arrivalsFetchers, getContext()).execute(ID); | new AsyncDataDownload(fragmentHelper,arrivalsFetchers, getContext()).execute(ID); | ||||
Log.d("MainActiv", "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() { | final LocationListener locListener = new LocationListener() { | ||||
@Override | @Override | ||||
public void onLocationChanged(Location location) { | public void onLocationChanged(Location location) { | ||||
Log.d(DEBUG_TAG, "Location changed"); | Log.d(DEBUG_TAG, "Location changed"); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 85 Lines • ▼ Show 20 Lines | class NearbyStopsRequester implements Runnable { | ||||
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 0.1f, listener); | 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