Search in sources :

Example 11 with GBDevice

use of nodomain.freeyourgadget.gadgetbridge.impl.GBDevice in project Gadgetbridge by Freeyourgadget.

the class DBHelper method createDevice.

private static Device createDevice(GBDevice gbDevice, DaoSession session) {
    Device device = new Device();
    ensureDeviceUpToDate(device, gbDevice, session);
    return device;
}
Also used : Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)

Example 12 with GBDevice

use of nodomain.freeyourgadget.gadgetbridge.impl.GBDevice in project Gadgetbridge by Freeyourgadget.

the class AbstractDeviceCoordinator method deleteDevice.

@Override
public void deleteDevice(final GBDevice gbDevice) throws GBException {
    LOG.info("will try to delete device: " + gbDevice.getName());
    if (gbDevice.isConnected() || gbDevice.isConnecting()) {
        GBApplication.deviceService().disconnect();
    }
    try (DBHandler dbHandler = GBApplication.acquireDB()) {
        DaoSession session = dbHandler.getDaoSession();
        Device device = DBHelper.findDevice(gbDevice, session);
        if (device != null) {
            deleteDevice(gbDevice, device, session);
            QueryBuilder<?> qb = session.getDeviceAttributesDao().queryBuilder();
            qb.where(DeviceAttributesDao.Properties.DeviceId.eq(device.getId())).buildDelete().executeDeleteWithoutDetachingEntities();
            session.getDeviceDao().delete(device);
        } else {
            LOG.info("device to delete not found in db: " + gbDevice);
        }
    } catch (Exception e) {
        throw new GBException("Error deleting device: " + e.getMessage(), e);
    }
}
Also used : DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) BluetoothDevice(android.bluetooth.BluetoothDevice) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) GBException(nodomain.freeyourgadget.gadgetbridge.GBException) GBException(nodomain.freeyourgadget.gadgetbridge.GBException) DaoSession(nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)

Example 13 with GBDevice

use of nodomain.freeyourgadget.gadgetbridge.impl.GBDevice in project Gadgetbridge by Freeyourgadget.

the class DeviceManager method refreshPairedDevices.

private void refreshPairedDevices() {
    Set<GBDevice> availableDevices = DeviceHelper.getInstance().getAvailableDevices(context);
    deviceList.retainAll(availableDevices);
    for (GBDevice availableDevice : availableDevices) {
        if (!deviceList.contains(availableDevice)) {
            deviceList.add(availableDevice);
        }
    }
    Collections.sort(deviceList, new Comparator<GBDevice>() {

        @Override
        public int compare(GBDevice lhs, GBDevice rhs) {
            return Collator.getInstance().compare(lhs.getName(), rhs.getName());
        }
    });
    notifyDevicesChanged();
}
Also used : GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)

Example 14 with GBDevice

use of nodomain.freeyourgadget.gadgetbridge.impl.GBDevice 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 15 with GBDevice

use of nodomain.freeyourgadget.gadgetbridge.impl.GBDevice in project Gadgetbridge by Freeyourgadget.

the class ControlCenterv2 method refreshPairedDevices.

private void refreshPairedDevices() {
    List<GBDevice> deviceList = deviceManager.getDevices();
    GBDevice connectedDevice = null;
    for (GBDevice device : deviceList) {
        if (device.isConnected() || device.isConnecting()) {
            connectedDevice = device;
            break;
        }
    }
    if (deviceList.isEmpty()) {
        background.setVisibility(View.VISIBLE);
    } else {
        background.setVisibility(View.INVISIBLE);
    }
    mGBDeviceAdapter.notifyDataSetChanged();
}
Also used : GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)

Aggregations

GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)23 Device (nodomain.freeyourgadget.gadgetbridge.entities.Device)7 BluetoothDevice (android.bluetooth.BluetoothDevice)4 DBHandler (nodomain.freeyourgadget.gadgetbridge.database.DBHandler)3 DeviceAttributes (nodomain.freeyourgadget.gadgetbridge.entities.DeviceAttributes)3 Test (org.junit.Test)3 Intent (android.content.Intent)2 View (android.view.View)2 ListView (android.widget.ListView)2 TextView (android.widget.TextView)2 ArrayList (java.util.ArrayList)2 DaoSession (nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)2 DeviceDao (nodomain.freeyourgadget.gadgetbridge.entities.DeviceDao)2 Activity (android.app.Activity)1 AlertDialog (android.app.AlertDialog)1 BluetoothAdapter (android.bluetooth.BluetoothAdapter)1 DialogInterface (android.content.DialogInterface)1 IntentFilter (android.content.IntentFilter)1 Uri (android.net.Uri)1 Snackbar (android.support.design.widget.Snackbar)1