Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/ActivityPrincipal.java
package it.reyboz.bustorino; | package it.reyboz.bustorino; | ||||
import android.content.Intent; | |||||
import android.content.SharedPreferences; | |||||
import android.content.res.Configuration; | import android.content.res.Configuration; | ||||
import android.content.res.Resources; | import android.net.Uri; | ||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.util.Log; | import android.util.Log; | ||||
import android.view.Menu; | |||||
import android.view.MenuItem; | import android.view.MenuItem; | ||||
import android.widget.Toast; | |||||
import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
import androidx.annotation.Nullable; | import androidx.annotation.Nullable; | ||||
import androidx.appcompat.app.ActionBarDrawerToggle; | import androidx.appcompat.app.ActionBarDrawerToggle; | ||||
import androidx.appcompat.widget.Toolbar; | import androidx.appcompat.widget.Toolbar; | ||||
import androidx.core.app.NavUtils; | |||||
import androidx.core.view.GravityCompat; | import androidx.core.view.GravityCompat; | ||||
import androidx.drawerlayout.widget.DrawerLayout; | import androidx.drawerlayout.widget.DrawerLayout; | ||||
import androidx.fragment.app.Fragment; | |||||
import androidx.fragment.app.FragmentManager; | |||||
import androidx.fragment.app.FragmentTransaction; | |||||
import androidx.work.BackoffPolicy; | |||||
import androidx.work.Constraints; | |||||
import androidx.work.ExistingPeriodicWorkPolicy; | |||||
import androidx.work.NetworkType; | |||||
import androidx.work.PeriodicWorkRequest; | |||||
import androidx.work.WorkInfo; | |||||
import androidx.work.WorkManager; | |||||
import com.google.android.material.navigation.NavigationView; | import com.google.android.material.navigation.NavigationView; | ||||
import com.google.android.material.snackbar.Snackbar; | |||||
import java.util.concurrent.TimeUnit; | |||||
import it.reyboz.bustorino.data.DBUpdateWorker; | |||||
import it.reyboz.bustorino.data.DatabaseUpdate; | |||||
import it.reyboz.bustorino.fragments.FragmentKind; | |||||
import it.reyboz.bustorino.fragments.FragmentListenerMain; | |||||
import it.reyboz.bustorino.fragments.MainScreenFragment; | |||||
import it.reyboz.bustorino.middleware.GeneralActivity; | import it.reyboz.bustorino.middleware.GeneralActivity; | ||||
public class ActivityPrincipal extends GeneralActivity { | import static it.reyboz.bustorino.backend.utils.getBusStopIDFromUri; | ||||
import static it.reyboz.bustorino.backend.utils.openIceweasel; | |||||
public class ActivityPrincipal extends GeneralActivity implements FragmentListenerMain { | |||||
private DrawerLayout mDrawer; | private DrawerLayout mDrawer; | ||||
private NavigationView mNavView; | private NavigationView mNavView; | ||||
private ActionBarDrawerToggle drawerToggle; | private ActionBarDrawerToggle drawerToggle; | ||||
private final static String DEBUG_TAG="BusTO Act Principal"; | private final static String DEBUG_TAG="BusTO Act Principal"; | ||||
private Snackbar snackbar; | |||||
@Override | @Override | ||||
protected void onCreate(@Nullable Bundle savedInstanceState) { | protected void onCreate(@Nullable Bundle savedInstanceState) { | ||||
super.onCreate(savedInstanceState); | super.onCreate(savedInstanceState); | ||||
setContentView(R.layout.new_main_activity); | setContentView(R.layout.activity_principal); | ||||
final SharedPreferences theShPr = getMainSharedPreferences(); | |||||
Toolbar mToolbar = findViewById(R.id.default_toolbar); | Toolbar mToolbar = findViewById(R.id.default_toolbar); | ||||
setSupportActionBar(mToolbar); | setSupportActionBar(mToolbar); | ||||
if (getSupportActionBar()!=null) | if (getSupportActionBar()!=null) | ||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true); | getSupportActionBar().setDisplayHomeAsUpEnabled(true); | ||||
else Log.w(DEBUG_TAG, "NO ACTION BAR"); | else Log.w(DEBUG_TAG, "NO ACTION BAR"); | ||||
mToolbar.setOnMenuItemClickListener(new ToolbarItemClickListener()); | |||||
mDrawer = findViewById(R.id.drawer_layout); | mDrawer = findViewById(R.id.drawer_layout); | ||||
drawerToggle = setupDrawerToggle(mToolbar); | drawerToggle = setupDrawerToggle(mToolbar); | ||||
// Setup toggle to display hamburger icon with nice animation | // Setup toggle to display hamburger icon with nice animation | ||||
drawerToggle.setDrawerIndicatorEnabled(true); | drawerToggle.setDrawerIndicatorEnabled(true); | ||||
drawerToggle.syncState(); | drawerToggle.syncState(); | ||||
mDrawer.addDrawerListener(drawerToggle); | mDrawer.addDrawerListener(drawerToggle); | ||||
mNavView = findViewById(R.id.nvView); | mNavView = findViewById(R.id.nvView); | ||||
setupDrawerContent(mNavView); | setupDrawerContent(mNavView); | ||||
/// LEGACY CODE | |||||
//---------------------------- START INTENT CHECK QUEUE ------------------------------------ | |||||
// Intercept calls from URL intent | |||||
boolean tryedFromIntent = false; | |||||
String busStopID = null; | |||||
Uri data = getIntent().getData(); | |||||
if (data != null) { | |||||
busStopID = getBusStopIDFromUri(data); | |||||
tryedFromIntent = true; | |||||
} | |||||
// Intercept calls from other activities | |||||
if (!tryedFromIntent) { | |||||
Bundle b = getIntent().getExtras(); | |||||
if (b != null) { | |||||
busStopID = b.getString("bus-stop-ID"); | |||||
/** | |||||
* I'm not very sure if you are coming from an Intent. | |||||
* Some launchers work in strange ways. | |||||
*/ | |||||
tryedFromIntent = busStopID != null; | |||||
} | |||||
} | |||||
//---------------------------- END INTENT CHECK QUEUE -------------------------------------- | |||||
if (busStopID == null) { | |||||
// Show keyboard if can't start from intent | |||||
// JUST DON'T | |||||
// showKeyboard(); | |||||
// You haven't obtained anything... from an intent? | |||||
if (tryedFromIntent) { | |||||
// This shows a luser warning | |||||
Toast.makeText(getApplicationContext(), | |||||
R.string.insert_bus_stop_number_error, Toast.LENGTH_SHORT).show(); | |||||
} | |||||
} else { | |||||
// If you are here an intent has worked successfully | |||||
//setBusStopSearchByIDEditText(busStopID); | |||||
requestArrivalsForStopID(busStopID); | |||||
} | |||||
//Try (hopefully) database update | |||||
PeriodicWorkRequest wr = new PeriodicWorkRequest.Builder(DBUpdateWorker.class, 1, TimeUnit.DAYS) | |||||
.setBackoffCriteria(BackoffPolicy.LINEAR, 30, TimeUnit.MINUTES) | |||||
.setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED) | |||||
.build()) | |||||
.build(); | |||||
final WorkManager workManager = WorkManager.getInstance(this); | |||||
final int version = theShPr.getInt(DatabaseUpdate.DB_VERSION_KEY, -10); | |||||
if (version >= 0) | |||||
workManager.enqueueUniquePeriodicWork(DBUpdateWorker.DEBUG_TAG, | |||||
ExistingPeriodicWorkPolicy.KEEP, wr); | |||||
else workManager.enqueueUniquePeriodicWork(DBUpdateWorker.DEBUG_TAG, | |||||
ExistingPeriodicWorkPolicy.REPLACE, wr); | |||||
/* | |||||
Set database update | |||||
*/ | |||||
workManager.getWorkInfosForUniqueWorkLiveData(DBUpdateWorker.DEBUG_TAG) | |||||
.observe(this, workInfoList -> { | |||||
// If there are no matching work info, do nothing | |||||
if (workInfoList == null || workInfoList.isEmpty()) { | |||||
return; | |||||
} | |||||
Log.d(DEBUG_TAG, "WorkerInfo: "+workInfoList); | |||||
boolean showProgress = false; | |||||
for (WorkInfo workInfo : workInfoList) { | |||||
if (workInfo.getState() == WorkInfo.State.RUNNING) { | |||||
showProgress = true; | |||||
} | |||||
} | |||||
if (showProgress) { | |||||
createDefaultSnackbar(); | |||||
} else { | |||||
if(snackbar!=null) { | |||||
snackbar.dismiss(); | |||||
snackbar = null; | |||||
} | |||||
} | |||||
}); | |||||
// show the main fragment | |||||
showMainFragment(); | |||||
} | } | ||||
private ActionBarDrawerToggle setupDrawerToggle(Toolbar toolbar) { | private ActionBarDrawerToggle setupDrawerToggle(Toolbar toolbar) { | ||||
// NOTE: Make sure you pass in a valid toolbar reference. ActionBarDrawToggle() does not require it | // NOTE: Make sure you pass in a valid toolbar reference. ActionBarDrawToggle() does not require it | ||||
// and will not render the hamburger icon without it. | // and will not render the hamburger icon without it. | ||||
return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close); | return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close); | ||||
} | } | ||||
private void setupDrawerContent(NavigationView navigationView) { | private void setupDrawerContent(NavigationView navigationView) { | ||||
navigationView.setNavigationItemSelectedListener( | navigationView.setNavigationItemSelectedListener( | ||||
menuItem -> { | menuItem -> { | ||||
if (menuItem.getItemId() == R.id.drawer_action_settings) { | |||||
Log.d("MAINBusTO", "Pressed button preferences"); | |||||
closeDrawerIfOpen(); | |||||
startActivity(new Intent(ActivityPrincipal.this, ActivitySettings.class)); | |||||
return true; | |||||
} | |||||
//selectDrawerItem(menuItem); | //selectDrawerItem(menuItem); | ||||
Log.d(DEBUG_TAG, "pressed item "+menuItem.toString()); | Log.d(DEBUG_TAG, "pressed item "+menuItem.toString()); | ||||
return true; | return true; | ||||
}); | }); | ||||
} | } | ||||
private void closeDrawerIfOpen(){ | |||||
if (mDrawer.isDrawerOpen(GravityCompat.START)) | |||||
mDrawer.closeDrawer(GravityCompat.START); | |||||
} | |||||
// `onPostCreate` called when activity start-up is complete after `onStart()` | // `onPostCreate` called when activity start-up is complete after `onStart()` | ||||
// NOTE 1: Make sure to override the method with only a single `Bundle` argument | // NOTE 1: Make sure to override the method with only a single `Bundle` argument | ||||
// Note 2: Make sure you implement the correct `onPostCreate(Bundle savedInstanceState)` method. | // Note 2: Make sure you implement the correct `onPostCreate(Bundle savedInstanceState)` method. | ||||
// There are 2 signatures and only `onPostCreate(Bundle state)` shows the hamburger icon. | // There are 2 signatures and only `onPostCreate(Bundle state)` shows the hamburger icon. | ||||
@Override | @Override | ||||
protected void onPostCreate(Bundle savedInstanceState) { | protected void onPostCreate(Bundle savedInstanceState) { | ||||
super.onPostCreate(savedInstanceState); | super.onPostCreate(savedInstanceState); | ||||
// Sync the toggle state after onRestoreInstanceState has occurred. | // Sync the toggle state after onRestoreInstanceState has occurred. | ||||
drawerToggle.syncState(); | drawerToggle.syncState(); | ||||
} | } | ||||
@Override | @Override | ||||
public void onConfigurationChanged(@NonNull Configuration newConfig) { | public void onConfigurationChanged(@NonNull Configuration newConfig) { | ||||
super.onConfigurationChanged(newConfig); | super.onConfigurationChanged(newConfig); | ||||
// Pass any configuration change to the drawer toggles | // Pass any configuration change to the drawer toggles | ||||
drawerToggle.onConfigurationChanged(newConfig); | drawerToggle.onConfigurationChanged(newConfig); | ||||
} | } | ||||
@Override | @Override | ||||
public boolean onCreateOptionsMenu(Menu menu) { | |||||
getMenuInflater().inflate(R.menu.extra_menu_items, menu); | |||||
return super.onCreateOptionsMenu(menu); | |||||
} | |||||
@Override | |||||
public boolean onOptionsItemSelected(@NonNull MenuItem item) { | public boolean onOptionsItemSelected(@NonNull MenuItem item) { | ||||
int[] cases = {R.id.nav_arrivals, R.id.nav_favorites_item}; | int[] cases = {R.id.nav_arrivals, R.id.nav_favorites_item}; | ||||
Log.d(DEBUG_TAG, "Item pressed"); | |||||
switch (item.getItemId()){ | switch (item.getItemId()){ | ||||
case android.R.id.home: | case android.R.id.home: | ||||
mDrawer.openDrawer(GravityCompat.START); | mDrawer.openDrawer(GravityCompat.START); | ||||
return true; | return true; | ||||
case R.id.nav_arrivals: | |||||
//do something | |||||
break; | |||||
default: | default: | ||||
} | } | ||||
if (drawerToggle.onOptionsItemSelected(item)) { | if (drawerToggle.onOptionsItemSelected(item)) { | ||||
return true; | return true; | ||||
} | } | ||||
return super.onOptionsItemSelected(item); | return super.onOptionsItemSelected(item); | ||||
} | } | ||||
@Override | @Override | ||||
public void onBackPressed() { | public void onBackPressed() { | ||||
boolean foundFragment = false; | |||||
Fragment shownFrag = getSupportFragmentManager().findFragmentById(R.id.mainActContentFrame); | |||||
if (mDrawer.isDrawerOpen(GravityCompat.START)) | if (mDrawer.isDrawerOpen(GravityCompat.START)) | ||||
mDrawer.closeDrawer(GravityCompat.START); | mDrawer.closeDrawer(GravityCompat.START); | ||||
else if(shownFrag != null && shownFrag.isVisible() && shownFrag.getChildFragmentManager().getBackStackEntryCount() > 0){ | |||||
shownFrag.getChildFragmentManager().popBackStack(); | |||||
} | |||||
else if (getSupportFragmentManager().getBackStackEntryCount() > 0) { | |||||
getSupportFragmentManager().popBackStack(); | |||||
} | |||||
else | else | ||||
super.onBackPressed(); | super.onBackPressed(); | ||||
} | } | ||||
private void createDefaultSnackbar() { | |||||
if (snackbar == null) { | |||||
snackbar = Snackbar.make(findViewById(R.id.searchButton), R.string.database_update_message, Snackbar.LENGTH_INDEFINITE); | |||||
} | |||||
snackbar.show(); | |||||
} | |||||
private MainScreenFragment showMainFragment(){ | |||||
FragmentManager fraMan = getSupportFragmentManager(); | |||||
MainScreenFragment fragment = MainScreenFragment.newInstance(); | |||||
FragmentTransaction transaction = fraMan.beginTransaction(); | |||||
transaction.replace(R.id.mainActContentFrame, fragment, MainScreenFragment.FRAGMENT_TAG); | |||||
transaction.commit(); | |||||
return fragment; | |||||
} | |||||
@Nullable | |||||
private MainScreenFragment getMainFragmentIfVisible(){ | |||||
FragmentManager fraMan = getSupportFragmentManager(); | |||||
Fragment fragment = fraMan.findFragmentByTag(MainScreenFragment.FRAGMENT_TAG); | |||||
if (fragment!= null && fragment.isVisible()) return (MainScreenFragment) fragment; | |||||
else return null; | |||||
} | |||||
@Override | |||||
public void showFloatingActionButton(boolean yes) { | |||||
//TODO | |||||
} | |||||
@Override | |||||
public void readyGUIfor(FragmentKind fragmentType) { | |||||
MainScreenFragment probableFragment = getMainFragmentIfVisible(); | |||||
if (probableFragment!=null){ | |||||
probableFragment.readyGUIfor(fragmentType); | |||||
} | |||||
} | |||||
@Override | |||||
public void requestArrivalsForStopID(String ID) { | |||||
FragmentManager fraMan = getSupportFragmentManager(); | |||||
Fragment fragment = fraMan.findFragmentByTag(MainScreenFragment.FRAGMENT_TAG); | |||||
MainScreenFragment mainScreenFragment = null; | |||||
if (fragment==null | !(fragment instanceof MainScreenFragment)){ | |||||
mainScreenFragment = showMainFragment(); | |||||
} | |||||
else if(!fragment.isVisible()){ | |||||
fraMan.beginTransaction().replace(R.id.mainActContentFrame, fragment) | |||||
.addToBackStack(null) | |||||
.commit(); | |||||
mainScreenFragment = (MainScreenFragment) fragment; | |||||
Log.d(DEBUG_TAG, "Found the main fragment"); | |||||
} else{ | |||||
mainScreenFragment = (MainScreenFragment) fragment; | |||||
} | |||||
mainScreenFragment.requestArrivalsForStopID(ID); | |||||
} | |||||
@Override | |||||
public void toggleSpinner(boolean state) { | |||||
MainScreenFragment probableFragment = getMainFragmentIfVisible(); | |||||
if (probableFragment!=null){ | |||||
probableFragment.toggleSpinner(state); | |||||
} | |||||
} | |||||
@Override | |||||
public void enableRefreshLayout(boolean yes) { | |||||
MainScreenFragment probableFragment = getMainFragmentIfVisible(); | |||||
if (probableFragment!=null){ | |||||
probableFragment.enableRefreshLayout(yes); | |||||
} | |||||
} | |||||
class ToolbarItemClickListener implements Toolbar.OnMenuItemClickListener{ | |||||
@Override | |||||
public boolean onMenuItemClick(MenuItem item) { | |||||
switch (item.getItemId()) { | |||||
case R.id.action_about: | |||||
startActivity(new Intent(ActivityPrincipal.this, ActivityAbout.class)); | |||||
return true; | |||||
case R.id.action_hack: | |||||
openIceweasel(getString(R.string.hack_url), getApplicationContext()); | |||||
return true; | |||||
case R.id.action_source: | |||||
openIceweasel("https://gitpull.it/source/libre-busto/", getApplicationContext()); | |||||
return true; | |||||
case R.id.action_licence: | |||||
openIceweasel("https://www.gnu.org/licenses/gpl-3.0.html", getApplicationContext()); | |||||
return true; | |||||
default: | |||||
} | |||||
return false; | |||||
} | |||||
} | |||||
} | } |
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