Search in sources :

Example 11 with GBApplication

use of nodomain.freeyourgadget.gadgetbridge.GBApplication in project Gadgetbridge by Freeyourgadget.

the class DebugActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_debug);
    IntentFilter filter = new IntentFilter();
    filter.addAction(ACTION_REPLY);
    filter.addAction(DeviceService.ACTION_REALTIME_SAMPLES);
    LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter);
    // for ACTION_REPLY
    registerReceiver(mReceiver, filter);
    editContent = findViewById(R.id.editContent);
    final ArrayList<String> spinnerArray = new ArrayList<>();
    for (NotificationType notificationType : NotificationType.values()) {
        spinnerArray.add(notificationType.name());
    }
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
    sendTypeSpinner = findViewById(R.id.sendTypeSpinner);
    sendTypeSpinner.setAdapter(spinnerArrayAdapter);
    Button sendButton = findViewById(R.id.sendButton);
    sendButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            NotificationSpec notificationSpec = new NotificationSpec();
            String testString = editContent.getText().toString();
            notificationSpec.phoneNumber = testString;
            notificationSpec.body = testString;
            notificationSpec.sender = testString;
            notificationSpec.subject = testString;
            notificationSpec.type = NotificationType.values()[sendTypeSpinner.getSelectedItemPosition()];
            notificationSpec.pebbleColor = notificationSpec.type.color;
            GBApplication.deviceService().onNotification(notificationSpec);
        }
    });
    Button incomingCallButton = findViewById(R.id.incomingCallButton);
    incomingCallButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            CallSpec callSpec = new CallSpec();
            callSpec.command = CallSpec.CALL_INCOMING;
            callSpec.number = editContent.getText().toString();
            GBApplication.deviceService().onSetCallState(callSpec);
        }
    });
    Button outgoingCallButton = findViewById(R.id.outgoingCallButton);
    outgoingCallButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            CallSpec callSpec = new CallSpec();
            callSpec.command = CallSpec.CALL_OUTGOING;
            callSpec.number = editContent.getText().toString();
            GBApplication.deviceService().onSetCallState(callSpec);
        }
    });
    Button startCallButton = findViewById(R.id.startCallButton);
    startCallButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            CallSpec callSpec = new CallSpec();
            callSpec.command = CallSpec.CALL_START;
            GBApplication.deviceService().onSetCallState(callSpec);
        }
    });
    Button endCallButton = findViewById(R.id.endCallButton);
    endCallButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            CallSpec callSpec = new CallSpec();
            callSpec.command = CallSpec.CALL_END;
            GBApplication.deviceService().onSetCallState(callSpec);
        }
    });
    Button rebootButton = findViewById(R.id.rebootButton);
    rebootButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            GBApplication.deviceService().onReset(GBDeviceProtocol.RESET_FLAGS_REBOOT);
        }
    });
    Button factoryResetButton = findViewById(R.id.factoryResetButton);
    factoryResetButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            new AlertDialog.Builder(DebugActivity.this).setCancelable(true).setTitle(R.string.debugactivity_really_factoryreset_title).setMessage(R.string.debugactivity_really_factoryreset).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    GBApplication.deviceService().onReset(GBDeviceProtocol.RESET_FLAGS_FACTORY_RESET);
                }
            }).setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            }).show();
        }
    });
    Button heartRateButton = findViewById(R.id.HeartRateButton);
    heartRateButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            GB.toast("Measuring heart rate, please wait...", Toast.LENGTH_LONG, GB.INFO);
            GBApplication.deviceService().onHeartRateTest();
        }
    });
    Button setFetchTimeButton = findViewById(R.id.SetFetchTimeButton);
    setFetchTimeButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final Calendar currentDate = Calendar.getInstance();
            Context context = getApplicationContext();
            if (context instanceof GBApplication) {
                GBApplication gbApp = (GBApplication) context;
                final GBDevice device = gbApp.getDeviceManager().getSelectedDevice();
                if (device != null) {
                    new DatePickerDialog(DebugActivity.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);
                            long timestamp = date.getTimeInMillis() - 1000;
                            GB.toast("Setting lastSyncTimeMillis: " + timestamp, Toast.LENGTH_LONG, GB.INFO);
                            SharedPreferences.Editor editor = GBApplication.getDeviceSpecificSharedPrefs(device.getAddress()).edit();
                            // FIXME: key reconstruction is BAD
                            editor.remove("lastSyncTimeMillis");
                            editor.putLong("lastSyncTimeMillis", timestamp);
                            editor.apply();
                        }
                    }, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE)).show();
                } else {
                    GB.toast("Device not selected/connected", Toast.LENGTH_LONG, GB.INFO);
                }
            }
        }
    });
    Button setMusicInfoButton = findViewById(R.id.setMusicInfoButton);
    setMusicInfoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            MusicSpec musicSpec = new MusicSpec();
            String testString = editContent.getText().toString();
            musicSpec.artist = testString + "(artist)";
            musicSpec.album = testString + "(album)";
            musicSpec.track = testString + "(track)";
            musicSpec.duration = 10;
            musicSpec.trackCount = 5;
            musicSpec.trackNr = 2;
            GBApplication.deviceService().onSetMusicInfo(musicSpec);
            MusicStateSpec stateSpec = new MusicStateSpec();
            stateSpec.position = 0;
            // playing
            stateSpec.state = 0x01;
            stateSpec.playRate = 100;
            stateSpec.repeat = 1;
            stateSpec.shuffle = 1;
            GBApplication.deviceService().onSetMusicState(stateSpec);
        }
    });
    Button setTimeButton = findViewById(R.id.setTimeButton);
    setTimeButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            GBApplication.deviceService().onSetTime();
        }
    });
    Button testNotificationButton = findViewById(R.id.testNotificationButton);
    testNotificationButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            testNotification();
        }
    });
    Button testPebbleKitNotificationButton = findViewById(R.id.testPebbleKitNotificationButton);
    testPebbleKitNotificationButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            testPebbleKitNotification();
        }
    });
    Button fetchDebugLogsButton = findViewById(R.id.fetchDebugLogsButton);
    fetchDebugLogsButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            GBApplication.deviceService().onFetchRecordedData(RecordedDataTypes.TYPE_DEBUGLOGS);
        }
    });
    Button testNewFunctionalityButton = findViewById(R.id.testNewFunctionality);
    testNewFunctionalityButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            testNewFunctionality();
        }
    });
    Button shareLogButton = findViewById(R.id.shareLog);
    shareLogButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showWarning();
        }
    });
    Button showWidgetsButton = findViewById(R.id.showWidgetsButton);
    showWidgetsButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showAllRegisteredAppWidgets();
        }
    });
    Button unregisterWidgetsButton = findViewById(R.id.deleteWidgets);
    unregisterWidgetsButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            unregisterAllRegisteredAppWidgets();
        }
    });
    Button showWidgetsPrefsButton = findViewById(R.id.showWidgetsPrefs);
    showWidgetsPrefsButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showAppWidgetsPrefs();
        }
    });
    Button deleteWidgetsPrefsButton = findViewById(R.id.deleteWidgetsPrefs);
    deleteWidgetsPrefsButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            deleteWidgetsPrefs();
        }
    });
    Button removeDevicePreferencesButton = findViewById(R.id.removeDevicePreferences);
    removeDevicePreferencesButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Context context = getApplicationContext();
            GBApplication gbApp = (GBApplication) context;
            final GBDevice device = gbApp.getDeviceManager().getSelectedDevice();
            if (device != null) {
                GBApplication.deleteDeviceSpecificSharedPrefs(device.getAddress());
            }
        }
    });
    Button runDebugFunction = findViewById(R.id.runDebugFunction);
    runDebugFunction.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        // SharedPreferences.Editor editor = GBApplication.getPrefs().getPreferences().edit();
        // editor.remove("notification_list_is_blacklist").apply();
        }
    });
    Button addDeviceButtonDebug = findViewById(R.id.addDeviceButtonDebug);
    addDeviceButtonDebug.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            LinkedHashMap<String, Pair<Long, Integer>> allDevices;
            allDevices = getAllSupportedDevices(getApplicationContext());
            final LinearLayout linearLayout = new LinearLayout(DebugActivity.this);
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            final LinearLayout macLayout = new LinearLayout(DebugActivity.this);
            macLayout.setOrientation(LinearLayout.HORIZONTAL);
            macLayout.setPadding(20, 0, 20, 0);
            final TextView textView = new TextView(DebugActivity.this);
            textView.setText("MAC Address: ");
            final EditText editText = new EditText(DebugActivity.this);
            selectedTestDeviceMAC = randomMac();
            editText.setText(selectedTestDeviceMAC);
            editText.addTextChangedListener(new TextWatcher() {

                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    selectedTestDeviceMAC = editable.toString();
                }
            });
            macLayout.addView(textView);
            macLayout.addView(editText);
            final Spinner deviceListSpinner = new Spinner(DebugActivity.this);
            ArrayList<SpinnerWithIconItem> deviceListArray = new ArrayList<>();
            for (Map.Entry<String, Pair<Long, Integer>> item : allDevices.entrySet()) {
                deviceListArray.add(new SpinnerWithIconItem(item.getKey(), item.getValue().first, item.getValue().second));
            }
            final SpinnerWithIconAdapter deviceListAdapter = new SpinnerWithIconAdapter(DebugActivity.this, R.layout.spinner_with_image_layout, R.id.spinner_item_text, deviceListArray);
            deviceListSpinner.setAdapter(deviceListAdapter);
            addListenerOnSpinnerDeviceSelection(deviceListSpinner);
            linearLayout.addView(deviceListSpinner);
            linearLayout.addView(macLayout);
            new AlertDialog.Builder(DebugActivity.this).setCancelable(true).setTitle(R.string.add_test_device).setView(linearLayout).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    createTestDevice(DebugActivity.this, selectedTestDeviceKey, selectedTestDeviceMAC);
                }
            }).setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            }).show();
        }
    });
    CheckBox activity_list_debug_extra_time_range = findViewById(R.id.activity_list_debug_extra_time_range);
    activity_list_debug_extra_time_range.setAllCaps(true);
    boolean activity_list_debug_extra_time_range_value = GBApplication.getPrefs().getPreferences().getBoolean("activity_list_debug_extra_time_range", false);
    activity_list_debug_extra_time_range.setChecked(activity_list_debug_extra_time_range_value);
    activity_list_debug_extra_time_range.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            GBApplication.getPrefs().getPreferences().getBoolean("activity_list_debug_extra_time_range", false);
            SharedPreferences.Editor editor = GBApplication.getPrefs().getPreferences().edit();
            editor.putBoolean("activity_list_debug_extra_time_range", b).apply();
        }
    });
}
Also used : AlertDialog(android.app.AlertDialog) GBApplication(nodomain.freeyourgadget.gadgetbridge.GBApplication) DialogInterface(android.content.DialogInterface) Spinner(android.widget.Spinner) ArrayList(java.util.ArrayList) SpinnerWithIconItem(nodomain.freeyourgadget.gadgetbridge.adapter.SpinnerWithIconItem) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) LinkedHashMap(java.util.LinkedHashMap) MusicStateSpec(nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec) MusicSpec(nodomain.freeyourgadget.gadgetbridge.model.MusicSpec) Button(android.widget.Button) CompoundButton(android.widget.CompoundButton) CallSpec(nodomain.freeyourgadget.gadgetbridge.model.CallSpec) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) TextView(android.widget.TextView) Context(android.content.Context) EditText(android.widget.EditText) IntentFilter(android.content.IntentFilter) DatePickerDialog(android.app.DatePickerDialog) Calendar(java.util.Calendar) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) CheckBox(android.widget.CheckBox) NotificationType(nodomain.freeyourgadget.gadgetbridge.model.NotificationType) NotificationSpec(nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec) DatePicker(android.widget.DatePicker) ArrayAdapter(android.widget.ArrayAdapter) LinearLayout(android.widget.LinearLayout) SpinnerWithIconAdapter(nodomain.freeyourgadget.gadgetbridge.adapter.SpinnerWithIconAdapter) CompoundButton(android.widget.CompoundButton)

Example 12 with GBApplication

use of nodomain.freeyourgadget.gadgetbridge.GBApplication in project Gadgetbridge by Freeyourgadget.

the class ExternalPebbleJSActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle extras = getIntent().getExtras();
    boolean showConfig = false;
    UUID currentUUID = null;
    GBDevice currentDevice = null;
    if (extras == null) {
        confUri = getIntent().getData();
        if (confUri.getScheme().equals("gadgetbridge")) {
            try {
                currentUUID = UUID.fromString(confUri.getHost());
            } catch (IllegalArgumentException e) {
                LOG.error("UUID in incoming configuration is not a valid UUID: " + confUri.toString());
            }
            // first check if we are still connected to a pebble
            DeviceManager deviceManager = ((GBApplication) getApplication()).getDeviceManager();
            List<GBDevice> deviceList = deviceManager.getDevices();
            for (GBDevice device : deviceList) {
                if (device.getState() == GBDevice.State.INITIALIZED) {
                    if (device.getType().equals(DeviceType.PEBBLE)) {
                        currentDevice = device;
                        break;
                    } else {
                        LOG.error("attempting to load pebble configuration but a different device type is connected!!!");
                        finish();
                        return;
                    }
                }
            }
            if (currentDevice == null) {
                // then try to reconnect to last connected device
                String btDeviceAddress = GBApplication.getPrefs().getPreferences().getString("last_device_address", null);
                if (btDeviceAddress != null) {
                    GBDevice candidate = DeviceHelper.getInstance().findAvailableDevice(btDeviceAddress, this);
                    if (!candidate.isConnected() && candidate.getType() == DeviceType.PEBBLE) {
                        Intent intent = new Intent(this, DeviceCommunicationService.class).setAction(ACTION_CONNECT).putExtra(GBDevice.EXTRA_DEVICE, currentDevice);
                        this.startService(intent);
                        currentDevice = candidate;
                    }
                }
            }
            // we are getting incoming configuration data
            showConfig = true;
        }
    } else {
        currentDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE);
        currentUUID = (UUID) extras.getSerializable(DeviceService.EXTRA_APP_UUID);
        if (extras.getBoolean(START_BG_WEBVIEW, false)) {
            startBackgroundWebViewAndFinish();
            return;
        }
        showConfig = extras.getBoolean(SHOW_CONFIG, false);
    }
    if (GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
        if (showConfig) {
            Objects.requireNonNull(currentDevice, "Must provide a device when invoking this activity");
            Objects.requireNonNull(currentUUID, "Must provide a uuid when invoking this activity");
            WebViewSingleton.getInstance().runJavascriptInterface(this, currentDevice, currentUUID);
        }
        // FIXME: is this really supposed to be outside the check for SHOW_CONFIG?
        setupBGWebView();
    } else {
        Objects.requireNonNull(currentDevice, "Must provide a device when invoking this activity without bgjs");
        Objects.requireNonNull(currentUUID, "Must provide a uuid when invoking this activity without bgjs");
        setupLegacyWebView(currentDevice, currentUUID);
    }
}
Also used : GBApplication(nodomain.freeyourgadget.gadgetbridge.GBApplication) Bundle(android.os.Bundle) DeviceManager(nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager) Intent(android.content.Intent) UUID(java.util.UUID) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)

Example 13 with GBApplication

use of nodomain.freeyourgadget.gadgetbridge.GBApplication in project Gadgetbridge by Freeyourgadget.

the class WidgetConfigurationActivity method getAllDevices.

public LinkedHashMap getAllDevices(Context appContext) {
    DaoSession daoSession;
    GBApplication gbApp = (GBApplication) appContext;
    LinkedHashMap<String, Pair<String, Integer>> newMap = new LinkedHashMap<>(1);
    List<? extends GBDevice> devices = gbApp.getDeviceManager().getDevices();
    try (DBHandler handler = GBApplication.acquireDB()) {
        daoSession = handler.getDaoSession();
        for (GBDevice device : devices) {
            DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device);
            Device dbDevice = DBHelper.findDevice(device, daoSession);
            int icon = device.isInitialized() ? device.getType().getIcon() : device.getType().getDisabledIcon();
            if (dbDevice != null && coordinator != null && (coordinator.supportsActivityDataFetching() || coordinator.supportsActivityTracking()) && !newMap.containsKey(device.getAliasOrName())) {
                newMap.put(device.getAliasOrName(), new Pair(device.getAddress(), icon));
            }
        }
    } catch (Exception e) {
        LOG.error("Error getting list of all devices: " + e);
    }
    return newMap;
}
Also used : GBApplication(nodomain.freeyourgadget.gadgetbridge.GBApplication) Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) LinkedHashMap(java.util.LinkedHashMap) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) DeviceCoordinator(nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator) DaoSession(nodomain.freeyourgadget.gadgetbridge.entities.DaoSession) Pair(android.util.Pair)

Aggregations

GBApplication (nodomain.freeyourgadget.gadgetbridge.GBApplication)13 GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)9 Context (android.content.Context)7 DeviceCoordinator (nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator)6 Bundle (android.os.Bundle)5 ArrayList (java.util.ArrayList)5 LinkedHashMap (java.util.LinkedHashMap)5 Intent (android.content.Intent)4 Pair (android.util.Pair)4 View (android.view.View)4 TextView (android.widget.TextView)4 ArrayAdapter (android.widget.ArrayAdapter)3 EditText (android.widget.EditText)3 LinearLayout (android.widget.LinearLayout)3 DBHandler (nodomain.freeyourgadget.gadgetbridge.database.DBHandler)3 DaoSession (nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)3 Device (nodomain.freeyourgadget.gadgetbridge.entities.Device)3 AlertDialog (android.app.AlertDialog)2 DatePickerDialog (android.app.DatePickerDialog)2 DialogInterface (android.content.DialogInterface)2