Search in sources :

Example 1 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 2 with GBDevice

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

the class FwAppInstallerActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_appinstaller);
    GBDevice dev = getIntent().getParcelableExtra(GBDevice.EXTRA_DEVICE);
    if (dev != null) {
        device = dev;
    }
    if (savedInstanceState != null) {
        mDetails = savedInstanceState.getParcelableArrayList(ITEM_DETAILS);
        if (mDetails == null) {
            mDetails = new ArrayList<>();
        }
    }
    mayConnect = true;
    itemListView = (ListView) findViewById(R.id.itemListView);
    mItemAdapter = new ItemWithDetailsAdapter(this, mItems);
    itemListView.setAdapter(mItemAdapter);
    fwAppInstallTextView = (TextView) findViewById(R.id.infoTextView);
    installButton = (Button) findViewById(R.id.installButton);
    mProgressBar = (ProgressBar) findViewById(R.id.installProgressBar);
    detailsListView = (ListView) findViewById(R.id.detailsListView);
    mDetailsItemAdapter = new ItemWithDetailsAdapter(this, mDetails);
    mDetailsItemAdapter.setSize(ItemWithDetailsAdapter.SIZE_SMALL);
    detailsListView.setAdapter(mDetailsItemAdapter);
    setInstallEnabled(false);
    IntentFilter filter = new IntentFilter();
    filter.addAction(GBApplication.ACTION_QUIT);
    filter.addAction(GBDevice.ACTION_DEVICE_CHANGED);
    filter.addAction(GB.ACTION_DISPLAY_MESSAGE);
    LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter);
    installButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            setInstallEnabled(false);
            installHandler.onStartInstall(device);
            GBApplication.deviceService().onInstallApp(uri);
        }
    });
    uri = getIntent().getData();
    if (uri == null) {
        //for "share" intent
        uri = getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
    }
    installHandler = findInstallHandlerFor(uri);
    if (installHandler == null) {
        setInfoText(getString(R.string.installer_activity_unable_to_find_handler));
    } else {
        setInfoText(getString(R.string.installer_activity_wait_while_determining_status));
        // needed to get the device
        if (device == null || !device.isConnected()) {
            connect();
        } else {
            GBApplication.deviceService().requestDeviceInfo();
        }
    }
}
Also used : IntentFilter(android.content.IntentFilter) ItemWithDetailsAdapter(nodomain.freeyourgadget.gadgetbridge.adapter.ItemWithDetailsAdapter) View(android.view.View) TextView(android.widget.TextView) ListView(android.widget.ListView) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)

Example 3 with GBDevice

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

the class DeviceCommunicationServiceTestCase method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    mockSupport = null;
    realSupport = new TestDeviceSupport();
    realSupport.setContext(new GBDevice(TEST_DEVICE_ADDRESS, "Test Device", DeviceType.TEST), null, getContext());
    mockSupport = Mockito.spy(realSupport);
    DeviceCommunicationService.setDeviceSupportFactory(new TestDeviceSupportFactory(getContext()));
    mDeviceService = new TestDeviceService(getContext());
}
Also used : GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)

Example 4 with GBDevice

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

the class EntitiesTest method testDBHelper.

@Test
public void testDBHelper() {
    GBDevice dummyGBDevice = createDummyGDevice("00:00:00:00:01");
    Device device = DBHelper.getDevice(dummyGBDevice, daoSession);
    assertNotNull(device);
    assertEquals("00:00:00:00:01", device.getIdentifier());
    assertEquals("Testie", device.getName());
    assertEquals("4.0", device.getModel());
    assertEquals(DeviceType.TEST.getKey(), device.getType());
    DeviceAttributes attributes = device.getDeviceAttributesList().get(0);
    assertNotNull(attributes);
    assertEquals("1.2.3", attributes.getFirmwareVersion1());
}
Also used : GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) DeviceAttributes(nodomain.freeyourgadget.gadgetbridge.entities.DeviceAttributes) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) Test(org.junit.Test)

Example 5 with GBDevice

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

the class DeviceCommunicationServiceTestCase method testNotConnected.

@Test
public void testNotConnected() {
    GBDevice device = getDevice();
    assertEquals(GBDevice.State.NOT_CONNECTED, device.getState());
    // verify that the events like onFindDevice do not reach the DeviceSupport instance,
    // because not connected
    InOrder inOrder = Mockito.inOrder(mockSupport);
    mDeviceService.onFindDevice(true);
    inOrder.verify(mockSupport, Mockito.times(0)).onFindDevice(true);
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) Test(org.junit.Test)

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