Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/ActivityMain.java
| Show All 12 Lines | BusTO - Arrival times for Turin public transports. | ||||
| GNU General Public License for more details. | GNU General Public License for more details. | ||||
| You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
| */ | */ | ||||
| package it.reyboz.bustorino; | package it.reyboz.bustorino; | ||||
| import android.Manifest; | import android.Manifest; | ||||
| import android.app.Activity; | |||||
| import android.content.Context; | import android.content.Context; | ||||
| import android.content.Intent; | import android.content.Intent; | ||||
| import android.content.SharedPreferences; | import android.content.SharedPreferences; | ||||
| import android.content.pm.PackageManager; | import android.content.pm.PackageManager; | ||||
| import android.content.res.Resources; | import android.content.res.Resources; | ||||
| import android.database.sqlite.SQLiteDatabase; | import android.database.sqlite.SQLiteDatabase; | ||||
| import android.location.*; | import android.location.*; | ||||
| import android.net.Uri; | import android.net.Uri; | ||||
| Show All 35 Lines | |||||
| import com.google.android.material.floatingactionbutton.FloatingActionButton; | import com.google.android.material.floatingactionbutton.FloatingActionButton; | ||||
| import it.reyboz.bustorino.backend.*; | import it.reyboz.bustorino.backend.*; | ||||
| import it.reyboz.bustorino.data.DBUpdateWorker; | import it.reyboz.bustorino.data.DBUpdateWorker; | ||||
| import it.reyboz.bustorino.data.DatabaseUpdate; | import it.reyboz.bustorino.data.DatabaseUpdate; | ||||
| import it.reyboz.bustorino.data.UserDB; | import it.reyboz.bustorino.data.UserDB; | ||||
| import it.reyboz.bustorino.fragments.*; | import it.reyboz.bustorino.fragments.*; | ||||
| import it.reyboz.bustorino.middleware.*; | import it.reyboz.bustorino.middleware.*; | ||||
| import it.reyboz.bustorino.util.Permissions; | |||||
| import java.util.Arrays; | import java.util.Arrays; | ||||
| import java.util.List; | import java.util.List; | ||||
| import java.util.concurrent.TimeUnit; | import java.util.concurrent.TimeUnit; | ||||
| import static it.reyboz.bustorino.backend.utils.getBusStopIDFromUri; | |||||
| public class ActivityMain extends GeneralActivity implements FragmentListenerMain { | public class ActivityMain extends GeneralActivity implements FragmentListenerMain { | ||||
| /* | /* | ||||
| * Layout elements | * Layout elements | ||||
| */ | */ | ||||
| private EditText busStopSearchByIDEditText; | private EditText busStopSearchByIDEditText; | ||||
| private EditText busStopSearchByNameEditText; | private EditText busStopSearchByNameEditText; | ||||
| private ProgressBar progressBar; | private ProgressBar progressBar; | ||||
| ▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | public class ActivityMain extends GeneralActivity implements FragmentListenerMain { | ||||
| private FragmentHelper fh; | private FragmentHelper fh; | ||||
| ///////////////////////////////// EVENT HANDLERS /////////////////////////////////////////////// | ///////////////////////////////// EVENT HANDLERS /////////////////////////////////////////////// | ||||
| /* | /* | ||||
| * @see swipeRefreshLayout | * @see swipeRefreshLayout | ||||
| */ | */ | ||||
| private final Handler theHandler = new Handler(); | private final Handler theHandler = new Handler(); | ||||
| private final Runnable refreshing = new Runnable() { | private final Runnable refreshStop = new Runnable() { | ||||
| public void run() { | public void run() { | ||||
| if (framan.findFragmentById(R.id.resultFrame) instanceof ArrivalsFragment) { | 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){ | |||||
| new AsyncDataDownload(fh, arrivalsFetchers, getApplicationContext()).execute(); | |||||
| } else{ | |||||
| String stopName = fragment.getStopID(); | String stopName = fragment.getStopID(); | ||||
| new AsyncDataDownload(fh, fragment.getCurrentFetchersAsArray()).execute(stopName); | new AsyncDataDownload(fh, fragment.getCurrentFetchersAsArray(), getApplicationContext()).execute(stopName); | ||||
| } | |||||
| } else //we create a new fragment, which is WRONG | } else //we create a new fragment, which is WRONG | ||||
| new AsyncDataDownload(fh, arrivalsFetchers).execute(); | new AsyncDataDownload(fh, arrivalsFetchers, getApplicationContext()).execute(); | ||||
| } | } | ||||
| }; | }; | ||||
| //// MAIN METHOD /// | //// MAIN METHOD /// | ||||
| @Override | @Override | ||||
| protected void onCreate(Bundle savedInstanceState) { | protected void onCreate(Bundle savedInstanceState) { | ||||
| Show All 34 Lines | protected void onCreate(Bundle savedInstanceState) { | ||||
| onSearchClick(v); | onSearchClick(v); | ||||
| return true; | return true; | ||||
| } | } | ||||
| return false; | return false; | ||||
| }); | }); | ||||
| // Called when the layout is pulled down | // Called when the layout is pulled down | ||||
| swipeRefreshLayout | swipeRefreshLayout | ||||
| .setOnRefreshListener(() -> theHandler.post(refreshing)); | .setOnRefreshListener(() -> theHandler.post(refreshStop)); | ||||
| /** | /** | ||||
| * @author Marco Gagino!!! | * @author Marco Gagino!!! | ||||
| */ | */ | ||||
| //swipeRefreshLayout.setColorSchemeColors(R.color.blue_500, R.color.orange_500); // setColorScheme is deprecated, setColorSchemeColors isn't | //swipeRefreshLayout.setColorSchemeColors(R.color.blue_500, R.color.orange_500); // setColorScheme is deprecated, setColorSchemeColors isn't | ||||
| swipeRefreshLayout.setColorSchemeResources(R.color.blue_500, R.color.orange_500); | swipeRefreshLayout.setColorSchemeResources(R.color.blue_500, R.color.orange_500); | ||||
| fh = new FragmentHelper(this, R.id.listRefreshLayout, R.id.resultFrame); | fh = new FragmentHelper(this, framan, getApplicationContext(),R.id.resultFrame); | ||||
| setSearchModeBusStopID(); | setSearchModeBusStopID(); | ||||
| //---------------------------- START INTENT CHECK QUEUE ------------------------------------ | //---------------------------- START INTENT CHECK QUEUE ------------------------------------ | ||||
| // Intercept calls from URL intent | // Intercept calls from URL intent | ||||
| boolean tryedFromIntent = false; | boolean tryedFromIntent = false; | ||||
| String busStopID = null; | String busStopID = null; | ||||
| ▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | protected void onCreate(Bundle savedInstanceState) { | ||||
| // forcing it as user name even though it could be standard name, it doesn't really matter | // forcing it as user name even though it could be standard name, it doesn't really matter | ||||
| nextStop.setStopUserName(busStopDisplayName); | nextStop.setStopUserName(busStopDisplayName); | ||||
| //set stop as last succe | //set stop as last succe | ||||
| fh.setLastSuccessfullySearchedBusStop(nextStop); | fh.setLastSuccessfullySearchedBusStop(nextStop); | ||||
| */ | */ | ||||
| requestArrivalsForStopID(busStopID); | requestArrivalsForStopID(busStopID); | ||||
| } | } | ||||
| //Try (hopefully) database update | //Try (hopefully) database update | ||||
| //TODO: Check if service shows the notification | |||||
| //Old code for the db update | |||||
| //DatabaseUpdateService.startDBUpdate(getApplicationContext()); | |||||
| PeriodicWorkRequest wr = new PeriodicWorkRequest.Builder(DBUpdateWorker.class, 1, TimeUnit.DAYS) | PeriodicWorkRequest wr = new PeriodicWorkRequest.Builder(DBUpdateWorker.class, 1, TimeUnit.DAYS) | ||||
| .setBackoffCriteria(BackoffPolicy.LINEAR, 30, TimeUnit.MINUTES) | .setBackoffCriteria(BackoffPolicy.LINEAR, 30, TimeUnit.MINUTES) | ||||
| .setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED) | .setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED) | ||||
| .build()) | .build()) | ||||
| .build(); | .build(); | ||||
| final WorkManager workManager = WorkManager.getInstance(this); | final WorkManager workManager = WorkManager.getInstance(this); | ||||
| Show All 40 Lines | protected void onCreate(Bundle savedInstanceState) { | ||||
| //SETUP LOCATION | //SETUP LOCATION | ||||
| locmgr = (LocationManager) getSystemService(LOCATION_SERVICE); | locmgr = (LocationManager) getSystemService(LOCATION_SERVICE); | ||||
| 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); | ||||
| //We want the nearby bus stops! | //We want the nearby bus stops! | ||||
| theHandler.post(new NearbyStopsRequester()); | theHandler.post(new NearbyStopsRequester(this)); | ||||
| //If there are no providers available, then, wait for them | //If there are no providers available, then, wait for them | ||||
| Log.d("MainActivity", "Created"); | Log.d("MainActivity", "Created"); | ||||
| } | } | ||||
| /* | /* | ||||
| Show All 29 Lines | public class ActivityMain extends GeneralActivity implements FragmentListenerMain { | ||||
| } | } | ||||
| @Override | @Override | ||||
| protected void onResume() { | protected void onResume() { | ||||
| super.onResume(); | super.onResume(); | ||||
| fh.setBlockAllActivities(false); | fh.setBlockAllActivities(false); | ||||
| //TODO: check if current LiveData-bound observer works | //TODO: check if current LiveData-bound observer works | ||||
| if (pendingNearbyStopsRequest) | if (pendingNearbyStopsRequest) | ||||
| theHandler.post(new NearbyStopsRequester()); | theHandler.post(new NearbyStopsRequester(this)); | ||||
| ActionBar bar = getSupportActionBar(); | ActionBar bar = getSupportActionBar(); | ||||
| if(bar!=null) bar.show(); | if(bar!=null) bar.show(); | ||||
| else Log.w(DEBUG_TAG, "ACTION BAR IS NULL"); | else Log.w(DEBUG_TAG, "ACTION BAR IS NULL"); | ||||
| //check if we can display the experiments or not | //check if we can display the experiments or not | ||||
| SharedPreferences shPr = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); | SharedPreferences shPr = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); | ||||
| boolean exper_On = shPr.getBoolean(getString(R.string.pref_key_experimental), false); | boolean exper_On = shPr.getBoolean(getString(R.string.pref_key_experimental), false); | ||||
| //Log.w(DEBUG_TAG, "Preference experimental is "+exper_On); | //Log.w(DEBUG_TAG, "Preference experimental is "+exper_On); | ||||
| ▲ Show 20 Lines • Show All 89 Lines • ▼ Show 20 Lines | public class ActivityMain extends GeneralActivity implements FragmentListenerMain { | ||||
| */ | */ | ||||
| public void onSearchClick(View v) { | public void onSearchClick(View v) { | ||||
| if (searchMode == SEARCH_BY_ID) { | if (searchMode == SEARCH_BY_ID) { | ||||
| String busStopID = busStopSearchByIDEditText.getText().toString(); | String busStopID = busStopSearchByIDEditText.getText().toString(); | ||||
| requestArrivalsForStopID(busStopID); | requestArrivalsForStopID(busStopID); | ||||
| } else { // searchMode == SEARCH_BY_NAME | } else { // searchMode == SEARCH_BY_NAME | ||||
| String query = busStopSearchByNameEditText.getText().toString(); | String query = busStopSearchByNameEditText.getText().toString(); | ||||
| //new asyncWgetBusStopSuggestions(query, stopsDB, StopsFindersByNameRecursionHelper); | //new asyncWgetBusStopSuggestions(query, stopsDB, StopsFindersByNameRecursionHelper); | ||||
| new AsyncDataDownload(fh, stopsFinderByNames).execute(query); | new AsyncDataDownload(fh, stopsFinderByNames, getApplicationContext()).execute(query); | ||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| * PERMISSION STUFF | * PERMISSION STUFF | ||||
| **/ | **/ | ||||
| @Override | @Override | ||||
| public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | ||||
| super.onRequestPermissionsResult(requestCode, permissions, grantResults); | super.onRequestPermissionsResult(requestCode, permissions, grantResults); | ||||
| switch (requestCode) { | switch (requestCode) { | ||||
| case PERMISSION_REQUEST_POSITION: | case PERMISSION_REQUEST_POSITION: | ||||
| if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | ||||
| setOption(LOCATION_PERMISSION_GIVEN, true); | setOption(LOCATION_PERMISSION_GIVEN, true); | ||||
| //if we sent a request for a new NearbyStopsFragment | //if we sent a request for a new NearbyStopsFragment | ||||
| if (pendingNearbyStopsRequest) { | if (pendingNearbyStopsRequest) { | ||||
| pendingNearbyStopsRequest = false; | pendingNearbyStopsRequest = false; | ||||
| theHandler.post(new NearbyStopsRequester()); | theHandler.post(new NearbyStopsRequester(this)); | ||||
| } | } | ||||
| } else { | } else { | ||||
| //permission denied | //permission denied | ||||
| setOption(LOCATION_PERMISSION_GIVEN, false); | setOption(LOCATION_PERMISSION_GIVEN, false); | ||||
| } | } | ||||
| //add other cases for permissions | //add other cases for permissions | ||||
| break; | break; | ||||
| Show All 32 Lines | public void requestArrivalsForStopID(String ID) { | ||||
| // 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(fh,fragment.getCurrentFetchersAsArray()).execute(ID); | new AsyncDataDownload(fh,fragment.getCurrentFetchersAsArray(), this).execute(ID); | ||||
| } else{ | } else{ | ||||
| new AsyncDataDownload(fh, arrivalsFetchers).execute(ID); | new AsyncDataDownload(fh, arrivalsFetchers, this).execute(ID); | ||||
| } | } | ||||
| } | } | ||||
| else { | else { | ||||
| new AsyncDataDownload(fh,arrivalsFetchers).execute(ID); | new AsyncDataDownload(fh, arrivalsFetchers, this).execute(ID); | ||||
| Log.d("MainActiv", "Started search for arrivals of stop " + ID); | Log.d("MainActiv", "Started search for arrivals of stop " + ID); | ||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| * QR scan button clicked | * QR scan button clicked | ||||
| * | * | ||||
| ▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | private void createDefaultSnackbar() { | ||||
| snackbar.show(); | snackbar.show(); | ||||
| } | } | ||||
| ///////////////////////////////// POSITION STUFF////////////////////////////////////////////// | ///////////////////////////////// POSITION STUFF////////////////////////////////////////////// | ||||
| private void resolveStopRequest(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 && pendingNearbyStopsRequest && locmgr.getProvider(provider).meetsCriteria(cr)) { | if (locmgr != null && pendingNearbyStopsRequest && locmgr.getProvider(provider).meetsCriteria(cr)) { | ||||
| pendingNearbyStopsRequest = false; | pendingNearbyStopsRequest = false; | ||||
| theHandler.post(new NearbyStopsRequester()); | theHandler.post(new NearbyStopsRequester(this)); | ||||
| } | } | ||||
| } | } | ||||
| 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 All 16 Lines | final LocationListener locListener = new LocationListener() { | ||||
| } | } | ||||
| }; | }; | ||||
| /** | /** | ||||
| * Run location requests separately and asynchronously | * Run location requests separately and asynchronously | ||||
| */ | */ | ||||
| class NearbyStopsRequester implements Runnable { | class NearbyStopsRequester implements Runnable { | ||||
| Activity runningAct; | |||||
| public NearbyStopsRequester(Activity runningAct) { | |||||
| this.runningAct = runningAct; | |||||
| } | |||||
| @Override | @Override | ||||
| public void run() { | public void run() { | ||||
| final boolean canRunPosition = Build.VERSION.SDK_INT < Build.VERSION_CODES.M || getOption(LOCATION_PERMISSION_GIVEN, false); | final boolean canRunPosition = Build.VERSION.SDK_INT < Build.VERSION_CODES.M || getOption(LOCATION_PERMISSION_GIVEN, false); | ||||
| final boolean noPermission = ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && | final boolean noPermission = ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && | ||||
| ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED; | ActivityCompat.checkSelfPermission(getApplicationContext(), 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 (!canRunPosition) { | ||||
| pendingNearbyStopsRequest = true; | pendingNearbyStopsRequest = true; | ||||
| assertLocationPermissions(); | Permissions.assertLocationPermissions(getApplicationContext(),runningAct); | ||||
| 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(getApplicationContext(), "Asked for permission position too many times", Toast.LENGTH_LONG).show(); | Toast.makeText(getApplicationContext(), "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) getSystemService(LOCATION_SERVICE); | LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE); | ||||
| Show All 33 Lines | private boolean anyLocationProviderMatchesCriteria(LocationManager mng, Criteria cr, boolean enabled) { | ||||
| for (String s : providers) { | for (String s : providers) { | ||||
| Log.d(DEBUG_TAG, "Provider " + s); | Log.d(DEBUG_TAG, "Provider " + s); | ||||
| } | } | ||||
| return providers.size() > 0; | return providers.size() > 0; | ||||
| } | } | ||||
| ///////////////////////////////// OTHER STUFF ////////////////////////////////////////////////// | ///////////////////////////////// OTHER STUFF ////////////////////////////////////////////////// | ||||
| /** | |||||
| * Check if the last Bus Stop is in the favorites | |||||
| * | |||||
| * @return | |||||
| */ | |||||
| public boolean isStopInFavorites(String busStopId) { | |||||
| boolean found = false; | |||||
| // no stop no party | |||||
| if (busStopId != null) { | |||||
| SQLiteDatabase userDB = new UserDB(getApplicationContext()).getReadableDatabase(); | |||||
| found = UserDB.isStopInFavorites(userDB, busStopId); | |||||
| } | |||||
| return found; | |||||
| } | |||||
| /** | |||||
| * Add the last Stop to favorites | |||||
| */ | |||||
| @Override | @Override | ||||
| public void showFloatingActionButton(boolean yes) { | public void showFloatingActionButton(boolean yes) { | ||||
| if (yes) floatingActionButton.show(); | if (yes) floatingActionButton.show(); | ||||
| else floatingActionButton.hide(); | else floatingActionButton.hide(); | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Lines | public void readyGUIfor(FragmentKind fragmentType) { | ||||
| default: | default: | ||||
| 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 | ||||
| } | } | ||||
| /** | private void openIceweasel(String url){ | ||||
| * Open an URL in the default browser. | utils.openIceweasel(url, this); | ||||
| * | |||||
| * @param url URL | |||||
| */ | |||||
| public void openIceweasel(String url) { | |||||
| Intent browserIntent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); | |||||
| startActivity(browserIntent1); | |||||
| } | |||||
| ///////////////////// INTENT HELPER //////////////////////////////////////////////////////////// | |||||
| /** | |||||
| * Try to extract the bus stop ID from a URi | |||||
| * | |||||
| * @param uri The URL | |||||
| * @return bus stop ID or null | |||||
| */ | |||||
| public static String getBusStopIDFromUri(Uri uri) { | |||||
| String busStopID; | |||||
| // everithing catches fire when passing null to a switch. | |||||
| String host = uri.getHost(); | |||||
| if (host == null) { | |||||
| Log.e("ActivityMain", "Not an URL: " + uri); | |||||
| return null; | |||||
| } | |||||
| switch (host) { | |||||
| case "m.gtt.to.it": | |||||
| // http://m.gtt.to.it/m/it/arrivi.jsp?n=1254 | |||||
| busStopID = uri.getQueryParameter("n"); | |||||
| if (busStopID == null) { | |||||
| Log.e("ActivityMain", "Expected ?n from: " + uri); | |||||
| } | |||||
| break; | |||||
| case "www.gtt.to.it": | |||||
| case "gtt.to.it": | |||||
| // http://www.gtt.to.it/cms/percorari/arrivi?palina=1254 | |||||
| busStopID = uri.getQueryParameter("palina"); | |||||
| if (busStopID == null) { | |||||
| Log.e("ActivityMain", "Expected ?palina from: " + uri); | |||||
| } | |||||
| break; | |||||
| default: | |||||
| Log.e("ActivityMain", "Unexpected intent URL: " + uri); | |||||
| busStopID = null; | |||||
| } | |||||
| return busStopID; | |||||
| } | } | ||||
| } | } | ||||
| 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