use of nodomain.freeyourgadget.gadgetbridge.GBApplication in project Gadgetbridge by Freeyourgadget.
the class BatteryInfoActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Context appContext = this.getApplicationContext();
if (appContext instanceof GBApplication) {
setContentView(R.layout.activity_battery_info);
}
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) {
gbDevice = bundle.getParcelable(GBDevice.EXTRA_DEVICE);
batteryIndex = bundle.getInt(GBDevice.BATTERY_INDEX, 0);
} else {
throw new IllegalArgumentException("Must provide a device when invoking this activity");
}
final BatteryInfoChartFragment batteryInfoChartFragment = new BatteryInfoChartFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.batteryChartFragmentHolder, batteryInfoChartFragment).commit();
timeTo = (int) (System.currentTimeMillis() / 1000);
batteryInfoChartFragment.setDateAndGetData(gbDevice, batteryIndex, timeFrom, timeTo);
TextView battery_status_device_name_text = (TextView) findViewById(R.id.battery_status_device_name);
TextView battery_status_battery_voltage = (TextView) findViewById(R.id.battery_status_battery_voltage);
TextView battery_status_extra_name = (TextView) findViewById(R.id.battery_status_extra_name);
final TextView battery_status_date_from_text = (TextView) findViewById(R.id.battery_status_date_from_text);
final TextView battery_status_date_to_text = (TextView) findViewById(R.id.battery_status_date_to_text);
final SeekBar battery_status_time_span_seekbar = (SeekBar) findViewById(R.id.battery_status_time_span_seekbar);
final TextView battery_status_time_span_text = (TextView) findViewById(R.id.battery_status_time_span_text);
LinearLayout battery_status_date_to_layout = (LinearLayout) findViewById(R.id.battery_status_date_to_layout);
battery_status_time_span_seekbar.setMax(5);
battery_status_time_span_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
String text;
switch(i) {
case 0:
text = getString(R.string.calendar_day);
timeFrom = DateTimeUtils.shiftDays(timeTo, -1);
break;
case 1:
text = getString(R.string.calendar_week);
timeFrom = DateTimeUtils.shiftDays(timeTo, -7);
break;
case 2:
text = getString(R.string.calendar_two_weeks);
timeFrom = DateTimeUtils.shiftDays(timeTo, -14);
break;
case 3:
text = getString(R.string.calendar_month);
timeFrom = DateTimeUtils.shiftMonths(timeTo, -1);
break;
case 4:
text = getString(R.string.calendar_six_months);
timeFrom = DateTimeUtils.shiftMonths(timeTo, -6);
break;
case 5:
text = getString(R.string.calendar_year);
timeFrom = DateTimeUtils.shiftMonths(timeTo, -12);
break;
default:
text = getString(R.string.calendar_two_weeks);
timeFrom = DateTimeUtils.shiftDays(timeTo, -14);
}
battery_status_time_span_text.setText(text);
battery_status_date_from_text.setText(DateTimeUtils.formatDate(new Date(timeFrom * 1000L)));
battery_status_date_to_text.setText(DateTimeUtils.formatDate(new Date(timeTo * 1000L)));
batteryInfoChartFragment.setDateAndGetData(gbDevice, batteryIndex, timeFrom, timeTo);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
// Button battery_status_calendar_button = findViewById(R.id.battery_status_calendar_button);
battery_status_date_to_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar currentDate = Calendar.getInstance();
currentDate.setTimeInMillis(timeTo * 1000L);
Context context = getApplicationContext();
if (context instanceof GBApplication) {
new DatePickerDialog(BatteryInfoActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar date = Calendar.getInstance();
date.set(year, monthOfYear, dayOfMonth);
timeTo = (int) (date.getTimeInMillis() / 1000);
battery_status_date_to_text.setText(DateTimeUtils.formatDate(new Date(timeTo * 1000L)));
battery_status_time_span_seekbar.setProgress(0);
battery_status_time_span_seekbar.setProgress(1);
batteryInfoChartFragment.setDateAndGetData(gbDevice, batteryIndex, timeFrom, timeTo);
}
}, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE)).show();
}
}
});
battery_status_time_span_seekbar.setProgress(2);
ImageView battery_status_device_icon = findViewById(R.id.battery_status_device_icon);
battery_status_device_icon.setImageResource(gbDevice.isInitialized() ? gbDevice.getType().getIcon() : gbDevice.getType().getDisabledIcon());
TextView battery_status_battery_level_text = (TextView) findViewById(R.id.battery_status_battery_level);
String level = gbDevice.getBatteryLevel(batteryIndex) > 0 ? String.format("%1s%%", gbDevice.getBatteryLevel(batteryIndex)) : "";
String voltage = gbDevice.getBatteryVoltage(batteryIndex) > 0 ? String.format("%1sV", gbDevice.getBatteryVoltage(batteryIndex)) : "";
battery_status_device_name_text.setText(gbDevice.getName());
battery_status_battery_level_text.setText(level);
battery_status_battery_voltage.setText(voltage);
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(gbDevice);
for (BatteryConfig batteryConfig : coordinator.getBatteryConfig()) {
if (batteryConfig.getBatteryIndex() == batteryIndex) {
battery_status_extra_name.setText(batteryConfig.getBatteryLabel());
battery_status_device_icon.setImageResource(batteryConfig.getBatteryIcon());
if (gbDevice.isInitialized()) {
battery_status_device_icon.setColorFilter(this.getResources().getColor(R.color.accent));
}
}
}
}
use of nodomain.freeyourgadget.gadgetbridge.GBApplication in project Gadgetbridge by Freeyourgadget.
the class WidgetAlarmsActivity method setAlarm.
public void setAlarm(int duration) {
// current timestamp
GregorianCalendar calendar = new GregorianCalendar();
if (duration > 0) {
calendar.add(Calendar.MINUTE, duration);
} else {
int userSleepDuration = new ActivityUser().getSleepDurationGoal();
// add preferred sleep duration
if (userSleepDuration > 0) {
calendar.add(Calendar.HOUR_OF_DAY, userSleepDuration);
} else {
// probably testing
calendar.add(Calendar.MINUTE, 1);
}
}
// overwrite the first alarm and activate it, without
Context appContext = this.getApplicationContext();
if (appContext instanceof GBApplication) {
GBApplication gbApp = (GBApplication) appContext;
GBDevice selectedDevice = gbApp.getDeviceManager().getSelectedDevice();
if (selectedDevice == null || !selectedDevice.isInitialized()) {
GB.toast(this, this.getString(R.string.appwidget_not_connected), Toast.LENGTH_LONG, GB.WARN);
return;
}
}
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
GB.toast(this, this.getString(R.string.appwidget_setting_alarm, hours, minutes), Toast.LENGTH_SHORT, GB.INFO);
Alarm alarm = AlarmUtils.createSingleShot(0, true, false, calendar);
ArrayList<Alarm> alarms = new ArrayList<>(1);
alarms.add(alarm);
GBApplication.deviceService().onSetAlarms(alarms);
}
use of nodomain.freeyourgadget.gadgetbridge.GBApplication in project Gadgetbridge by Freeyourgadget.
the class WidgetAlarmsActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context appContext = this.getApplicationContext();
GBDevice selectedDevice;
Bundle extras = getIntent().getExtras();
if (extras != null) {
selectedDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE);
} else {
GB.toast(this, "Error no device", Toast.LENGTH_LONG, GB.ERROR);
return;
}
if (appContext instanceof GBApplication) {
GBApplication gbApp = (GBApplication) appContext;
if (selectedDevice == null || !selectedDevice.isInitialized()) {
GB.toast(this, this.getString(R.string.not_connected), Toast.LENGTH_LONG, GB.INFO);
} else {
setContentView(R.layout.widget_alarms_activity_list);
int userSleepDuration = new ActivityUser().getSleepDurationGoal();
textView = findViewById(R.id.alarm5);
if (userSleepDuration > 0) {
Resources res = getResources();
textView.setText(res.getQuantityString(R.plurals.widget_alarm_target_hours, userSleepDuration, userSleepDuration));
} else {
textView.setVisibility(View.GONE);
}
}
}
}
use of nodomain.freeyourgadget.gadgetbridge.GBApplication in project Gadgetbridge by Freeyourgadget.
the class FwAppInstallerActivity method getAllCoordinatorsConnectedFirst.
private List<DeviceCoordinator> getAllCoordinatorsConnectedFirst() {
DeviceManager deviceManager = ((GBApplication) getApplicationContext()).getDeviceManager();
List<DeviceCoordinator> connectedCoordinators = new ArrayList<>();
List<DeviceCoordinator> allCoordinators = DeviceHelper.getInstance().getAllCoordinators();
List<DeviceCoordinator> sortedCoordinators = new ArrayList<>(allCoordinators.size());
GBDevice connectedDevice = deviceManager.getSelectedDevice();
if (connectedDevice != null && connectedDevice.isConnected()) {
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(connectedDevice);
if (coordinator != null) {
connectedCoordinators.add(coordinator);
}
}
sortedCoordinators.addAll(connectedCoordinators);
for (DeviceCoordinator coordinator : allCoordinators) {
if (!connectedCoordinators.contains(coordinator)) {
sortedCoordinators.add(coordinator);
}
}
return sortedCoordinators;
}
use of nodomain.freeyourgadget.gadgetbridge.GBApplication in project Gadgetbridge by Freeyourgadget.
the class DebugActivity method getAllSupportedDevices.
public static LinkedHashMap getAllSupportedDevices(Context appContext) {
LinkedHashMap<String, Pair<Long, Integer>> newMap = new LinkedHashMap<>(1);
GBApplication app = (GBApplication) appContext;
for (DeviceCoordinator coordinator : DeviceHelper.getInstance().getAllCoordinators()) {
DeviceType deviceType = coordinator.getDeviceType();
int icon = deviceType.getIcon();
String name = app.getString(deviceType.getName()) + " (" + coordinator.getManufacturer() + ")";
long deviceId = deviceType.getKey();
newMap.put(name, new Pair(deviceId, icon));
}
TreeMap<String, Pair<Long, Integer>> sortedMap = new TreeMap<>(newMap);
newMap = new LinkedHashMap<>(1);
newMap.put(app.getString(R.string.widget_settings_select_device_title), new Pair(SELECT_DEVICE, R.drawable.ic_device_unknown));
newMap.putAll(sortedMap);
return newMap;
}
Aggregations