2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-22 18:07:55 +00:00

Show number of custom devices in settings fragment

This commit is contained in:
Albert Vaca Cintora 2024-12-24 17:34:56 +01:00
parent 172822239c
commit 0923c8ecda
4 changed files with 30 additions and 7 deletions

View File

@ -269,6 +269,7 @@ SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted
<string name="invalid_device_name">Invalid device name</string>
<string name="shareplugin_text_saved">Received text, saved to clipboard</string>
<string name="custom_devices_settings">Custom device list</string>
<string name="custom_devices_settings_summary">%d devices added manually</string>
<string name="custom_device_list">Add devices by IP</string>
<string name="custom_device_deleted">Custom device deleted</string>
<string name="custom_device_list_help">If your device is not automatically detected you can add its IP address or hostname by clicking on the Floating Action Button</string>

View File

@ -386,7 +386,7 @@ public class LanLinkProvider extends BaseLinkProvider {
ThreadHelper.execute(() -> {
List<DeviceHost> hostList = CustomDevicesActivity
.getCustomDeviceList(PreferenceManager.getDefaultSharedPreferences(context));
.getCustomDeviceList(context);
if (TrustedNetworkHelper.isTrustedNetwork(context)) {
hostList.add(DeviceHost.BROADCAST); //Default: broadcast.

View File

@ -7,6 +7,7 @@
package org.kde.kdeconnect.UserInterface;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
@ -48,7 +49,6 @@ public class CustomDevicesActivity extends AppCompatActivity implements CustomDe
private ArrayList<DeviceHost> customDeviceList;
private EditTextAlertDialogFragment addDeviceDialog;
private SharedPreferences sharedPreferences;
private CustomDevicesAdapter customDevicesAdapter;
private DeletedCustomDevice lastDeletedCustomDevice;
private int editingDeviceAtPosition;
@ -70,9 +70,7 @@ public class CustomDevicesActivity extends AppCompatActivity implements CustomDe
fab.setOnClickListener(v -> showEditTextDialog(null));
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
customDeviceList = getCustomDeviceList(sharedPreferences);
customDeviceList = getCustomDeviceList(this);
customDeviceList.forEach(host -> host.checkReachable(() -> {
runOnUiThread(() -> customDevicesAdapter.notifyDataSetChanged());
return Unit.INSTANCE;
@ -131,6 +129,7 @@ public class CustomDevicesActivity extends AppCompatActivity implements CustomDe
}
private void saveList() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String serialized = TextUtils.join(IP_DELIM, customDeviceList);
sharedPreferences
.edit()
@ -154,7 +153,8 @@ public class CustomDevicesActivity extends AppCompatActivity implements CustomDe
return ipList;
}
public static ArrayList<DeviceHost> getCustomDeviceList(SharedPreferences sharedPreferences) {
public static ArrayList<DeviceHost> getCustomDeviceList(Context context) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String deviceListPrefs = sharedPreferences.getString(KEY_CUSTOM_DEVLIST_PREFERENCE, "");
ArrayList<DeviceHost> list = deserializeIpList(deviceListPrefs);
list.sort(Comparator.comparing(DeviceHost::toString));

View File

@ -6,6 +6,7 @@
package org.kde.kdeconnect.UserInterface
import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
@ -151,16 +152,37 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
}
private val REQUEST_REFRESH_DEVICES_BY_IP = 1
private lateinit var devicesByIpPref : Preference
/** Opens activity to configure device by IP when clicked */
private fun devicesByIpPref(context: Context) = Preference(context).apply {
devicesByIpPref = this
isPersistent = false
setTitle(R.string.custom_device_list)
updateDevicesByIpSummary()
onPreferenceClickListener = Preference.OnPreferenceClickListener {
startActivity(Intent(context, CustomDevicesActivity::class.java))
startActivityForResult(Intent(context, CustomDevicesActivity::class.java), REQUEST_REFRESH_DEVICES_BY_IP)
true
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_REFRESH_DEVICES_BY_IP) {
updateDevicesByIpSummary()
} else {
super.onActivityResult(requestCode, resultCode, data)
}
}
private fun updateDevicesByIpSummary() {
devicesByIpPref.setSummary(getString(
R.string.custom_devices_settings_summary,
CustomDevicesActivity.getCustomDeviceList(context).size
))
}
private fun udpBroadcastPref(context: Context) = SwitchPreference(context).apply {
setDefaultValue(true)
key = KEY_UDP_BROADCAST_ENABLED