Possible crash related to device name handling

BUG: 348844
This commit is contained in:
Albert Vaca
2015-06-14 00:57:21 -07:00
parent 5b694a3b3e
commit 2f77e24ad1
3 changed files with 38 additions and 21 deletions

View File

@@ -7,7 +7,7 @@
<EditTextPreference
android:key="device_name_preference"
android:title="@string/device_name"
android:summary="@string/device_name_preference_summary"
android:summary=""
android:dialogTitle="@string/device_name"
android:singleLine="true" />

View File

@@ -23,6 +23,7 @@ package org.kde.kdeconnect.Helpers;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.util.Log;
import java.util.HashMap;
@@ -335,16 +336,26 @@ public class DeviceHelper {
}
public static String getDeviceName() {
String dictName = humanReadableNames.get(Build.MODEL.replace(' ','_'));
if (dictName != null) return dictName;
if (Build.BRAND.equals("samsung") || Build.BRAND.equals("Samsung")) {
return "Samsung" + Build.MODEL;
String deviceName = null;
try {
String dictName = humanReadableNames.get(Build.MODEL.replace(' ', '_'));
if (dictName != null) {
deviceName = dictName;
} else if (Build.BRAND.equalsIgnoreCase("samsung")) {
deviceName = "Samsung " + Build.MODEL;
} else {
deviceName = Build.BRAND;
}
} catch (Exception e) {
//Some phones might not define BRAND or MODEL, ignore exceptions
Log.e("Exception", e.getMessage());
e.printStackTrace();
}
if (deviceName == null || deviceName.isEmpty()) {
return "Android"; //Could not find a name
} else {
return deviceName;
}
return Build.MODEL;
}
public static boolean isTablet() {

View File

@@ -29,7 +29,6 @@ import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;
@@ -73,21 +72,27 @@ public class MainSettingsActivity extends AppCompatPreferenceActivity {
}
private void initPreferences(final EditTextPreference deviceNamePref) {
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
deviceNamePref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newDeviceName) {
if (newDeviceName.toString().isEmpty()) {
public boolean onPreferenceChange(Preference preference, Object newDeviceNameObject) {
String newDeviceName = newDeviceNameObject == null ? "" : newDeviceNameObject.toString();
if (newDeviceName.isEmpty()) {
Toast.makeText(
MainSettingsActivity.this,
getString(R.string.invalid_device_name),
Toast.LENGTH_SHORT).show();
return false;
}else{
} else {
Log.i("MainSettingsActivity", "New device name: " + newDeviceName);
deviceNamePref.setSummary(getString(
R.string.device_name_preference_summary,
newDeviceName.toString()));
deviceNamePref.setSummary(newDeviceName);
//Broadcast the device information again since it has changed
BackgroundService.RunCommand(MainSettingsActivity.this, new BackgroundService.InstanceCallback() {
@@ -96,13 +101,13 @@ public class MainSettingsActivity extends AppCompatPreferenceActivity {
service.onNetworkChange();
}
});
return true;
}
}
});
deviceNamePref.setSummary(getString(
R.string.device_name_preference_summary,
sharedPreferences.getString(KEY_DEVICE_NAME_PREFERENCE,"")));
deviceNamePref.setSummary(sharedPreferences.getString(KEY_DEVICE_NAME_PREFERENCE,""));
}
/**
@@ -115,8 +120,9 @@ public class MainSettingsActivity extends AppCompatPreferenceActivity {
// 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, DeviceHelper.getDeviceName()).commit();
preferences.edit().putString(KEY_DEVICE_NAME_PREFERENCE, deviceName).commit();
}
}