Java tutorial
/* Copyright (C) 2016-2017 Andreas Shimokawa 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/>. */ package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble; import android.util.Pair; import android.widget.Toast; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes; import nodomain.freeyourgadget.gadgetbridge.model.Weather; import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; import nodomain.freeyourgadget.gadgetbridge.util.GB; class AppMessageHandlerHealthify extends AppMessageHandler { private Integer KEY_TEMPERATURE; private Integer KEY_CONDITIONS; AppMessageHandlerHealthify(UUID uuid, PebbleProtocol pebbleProtocol) { super(uuid, pebbleProtocol); try { JSONObject appKeys = getAppKeys(); KEY_TEMPERATURE = appKeys.getInt("TEMPERATURE"); KEY_CONDITIONS = appKeys.getInt("CONDITIONS"); } catch (JSONException e) { GB.toast("There was an error accessing the Helthify watchface configuration.", Toast.LENGTH_LONG, GB.ERROR); } catch (IOException ignore) { } } private byte[] encodeHelthifyWeatherMessage(WeatherSpec weatherSpec) { if (weatherSpec == null) { return null; } ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>(2); pairs.add(new Pair<>(KEY_CONDITIONS, (Object) weatherSpec.currentCondition)); pairs.add(new Pair<>(KEY_TEMPERATURE, (Object) (weatherSpec.currentTemp - 273))); byte[] weatherMessage = mPebbleProtocol .encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs); ByteBuffer buf = ByteBuffer.allocate(weatherMessage.length); buf.put(weatherMessage); return buf.array(); } @Override public GBDeviceEvent[] onAppStart() { WeatherSpec weatherSpec = Weather.getInstance().getWeatherSpec(); if (weatherSpec == null) { return new GBDeviceEvent[] { null }; } GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes(); sendBytes.encodedBytes = encodeHelthifyWeatherMessage(weatherSpec); return new GBDeviceEvent[] { sendBytes }; } @Override public byte[] encodeUpdateWeather(WeatherSpec weatherSpec) { return encodeHelthifyWeatherMessage(weatherSpec); } }