mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-29 05:07:40 +00:00
Use an EditTextPreference for changing the device name instead of an AlertDialog
This commit is contained in:
parent
6864cec3fa
commit
f4c80dd839
@ -2,7 +2,7 @@
|
|||||||
<resources>
|
<resources>
|
||||||
|
|
||||||
<style name="KdeConnectTheme.NoActionBar" parent="KdeConnectThemeBase.NoActionBar">
|
<style name="KdeConnectTheme.NoActionBar" parent="KdeConnectThemeBase.NoActionBar">
|
||||||
<item name="android:windowTranslucentStatus">true</item>
|
<item name="android:statusBarColor">#65000000</item>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
|
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
|
||||||
<item name="mainNavigationViewStyle">@style/MainNavigationView</item>
|
<item name="mainNavigationViewStyle">@style/MainNavigationView</item>
|
||||||
<item name="colorHighContrast">@android:color/black</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:textColorPrimary">@android:color/black</item>
|
||||||
<item name="android:textColor">@android:color/black</item>
|
<item name="android:textColor">@android:color/black</item>
|
||||||
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
|
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
|
||||||
|
@ -35,7 +35,7 @@ import java.util.HashMap;
|
|||||||
|
|
||||||
public class DeviceHelper {
|
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
|
//from https://github.com/meetup/android-device-names
|
||||||
//Converted to java using:
|
//Converted to java using:
|
||||||
|
@ -6,13 +6,13 @@ import android.content.Intent;
|
|||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.SubMenu;
|
import android.view.SubMenu;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.EditText;
|
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.google.android.material.navigation.NavigationView;
|
import com.google.android.material.navigation.NavigationView;
|
||||||
@ -24,12 +24,9 @@ import org.kde.kdeconnect_tp.R;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import androidx.appcompat.app.ActionBar;
|
import androidx.appcompat.app.ActionBar;
|
||||||
import androidx.appcompat.app.ActionBarDrawerToggle;
|
import androidx.appcompat.app.ActionBarDrawerToggle;
|
||||||
import androidx.appcompat.app.AlertDialog;
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
import androidx.core.view.GravityCompat;
|
import androidx.core.view.GravityCompat;
|
||||||
@ -38,7 +35,7 @@ import androidx.fragment.app.Fragment;
|
|||||||
import butterknife.BindView;
|
import butterknife.BindView;
|
||||||
import butterknife.ButterKnife;
|
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_ADD_DEVICE = 1; //0 means no-selection
|
||||||
private static final int MENU_ENTRY_SETTINGS = 2;
|
private static final int MENU_ENTRY_SETTINGS = 2;
|
||||||
@ -106,6 +103,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
mNavViewDeviceName.setText(deviceName);
|
mNavViewDeviceName.setText(deviceName);
|
||||||
|
|
||||||
preferences = getSharedPreferences("stored_menu_selection", Context.MODE_PRIVATE);
|
preferences = getSharedPreferences("stored_menu_selection", Context.MODE_PRIVATE);
|
||||||
|
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
|
||||||
|
|
||||||
mNavigationView.setNavigationItemSelectedListener(menuItem -> {
|
mNavigationView.setNavigationItemSelectedListener(menuItem -> {
|
||||||
mCurrentMenuEntry = menuItem.getItemId();
|
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) {
|
private String onPairResultFromNotification(String deviceId, String pairStatus) {
|
||||||
assert(deviceId != null);
|
assert(deviceId != null);
|
||||||
|
|
||||||
@ -366,44 +371,15 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
interface NameChangeCallback {
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||||
void onNameChanged(String newName);
|
switch (key) {
|
||||||
}
|
case DeviceHelper.KEY_DEVICE_NAME_PREFERENCE:
|
||||||
|
mNavViewDeviceName.setText(DeviceHelper.getDeviceName(this));
|
||||||
private final Set<NameChangeCallback> nameChangeSubscribers = new HashSet<>();
|
BackgroundService.RunCommand(this, BackgroundService::onNetworkChange);
|
||||||
|
break;
|
||||||
public void addNameChangeCallback(NameChangeCallback cb) {
|
default:
|
||||||
nameChangeSubscribers.add(cb);
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,28 +6,26 @@ import android.content.SharedPreferences;
|
|||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
|
||||||
|
import com.google.android.material.snackbar.Snackbar;
|
||||||
|
|
||||||
import org.kde.kdeconnect.BackgroundService;
|
import org.kde.kdeconnect.BackgroundService;
|
||||||
import org.kde.kdeconnect.Helpers.DeviceHelper;
|
import org.kde.kdeconnect.Helpers.DeviceHelper;
|
||||||
import org.kde.kdeconnect.Helpers.NotificationHelper;
|
import org.kde.kdeconnect.Helpers.NotificationHelper;
|
||||||
import org.kde.kdeconnect_tp.R;
|
import org.kde.kdeconnect_tp.R;
|
||||||
|
|
||||||
|
import androidx.preference.EditTextPreference;
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
import androidx.preference.PreferenceFragmentCompat;
|
import androidx.preference.PreferenceFragmentCompat;
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
import androidx.preference.SwitchPreferenceCompat;
|
import androidx.preference.SwitchPreferenceCompat;
|
||||||
import androidx.preference.TwoStatePreference;
|
import androidx.preference.TwoStatePreference;
|
||||||
|
|
||||||
public class SettingsFragment extends PreferenceFragmentCompat implements MainActivity.NameChangeCallback {
|
public class SettingsFragment extends PreferenceFragmentCompat {
|
||||||
|
|
||||||
private MainActivity mainActivity;
|
private MainActivity mainActivity;
|
||||||
private Preference renameDevice;
|
private EditTextPreference renameDevice;
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroy() {
|
|
||||||
mainActivity.removeNameChangeCallback(this);
|
|
||||||
super.onDestroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
||||||
@ -39,18 +37,30 @@ public class SettingsFragment extends PreferenceFragmentCompat implements MainAc
|
|||||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
|
|
||||||
// Rename device
|
// Rename device
|
||||||
mainActivity.addNameChangeCallback(this);
|
renameDevice = new EditTextPreference(context);
|
||||||
//TODO: Use an EditTextPreference
|
renameDevice.setKey(DeviceHelper.KEY_DEVICE_NAME_PREFERENCE);
|
||||||
renameDevice = new Preference(context);
|
|
||||||
renameDevice.setPersistent(false);
|
|
||||||
renameDevice.setSelectable(true);
|
renameDevice.setSelectable(true);
|
||||||
renameDevice.setOnPreferenceClickListener(preference -> {
|
|
||||||
mainActivity.openRenameDeviceDialog(context);
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
String deviceName = DeviceHelper.getDeviceName(context);
|
String deviceName = DeviceHelper.getDeviceName(context);
|
||||||
renameDevice.setTitle(R.string.settings_rename);
|
renameDevice.setTitle(R.string.settings_rename);
|
||||||
renameDevice.setSummary(deviceName);
|
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);
|
screen.addPreference(renameDevice);
|
||||||
|
|
||||||
|
|
||||||
@ -122,10 +132,4 @@ public class SettingsFragment extends PreferenceFragmentCompat implements MainAc
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onNameChanged(String newName) {
|
|
||||||
renameDevice.setSummary(newName);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user