fix boot error, persist last entered stations

master
Vitaliy Filippov 2016-02-25 01:38:13 +03:00
parent 56fbd90022
commit e453f5b13c
4 changed files with 52 additions and 44 deletions

View File

@ -24,6 +24,7 @@ import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView; import android.widget.AutoCompleteTextView;
import android.widget.ListAdapter; import android.widget.ListAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer; import org.xmlpull.v1.XmlSerializer;
@ -165,6 +166,7 @@ public class MainActivity extends AppCompatActivity
{ {
if (curFrom == null || curTo == null) if (curFrom == null || curTo == null)
return; return;
saveCurPrefs();
if (!loadTripsFile()) if (!loadTripsFile())
{ {
Intent intent = new Intent(this, ElectronReminderService.class); Intent intent = new Intent(this, ElectronReminderService.class);
@ -204,10 +206,18 @@ public class MainActivity extends AppCompatActivity
e.printStackTrace(); e.printStackTrace();
} }
} }
if (reminders.size() > 0) RecheckAlarmReceiver.setAlarm(this, reminders.size() > 0);
alarmer.setAlarm(this); }
else
alarmer.cancelAlarm(this); 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() protected void onResume()
@ -233,6 +243,13 @@ public class MainActivity extends AppCompatActivity
SharedPreferences sp = getSharedPreferences("prefs", MODE_PRIVATE); SharedPreferences sp = getSharedPreferences("prefs", MODE_PRIVATE);
curCity = sp.getString("cityId", "24461"); // Default is Moscow 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>(); reminders = new HashMap<String, SimpleXmlNode>();
trips = new ArrayList<Trip>(); trips = new ArrayList<Trip>();
@ -324,6 +341,7 @@ public class MainActivity extends AppCompatActivity
loadRemindersFile(); loadRemindersFile();
loadStations(); loadStations();
loadTrips();
} }
@Override @Override

View File

@ -16,9 +16,6 @@ import java.util.Calendar;
*/ */
public class RecheckAlarmReceiver extends WakefulBroadcastReceiver public class RecheckAlarmReceiver extends WakefulBroadcastReceiver
{ {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
@Override @Override
public void onReceive(Context context, Intent intent) public void onReceive(Context context, Intent intent)
{ {
@ -33,51 +30,38 @@ public class RecheckAlarmReceiver extends WakefulBroadcastReceiver
} }
/** /**
* Sets a repeating alarm that runs once a day at approximately 8:30 a.m. When the * Sets (or cancels) a repeating alarm that runs once a day at approximately 8:30 a.m. When the
* alarm fires, the app broadcasts an Intent to this WakefulBroadcastReceiver. * alarm fires, the app broadcasts an Intent to this WakefulBroadcastReceiver.
*
* @param context * @param context
*/ */
public void setAlarm(Context context) public static void setAlarm(Context context, boolean active)
{ {
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, RecheckAlarmReceiver.class); Intent intent = new Intent(context, RecheckAlarmReceiver.class);
intent.setAction("refresh_all"); intent.setAction("refresh_all");
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance(); if (active)
calendar.setTimeInMillis(System.currentTimeMillis()); {
// FIXME Remove hardcoded time Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8); calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.MINUTE, 30); // FIXME Remove hardcoded time
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// Set the alarm to fire at approximately 8:30 a.m., according to the device's // Set the alarm to fire at approximately 8:30 a.m., according to the device's
// clock, and to repeat once a day. // clock, and to repeat once a day.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent); alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
}
else
alarmMgr.cancel(alarmIntent);
// Enable {@code RecheckBootReceiver} to automatically restart the alarm when the device is rebooted. // Enable {@code RecheckBootReceiver} to automatically restart the alarm when the device is rebooted.
ComponentName receiver = new ComponentName(context, RecheckBootReceiver.class); ComponentName receiver = new ComponentName(context, RecheckBootReceiver.class);
PackageManager pm = context.getPackageManager(); PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver, pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, active ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
/**
* Cancels the alarm.
* @param context
*/
public void cancelAlarm(Context context)
{
// If the alarm has been set, cancel it.
if (alarmMgr != null)
alarmMgr.cancel(alarmIntent);
// Disable {@code RecheckBootReceiver} so that it doesn't automatically restart the
// alarm when the device is rebooted.
ComponentName receiver = new ComponentName(context, RecheckBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP); PackageManager.DONT_KILL_APP);
} }
} }

View File

@ -4,6 +4,9 @@ import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import simple.SimpleXml;
import simple.SimpleXmlNode;
/** /**
* This BroadcastReceiver automatically (re)starts the alarm when the device is * This BroadcastReceiver automatically (re)starts the alarm when the device is
* rebooted. This receiver is set to be disabled (android:enabled="false") in the * rebooted. This receiver is set to be disabled (android:enabled="false") in the
@ -13,12 +16,14 @@ import android.content.Intent;
*/ */
public class RecheckBootReceiver extends BroadcastReceiver public class RecheckBootReceiver extends BroadcastReceiver
{ {
RecheckAlarmReceiver alarm = new RecheckAlarmReceiver();
@Override @Override
public void onReceive(Context context, Intent intent) public void onReceive(Context context, Intent intent)
{ {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
alarm.setAlarm(context); {
SimpleXmlNode n = SimpleXml.readXmlFile(context.getFilesDir(), "reminders.xml");
if (n != null && n.childrenByName.get("reminder") != null)
RecheckAlarmReceiver.setAlarm(context, true);
}
} }
} }

View File

@ -16,9 +16,10 @@
<TableLayout <TableLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/tableLayout"
android:layout_alignParentTop="true" android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" android:layout_alignParentLeft="true"
android:id="@+id/tableLayout"> android:layout_alignParentStart="true">
<TableRow <TableRow
android:layout_width="match_parent" android:layout_width="match_parent"