Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/ActivityMain.java
Show All 25 Lines | |||||
import android.database.sqlite.SQLiteDatabase; | import android.database.sqlite.SQLiteDatabase; | ||||
import android.location.*; | import android.location.*; | ||||
import android.net.Uri; | import android.net.Uri; | ||||
import android.os.Build; | import android.os.Build; | ||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.os.Handler; | import android.os.Handler; | ||||
import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
import androidx.appcompat.app.ActionBar; | |||||
import androidx.appcompat.widget.Toolbar; | |||||
import androidx.core.app.ActivityCompat; | import androidx.core.app.ActivityCompat; | ||||
import androidx.work.BackoffPolicy; | import androidx.work.BackoffPolicy; | ||||
import androidx.work.Constraints; | import androidx.work.Constraints; | ||||
import androidx.work.ExistingPeriodicWorkPolicy; | import androidx.work.ExistingPeriodicWorkPolicy; | ||||
import androidx.work.NetworkType; | import androidx.work.NetworkType; | ||||
import androidx.work.PeriodicWorkRequest; | import androidx.work.PeriodicWorkRequest; | ||||
import androidx.work.WorkInfo; | import androidx.work.WorkInfo; | ||||
import androidx.work.WorkManager; | import androidx.work.WorkManager; | ||||
import com.google.android.material.snackbar.Snackbar; | import com.google.android.material.snackbar.Snackbar; | ||||
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.core.app.NavUtils; | import androidx.core.app.NavUtils; | ||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; | import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; | ||||
import android.util.Log; | import android.util.Log; | ||||
import android.view.KeyEvent; | |||||
import android.view.Menu; | import android.view.Menu; | ||||
import android.view.MenuItem; | import android.view.MenuItem; | ||||
import android.view.View; | import android.view.View; | ||||
import android.view.inputmethod.EditorInfo; | import android.view.inputmethod.EditorInfo; | ||||
import android.view.inputmethod.InputMethodManager; | import android.view.inputmethod.InputMethodManager; | ||||
import android.widget.*; | import android.widget.*; | ||||
import com.google.zxing.integration.android.IntentIntegrator; | import com.google.zxing.integration.android.IntentIntegrator; | ||||
import com.google.zxing.integration.android.IntentResult; | import com.google.zxing.integration.android.IntentResult; | ||||
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.DatabaseUpdate; | |||||
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 java.util.ArrayList; | |||||
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; | ||||
public class ActivityMain extends GeneralActivity implements FragmentListener { | 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; | ||||
private TextView howDoesItWorkTextView; | private TextView howDoesItWorkTextView; | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | public class ActivityMain extends GeneralActivity implements FragmentListenerMain { | ||||
}; | }; | ||||
//// MAIN METHOD /// | //// MAIN METHOD /// | ||||
@Override | @Override | ||||
protected void onCreate(Bundle savedInstanceState) { | protected void onCreate(Bundle savedInstanceState) { | ||||
super.onCreate(savedInstanceState); | super.onCreate(savedInstanceState); | ||||
framan = getSupportFragmentManager(); | framan = getSupportFragmentManager(); | ||||
final SharedPreferences theShPr = getMainSharedPreferences(); | final SharedPreferences theShPr = getMainSharedPreferences(); | ||||
/* | /* | ||||
* Database Access | * UI | ||||
*/ | */ | ||||
setContentView(R.layout.activity_main); | setContentView(R.layout.activity_main); | ||||
Toolbar defToolbar = findViewById(R.id.that_toolbar); | |||||
setSupportActionBar(defToolbar); | |||||
busStopSearchByIDEditText = findViewById(R.id.busStopSearchByIDEditText); | busStopSearchByIDEditText = findViewById(R.id.busStopSearchByIDEditText); | ||||
busStopSearchByNameEditText = findViewById(R.id.busStopSearchByNameEditText); | busStopSearchByNameEditText = findViewById(R.id.busStopSearchByNameEditText); | ||||
progressBar = findViewById(R.id.progressBar); | progressBar = findViewById(R.id.progressBar); | ||||
howDoesItWorkTextView = findViewById(R.id.howDoesItWorkTextView); | howDoesItWorkTextView = findViewById(R.id.howDoesItWorkTextView); | ||||
hideHintButton = findViewById(R.id.hideHintButton); | hideHintButton = findViewById(R.id.hideHintButton); | ||||
swipeRefreshLayout = findViewById(R.id.listRefreshLayout); | swipeRefreshLayout = findViewById(R.id.listRefreshLayout); | ||||
floatingActionButton = findViewById(R.id.floatingActionButton); | floatingActionButton = findViewById(R.id.floatingActionButton); | ||||
▲ Show 20 Lines • Show All 80 Lines • ▼ Show 20 Lines | protected void onCreate(Bundle savedInstanceState) { | ||||
//THIS PART SHOULDN'T BE NECESSARY SINCE THE LAST SUCCESSFULLY SEARCHED BUS | //THIS PART SHOULDN'T BE NECESSARY SINCE THE LAST SUCCESSFULLY SEARCHED BUS | ||||
// STOP IS ADDED AUTOMATICALLY | // STOP IS ADDED AUTOMATICALLY | ||||
Stop nextStop = new Stop(busStopID); | Stop nextStop = new Stop(busStopID); | ||||
// 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); | ||||
*/ | */ | ||||
createFragmentForStop(busStopID); | requestArrivalsForStopID(busStopID); | ||||
} | } | ||||
//Try (hopefully) database update | //Try (hopefully) database update | ||||
//TODO: Check if service shows the notification | //TODO: Check if service shows the notification | ||||
//Old code for the db update | //Old code for the db update | ||||
//DatabaseUpdateService.startDBUpdate(getApplicationContext()); | //DatabaseUpdateService.startDBUpdate(getApplicationContext()); | ||||
PeriodicWorkRequest wr = new PeriodicWorkRequest.Builder(DBUpdateWorker.class, 1, TimeUnit.DAYS) | PeriodicWorkRequest wr = new PeriodicWorkRequest.Builder(DBUpdateWorker.class, 1, TimeUnit.DAYS) | ||||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | protected void onCreate(Bundle savedInstanceState) { | ||||
theHandler.post(new NearbyStopsRequester()); | theHandler.post(new NearbyStopsRequester()); | ||||
//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"); | ||||
} | } | ||||
/** | /* | ||||
* Reload bus stop timetable when it's fulled resumed from background. | * Reload bus stop timetable when it's fulled resumed from background. | ||||
*/ | |||||
/** | |||||
* @Override protected void onPostResume() { | * @Override protected void onPostResume() { | ||||
* super.onPostResume(); | * super.onPostResume(); | ||||
* Log.d("ActivityMain", "onPostResume fired. Last successfully bus stop ID: " + fh.getLastSuccessfullySearchedBusStop()); | * Log.d("ActivityMain", "onPostResume fired. Last successfully bus stop ID: " + fh.getLastSuccessfullySearchedBusStop()); | ||||
* if (searchMode == SEARCH_BY_ID && fh.getLastSuccessfullySearchedBusStop() != null) { | * if (searchMode == SEARCH_BY_ID && fh.getLastSuccessfullySearchedBusStop() != null) { | ||||
* setBusStopSearchByIDEditText(fh.getLastSuccessfullySearchedBusStop().ID); | * setBusStopSearchByIDEditText(fh.getLastSuccessfullySearchedBusStop().ID); | ||||
* new AsyncDataDownload(AsyncDataDownload.RequestType.ARRIVALS,fh).execute(); | * new AsyncDataDownload(AsyncDataDownload.RequestType.ARRIVALS,fh).execute(); | ||||
* } else { | * } else { | ||||
* //we have new activity or we don't have a new searched stop. | * //we have new activity or we don't have a new searched stop. | ||||
Show All 20 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()); | ||||
ActionBar bar = getSupportActionBar(); | |||||
if(bar!=null) bar.show(); | |||||
else Log.w(DEBUG_TAG, "ACTION BAR IS NULL"); | |||||
} | } | ||||
@Override | @Override | ||||
public boolean onCreateOptionsMenu(Menu menu) { | public boolean onCreateOptionsMenu(Menu menu) { | ||||
// Inflate the menu; this adds items to the action bar if it is present. | // Inflate the menu; this adds items to the action bar if it is present. | ||||
getMenuInflater().inflate(R.menu.main, menu); | getMenuInflater().inflate(R.menu.main, menu); | ||||
actionHelpMenuItem = menu.findItem(R.id.action_help); | actionHelpMenuItem = menu.findItem(R.id.action_help); | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | public boolean onOptionsItemSelected(MenuItem item) { | ||||
openIceweasel("https://gitpull.it/source/libre-busto/"); | openIceweasel("https://gitpull.it/source/libre-busto/"); | ||||
return true; | return true; | ||||
case R.id.action_licence: | case R.id.action_licence: | ||||
openIceweasel("https://www.gnu.org/licenses/gpl-3.0.html"); | openIceweasel("https://www.gnu.org/licenses/gpl-3.0.html"); | ||||
return true; | return true; | ||||
case R.id.action_settings: | case R.id.action_settings: | ||||
Log.d("MAINBusTO", "Pressed button preferences"); | Log.d("MAINBusTO", "Pressed button preferences"); | ||||
startActivity(new Intent(ActivityMain.this, ActivitySettings.class)); | startActivity(new Intent(ActivityMain.this, ActivitySettings.class)); | ||||
return true; | |||||
case R.id.action_experiments: | |||||
startActivity(new Intent(this, ActivityPrincipal.class)); | |||||
} | } | ||||
return super.onOptionsItemSelected(item); | return super.onOptionsItemSelected(item); | ||||
} | } | ||||
/** | /** | ||||
* OK this is pure shit | * OK this is pure shit | ||||
* | * | ||||
* @param v View clicked | * @param v View clicked | ||||
*/ | */ | ||||
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(); | ||||
createFragmentForStop(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).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); | |||||
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()); | ||||
Show All 30 Lines | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | ||||
}*/ | }*/ | ||||
} | } | ||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
public void createFragmentForStop(String ID) { | public void requestArrivalsForStopID(String ID) { | ||||
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.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()).execute(ID); | ||||
} else{ | } else{ | ||||
new AsyncDataDownload(fh, arrivalsFetchers).execute(ID); | new AsyncDataDownload(fh, arrivalsFetchers).execute(ID); | ||||
} | } | ||||
} | } | ||||
else { | else { | ||||
Show All 26 Lines | public void onActivityResult(int requestCode, int resultCode, Intent intent) { | ||||
} catch (NullPointerException e) { | } catch (NullPointerException e) { | ||||
Toast.makeText(getApplicationContext(), | Toast.makeText(getApplicationContext(), | ||||
R.string.no_qrcode, Toast.LENGTH_SHORT).show(); | R.string.no_qrcode, Toast.LENGTH_SHORT).show(); | ||||
return; | return; | ||||
} | } | ||||
String busStopID = getBusStopIDFromUri(uri); | String busStopID = getBusStopIDFromUri(uri); | ||||
busStopSearchByIDEditText.setText(busStopID); | busStopSearchByIDEditText.setText(busStopID); | ||||
createFragmentForStop(busStopID); | requestArrivalsForStopID(busStopID); | ||||
} | } | ||||
public void onHideHint(View v) { | public void onHideHint(View v) { | ||||
hideHints(); | hideHints(); | ||||
setOption(OPTION_SHOW_LEGEND, false); | setOption(OPTION_SHOW_LEGEND, false); | ||||
} | } | ||||
public void onToggleKeyboardLayout(View v) { | public void onToggleKeyboardLayout(View v) { | ||||
▲ Show 20 Lines • Show All 111 Lines • ▼ Show 20 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 ////////////////////////////////////////////////// | ||||
/** | |||||
* Get the last successfully searched bus stop or NULL | |||||
* | |||||
* @return | |||||
*/ | |||||
@Override | |||||
public Stop getLastSuccessfullySearchedBusStop() { | |||||
return fh.getLastSuccessfullySearchedBusStop(); | |||||
} | |||||
/** | |||||
* Get the last successfully searched bus stop ID or NULL | |||||
* | |||||
* @return | |||||
*/ | |||||
@Override | |||||
public String getLastSuccessfullySearchedBusStopID() { | |||||
Stop stop = getLastSuccessfullySearchedBusStop(); | |||||
return stop == null ? null : stop.ID; | |||||
} | |||||
/** | |||||
* Update the star "Add to favorite" icon | |||||
*/ | |||||
@Override | |||||
public void updateStarIconFromLastBusStop() { | |||||
// no favorites no party! | |||||
addToFavorites = (ImageButton) findViewById(R.id.addToFavorites); | |||||
if (addToFavorites == null) { | |||||
Log.d("MainActivity", "Why the fuck the star is not here?!"); | |||||
return; | |||||
} | |||||
// check if there is a last Stop | |||||
String stopID = getLastSuccessfullySearchedBusStopID(); | |||||
if (stopID == null) { | |||||
addToFavorites.setVisibility(View.INVISIBLE); | |||||
} else { | |||||
// filled or outline? | |||||
if (isStopInFavorites(stopID)) { | |||||
addToFavorites.setImageResource(R.drawable.ic_star_filled); | |||||
} else { | |||||
addToFavorites.setImageResource(R.drawable.ic_star_outline); | |||||
} | |||||
addToFavorites.setVisibility(View.VISIBLE); | |||||
} | |||||
} | |||||
/** | /** | ||||
* Check if the last Bus Stop is in the favorites | * Check if the last Bus Stop is in the favorites | ||||
* | * | ||||
* @return | * @return | ||||
*/ | */ | ||||
public boolean isStopInFavorites(String busStopId) { | public boolean isStopInFavorites(String busStopId) { | ||||
boolean found = false; | boolean found = false; | ||||
// no stop no party | // no stop no party | ||||
if (busStopId != null) { | if (busStopId != null) { | ||||
SQLiteDatabase userDB = new UserDB(getApplicationContext()).getReadableDatabase(); | SQLiteDatabase userDB = new UserDB(getApplicationContext()).getReadableDatabase(); | ||||
found = UserDB.isStopInFavorites(userDB, busStopId); | found = UserDB.isStopInFavorites(userDB, busStopId); | ||||
} | } | ||||
return found; | return found; | ||||
} | } | ||||
/** | /** | ||||
* Add the last Stop to favorites | * Add the last Stop to favorites | ||||
*/ | */ | ||||
@Override | |||||
public void toggleLastStopToFavorites() { | |||||
Stop stop = getLastSuccessfullySearchedBusStop(); | |||||
if (stop != null) { | |||||
// toggle the status in background | |||||
new AsyncStopFavoriteAction(getApplicationContext(), AsyncStopFavoriteAction.Action.TOGGLE) { | |||||
/** | |||||
* Callback fired when the Stop is saved in the favorites | |||||
* @param result | |||||
*/ | |||||
@Override | |||||
protected void onPostExecute(Boolean result) { | |||||
super.onPostExecute(result); | |||||
// update the star icon | |||||
updateStarIconFromLastBusStop(); | |||||
} | |||||
}.execute(stop); | |||||
} else { | |||||
// this case have no sense, but just immediately update the favorite icon | |||||
updateStarIconFromLastBusStop(); | |||||
} | |||||
} | |||||
@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(); | ||||
} | } | ||||
@Override | @Override | ||||
public void enableRefreshLayout(boolean yes) { | public void enableRefreshLayout(boolean yes) { | ||||
▲ Show 20 Lines • Show All 151 Lines • ▼ Show 20 Lines | public static String getBusStopIDFromUri(Uri uri) { | ||||
break; | break; | ||||
default: | default: | ||||
Log.e("ActivityMain", "Unexpected intent URL: " + uri); | Log.e("ActivityMain", "Unexpected intent URL: " + uri); | ||||
busStopID = null; | busStopID = null; | ||||
} | } | ||||
return busStopID; | return busStopID; | ||||
} | } | ||||
public void changeStarType(String stopID) { | |||||
if (isStopInFavorites(stopID)) { | |||||
changeStarFilled(); | |||||
} else { | |||||
changeStarOutline(); | |||||
} | |||||
} | |||||
public void changeStarFilled() { | |||||
addToFavorites.setImageResource(R.drawable.ic_star_filled); | |||||
} | |||||
public void changeStarOutline() { | |||||
addToFavorites.setImageResource(R.drawable.ic_star_outline); | |||||
} | |||||
} | } | ||||
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