Commit f4c80dd8 authored by Erik Duisters's avatar Erik Duisters

Use an EditTextPreference for changing the device name instead of an AlertDialog

parent 6864cec3
......@@ -2,7 +2,7 @@
<resources>
<style name="KdeConnectTheme.NoActionBar" parent="KdeConnectThemeBase.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:statusBarColor">#65000000</item>
</style>
</resources>
......@@ -14,6 +14,7 @@
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
<item name="mainNavigationViewStyle">@style/MainNavigationView</item>
<item name="colorHighContrast">@android:color/black</item>
<!-- TODO: The 2 items below change to much (eg snackbar text is now black, should be white) -->
<item name="android:textColorPrimary">@android:color/black</item>
<item name="android:textColor">@android:color/black</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
......
......@@ -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:
......
......@@ -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<NameChangeCallback> 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;
}
}
}
......@@ -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);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment