Search in sources :

Example 1 with CalendarEventSpec

use of nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec in project Gadgetbridge by Freeyourgadget.

the class DeviceCommunicationService method onStartCommand.

@Override
public synchronized int onStartCommand(Intent intent, int flags, int startId) {
    if (intent == null) {
        LOG.info("no intent");
        return START_NOT_STICKY;
    }
    String action = intent.getAction();
    boolean pair = intent.getBooleanExtra(EXTRA_PERFORM_PAIR, false);
    if (action == null) {
        LOG.info("no action");
        return START_NOT_STICKY;
    }
    LOG.debug("Service startcommand: " + action);
    if (!action.equals(ACTION_START) && !action.equals(ACTION_CONNECT)) {
        if (!mStarted) {
            // using the service before issuing ACTION_START
            LOG.info("Must start service with " + ACTION_START + " or " + ACTION_CONNECT + " before using it: " + action);
            return START_NOT_STICKY;
        }
        if (mDeviceSupport == null || (!isInitialized() && !mDeviceSupport.useAutoConnect())) {
            // trying to send notification without valid Bluetooth connection
            if (mGBDevice != null) {
                // at least send back the current device state
                mGBDevice.sendDeviceUpdateIntent(this);
            }
            return START_STICKY;
        }
    }
    // when we get past this, we should have valid mDeviceSupport and mGBDevice instances
    Prefs prefs = getPrefs();
    switch(action) {
        case ACTION_START:
            start();
            break;
        case ACTION_CONNECT:
            // ensure started
            start();
            GBDevice gbDevice = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
            String btDeviceAddress = null;
            if (gbDevice == null) {
                if (prefs != null) {
                    // may be null in test cases
                    btDeviceAddress = prefs.getString("last_device_address", null);
                    if (btDeviceAddress != null) {
                        gbDevice = DeviceHelper.getInstance().findAvailableDevice(btDeviceAddress, this);
                    }
                }
            } else {
                btDeviceAddress = gbDevice.getAddress();
            }
            boolean autoReconnect = GBPrefs.AUTO_RECONNECT_DEFAULT;
            if (prefs != null && prefs.getPreferences() != null) {
                prefs.getPreferences().edit().putString("last_device_address", btDeviceAddress).apply();
                autoReconnect = getGBPrefs().getAutoReconnect();
            }
            if (gbDevice != null && !isConnecting() && !isConnected()) {
                setDeviceSupport(null);
                try {
                    DeviceSupport deviceSupport = mFactory.createDeviceSupport(gbDevice);
                    if (deviceSupport != null) {
                        setDeviceSupport(deviceSupport);
                        if (pair) {
                            deviceSupport.pair();
                        } else {
                            deviceSupport.setAutoReconnect(autoReconnect);
                            deviceSupport.connect();
                        }
                    } else {
                        GB.toast(this, getString(R.string.cannot_connect, "Can't create device support"), Toast.LENGTH_SHORT, GB.ERROR);
                    }
                } catch (Exception e) {
                    GB.toast(this, getString(R.string.cannot_connect, e.getMessage()), Toast.LENGTH_SHORT, GB.ERROR, e);
                    setDeviceSupport(null);
                }
            } else if (mGBDevice != null) {
                // send an update at least
                mGBDevice.sendDeviceUpdateIntent(this);
            }
            break;
        case ACTION_REQUEST_DEVICEINFO:
            mGBDevice.sendDeviceUpdateIntent(this);
            break;
        case ACTION_NOTIFICATION:
            {
                NotificationSpec notificationSpec = new NotificationSpec();
                notificationSpec.phoneNumber = intent.getStringExtra(EXTRA_NOTIFICATION_PHONENUMBER);
                notificationSpec.sender = intent.getStringExtra(EXTRA_NOTIFICATION_SENDER);
                notificationSpec.subject = intent.getStringExtra(EXTRA_NOTIFICATION_SUBJECT);
                notificationSpec.title = intent.getStringExtra(EXTRA_NOTIFICATION_TITLE);
                notificationSpec.body = intent.getStringExtra(EXTRA_NOTIFICATION_BODY);
                notificationSpec.sourceName = intent.getStringExtra(EXTRA_NOTIFICATION_SOURCENAME);
                notificationSpec.type = (NotificationType) intent.getSerializableExtra(EXTRA_NOTIFICATION_TYPE);
                notificationSpec.id = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1);
                notificationSpec.flags = intent.getIntExtra(EXTRA_NOTIFICATION_FLAGS, 0);
                if (notificationSpec.type == NotificationType.GENERIC_SMS && notificationSpec.phoneNumber != null) {
                    // FIXME: add this in external SMS Receiver?
                    notificationSpec.id = mRandom.nextInt();
                    GBApplication.getIDSenderLookup().add(notificationSpec.id, notificationSpec.phoneNumber);
                }
                if (((notificationSpec.flags & NotificationSpec.FLAG_WEARABLE_REPLY) > 0) || (notificationSpec.type == NotificationType.GENERIC_SMS && notificationSpec.phoneNumber != null)) {
                    // NOTE: maybe not where it belongs
                    if (prefs.getBoolean("pebble_force_untested", false)) {
                        // I would rather like to save that as an array in ShadredPreferences
                        // this would work but I dont know how to do the same in the Settings Activity's xml
                        ArrayList<String> replies = new ArrayList<>();
                        for (int i = 1; i <= 16; i++) {
                            String reply = prefs.getString("canned_reply_" + i, null);
                            if (reply != null && !reply.equals("")) {
                                replies.add(reply);
                            }
                        }
                        notificationSpec.cannedReplies = replies.toArray(new String[replies.size()]);
                    }
                }
                mDeviceSupport.onNotification(notificationSpec);
                break;
            }
        case ACTION_DELETE_NOTIFICATION:
            {
                mDeviceSupport.onDeleteNotification(intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1));
                break;
            }
        case ACTION_ADD_CALENDAREVENT:
            {
                CalendarEventSpec calendarEventSpec = new CalendarEventSpec();
                calendarEventSpec.id = intent.getLongExtra(EXTRA_CALENDAREVENT_ID, -1);
                calendarEventSpec.type = intent.getByteExtra(EXTRA_CALENDAREVENT_TYPE, (byte) -1);
                calendarEventSpec.timestamp = intent.getIntExtra(EXTRA_CALENDAREVENT_TIMESTAMP, -1);
                calendarEventSpec.durationInSeconds = intent.getIntExtra(EXTRA_CALENDAREVENT_DURATION, -1);
                calendarEventSpec.title = intent.getStringExtra(EXTRA_CALENDAREVENT_TITLE);
                calendarEventSpec.description = intent.getStringExtra(EXTRA_CALENDAREVENT_DESCRIPTION);
                mDeviceSupport.onAddCalendarEvent(calendarEventSpec);
                break;
            }
        case ACTION_DELETE_CALENDAREVENT:
            {
                long id = intent.getLongExtra(EXTRA_CALENDAREVENT_ID, -1);
                byte type = intent.getByteExtra(EXTRA_CALENDAREVENT_TYPE, (byte) -1);
                mDeviceSupport.onDeleteCalendarEvent(type, id);
                break;
            }
        case ACTION_REBOOT:
            {
                mDeviceSupport.onReboot();
                break;
            }
        case ACTION_HEARTRATE_TEST:
            {
                mDeviceSupport.onHeartRateTest();
                break;
            }
        case ACTION_FETCH_ACTIVITY_DATA:
            {
                mDeviceSupport.onFetchActivityData();
                break;
            }
        case ACTION_DISCONNECT:
            {
                mDeviceSupport.dispose();
                if (mGBDevice != null && mGBDevice.getState() == GBDevice.State.WAITING_FOR_RECONNECT) {
                    setReceiversEnableState(false);
                    mGBDevice.setState(GBDevice.State.NOT_CONNECTED);
                    mGBDevice.sendDeviceUpdateIntent(this);
                }
                mDeviceSupport = null;
                break;
            }
        case ACTION_FIND_DEVICE:
            {
                boolean start = intent.getBooleanExtra(EXTRA_FIND_START, false);
                mDeviceSupport.onFindDevice(start);
                break;
            }
        case ACTION_SET_CONSTANT_VIBRATION:
            {
                int intensity = intent.getIntExtra(EXTRA_VIBRATION_INTENSITY, 0);
                mDeviceSupport.onSetConstantVibration(intensity);
                break;
            }
        case ACTION_CALLSTATE:
            CallSpec callSpec = new CallSpec();
            callSpec.command = intent.getIntExtra(EXTRA_CALL_COMMAND, CallSpec.CALL_UNDEFINED);
            callSpec.number = intent.getStringExtra(EXTRA_CALL_PHONENUMBER);
            callSpec.name = intent.getStringExtra(EXTRA_CALL_DISPLAYNAME);
            mDeviceSupport.onSetCallState(callSpec);
            break;
        case ACTION_SETCANNEDMESSAGES:
            int type = intent.getIntExtra(EXTRA_CANNEDMESSAGES_TYPE, -1);
            String[] cannedMessages = intent.getStringArrayExtra(EXTRA_CANNEDMESSAGES);
            CannedMessagesSpec cannedMessagesSpec = new CannedMessagesSpec();
            cannedMessagesSpec.type = type;
            cannedMessagesSpec.cannedMessages = cannedMessages;
            mDeviceSupport.onSetCannedMessages(cannedMessagesSpec);
            break;
        case ACTION_SETTIME:
            mDeviceSupport.onSetTime();
            break;
        case ACTION_SETMUSICINFO:
            MusicSpec musicSpec = new MusicSpec();
            musicSpec.artist = intent.getStringExtra(EXTRA_MUSIC_ARTIST);
            musicSpec.album = intent.getStringExtra(EXTRA_MUSIC_ALBUM);
            musicSpec.track = intent.getStringExtra(EXTRA_MUSIC_TRACK);
            musicSpec.duration = intent.getIntExtra(EXTRA_MUSIC_DURATION, 0);
            musicSpec.trackCount = intent.getIntExtra(EXTRA_MUSIC_TRACKCOUNT, 0);
            musicSpec.trackNr = intent.getIntExtra(EXTRA_MUSIC_TRACKNR, 0);
            mDeviceSupport.onSetMusicInfo(musicSpec);
            break;
        case ACTION_SETMUSICSTATE:
            MusicStateSpec stateSpec = new MusicStateSpec();
            stateSpec.shuffle = intent.getByteExtra(EXTRA_MUSIC_SHUFFLE, (byte) 0);
            stateSpec.repeat = intent.getByteExtra(EXTRA_MUSIC_REPEAT, (byte) 0);
            stateSpec.position = intent.getIntExtra(EXTRA_MUSIC_POSITION, 0);
            stateSpec.playRate = intent.getIntExtra(EXTRA_MUSIC_RATE, 0);
            stateSpec.state = intent.getByteExtra(EXTRA_MUSIC_STATE, (byte) 0);
            mDeviceSupport.onSetMusicState(stateSpec);
            break;
        case ACTION_REQUEST_APPINFO:
            mDeviceSupport.onAppInfoReq();
            break;
        case ACTION_REQUEST_SCREENSHOT:
            mDeviceSupport.onScreenshotReq();
            break;
        case ACTION_STARTAPP:
            {
                UUID uuid = (UUID) intent.getSerializableExtra(EXTRA_APP_UUID);
                boolean start = intent.getBooleanExtra(EXTRA_APP_START, true);
                mDeviceSupport.onAppStart(uuid, start);
                break;
            }
        case ACTION_DELETEAPP:
            {
                UUID uuid = (UUID) intent.getSerializableExtra(EXTRA_APP_UUID);
                mDeviceSupport.onAppDelete(uuid);
                break;
            }
        case ACTION_APP_CONFIGURE:
            {
                UUID uuid = (UUID) intent.getSerializableExtra(EXTRA_APP_UUID);
                String config = intent.getStringExtra(EXTRA_APP_CONFIG);
                mDeviceSupport.onAppConfiguration(uuid, config);
                break;
            }
        case ACTION_APP_REORDER:
            {
                UUID[] uuids = (UUID[]) intent.getSerializableExtra(EXTRA_APP_UUID);
                mDeviceSupport.onAppReorder(uuids);
                break;
            }
        case ACTION_INSTALL:
            Uri uri = intent.getParcelableExtra(EXTRA_URI);
            if (uri != null) {
                LOG.info("will try to install app/fw");
                mDeviceSupport.onInstallApp(uri);
            }
            break;
        case ACTION_SET_ALARMS:
            ArrayList<Alarm> alarms = intent.getParcelableArrayListExtra(EXTRA_ALARMS);
            mDeviceSupport.onSetAlarms(alarms);
            break;
        case ACTION_ENABLE_REALTIME_STEPS:
            {
                boolean enable = intent.getBooleanExtra(EXTRA_BOOLEAN_ENABLE, false);
                mDeviceSupport.onEnableRealtimeSteps(enable);
                break;
            }
        case ACTION_ENABLE_HEARTRATE_SLEEP_SUPPORT:
            {
                boolean enable = intent.getBooleanExtra(EXTRA_BOOLEAN_ENABLE, false);
                mDeviceSupport.onEnableHeartRateSleepSupport(enable);
                break;
            }
        case ACTION_ENABLE_REALTIME_HEARTRATE_MEASUREMENT:
            {
                boolean enable = intent.getBooleanExtra(EXTRA_BOOLEAN_ENABLE, false);
                mDeviceSupport.onEnableRealtimeHeartRateMeasurement(enable);
                break;
            }
        case ACTION_SEND_CONFIGURATION:
            {
                String config = intent.getStringExtra(EXTRA_CONFIG);
                mDeviceSupport.onSendConfiguration(config);
                break;
            }
        case ACTION_TEST_NEW_FUNCTION:
            {
                mDeviceSupport.onTestNewFunction();
                break;
            }
        case ACTION_SEND_WEATHER:
            {
                WeatherSpec weatherSpec = new WeatherSpec();
                weatherSpec.timestamp = intent.getIntExtra(EXTRA_WEATHER_TIMESTAMP, 0);
                weatherSpec.location = intent.getStringExtra(EXTRA_WEATHER_LOCATION);
                weatherSpec.currentTemp = intent.getIntExtra(EXTRA_WEATHER_CURRENTTEMP, 0);
                weatherSpec.currentConditionCode = intent.getIntExtra(EXTRA_WEATHER_CURRENTCONDITIONCODE, 0);
                weatherSpec.currentCondition = intent.getStringExtra(EXTRA_WEATHER_CURRENTCONDITION);
                weatherSpec.todayMaxTemp = intent.getIntExtra(EXTRA_WEATHER_TODAYMAXTEMP, 0);
                weatherSpec.todayMinTemp = intent.getIntExtra(EXTRA_WEATHER_TODAYMINTEMP, 0);
                weatherSpec.tomorrowMaxTemp = intent.getIntExtra(EXTRA_WEATHER_TOMORROWMAXTEMP, 0);
                weatherSpec.tomorrowMinTemp = intent.getIntExtra(EXTRA_WEATHER_TOMORROWMINTEMP, 0);
                weatherSpec.tomorrowConditionCode = intent.getIntExtra(EXTRA_WEATHER_TOMORROWCONDITIONCODE, 0);
                mDeviceSupport.onSendWeather(weatherSpec);
                break;
            }
    }
    return START_STICKY;
}
Also used : ArrayList(java.util.ArrayList) Prefs(nodomain.freeyourgadget.gadgetbridge.util.Prefs) GBPrefs(nodomain.freeyourgadget.gadgetbridge.util.GBPrefs) Uri(android.net.Uri) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) MusicStateSpec(nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec) MusicSpec(nodomain.freeyourgadget.gadgetbridge.model.MusicSpec) CallSpec(nodomain.freeyourgadget.gadgetbridge.model.CallSpec) NotificationSpec(nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec) NotificationType(nodomain.freeyourgadget.gadgetbridge.model.NotificationType) Alarm(nodomain.freeyourgadget.gadgetbridge.model.Alarm) WeatherSpec(nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec) CannedMessagesSpec(nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec) EXTRA_APP_UUID(nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_APP_UUID) UUID(java.util.UUID) CalendarEventSpec(nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec)

Example 2 with CalendarEventSpec

use of nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec in project Gadgetbridge by Freeyourgadget.

the class AlarmReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    if (!GBApplication.getPrefs().getBoolean("send_sunrise_sunset", false)) {
        LOG.info("won't send sunrise and sunset events (disabled in preferences)");
        return;
    }
    LOG.info("will resend sunrise and sunset events");
    final GregorianCalendar dateTimeTomorrow = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    dateTimeTomorrow.set(Calendar.HOUR, 0);
    dateTimeTomorrow.set(Calendar.MINUTE, 0);
    dateTimeTomorrow.set(Calendar.SECOND, 0);
    dateTimeTomorrow.set(Calendar.MILLISECOND, 0);
    dateTimeTomorrow.add(GregorianCalendar.DAY_OF_MONTH, 1);
    /*
         * rotate ids ud reuse the id from two days ago for tomorrow, this way we will have
         * sunrise /sunset for 3 days while sending only sunrise/sunset per day
         */
    byte id_tomorrow = (byte) ((dateTimeTomorrow.getTimeInMillis() / (1000L * 60L * 60L * 24L)) % 3);
    GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNRISE, id_tomorrow);
    GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNSET, id_tomorrow);
    Prefs prefs = GBApplication.getPrefs();
    float latitude = prefs.getFloat("location_latitude", 0);
    float longitude = prefs.getFloat("location_longitude", 0);
    LOG.info("got longitude/latitude from preferences: " + latitude + "/" + longitude);
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED && prefs.getBoolean("use_updated_location_if_available", false)) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, false);
        if (provider != null) {
            Location lastKnownLocation = locationManager.getLastKnownLocation(provider);
            if (lastKnownLocation != null) {
                latitude = (float) lastKnownLocation.getLatitude();
                longitude = (float) lastKnownLocation.getLongitude();
                LOG.info("got longitude/latitude from last known location: " + latitude + "/" + longitude);
            }
        }
    }
    GregorianCalendar[] sunriseTransitSetTomorrow = SPA.calculateSunriseTransitSet(dateTimeTomorrow, latitude, longitude, DeltaT.estimate(dateTimeTomorrow));
    CalendarEventSpec calendarEventSpec = new CalendarEventSpec();
    calendarEventSpec.durationInSeconds = 0;
    calendarEventSpec.description = null;
    calendarEventSpec.type = CalendarEventSpec.TYPE_SUNRISE;
    calendarEventSpec.title = "Sunrise";
    if (sunriseTransitSetTomorrow[0] != null) {
        calendarEventSpec.id = id_tomorrow;
        calendarEventSpec.timestamp = (int) (sunriseTransitSetTomorrow[0].getTimeInMillis() / 1000);
        GBApplication.deviceService().onAddCalendarEvent(calendarEventSpec);
    }
    calendarEventSpec.type = CalendarEventSpec.TYPE_SUNSET;
    calendarEventSpec.title = "Sunset";
    if (sunriseTransitSetTomorrow[2] != null) {
        calendarEventSpec.id = id_tomorrow;
        calendarEventSpec.timestamp = (int) (sunriseTransitSetTomorrow[2].getTimeInMillis() / 1000);
        GBApplication.deviceService().onAddCalendarEvent(calendarEventSpec);
    }
}
Also used : LocationManager(android.location.LocationManager) GregorianCalendar(java.util.GregorianCalendar) Criteria(android.location.Criteria) Prefs(nodomain.freeyourgadget.gadgetbridge.util.Prefs) CalendarEventSpec(nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec) Location(android.location.Location)

Aggregations

CalendarEventSpec (nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec)2 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)2 Criteria (android.location.Criteria)1 Location (android.location.Location)1 LocationManager (android.location.LocationManager)1 Uri (android.net.Uri)1 ArrayList (java.util.ArrayList)1 GregorianCalendar (java.util.GregorianCalendar)1 UUID (java.util.UUID)1 GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)1 Alarm (nodomain.freeyourgadget.gadgetbridge.model.Alarm)1 CallSpec (nodomain.freeyourgadget.gadgetbridge.model.CallSpec)1 CannedMessagesSpec (nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec)1 EXTRA_APP_UUID (nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_APP_UUID)1 MusicSpec (nodomain.freeyourgadget.gadgetbridge.model.MusicSpec)1 MusicStateSpec (nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec)1 NotificationSpec (nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec)1 NotificationType (nodomain.freeyourgadget.gadgetbridge.model.NotificationType)1 WeatherSpec (nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec)1 GBPrefs (nodomain.freeyourgadget.gadgetbridge.util.GBPrefs)1