2017-03-10 14:53:19 +01:00
|
|
|
/* Copyright (C) 2015-2017 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
|
|
|
Gobbetti, Lem Dulfo
|
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2015-06-24 20:14:08 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
2017-04-08 18:44:15 +02:00
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2015-06-26 17:22:42 +02:00
|
|
|
import android.view.MenuItem;
|
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;
|
2017-01-26 20:55:00 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2016-04-25 23:18:55 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
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
|
|
|
|
|
|
|
|
2017-09-03 01:02:31 +02:00
|
|
|
public class ConfigureAlarms extends AbstractGBActivity {
|
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;
|
2017-01-26 20:55:00 +01:00
|
|
|
private GBDevice device;
|
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);
|
|
|
|
|
2017-01-26 20:55:00 +01:00
|
|
|
device = getIntent().getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
|
|
|
|
2016-04-25 23:18:55 +02:00
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
preferencesAlarmListSet = prefs.getStringSet(PREF_MIBAND_ALARMS, new HashSet<String>());
|
2015-06-26 17:22:42 +02:00
|
|
|
if (preferencesAlarmListSet.isEmpty()) {
|
|
|
|
//initialize the preferences
|
|
|
|
preferencesAlarmListSet = new HashSet<>(Arrays.asList(GBAlarm.DEFAULT_ALARMS));
|
2016-04-25 23:18:55 +02:00
|
|
|
prefs.getPreferences().edit().putStringSet(PREF_MIBAND_ALARMS, preferencesAlarmListSet).apply();
|
2015-06-26 17:22:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mGBAlarmListAdapter = new GBAlarmListAdapter(this, preferencesAlarmListSet);
|
|
|
|
|
2017-04-08 18:44:15 +02:00
|
|
|
RecyclerView alarmsRecyclerView = (RecyclerView) findViewById(R.id.alarm_list);
|
|
|
|
alarmsRecyclerView.setHasFixedSize(true);
|
|
|
|
alarmsRecyclerView.setLayoutManager(new LinearLayoutManager(this));
|
|
|
|
alarmsRecyclerView.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() {
|
2016-04-25 23:18:55 +02:00
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
preferencesAlarmListSet = prefs.getStringSet(PREF_MIBAND_ALARMS, new HashSet<String>());
|
2016-04-25 23:39:03 +02:00
|
|
|
int reservedSlots = prefs.getInt(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;
|
2017-01-26 20:55:00 +01:00
|
|
|
Intent startIntent = new Intent(getApplicationContext(), AlarmDetails.class);
|
2015-06-26 17:22:42 +02:00
|
|
|
startIntent.putExtra("alarm", alarm);
|
2017-01-26 20:55:00 +01:00
|
|
|
startIntent.putExtra(GBDevice.EXTRA_DEVICE, getDevice());
|
2015-10-18 23:52:59 +02:00
|
|
|
startActivityForResult(startIntent, REQ_CONFIGURE_ALARM);
|
2015-06-26 17:22:42 +02:00
|
|
|
}
|
|
|
|
|
2017-01-26 20:55:00 +01:00
|
|
|
private GBDevice getDevice() {
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|