Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/ActivityFavorites.java
Show All 11 Lines | BusTO - Arrival times for Turin public transports. | ||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
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.database.Cursor; | |||||
import android.support.v4.app.LoaderManager; | |||||
import android.support.v4.content.Loader; | |||||
import android.widget.*; | |||||
import it.reyboz.bustorino.backend.Stop; | |||||
import it.reyboz.bustorino.adapters.StopAdapter; | |||||
import it.reyboz.bustorino.middleware.AsyncStopFavoriteAction; | |||||
import it.reyboz.bustorino.middleware.StopsDB; | |||||
import it.reyboz.bustorino.middleware.UserDB; | |||||
import android.app.AlertDialog; | import android.app.AlertDialog; | ||||
import android.content.Context; | import android.content.Context; | ||||
import android.content.DialogInterface; | import android.content.DialogInterface; | ||||
import android.net.Uri; | import android.content.Intent; | ||||
import android.database.Cursor; | |||||
import android.database.sqlite.SQLiteDatabase; | |||||
import android.os.AsyncTask; | import android.os.AsyncTask; | ||||
import android.os.Bundle; | |||||
import android.support.v4.app.LoaderManager; | |||||
import android.support.v4.app.NavUtils; | import android.support.v4.app.NavUtils; | ||||
import android.support.v4.content.Loader; | |||||
import android.support.v7.app.ActionBar; | import android.support.v7.app.ActionBar; | ||||
import android.support.v7.app.AppCompatActivity; | import android.support.v7.app.AppCompatActivity; | ||||
import android.view.ContextMenu; | import android.view.ContextMenu; | ||||
import android.view.ContextMenu.ContextMenuInfo; | import android.view.ContextMenu.ContextMenuInfo; | ||||
import android.view.LayoutInflater; | import android.view.LayoutInflater; | ||||
import android.view.MenuInflater; | import android.view.MenuInflater; | ||||
import android.view.MenuItem; | import android.view.MenuItem; | ||||
import android.view.View; | import android.view.View; | ||||
import android.widget.AdapterView; | |||||
import android.widget.AdapterView.AdapterContextMenuInfo; | import android.widget.AdapterView.AdapterContextMenuInfo; | ||||
import android.content.Intent; | import android.widget.EditText; | ||||
import android.database.sqlite.SQLiteDatabase; | import android.widget.ImageView; | ||||
import android.os.Bundle; | import android.widget.ListView; | ||||
import android.widget.TextView; | |||||
import android.widget.Toast; | |||||
import java.util.List; | import java.util.List; | ||||
import it.reyboz.bustorino.adapters.StopAdapter; | |||||
import it.reyboz.bustorino.backend.Stop; | |||||
import it.reyboz.bustorino.middleware.AsyncStopFavoriteAction; | |||||
import it.reyboz.bustorino.middleware.StopsDB; | |||||
import it.reyboz.bustorino.middleware.UserDB; | |||||
public class ActivityFavorites extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> { | public class ActivityFavorites extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> { | ||||
private ListView favoriteListView; | private ListView favoriteListView; | ||||
private SQLiteDatabase userDB; | private SQLiteDatabase userDB; | ||||
private EditText bus_stop_name; | private EditText bus_stop_name; | ||||
@Override | @Override | ||||
protected void onCreate(Bundle savedInstanceState) { | protected void onCreate(Bundle savedInstanceState) { | ||||
super.onCreate(savedInstanceState); | super.onCreate(savedInstanceState); | ||||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | public boolean onContextItemSelected(MenuItem item) { | ||||
return true; | return true; | ||||
case R.id.action_view_on_map: | case R.id.action_view_on_map: | ||||
final String theGeoUrl = busStop.getGeoURL(); | final String theGeoUrl = busStop.getGeoURL(); | ||||
if(theGeoUrl==null){ | if(theGeoUrl==null){ | ||||
//doesn't have a position | //doesn't have a position | ||||
Toast.makeText(getApplicationContext(),R.string.cannot_show_on_map_no_position,Toast.LENGTH_SHORT).show(); | Toast.makeText(getApplicationContext(),R.string.cannot_show_on_map_no_position,Toast.LENGTH_SHORT).show(); | ||||
return true; | return true; | ||||
} | } | ||||
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(theGeoUrl)); | |||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | // start ActivityMap with these extras in intent | ||||
if(intent.resolveActivity(getPackageManager())!=null) | Intent intent = new Intent(ActivityFavorites.this, ActivityMap.class); | ||||
Bundle b = new Bundle(); | |||||
b.putDouble("lat", busStop.getLatitude()); | |||||
b.putDouble("lon", busStop.getLongitude()); | |||||
b.putString("name", busStop.getStopDefaultName()); | |||||
b.putString("ID", busStop.ID); | |||||
intent.putExtras(b); | |||||
startActivity(intent); | startActivity(intent); | ||||
else { | |||||
Toast.makeText(getApplicationContext(),R.string.cannot_show_on_map_no_activity,Toast.LENGTH_SHORT).show(); | |||||
} | |||||
return true; | return true; | ||||
default: | default: | ||||
return super.onContextItemSelected(item); | return super.onContextItemSelected(item); | ||||
} | } | ||||
} | } | ||||
void createFavoriteList() { | void createFavoriteList() { | ||||
// TODO: memoize default list, query only user names every time? | // TODO: memoize default list, query only user names every time? | ||||
▲ Show 20 Lines • Show All 103 Lines • ▼ Show 20 Lines | private class AsyncGetFavorites extends AsyncTask<Void, Void, List<Stop>> { | ||||
@Override | @Override | ||||
protected void onPostExecute(List<Stop> busStops) { | protected void onPostExecute(List<Stop> busStops) { | ||||
// If no data is found show a friendly message | // If no data is found show a friendly message | ||||
if (busStops.size() == 0) { | if (busStops.size() == 0) { | ||||
favoriteListView.setVisibility(View.INVISIBLE); | favoriteListView.setVisibility(View.INVISIBLE); | ||||
TextView favoriteTipTextView = (TextView) findViewById(R.id.favoriteTipTextView); | TextView favoriteTipTextView = (TextView) findViewById(R.id.favoriteTipTextView); | ||||
assert favoriteTipTextView != null; | assert favoriteTipTextView != null; | ||||
favoriteTipTextView.setVisibility(View.VISIBLE); | favoriteTipTextView.setVisibility(View.VISIBLE); | ||||
ImageView angeryBusImageView = (ImageView) findViewById(R.id.angeryBusImageView); | |||||
angeryBusImageView.setVisibility(View.VISIBLE); | |||||
} | } | ||||
/* There's a nice method called notifyDataSetChanged() to avoid building the ListView | /* There's a nice method called notifyDataSetChanged() to avoid building the ListView | ||||
* all over again. This method exists in a billion answers on Stack Overflow, but | * all over again. This method exists in a billion answers on Stack Overflow, but | ||||
* it's nowhere to be seen around here, Android Studio can't find it no matter what. | * it's nowhere to be seen around here, Android Studio can't find it no matter what. | ||||
* Anyway, it only works from Android 2.3 onward (which is why it refuses to appear, I | * Anyway, it only works from Android 2.3 onward (which is why it refuses to appear, I | ||||
* guess) and requires to modify the list with .add() and .clear() and some other | * guess) and requires to modify the list with .add() and .clear() and some other | ||||
* methods, so to update a single stop we need to completely rebuild the list for no | * methods, so to update a single stop we need to completely rebuild the list for no | ||||
Show All 36 Lines |
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