mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-22 01:51:47 +00:00
Added a settings activity to change the publicly visible device name
When changed the new name will be used instead of the one from HumanDeviceNames REVIEW: 113204 BUG: 325061
This commit is contained in:
parent
0595662efc
commit
9dbe1ec9cf
@ -48,6 +48,15 @@
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name="org.kde.kdeconnect.UserInterface.MainSettingsActivity"
|
||||
android:label="@string/settings"
|
||||
android:parentActivityName="org.kde.kdeconnect.UserInterface.MainActivity"
|
||||
>
|
||||
<meta-data android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value="org.kde.kdeconnect.UserInterface.MainActivity" />
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:theme="@style/Theme.AppCompat"
|
||||
android:name="org.kde.kdeconnect.UserInterface.DeviceActivity"
|
||||
|
@ -14,7 +14,7 @@ import android.util.Log;
|
||||
import org.kde.kdeconnect.Backends.BaseLink;
|
||||
import org.kde.kdeconnect.Backends.BaseLinkProvider;
|
||||
import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider;
|
||||
import org.kde.kdeconnect.Backends.LoopbackBackend.LoopbackLinkProvider;
|
||||
import org.kde.kdeconnect.UserInterface.MainSettingsActivity;
|
||||
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
@ -179,6 +179,7 @@ public class BackgroundService extends Service {
|
||||
Log.i("BackgroundService","Service not started yet, initializing...");
|
||||
|
||||
initializeRsaKeys();
|
||||
MainSettingsActivity.initializeDeviceName(this);
|
||||
loadRememberedDevicesFromSettings();
|
||||
registerLinkProviders();
|
||||
|
||||
|
@ -154,7 +154,7 @@ public class HumanDeviceNames {
|
||||
humanReadableNames.put("XT907","Motorola Droid Razr M");
|
||||
}
|
||||
|
||||
static String getDeviceName() {
|
||||
public static String getDeviceName() {
|
||||
|
||||
String dictName = humanReadableNames.get(Build.MODEL.replace(' ','_'));
|
||||
if (dictName != null) return dictName;
|
||||
|
@ -10,6 +10,8 @@ import android.util.Log;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.kde.kdeconnect.UserInterface.MainSettingsActivity;
|
||||
import org.kde.kdeconnect_tp.R;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
@ -221,7 +223,10 @@ public class NetworkPackage {
|
||||
String deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
|
||||
try {
|
||||
np.mBody.put("deviceId", deviceId);
|
||||
np.mBody.put("deviceName", HumanDeviceNames.getDeviceName());
|
||||
np.mBody.put("deviceName",
|
||||
PreferenceManager.getDefaultSharedPreferences(context).getString(
|
||||
MainSettingsActivity.KEY_DEVICE_NAME_PREFERENCE,
|
||||
HumanDeviceNames.getDeviceName()));
|
||||
np.mBody.put("protocolVersion", NetworkPackage.ProtocolVersion);
|
||||
} catch (JSONException e) {
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package org.kde.kdeconnect.UserInterface;
|
||||
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.ActionBar;
|
||||
@ -66,6 +67,9 @@ public class MainActivity extends ActionBarActivity {
|
||||
}
|
||||
}).start();
|
||||
break;
|
||||
case R.id.menu_settings:
|
||||
startActivity(new Intent(this,MainSettingsActivity.class));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -79,7 +83,6 @@ public class MainActivity extends ActionBarActivity {
|
||||
|
||||
ActionBar actionBar = getSupportActionBar();
|
||||
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -0,0 +1,103 @@
|
||||
package org.kde.kdeconnect.UserInterface;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.preference.EditTextPreference;
|
||||
import android.preference.Preference;
|
||||
import android.preference.PreferenceActivity;
|
||||
import android.preference.PreferenceFragment;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.kde.kdeconnect.HumanDeviceNames;
|
||||
import org.kde.kdeconnect_tp.R;
|
||||
|
||||
public class MainSettingsActivity extends PreferenceActivity{
|
||||
|
||||
public static final String KEY_DEVICE_NAME_PREFERENCE = "device_name_preference";
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
initializeDeviceName(this);
|
||||
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
|
||||
addPreferencesOldApi();
|
||||
} else {
|
||||
getFragmentManager().beginTransaction().
|
||||
replace(android.R.id.content, new GeneralPrefsFragment()).commit();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private void addPreferencesOldApi() {
|
||||
addPreferencesFromResource(R.xml.general_preferences);
|
||||
initPreferences((EditTextPreference) findPreference(KEY_DEVICE_NAME_PREFERENCE));
|
||||
}
|
||||
|
||||
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()) {
|
||||
Toast.makeText(
|
||||
MainSettingsActivity.this,
|
||||
getString(R.string.invalid_device_name),
|
||||
Toast.LENGTH_SHORT).show();
|
||||
return false;
|
||||
}else{
|
||||
Log.i("MainSettingsActivity", "New device name: " + newDeviceName);
|
||||
deviceNamePref.setSummary(getString(
|
||||
R.string.device_name_preference_summary,
|
||||
newDeviceName.toString()));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
});
|
||||
deviceNamePref.setSummary(getString(
|
||||
R.string.device_name_preference_summary,
|
||||
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
|
||||
*/
|
||||
public static void initializeDeviceName(Context context){
|
||||
// I could have used getDefaultSharedPreferences(context).contains but we need to check
|
||||
// to checkAgainst empty String also.
|
||||
String deviceName=PreferenceManager.getDefaultSharedPreferences(context).getString(
|
||||
KEY_DEVICE_NAME_PREFERENCE,
|
||||
"");
|
||||
if(deviceName.isEmpty()){
|
||||
Log.i("MainSettingsActivity", "New device name: " + deviceName);
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit().putString(
|
||||
KEY_DEVICE_NAME_PREFERENCE,
|
||||
HumanDeviceNames.getDeviceName()).commit();
|
||||
}
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
||||
public static class GeneralPrefsFragment extends PreferenceFragment {
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
addPreferencesFromResource(R.xml.general_preferences);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
if (getActivity() != null) {
|
||||
((MainSettingsActivity)getActivity()).initPreferences(
|
||||
(EditTextPreference) findPreference(KEY_DEVICE_NAME_PREFERENCE));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -18,4 +18,11 @@
|
||||
kdeconnect:actionViewClass="android.widget.ProgressBar"
|
||||
/>
|
||||
|
||||
<item
|
||||
android:id="@+id/menu_settings"
|
||||
android:icon="@drawable/action_settings"
|
||||
android:orderInCategory="300"
|
||||
android:title="@string/settings"
|
||||
kdeconnect:showAsAction="never"
|
||||
/>
|
||||
</menu>
|
@ -57,6 +57,9 @@
|
||||
<string name="share_to">Share To...</string>
|
||||
<string name="protocol_version_older">This device uses an old protocol version</string>
|
||||
<string name="protocol_version_newer">This device uses a newer protocol version</string>
|
||||
|
||||
<string name="general_settings">General Settings</string>
|
||||
<string name="device_name">Device name</string>
|
||||
<string name="device_name_preference_summary">%s</string>
|
||||
<string name="invalid_device_name">Invalid device name</string>
|
||||
|
||||
</resources>
|
||||
|
15
src/main/res/xml/general_preferences.xml
Normal file
15
src/main/res/xml/general_preferences.xml
Normal file
@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<PreferenceCategory
|
||||
android:title="@string/general_settings">
|
||||
|
||||
<EditTextPreference
|
||||
android:key="device_name_preference"
|
||||
android:title="@string/device_name"
|
||||
android:summary="@string/device_name_preference_summary"
|
||||
android:dialogTitle="@string/device_name"
|
||||
android:singleLine="true" />
|
||||
|
||||
</PreferenceCategory>
|
||||
</PreferenceScreen>
|
Loading…
x
Reference in New Issue
Block a user