diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 315cccd..182fb7e 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,47 +1,39 @@ + android:label="@string/app_name" android:screenOrientation="portrait"> - + android:label="@string/about" > - + android:label="@string/title_activity_favorites" > \ No newline at end of file diff --git a/res/layout/main_activity.xml b/res/layout/activity_main.xml similarity index 100% rename from res/layout/main_activity.xml rename to res/layout/activity_main.xml diff --git a/res/layout/bus_stop_entry.xml b/res/layout/bus_line_passage_entry.xml similarity index 100% copy from res/layout/bus_stop_entry.xml copy to res/layout/bus_line_passage_entry.xml diff --git a/res/layout/bus_stop_entry.xml b/res/layout/bus_stop_entry.xml index c31971c..33952c8 100644 --- a/res/layout/bus_stop_entry.xml +++ b/res/layout/bus_stop_entry.xml @@ -1,36 +1,30 @@ - - - + android:textAppearance="?android:attr/textAppearanceMedium" > + + + \ No newline at end of file diff --git a/src/it/reyboz/bustorino/FavoritesActivity.java b/src/it/reyboz/bustorino/FavoritesActivity.java index e625852..99351d2 100644 --- a/src/it/reyboz/bustorino/FavoritesActivity.java +++ b/src/it/reyboz/bustorino/FavoritesActivity.java @@ -1,73 +1,102 @@ package it.reyboz.bustorino; import java.util.ArrayList; import java.util.HashMap; import android.support.v7.app.ActionBarActivity; +import android.util.Log; import android.view.View; +import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; +import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; public class FavoritesActivity extends ActionBarActivity { private DBBusTo mDbHelper; private SQLiteDatabase db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_favorites); mDbHelper = new DBBusTo(this); db = mDbHelper.getWritableDatabase(); ArrayList> data = new ArrayList>(); String query = DBBusTo.SELECT_ALL_FROM + DBBusTo.BusStop.TABLE_NAME + DBBusTo.WHERE + DBBusTo .somethingEqualsInt(DBBusTo.BusStop.COLUMN_NAME_BUSSTOP_ISFAVORITE) + DBBusTo.ORDER_BY + DBBusTo.BusStop.COLUMN_NAME_BUSSTOP_ID + DBBusTo.ASC; Cursor cursor = db.rawQuery(query, new String[] { "1" }); while (cursor.moveToNext()) { HashMap singleEntry = new HashMap(); int busStopIDIndex = cursor .getColumnIndex(DBBusTo.BusStop.COLUMN_NAME_BUSSTOP_ID); int busStopNameIndex = cursor .getColumnIndex(DBBusTo.BusStop.COLUMN_NAME_BUSSTOP_NAME); int busStopID = cursor.getInt(busStopIDIndex); String busStopName = cursor.getString(busStopNameIndex); - singleEntry.put("icon", busStopID); - singleEntry.put("line-name", busStopName); + singleEntry.put("bus-stop-ID", busStopID); + singleEntry.put("bus-stop-name", busStopName); data.add(singleEntry); } cursor.close(); ListView favoriteListView = (ListView) findViewById(R.id.favoriteListView); // If no data is found show a friendly message if (data.isEmpty()) { favoriteListView.setVisibility(View.INVISIBLE); TextView favoriteTipTextView = (TextView) findViewById(R.id.favoriteTipTextView); favoriteTipTextView.setVisibility(View.VISIBLE); } // Show results - String[] from = { "icon", "line-name" }; - int[] to = { R.id.busLineIcon, R.id.busLine }; + String[] from = { "bus-stop-ID", "bus-stop-name" }; + int[] to = { R.id.busStopID, R.id.busStopName }; SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), data, R.layout.bus_stop_entry, from, to); favoriteListView.setAdapter(adapter); + favoriteListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + public void onItemClick(AdapterView av, View view, + int i, long l) { + View child = av.getChildAt(i); + TextView busStopIDTextView = (TextView) child + .findViewById(R.id.busStopID); + String busStopID = busStopIDTextView.getText().toString(); + Log.d("bus-torino", "bustorino tapped on busstop: " + busStopID); + Intent intent = new Intent(FavoritesActivity.this, MainActivity.class); + Bundle b = new Bundle(); + b.putString("busStopID", busStopID); + intent.putExtras(b); + startActivity(intent); + finish(); + } + }); + favoriteListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { + + @Override + public boolean onItemLongClick(AdapterView parent, View view, + int position, long id) { + // TODO Auto-generated method stub + return false; + } + + }); // Close DB connection db.close(); } } diff --git a/src/it/reyboz/bustorino/MainActivity.java b/src/it/reyboz/bustorino/MainActivity.java index df61484..645a35c 100644 --- a/src/it/reyboz/bustorino/MainActivity.java +++ b/src/it/reyboz/bustorino/MainActivity.java @@ -1,236 +1,251 @@ package it.reyboz.bustorino; import java.util.ArrayList; import java.util.HashMap; import it.reyboz.bustorino.GTTSiteSucker.PassagesBusLine; import it.reyboz.bustorino.GTTSiteSucker.BusStop; import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; -import android.view.WindowManager; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputMethodManager; import android.widget.AdapterView; import android.widget.EditText; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; import android.view.KeyEvent; public class MainActivity extends ActionBarActivity { private MyAsyncWget myAsyncWget; private TextView busStationName; private EditText busStopNumberEditText; private ProgressBar annoyingFedbackProgressBar; private Integer lastSearchedBusStopID; private DBBusTo mDbHelper; private SQLiteDatabase db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.main_activity); + setContentView(R.layout.activity_main); busStationName = (TextView) findViewById(R.id.busStationName); busStopNumberEditText = (EditText) findViewById(R.id.busStopNumberEditText); annoyingFedbackProgressBar = (ProgressBar) findViewById(R.id.annoyingFedbackProgress); annoyingFedbackProgressBar.setVisibility(View.INVISIBLE); myAsyncWget = new MyAsyncWget(); busStopNumberEditText .setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { searchClick(v); return true; } return false; } }); // Gets the data repository in write mode mDbHelper = new DBBusTo(this); db = mDbHelper.getWritableDatabase(); + + // Intercept calls from other part of the apps + Bundle b = getIntent().getExtras(); + if(b != null) { + String busStopID = b.getString("busStopID"); + if(busStopID != null) { + launchSearchAction(busStopID); + busStopNumberEditText.setText(busStopID); + } + } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. switch (item.getItemId()) { /* * case R.id.action_settings: Intent intentSettings = new * Intent(MainActivity.this,AboutActivity.class); * startActivity(intentSettings); return true; */ case R.id.action_about: Intent intentAbout = new Intent(MainActivity.this, AboutActivity.class); startActivity(intentAbout); return true; case R.id.action_favorites: Intent intentFavorites = new Intent(MainActivity.this, FavoritesActivity.class); startActivity(intentFavorites); return true; } return super.onOptionsItemSelected(item); } /** * Catch the Async action! * * @author boz */ public class MyAsyncWget extends AsyncWget { protected void onPostExecute(String result) { ArrayList> data = new ArrayList>(); BusStop busStop = GTTSiteSucker .arrivalTimesBylineHTMLSucker(result); // Remember that the user searched this at last lastSearchedBusStopID = busStop.getBusStopID(); PassagesBusLine[] passagesBusLine = busStop.getPassagesBusLine(); for (PassagesBusLine passageBusLine : passagesBusLine) { HashMap singleEntry = new HashMap(); singleEntry.put("icon", passageBusLine.getBusLineName()); singleEntry.put("line-name", passageBusLine.getTimePassagesString()); data.add(singleEntry); } if (passagesBusLine.length != 0) { // Hide the keyboard before showing results hideKeyboard(); Integer busStopID = busStop.getBusStopID(); String busStopName = busStop.getBusStopName(); String busStopNameDisplay = "---"; if (busStopName == null && busStopID != null) { busStopNameDisplay = String.valueOf((busStopID)); } else { busStopNameDisplay = busStopName; } busStationName.setText(String.format( getResources().getString(R.string.passages), busStopNameDisplay)); // Insert the GTTBusStop in the DB if (busStopID != null) { ContentValues values = new ContentValues(); values.put(DBBusTo.BusStop.COLUMN_NAME_BUSSTOP_ID, busStopID); values.put(DBBusTo.BusStop.COLUMN_NAME_BUSSTOP_NAME, busStopName); long lastInserted = db.insertWithOnConflict( DBBusTo.BusStop.TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_IGNORE); Log.d("bus-torino", "bustorinoDB last BusStop inserted: " + lastInserted); } // Show results String[] from = { "icon", "line-name" }; int[] to = { R.id.busLineIcon, R.id.busLine }; SimpleAdapter adapter = new SimpleAdapter( - getApplicationContext(), data, R.layout.bus_stop_entry, + getApplicationContext(), data, R.layout.bus_line_passage_entry, from, to); ListView tpm = (ListView) findViewById(R.id.resultsListView); tpm.setAdapter(adapter); tpm.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView av, View view, int i, long l) { View child = av.getChildAt(i); TextView busLineIcon = (TextView) child .findViewById(R.id.busLineIcon); + String busLine = busLineIcon.getText().toString(); + Log.d("bus-torino", "bustorino tapped on busline: " + busLine); //Toast.makeText(MainActivity.this, // "myPos " + i + " " + busLineIcon.getText(), // Toast.LENGTH_LONG).show(); } }); } else { Toast.makeText(getApplicationContext(), R.string.no_arrival_times, Toast.LENGTH_SHORT).show(); } annoyingFedbackProgressBar.setVisibility(View.INVISIBLE); } } public void searchClick(View v) { - String query = busStopNumberEditText.getText().toString(); - if (query.isEmpty()) { + String busStopID = busStopNumberEditText.getText().toString(); + launchSearchAction(busStopID); + } + + public void launchSearchAction(String busStopID) { + if (busStopID.isEmpty()) { Toast.makeText(getApplicationContext(), R.string.insert_bus_stop_number, Toast.LENGTH_SHORT).show(); } else if (!NetworkTools.isConnected(this)) { NetworkTools.showNetworkError(this); } else { annoyingFedbackProgressBar.setVisibility(View.VISIBLE); myAsyncWget.cancel(true); myAsyncWget = new MyAsyncWget(); - myAsyncWget.execute(GTTSiteSucker.arrivalTimesByLineQuery(query)); + myAsyncWget.execute(GTTSiteSucker.arrivalTimesByLineQuery(busStopID)); } } public void addInFavorites(View v) { if (lastSearchedBusStopID != null) { ContentValues newValues = new ContentValues(); newValues.put(DBBusTo.BusStop.COLUMN_NAME_BUSSTOP_ISFAVORITE, 1); db.update( DBBusTo.BusStop.TABLE_NAME, newValues, DBBusTo.somethingEqualsInt(DBBusTo.BusStop.COLUMN_NAME_BUSSTOP_ID), new String[] { String.valueOf(lastSearchedBusStopID) }); Toast.makeText(getApplicationContext(), R.string.added_in_favorites, Toast.LENGTH_SHORT).show(); } } private void hideKeyboard() { InputMethodManager inputManager = (InputMethodManager) this .getSystemService(Context.INPUT_METHOD_SERVICE); // Check if no view has focus: View view = this.getCurrentFocus(); if (view != null) { inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } /* * private void spamThePoorUser() { AlertDialog.Builder alert = new * AlertDialog.Builder(this); alert.setTitle(R.string.app_name); * alert.setMessage(R.string.app_description); * alert.setNegativeButton(android.R.string.ok, new * DialogInterface.OnClickListener() { public void onClick(DialogInterface * dialog, int whichButton) { } }); * * alert.show(); } */ }