407 lines
13 KiB
Java
407 lines
13 KiB
Java
package ru.vfilippov.electronreminder;
|
|
|
|
import android.app.AlertDialog;
|
|
import android.content.BroadcastReceiver;
|
|
import android.content.Context;
|
|
import android.content.DialogInterface;
|
|
import android.content.Intent;
|
|
import android.content.IntentFilter;
|
|
import android.content.SharedPreferences;
|
|
import android.support.v4.content.LocalBroadcastManager;
|
|
import android.support.v7.app.AppCompatActivity;
|
|
import android.os.Bundle;
|
|
import android.text.Editable;
|
|
import android.text.TextWatcher;
|
|
import android.util.Log;
|
|
import android.util.Xml;
|
|
import android.view.ContextThemeWrapper;
|
|
import android.view.LayoutInflater;
|
|
import android.view.Menu;
|
|
import android.view.MenuItem;
|
|
import android.view.View;
|
|
import android.widget.AdapterView;
|
|
import android.widget.ArrayAdapter;
|
|
import android.widget.AutoCompleteTextView;
|
|
import android.widget.ListAdapter;
|
|
import android.widget.ListView;
|
|
import android.widget.TextView;
|
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
|
import org.xmlpull.v1.XmlSerializer;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import simple.SimpleXml;
|
|
import simple.SimpleXmlNode;
|
|
|
|
public class MainActivity extends AppCompatActivity
|
|
{
|
|
List<Trip> trips;
|
|
Map<String,SimpleXmlNode> reminders;
|
|
ArrayAdapter<String> stationsAdapter, citiesAdapter;
|
|
List<String> stations;
|
|
Map<String,String> stationIds;
|
|
String curCity, curFrom, curTo, curFromName, curToName;
|
|
|
|
private final String TAG = "ElectronReminder";
|
|
|
|
private AlertDialog citySelectDialog = null;
|
|
private RecheckAlarmReceiver alarmer = new RecheckAlarmReceiver();
|
|
|
|
private BroadcastReceiver srvReceiver = new BroadcastReceiver()
|
|
{
|
|
@Override
|
|
public void onReceive(Context context, Intent intent)
|
|
{
|
|
if (intent.getAction() == "cities_loaded")
|
|
loadCitiesFile();
|
|
else if (intent.getAction() == "stations_loaded")
|
|
loadStationsFile();
|
|
else if (intent.getAction() == "trips_loaded")
|
|
loadTripsFile();
|
|
}
|
|
};
|
|
|
|
private boolean loadCitiesFile()
|
|
{
|
|
SimpleXmlNode n = SimpleXml.readXmlFile(getFilesDir(), "suburban_cities.xml");
|
|
if (n != null)
|
|
{
|
|
if (citiesAdapter != null)
|
|
citiesAdapter.notifyDataSetChanged();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private void loadCities()
|
|
{
|
|
if (!loadCitiesFile())
|
|
{
|
|
Intent intent = new Intent(this, ElectronReminderService.class);
|
|
intent.setAction("load_cities");
|
|
intent.putExtra("cityId", curCity);
|
|
startService(intent);
|
|
}
|
|
}
|
|
|
|
private boolean loadStationsFile()
|
|
{
|
|
SimpleXmlNode n = SimpleXml.readXmlFile(getFilesDir(), "stations_"+curCity+".xml");
|
|
if (n != null)
|
|
{
|
|
List<SimpleXmlNode> sts = n.childrenByName.get("station");
|
|
stations.clear();
|
|
stationIds.clear();
|
|
int i = 0;
|
|
for (SimpleXmlNode s: sts)
|
|
{
|
|
stations.add(s.attributes.get("title"));
|
|
stationIds.put(s.attributes.get("title"), s.attributes.get("esr"));
|
|
i++;
|
|
}
|
|
stationsAdapter.notifyDataSetChanged();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private void loadStations()
|
|
{
|
|
if (curCity == null)
|
|
return;
|
|
if (!loadStationsFile())
|
|
{
|
|
Intent intent = new Intent(this, ElectronReminderService.class);
|
|
intent.setAction("load_stations");
|
|
intent.putExtra("cityId", curCity);
|
|
startService(intent);
|
|
}
|
|
}
|
|
|
|
private void loadRemindersFile()
|
|
{
|
|
SimpleXmlNode n = SimpleXml.readXmlFile(getFilesDir(), "reminders.xml");
|
|
if (n != null)
|
|
{
|
|
reminders.clear();
|
|
for (SimpleXmlNode r: n.childrenByName.get("reminder"))
|
|
{
|
|
String key = r.attributes.get("from")+"|"+r.attributes.get("to")+"|"+r.attributes.get("time");
|
|
reminders.put(key, r);
|
|
}
|
|
}
|
|
}
|
|
|
|
private boolean loadTripsFile()
|
|
{
|
|
SimpleXmlNode xmltrips = SimpleXml.readXmlFile(getFilesDir(), "trips_"+curFrom+"_"+curTo+".xml");
|
|
if (xmltrips != null && xmltrips.childrenByName.get("segment") != null)
|
|
{
|
|
trips.clear();
|
|
for (SimpleXmlNode s: xmltrips.childrenByName.get("segment"))
|
|
{
|
|
Trip t = new Trip();
|
|
t.arrival = s.attributes.get("arrival").substring(11);
|
|
t.departure = s.attributes.get("departure").substring(11);
|
|
t.title = s.attributes.get("title");
|
|
t.reminderOn = reminders != null && reminders.containsKey(curFrom+"|"+curTo+"|"+t.departure);
|
|
trips.add(t);
|
|
}
|
|
ListView v = (ListView)findViewById(R.id.tripList);
|
|
((TripItemAdapter)v.getAdapter()).notifyDataSetChanged();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private void loadTrips()
|
|
{
|
|
if (curFrom == null || curTo == null)
|
|
return;
|
|
saveCurPrefs();
|
|
if (!loadTripsFile())
|
|
{
|
|
Intent intent = new Intent(this, ElectronReminderService.class);
|
|
intent.setAction("load_trips");
|
|
intent.putExtra("from", curFrom);
|
|
intent.putExtra("to", curTo);
|
|
startService(intent);
|
|
}
|
|
}
|
|
|
|
private void saveReminders()
|
|
{
|
|
File f = new File(getFilesDir(), "reminders.xml");
|
|
FileOutputStream fs = null;
|
|
try
|
|
{
|
|
fs = new FileOutputStream(f);
|
|
SimpleXmlNode n = new SimpleXmlNode();
|
|
n.name = "reminders";
|
|
for (SimpleXmlNode r: reminders.values())
|
|
n.appendChild(r);
|
|
SimpleXml.write(n, fs);
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
e.printStackTrace();
|
|
}
|
|
finally
|
|
{
|
|
try
|
|
{
|
|
if (fs != null)
|
|
fs.close();
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
RecheckAlarmReceiver.setAlarm(this, reminders.size() > 0);
|
|
}
|
|
|
|
private void saveCurPrefs()
|
|
{
|
|
SharedPreferences sp = getSharedPreferences("prefs", MODE_PRIVATE);
|
|
SharedPreferences.Editor e = sp.edit();
|
|
e.putString("curTo", curTo);
|
|
e.putString("curFrom", curFrom);
|
|
e.putString("curToName", curToName);
|
|
e.putString("curFromName", curFromName);
|
|
e.commit();
|
|
}
|
|
|
|
protected void onResume()
|
|
{
|
|
super.onResume();
|
|
IntentFilter flt = new IntentFilter("cities_loaded");
|
|
flt.addAction("stations_loaded");
|
|
flt.addAction("trips_loaded");
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(srvReceiver, flt);
|
|
}
|
|
|
|
protected void onPause()
|
|
{
|
|
super.onPause();
|
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(srvReceiver);
|
|
}
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState)
|
|
{
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_main);
|
|
|
|
SharedPreferences sp = getSharedPreferences("prefs", MODE_PRIVATE);
|
|
curCity = sp.getString("cityId", "24461"); // Default is Moscow
|
|
curTo = sp.getString("curTo", null);
|
|
curFrom = sp.getString("curFrom", null);
|
|
curToName = sp.getString("curToName", null);
|
|
curFromName = sp.getString("curFromName", null);
|
|
((TextView)findViewById(R.id.fromView)).setText(curFromName == null ? "" : curFromName);
|
|
((TextView)findViewById(R.id.toView)).setText(curToName == null ? "" : curToName);
|
|
|
|
reminders = new HashMap<String, SimpleXmlNode>();
|
|
|
|
trips = new ArrayList<Trip>();
|
|
ListView v = (ListView)findViewById(R.id.tripList);
|
|
v.setAdapter(new TripItemAdapter(this, trips));
|
|
v.setOnItemClickListener(new AdapterView.OnItemClickListener()
|
|
{
|
|
@Override
|
|
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
|
|
{
|
|
Trip t = trips.get(i);
|
|
String key = curFrom+"|"+curTo+"|"+trips.get(i).departure;
|
|
if (t.reminderOn)
|
|
reminders.remove(key);
|
|
else
|
|
{
|
|
SimpleXmlNode n = new SimpleXmlNode();
|
|
n.name = "reminder";
|
|
n.attributes.put("from", curFrom);
|
|
n.attributes.put("to", curTo);
|
|
n.attributes.put("time", t.departure);
|
|
n.attributes.put("fromName", curFromName);
|
|
n.attributes.put("toName", curToName);
|
|
reminders.put(key, n);
|
|
}
|
|
t.reminderOn = !t.reminderOn;
|
|
((TripItemAdapter)adapterView.getAdapter()).notifyDataSetChanged();
|
|
saveReminders();
|
|
}
|
|
});
|
|
|
|
stations = new ArrayList<String>();
|
|
stationIds = new HashMap<String,String>();
|
|
stationsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, stations);
|
|
|
|
AutoCompleteTextView stFrom = (AutoCompleteTextView)findViewById(R.id.fromView);
|
|
stFrom.setAdapter(stationsAdapter);
|
|
stFrom.addTextChangedListener(new TextWatcher()
|
|
{
|
|
@Override
|
|
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
|
|
{
|
|
}
|
|
|
|
@Override
|
|
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
|
|
{
|
|
String id = stationIds.get(charSequence.toString());
|
|
if (id != null)
|
|
{
|
|
curFrom = id;
|
|
curFromName = charSequence.toString();
|
|
loadTrips();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void afterTextChanged(Editable editable)
|
|
{
|
|
}
|
|
});
|
|
|
|
AutoCompleteTextView stTo = (AutoCompleteTextView)findViewById(R.id.toView);
|
|
stTo.setAdapter(stationsAdapter);
|
|
stTo.addTextChangedListener(new TextWatcher()
|
|
{
|
|
@Override
|
|
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
|
|
{
|
|
}
|
|
|
|
@Override
|
|
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
|
|
{
|
|
String id = stationIds.get(charSequence.toString());
|
|
if (id != null)
|
|
{
|
|
curTo = id;
|
|
curToName = charSequence.toString();
|
|
loadTrips();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void afterTextChanged(Editable editable)
|
|
{
|
|
}
|
|
});
|
|
|
|
loadRemindersFile();
|
|
loadStations();
|
|
loadTrips();
|
|
}
|
|
|
|
@Override
|
|
public boolean onCreateOptionsMenu(Menu menu)
|
|
{
|
|
// Inflate the menu; this adds items to the action bar if it is present.
|
|
getMenuInflater().inflate(R.menu.menu_main, menu);
|
|
return true;
|
|
}
|
|
|
|
private void changeCity()
|
|
{
|
|
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Black));
|
|
|
|
loadCities();
|
|
ArrayList<String> cities = new ArrayList<String>();
|
|
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
|
|
android.R.layout.simple_dropdown_item_1line, cities);
|
|
|
|
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
final View searchView = inflater.inflate(R.layout.city_autocomplete, null);
|
|
|
|
final AutoCompleteTextView cityName = (AutoCompleteTextView)searchView.findViewById(R.id.city_name);
|
|
cityName.setAdapter(adapter);
|
|
|
|
alertDialogBuilder.setView(searchView);
|
|
alertDialogBuilder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
|
|
{
|
|
@Override
|
|
public void onClick(DialogInterface dialog, int i)
|
|
{
|
|
dialog.dismiss();
|
|
}
|
|
});
|
|
AlertDialog alertDialog = alertDialogBuilder.create();
|
|
alertDialog.show();
|
|
}
|
|
|
|
@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.
|
|
int id = item.getItemId();
|
|
|
|
if (id == R.id.action_change_city)
|
|
{
|
|
//Intent intent = new Intent(this, SettingsActivity.class);
|
|
//startActivity(intent);
|
|
return true;
|
|
}
|
|
else if (id == R.id.action_check_all)
|
|
{
|
|
Intent intent = new Intent(this, ElectronReminderService.class);
|
|
intent.setAction("refresh_all");
|
|
startService(intent);
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
}
|
|
}
|