From 53d9b85d8b5ef0e35180d42af1b7d12bd8ab0634 Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Thu, 3 Sep 2015 03:04:36 -0700 Subject: [PATCH] It's now possible to rename your device from the new UI --- res/values/strings.xml | 2 + src/org/kde/kdeconnect/BackgroundService.java | 1 - .../kde/kdeconnect/Helpers/DeviceHelper.java | 25 +++++++++++- src/org/kde/kdeconnect/NetworkPackage.java | 5 +-- .../NewUserInterface/MaterialActivity.java | 38 ++++++++++++++++++- .../UserInterface/MainSettingsActivity.java | 27 ++----------- 6 files changed, 67 insertions(+), 31 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 4186cc6c..bec0147c 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -148,5 +148,7 @@ KDE Connect Devices "Other devices running KDE Connect in your same network should appear here Device paired + Rename device + Rename diff --git a/src/org/kde/kdeconnect/BackgroundService.java b/src/org/kde/kdeconnect/BackgroundService.java index 37baddb6..b2f3c8f6 100644 --- a/src/org/kde/kdeconnect/BackgroundService.java +++ b/src/org/kde/kdeconnect/BackgroundService.java @@ -202,7 +202,6 @@ public class BackgroundService extends Service { Log.i("KDE/BackgroundService","Service not started yet, initializing..."); initializeRsaKeys(); - MainSettingsActivity.initializeDeviceName(this); loadRememberedDevicesFromSettings(); registerLinkProviders(); diff --git a/src/org/kde/kdeconnect/Helpers/DeviceHelper.java b/src/org/kde/kdeconnect/Helpers/DeviceHelper.java index d3df0296..8a0e0fd6 100644 --- a/src/org/kde/kdeconnect/Helpers/DeviceHelper.java +++ b/src/org/kde/kdeconnect/Helpers/DeviceHelper.java @@ -20,15 +20,20 @@ package org.kde.kdeconnect.Helpers; +import android.content.Context; +import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; +import android.preference.PreferenceManager; import android.util.Log; import java.util.HashMap; public class DeviceHelper { + public static final String KEY_DEVICE_NAME_PREFERENCE = "device_name_preference"; + //from https://github.com/meetup/android-device-names //Converted to java using: //cat android_models.properties | awk -F'=' '{sub(/ *$/, "", $1)} sub(/^ */, "", $2) { if ($2 != "") print "humanReadableNames.put(\""$1"\",\"" $2 "\");"}' @@ -435,7 +440,7 @@ public class DeviceHelper { } - public static String getDeviceName() { + public static String getAndroidDeviceName() { String deviceName = null; try { String dictName = humanReadableNames.get(Build.MODEL.replace(' ', '_')); @@ -465,4 +470,22 @@ public class DeviceHelper { return isLarge; } + //It returns getAndroidDeviceName() if no user-defined name has been set with setDeviceName(). + public static String getDeviceName(Context context){ + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + // Could use prefrences.contains but would need to check for empty String anyway. + String deviceName = preferences.getString(KEY_DEVICE_NAME_PREFERENCE, ""); + if (deviceName.isEmpty()){ + deviceName = DeviceHelper.getAndroidDeviceName(); + Log.i("MainSettingsActivity", "New device name: " + deviceName); + preferences.edit().putString(KEY_DEVICE_NAME_PREFERENCE, deviceName).apply(); + } + return deviceName; + } + + public static void setDeviceName(Context context, String name){ + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + preferences.edit().putString(KEY_DEVICE_NAME_PREFERENCE, name).apply(); + } + } diff --git a/src/org/kde/kdeconnect/NetworkPackage.java b/src/org/kde/kdeconnect/NetworkPackage.java index a4ef7d0f..51d80217 100644 --- a/src/org/kde/kdeconnect/NetworkPackage.java +++ b/src/org/kde/kdeconnect/NetworkPackage.java @@ -250,10 +250,7 @@ public class NetworkPackage { String deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); try { np.mBody.put("deviceId", deviceId); - np.mBody.put("deviceName", - PreferenceManager.getDefaultSharedPreferences(context).getString( - MainSettingsActivity.KEY_DEVICE_NAME_PREFERENCE, - DeviceHelper.getDeviceName())); + np.mBody.put("deviceName", DeviceHelper.getDeviceName(context)); np.mBody.put("protocolVersion", NetworkPackage.ProtocolVersion); np.mBody.put("deviceType", DeviceHelper.isTablet()? "tablet" : "phone"); } catch (Exception e) { diff --git a/src/org/kde/kdeconnect/NewUserInterface/MaterialActivity.java b/src/org/kde/kdeconnect/NewUserInterface/MaterialActivity.java index 89d70b90..3c546688 100644 --- a/src/org/kde/kdeconnect/NewUserInterface/MaterialActivity.java +++ b/src/org/kde/kdeconnect/NewUserInterface/MaterialActivity.java @@ -1,6 +1,8 @@ package org.kde.kdeconnect.NewUserInterface; +import android.app.AlertDialog; import android.content.Context; +import android.content.DialogInterface; import android.content.SharedPreferences; import android.os.Bundle; import android.support.annotation.NonNull; @@ -15,10 +17,14 @@ import android.text.TextUtils; import android.util.Log; import android.view.Menu; import android.view.MenuItem; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.EditText; import android.widget.TextView; import org.kde.kdeconnect.BackgroundService; import org.kde.kdeconnect.Device; +import org.kde.kdeconnect.Helpers.DeviceHelper; import org.kde.kdeconnect.UserInterface.MainSettingsActivity; import org.kde.kdeconnect_tp.R; @@ -54,10 +60,36 @@ public class MaterialActivity extends AppCompatActivity { mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); - String deviceName = MainSettingsActivity.initializeDeviceName(this); - TextView nameView = (TextView) mDrawerLayout.findViewById(R.id.device_name); + String deviceName = DeviceHelper.getDeviceName(this); + final TextView nameView = (TextView) mDrawerLayout.findViewById(R.id.device_name); nameView.setText(deviceName); + nameView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + final EditText deviceNameEdit = new EditText(MaterialActivity.this); + String deviceName = DeviceHelper.getDeviceName(MaterialActivity.this); + deviceNameEdit.setText(deviceName); + new AlertDialog.Builder(MaterialActivity.this) + .setView(deviceNameEdit) + .setPositiveButton(R.string.device_rename_confirm, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + String deviceName = deviceNameEdit.getText().toString(); + DeviceHelper.setDeviceName(MaterialActivity.this, deviceName); + nameView.setText(deviceName); + } + }) + .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + } + }) + .setTitle(R.string.device_rename_title) + .show(); + } + }); + mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { @@ -170,6 +202,8 @@ public class MaterialActivity extends AppCompatActivity { updateComputerList(); } + //TODO: Make it accept two parameters, a constant with the type of screen and the device id in + //case the screen is for a device, or even three parameters and the third one be the plugin id? public void onDeviceSelected(String deviceId) { mCurrentDevice = deviceId; diff --git a/src/org/kde/kdeconnect/UserInterface/MainSettingsActivity.java b/src/org/kde/kdeconnect/UserInterface/MainSettingsActivity.java index 73c8b2f7..bdab734b 100644 --- a/src/org/kde/kdeconnect/UserInterface/MainSettingsActivity.java +++ b/src/org/kde/kdeconnect/UserInterface/MainSettingsActivity.java @@ -39,12 +39,10 @@ import org.kde.kdeconnect_tp.R; public class MainSettingsActivity extends AppCompatPreferenceActivity { - public static final String KEY_DEVICE_NAME_PREFERENCE = "device_name_preference"; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - initializeDeviceName(this); + DeviceHelper.getDeviceName(this); //To make sure the preference is initialized if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { addPreferencesOldApi(); @@ -68,7 +66,7 @@ public class MainSettingsActivity extends AppCompatPreferenceActivity { @SuppressWarnings("deprecation") private void addPreferencesOldApi() { addPreferencesFromResource(R.xml.general_preferences); - initPreferences((EditTextPreference) findPreference(KEY_DEVICE_NAME_PREFERENCE)); + initPreferences((EditTextPreference) findPreference(DeviceHelper.KEY_DEVICE_NAME_PREFERENCE)); } private void initPreferences(final EditTextPreference deviceNamePref) { @@ -107,24 +105,7 @@ public class MainSettingsActivity extends AppCompatPreferenceActivity { } }); - deviceNamePref.setSummary(sharedPreferences.getString(KEY_DEVICE_NAME_PREFERENCE,"")); - } - - /** - * Until now it sets only the default deviceName (if not already set). - * It's safe to call this multiple time because doesn't override any previous value. - * @param context the application context - */ - public static String initializeDeviceName(Context context){ - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - // Could use prefrences.contains but would need to check for empty String anyway. - String deviceName = preferences.getString(KEY_DEVICE_NAME_PREFERENCE, ""); - if (deviceName.isEmpty()){ - deviceName = DeviceHelper.getDeviceName(); - Log.i("MainSettingsActivity", "New device name: " + deviceName); - preferences.edit().putString(KEY_DEVICE_NAME_PREFERENCE, deviceName).apply(); - } - return deviceName; + deviceNamePref.setSummary(sharedPreferences.getString(DeviceHelper.KEY_DEVICE_NAME_PREFERENCE,"")); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) @@ -140,7 +121,7 @@ public class MainSettingsActivity extends AppCompatPreferenceActivity { super.onActivityCreated(savedInstanceState); if (getActivity() != null) { ((MainSettingsActivity)getActivity()).initPreferences( - (EditTextPreference) findPreference(KEY_DEVICE_NAME_PREFERENCE)); + (EditTextPreference) findPreference(DeviceHelper.KEY_DEVICE_NAME_PREFERENCE)); } } }