2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-30 13:47:41 +00:00

It's now possible to rename your device from the new UI

This commit is contained in:
Albert Vaca 2015-09-03 03:04:36 -07:00
parent e71e409a38
commit 53d9b85d8b
6 changed files with 67 additions and 31 deletions

View File

@ -148,5 +148,7 @@
<string name="pairing_title">KDE Connect Devices</string>
<string name="pairing_description">"Other devices running KDE Connect in your same network should appear here</string>
<string name="device_paired">Device paired</string>
<string name="device_rename_title">Rename device</string>
<string name="device_rename_confirm">Rename</string>
</resources>

View File

@ -202,7 +202,6 @@ public class BackgroundService extends Service {
Log.i("KDE/BackgroundService","Service not started yet, initializing...");
initializeRsaKeys();
MainSettingsActivity.initializeDeviceName(this);
loadRememberedDevicesFromSettings();
registerLinkProviders();

View File

@ -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();
}
}

View File

@ -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) {

View File

@ -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;

View File

@ -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));
}
}
}