-
Notifications
You must be signed in to change notification settings - Fork 818
Add MQTT dependencies, settings UI, and test coverage (CATROID-1670, CATROID-1672) #5220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 7 commits
1f49480
3ce2096
638bd54
4995ba8
222dd13
cc34243
ad8a53e
ddc7440
3eb0c8d
a066366
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * Catroid: An on-device visual programming system for Android devices | ||
| * Copyright (C) 2010-2025 The Catrobat Team | ||
| * (<http://developer.catrobat.org/credits>) | ||
| * | ||
| * This program 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. | ||
| * | ||
| * An additional term exception under section 7 of the GNU Affero | ||
| * General Public License, version 3, is available at | ||
| * http://developer.catrobat.org/license_additional_term | ||
| * | ||
| * This program 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 org.catrobat.catroid.ui.settingsfragments | ||
|
|
||
| import android.os.Bundle | ||
| import android.preference.CheckBoxPreference | ||
| import android.preference.EditTextPreference | ||
| import android.preference.Preference | ||
| import android.preference.PreferenceCategory | ||
| import android.preference.PreferenceFragment | ||
| import android.widget.Toast | ||
| import androidx.annotation.Nullable | ||
| import androidx.annotation.VisibleForTesting | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import org.catrobat.catroid.R | ||
| import org.catrobat.catroid.ui.settingsfragments.SettingsFragment.MQTT_CONNECTION_SETTINGS_CATEGORY | ||
| import org.catrobat.catroid.ui.settingsfragments.SettingsFragment.MQTT_CLIENT_ID | ||
| import org.catrobat.catroid.ui.settingsfragments.SettingsFragment.MQTT_HOST | ||
| import org.catrobat.catroid.ui.settingsfragments.SettingsFragment.MQTT_PASSWORD | ||
| import org.catrobat.catroid.ui.settingsfragments.SettingsFragment.MQTT_PORT | ||
| import org.catrobat.catroid.ui.settingsfragments.SettingsFragment.MQTT_USERNAME | ||
| import org.catrobat.catroid.ui.settingsfragments.SettingsFragment.SETTINGS_SHOW_MQTT_BRICKS | ||
|
|
||
| class MqttSettingsFragment : PreferenceFragment() { | ||
|
|
||
| override fun onResume() { | ||
| super.onResume() | ||
| (activity as AppCompatActivity).supportActionBar?.title = preferenceScreen.title | ||
| } | ||
|
|
||
| override fun onActivityCreated(@Nullable savedInstanceState: Bundle?) { | ||
Check warningCode scanning / Android Lint Kotlin nullability annotation Warning
Do not use @Nullable in Kotlin; the nullability is already implied by the Kotlin type Bundle? ending with ?
|
||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| super.onActivityCreated(savedInstanceState) | ||
| SettingsFragment.setToChosenLanguage(activity) | ||
| addPreferencesFromResource(R.xml.mqtt_preferences) | ||
|
|
||
| val mqttCheckBoxPreference = findPreference(SETTINGS_SHOW_MQTT_BRICKS) as CheckBoxPreference | ||
| val mqttConnectionSettings = findPreference(MQTT_CONNECTION_SETTINGS_CATEGORY) as PreferenceCategory | ||
| mqttConnectionSettings.isEnabled = mqttCheckBoxPreference.isChecked | ||
|
|
||
| mqttCheckBoxPreference.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, isChecked -> | ||
| mqttConnectionSettings.isEnabled = isChecked as Boolean | ||
| true | ||
| } | ||
|
|
||
| bindSummaryToValue(findPreference(MQTT_HOST) as EditTextPreference) | ||
| bindSummaryToValue(findPreference(MQTT_USERNAME) as EditTextPreference) | ||
| bindSummaryToValue(findPreference(MQTT_CLIENT_ID) as EditTextPreference) | ||
|
|
||
| val portPreference = findPreference(MQTT_PORT) as EditTextPreference | ||
| portPreference.summary = portPreference.text | ||
| portPreference.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue -> | ||
| if (!isValidPort(newValue.toString())) { | ||
| Toast.makeText(activity, R.string.preference_mqtt_port_invalid, Toast.LENGTH_SHORT).show() | ||
| return@OnPreferenceChangeListener false | ||
| } | ||
| portPreference.summary = newValue.toString() | ||
| true | ||
| } | ||
|
|
||
| val passwordPreference = findPreference(MQTT_PASSWORD) as EditTextPreference | ||
| passwordPreference.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, _ -> true } | ||
| } | ||
|
|
||
| private fun bindSummaryToValue(preference: EditTextPreference) { | ||
| preference.summary = preference.text | ||
| preference.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { pref, newValue -> | ||
| (pref as EditTextPreference).summary = newValue.toString() | ||
| true | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| @JvmField val TAG: String = MqttSettingsFragment::class.java.simpleName | ||
|
|
||
| @VisibleForTesting | ||
| @JvmStatic | ||
| fun isValidPort(value: String): Boolean { | ||
| val port = value.toIntOrNull() ?: return false | ||
| return port in 1..65535 | ||
Check warningCode scanning / detekt Report magic numbers. Magic number is a numeric literal that is not defined as a constant and hence it's unclear what the purpose of this number is. It's better to declare such numbers as constants and give them a proper name. By default, -1, 0, 1, and 2 are not considered to be magic numbers. Warning
This expression contains a magic number. Consider defining it to a well named constant.
Check warningCode scanning / detekt Report missing or invalid underscores in base 10 numbers. Numeric literals should be underscore separated to increase readability. Warning
This number should be separated by underscores in order to increase readability.
|
||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!-- | ||
| ~ Catroid: An on-device visual programming system for Android devices | ||
| ~ Copyright (C) 2010-2025 The Catrobat Team | ||
| ~ (<http://developer.catrobat.org/credits>) | ||
| ~ | ||
| ~ This program 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. | ||
| ~ | ||
| ~ An additional term exception under section 7 of the GNU Affero | ||
| ~ General Public License, version 3, is available at | ||
| ~ http://developer.catrobat.org/license_additional_term | ||
| ~ | ||
| ~ This program 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/>. | ||
| --> | ||
| <PreferenceScreen | ||
| android:key="settings_mqtt_screen" | ||
| android:summary="@string/preference_description_mqtt_bricks" | ||
| android:title="@string/preference_title_enable_mqtt_bricks" | ||
| xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
|
||
| <CheckBoxPreference | ||
| android:defaultValue="false" | ||
| android:key="setting_mqtt_bricks" | ||
| android:summary="@string/preference_description_mqtt_enabled" | ||
| android:title="@string/preference_title_mqtt_enabled" /> | ||
|
|
||
| <PreferenceCategory | ||
| android:key="setting_mqtt_category" | ||
| android:title="@string/preference_title_mqtt_category"> | ||
|
|
||
| <EditTextPreference | ||
| android:defaultValue="192.168.0.1" | ||
| android:key="setting_mqtt_host" | ||
| android:summary="@string/preference_description_mqtt_broker_host" | ||
| android:title="@string/preference_title_mqtt_broker_host" /> | ||
|
|
||
| <EditTextPreference | ||
| android:defaultValue="1883" | ||
| android:inputType="number" | ||
| android:key="setting_mqtt_port" | ||
| android:summary="@string/preference_description_mqtt_broker_port" | ||
| android:title="@string/preference_title_mqtt_broker_port" /> | ||
|
|
||
| <CheckBoxPreference | ||
| android:defaultValue="false" | ||
| android:key="setting_mqtt_tls" | ||
| android:title="@string/preference_title_mqtt_use_tls" /> | ||
|
|
||
| <EditTextPreference | ||
| android:defaultValue="" | ||
| android:key="setting_mqtt_username" | ||
| android:title="@string/preference_title_mqtt_username" /> | ||
|
|
||
| <EditTextPreference | ||
| android:defaultValue="" | ||
| android:inputType="textPassword" | ||
| android:key="setting_mqtt_password" | ||
| android:title="@string/preference_title_mqtt_password" /> | ||
|
|
||
| <EditTextPreference | ||
| android:defaultValue="" | ||
| android:key="setting_mqtt_client_id" | ||
| android:title="@string/preference_title_mqtt_client_id" /> | ||
|
|
||
| </PreferenceCategory> | ||
|
|
||
| </PreferenceScreen> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Catroid: An on-device visual programming system for Android devices | ||
| * Copyright (C) 2010-2026 The Catrobat Team | ||
| * (<http://developer.catrobat.org/credits>) | ||
| * | ||
| * This program 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. | ||
| * | ||
| * An additional term exception under section 7 of the GNU Affero | ||
| * General Public License, version 3, is available at | ||
| * http://developer.catrobat.org/license_additional_term | ||
| * | ||
| * This program 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 org.catrobat.catroid.test.mqtt | ||
|
|
||
| import org.eclipse.paho.client.mqttv3.MqttCallback | ||
| import org.eclipse.paho.client.mqttv3.MqttClient | ||
| import org.eclipse.paho.client.mqttv3.MqttConnectOptions | ||
| import org.eclipse.paho.client.mqttv3.MqttException | ||
| import org.eclipse.paho.client.mqttv3.MqttMessage | ||
| import org.junit.Assert.assertNotNull | ||
| import org.junit.Test | ||
|
|
||
| class MqttDependencySanityTest { | ||
|
|
||
| @Test | ||
| fun testPahoClientClassesAreAvailable() { | ||
| assertNotNull(MqttClient::class.java) | ||
| assertNotNull(MqttConnectOptions::class.java) | ||
| assertNotNull(MqttMessage::class.java) | ||
| assertNotNull(MqttException::class.java) | ||
| assertNotNull(MqttCallback::class.java) | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.