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.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
@ -165,6 +166,7 @@ public class MainActivity extends AppCompatActivity
{
if (curFrom == null || curTo == null)
return;
saveCurPrefs();
if (!loadTripsFile())
{
Intent intent = new Intent(this, ElectronReminderService.class);
@ -204,10 +206,18 @@ public class MainActivity extends AppCompatActivity
e.printStackTrace();
}
}
if (reminders.size() > 0)
alarmer.setAlarm(this);
else
alarmer.cancelAlarm(this);
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()
@ -233,6 +243,13 @@ public class MainActivity extends AppCompatActivity
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>();
@ -324,6 +341,7 @@ public class MainActivity extends AppCompatActivity
loadRemindersFile();
loadStations();
loadTrips();
}
@Override

View File

@ -16,9 +16,6 @@ import java.util.Calendar;
*/
public class RecheckAlarmReceiver extends WakefulBroadcastReceiver
{
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
@Override
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.
*
* @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.setAction("refresh_all");
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
// FIXME Remove hardcoded time
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
if (active)
{
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
// 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
// clock, and to repeat once a day.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
// Set the alarm to fire at approximately 8:30 a.m., according to the device's
// clock, and to repeat once a day.
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.
ComponentName receiver = new ComponentName(context, RecheckBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
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,
active ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
}

View File

@ -4,6 +4,9 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import simple.SimpleXml;
import simple.SimpleXmlNode;
/**
* This BroadcastReceiver automatically (re)starts the alarm when the device is
* 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
{
RecheckAlarmReceiver alarm = new RecheckAlarmReceiver();
@Override
public void onReceive(Context context, Intent intent)
{
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
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tableLayout"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/tableLayout">
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TableRow
android:layout_width="match_parent"