2015-06-24 20:14:08 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.preference.PreferenceManager;
|
2015-06-26 17:22:42 +02:00
|
|
|
import android.view.MenuItem;
|
2016-04-10 00:13:25 +02:00
|
|
|
import android.widget.ListView;
|
2015-06-24 20:14:08 +02:00
|
|
|
|
2015-06-26 17:22:42 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
2015-06-24 20:14:08 +02:00
|
|
|
|
2015-08-21 00:58:18 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2015-06-24 20:14:08 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.adapter.GBAlarmListAdapter;
|
2015-12-17 17:02:00 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBAlarm;
|
2015-06-24 20:14:08 +02:00
|
|
|
|
2015-08-03 23:09:49 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MIBAND_ALARMS;
|
2015-06-24 20:14:08 +02:00
|
|
|
|
|
|
|
|
2016-04-14 15:21:25 +02:00
|
|
|
public class ConfigureAlarms extends GBActivity {
|
2015-06-24 20:14:08 +02:00
|
|
|
|
2015-10-18 23:52:59 +02:00
|
|
|
private static final int REQ_CONFIGURE_ALARM = 1;
|
|
|
|
|
2015-06-24 20:14:08 +02:00
|
|
|
private GBAlarmListAdapter mGBAlarmListAdapter;
|
2015-06-26 17:22:42 +02:00
|
|
|
private Set<String> preferencesAlarmListSet;
|
2015-10-18 23:52:59 +02:00
|
|
|
private boolean avoidSendAlarmsToDevice;
|
2015-06-24 20:14:08 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2015-06-26 17:22:42 +02:00
|
|
|
|
2015-06-24 20:14:08 +02:00
|
|
|
setContentView(R.layout.activity_configure_alarms);
|
|
|
|
|
|
|
|
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2015-06-26 17:22:42 +02:00
|
|
|
preferencesAlarmListSet = sharedPrefs.getStringSet(PREF_MIBAND_ALARMS, new HashSet<String>());
|
|
|
|
if (preferencesAlarmListSet.isEmpty()) {
|
|
|
|
//initialize the preferences
|
|
|
|
preferencesAlarmListSet = new HashSet<>(Arrays.asList(GBAlarm.DEFAULT_ALARMS));
|
2015-10-18 23:52:59 +02:00
|
|
|
sharedPrefs.edit().putStringSet(PREF_MIBAND_ALARMS, preferencesAlarmListSet).apply();
|
2015-06-26 17:22:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mGBAlarmListAdapter = new GBAlarmListAdapter(this, preferencesAlarmListSet);
|
|
|
|
|
2016-04-10 00:13:25 +02:00
|
|
|
ListView listView = (ListView) findViewById(R.id.alarm_list);
|
|
|
|
listView.setAdapter(mGBAlarmListAdapter);
|
2015-10-18 23:52:59 +02:00
|
|
|
updateAlarmsFromPrefs();
|
|
|
|
}
|
2015-06-26 17:22:42 +02:00
|
|
|
|
2015-10-18 23:52:59 +02:00
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
if (!avoidSendAlarmsToDevice) {
|
|
|
|
sendAlarmsToDevice();
|
|
|
|
}
|
|
|
|
super.onPause();
|
2015-06-26 17:22:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-10-18 23:52:59 +02:00
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
if (requestCode == REQ_CONFIGURE_ALARM) {
|
|
|
|
avoidSendAlarmsToDevice = false;
|
|
|
|
updateAlarmsFromPrefs();
|
|
|
|
}
|
|
|
|
}
|
2015-06-26 17:22:42 +02:00
|
|
|
|
2015-10-18 23:52:59 +02:00
|
|
|
private void updateAlarmsFromPrefs() {
|
2015-06-26 17:22:42 +02:00
|
|
|
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
preferencesAlarmListSet = sharedPrefs.getStringSet(PREF_MIBAND_ALARMS, new HashSet<String>());
|
2015-12-17 17:02:00 +01:00
|
|
|
int reservedSlots = Integer.parseInt(sharedPrefs.getString(MiBandConst.PREF_MIBAND_RESERVE_ALARM_FOR_CALENDAR, "0"));
|
2015-06-26 17:22:42 +02:00
|
|
|
|
2015-12-17 17:02:00 +01:00
|
|
|
mGBAlarmListAdapter.setAlarmList(preferencesAlarmListSet, reservedSlots);
|
2015-06-26 17:22:42 +02:00
|
|
|
mGBAlarmListAdapter.notifyDataSetChanged();
|
2015-06-24 20:14:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-06-26 17:22:42 +02:00
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
// back button
|
|
|
|
finish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void configureAlarm(GBAlarm alarm) {
|
2015-10-18 23:52:59 +02:00
|
|
|
avoidSendAlarmsToDevice = true;
|
2015-06-26 17:22:42 +02:00
|
|
|
Intent startIntent;
|
|
|
|
startIntent = new Intent(getApplicationContext(), AlarmDetails.class);
|
|
|
|
startIntent.putExtra("alarm", alarm);
|
2015-10-18 23:52:59 +02:00
|
|
|
startActivityForResult(startIntent, REQ_CONFIGURE_ALARM);
|
2015-06-26 17:22:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void sendAlarmsToDevice() {
|
2015-08-21 00:58:18 +02:00
|
|
|
GBApplication.deviceService().onSetAlarms(mGBAlarmListAdapter.getAlarmList());
|
2015-06-24 20:14:08 +02:00
|
|
|
}
|
|
|
|
}
|