diff --git a/res/values-v21/styles.xml b/res/values-v21/styles.xml
index afb95d3b..d8fd8b3e 100644
--- a/res/values-v21/styles.xml
+++ b/res/values-v21/styles.xml
@@ -2,7 +2,7 @@
diff --git a/res/values/styles.xml b/res/values/styles.xml
index e08efdd4..18bd54c0 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -14,6 +14,7 @@
- @style/ThemeOverlay.AppCompat.Light
- @style/MainNavigationView
- @android:color/black
+
- @android:color/black
- @android:color/black
- @style/PreferenceThemeOverlay
diff --git a/src/org/kde/kdeconnect/Helpers/DeviceHelper.java b/src/org/kde/kdeconnect/Helpers/DeviceHelper.java
index 1ae7dd12..b52e6903 100644
--- a/src/org/kde/kdeconnect/Helpers/DeviceHelper.java
+++ b/src/org/kde/kdeconnect/Helpers/DeviceHelper.java
@@ -35,7 +35,7 @@ import java.util.HashMap;
public class DeviceHelper {
- private static final String KEY_DEVICE_NAME_PREFERENCE = "device_name_preference";
+ public static final String KEY_DEVICE_NAME_PREFERENCE = "device_name_preference";
//from https://github.com/meetup/android-device-names
//Converted to java using:
diff --git a/src/org/kde/kdeconnect/UserInterface/MainActivity.java b/src/org/kde/kdeconnect/UserInterface/MainActivity.java
index 40fee48f..5d900bfc 100644
--- a/src/org/kde/kdeconnect/UserInterface/MainActivity.java
+++ b/src/org/kde/kdeconnect/UserInterface/MainActivity.java
@@ -6,13 +6,13 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
+import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
-import android.widget.EditText;
import android.widget.TextView;
import com.google.android.material.navigation.NavigationView;
@@ -24,12 +24,9 @@ import org.kde.kdeconnect_tp.R;
import java.util.Collection;
import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Set;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.ActionBarDrawerToggle;
-import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
@@ -38,7 +35,7 @@ import androidx.fragment.app.Fragment;
import butterknife.BindView;
import butterknife.ButterKnife;
-public class MainActivity extends AppCompatActivity {
+public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final int MENU_ENTRY_ADD_DEVICE = 1; //0 means no-selection
private static final int MENU_ENTRY_SETTINGS = 2;
@@ -106,6 +103,7 @@ public class MainActivity extends AppCompatActivity {
mNavViewDeviceName.setText(deviceName);
preferences = getSharedPreferences("stored_menu_selection", Context.MODE_PRIVATE);
+ PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
mNavigationView.setNavigationItemSelectedListener(menuItem -> {
mCurrentMenuEntry = menuItem.getItemId();
@@ -184,6 +182,13 @@ public class MainActivity extends AppCompatActivity {
}
}
+ @Override
+ protected void onDestroy() {
+ super.onDestroy();
+
+ PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
+ }
+
private String onPairResultFromNotification(String deviceId, String pairStatus) {
assert(deviceId != null);
@@ -366,44 +371,15 @@ public class MainActivity extends AppCompatActivity {
}
}
-
- interface NameChangeCallback {
- void onNameChanged(String newName);
- }
-
- private final Set nameChangeSubscribers = new HashSet<>();
-
- public void addNameChangeCallback(NameChangeCallback cb) {
- nameChangeSubscribers.add(cb);
- }
-
- public void removeNameChangeCallback(NameChangeCallback cb) {
- nameChangeSubscribers.remove(cb);
- }
-
- public void openRenameDeviceDialog(Context context) {
- final EditText deviceNameEdit = new EditText(this);
- String deviceName = DeviceHelper.getDeviceName(this);
- deviceNameEdit.setText(deviceName);
- float dpi = this.getResources().getDisplayMetrics().density;
- deviceNameEdit.setPadding( ((int) (18 * dpi)), ((int) (16 * dpi)), ((int) (18 * dpi)), ((int) (12 * dpi)) );
- new AlertDialog.Builder(context)
- .setView(deviceNameEdit)
- .setPositiveButton(R.string.device_rename_confirm, (dialog, which) -> {
- String newDeviceName = deviceNameEdit.getText().toString();
- DeviceHelper.setDeviceName(this, newDeviceName);
- this.updateDeviceNameFromMenu(newDeviceName);
- BackgroundService.RunCommand(this, BackgroundService::onNetworkChange);
- for (NameChangeCallback callback : nameChangeSubscribers) {
- callback.onNameChanged(newDeviceName);
- }
- })
- .setNegativeButton(R.string.cancel, (dialog, which) -> { })
- .setTitle(R.string.device_rename_title)
- .show();
- }
-
- private void updateDeviceNameFromMenu(String newDeviceName) {
- mNavViewDeviceName.setText(newDeviceName);
+ @Override
+ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
+ switch (key) {
+ case DeviceHelper.KEY_DEVICE_NAME_PREFERENCE:
+ mNavViewDeviceName.setText(DeviceHelper.getDeviceName(this));
+ BackgroundService.RunCommand(this, BackgroundService::onNetworkChange);
+ break;
+ default:
+ break;
+ }
}
}
diff --git a/src/org/kde/kdeconnect/UserInterface/SettingsFragment.java b/src/org/kde/kdeconnect/UserInterface/SettingsFragment.java
index 3995ee20..1b830510 100644
--- a/src/org/kde/kdeconnect/UserInterface/SettingsFragment.java
+++ b/src/org/kde/kdeconnect/UserInterface/SettingsFragment.java
@@ -6,28 +6,26 @@ import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
+import android.text.TextUtils;
+
+import com.google.android.material.snackbar.Snackbar;
import org.kde.kdeconnect.BackgroundService;
import org.kde.kdeconnect.Helpers.DeviceHelper;
import org.kde.kdeconnect.Helpers.NotificationHelper;
import org.kde.kdeconnect_tp.R;
+import androidx.preference.EditTextPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreferenceCompat;
import androidx.preference.TwoStatePreference;
-public class SettingsFragment extends PreferenceFragmentCompat implements MainActivity.NameChangeCallback {
+public class SettingsFragment extends PreferenceFragmentCompat {
private MainActivity mainActivity;
- private Preference renameDevice;
-
- @Override
- public void onDestroy() {
- mainActivity.removeNameChangeCallback(this);
- super.onDestroy();
- }
+ private EditTextPreference renameDevice;
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
@@ -39,18 +37,30 @@ public class SettingsFragment extends PreferenceFragmentCompat implements MainAc
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
// Rename device
- mainActivity.addNameChangeCallback(this);
- //TODO: Use an EditTextPreference
- renameDevice = new Preference(context);
- renameDevice.setPersistent(false);
+ renameDevice = new EditTextPreference(context);
+ renameDevice.setKey(DeviceHelper.KEY_DEVICE_NAME_PREFERENCE);
renameDevice.setSelectable(true);
- renameDevice.setOnPreferenceClickListener(preference -> {
- mainActivity.openRenameDeviceDialog(context);
- return true;
- });
String deviceName = DeviceHelper.getDeviceName(context);
renameDevice.setTitle(R.string.settings_rename);
renameDevice.setSummary(deviceName);
+ renameDevice.setDialogTitle(R.string.device_rename_title);
+ renameDevice.setText(deviceName);
+ renameDevice.setPositiveButtonText(R.string.device_rename_confirm);
+ renameDevice.setNegativeButtonText(R.string.cancel);
+ renameDevice.setOnPreferenceChangeListener((preference, newValue) -> {
+ String name = (String) newValue;
+
+ if (TextUtils.isEmpty(name)) {
+ if (getView() != null) {
+ Snackbar.make(getView(), R.string.invalid_device_name, Snackbar.LENGTH_LONG).show();
+ }
+ return false;
+ }
+
+ renameDevice.setSummary((String)newValue);
+ return true;
+ });
+
screen.addPreference(renameDevice);
@@ -122,10 +132,4 @@ public class SettingsFragment extends PreferenceFragmentCompat implements MainAc
}
-
- @Override
- public void onNameChanged(String newName) {
- renameDevice.setSummary(newName);
- }
-
}