Skip to content

Commit

Permalink
Fix issue with URL migration
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Sep 2, 2017
1 parent 43e108d commit 70d6dfe
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 25 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
buildConfigField "boolean", "HIDDEN_APP", "false"
minSdkVersion 14
targetSdkVersion 26
versionCode 38
versionName '5.1'
versionCode 39
versionName '5.2'
}

lintOptions {
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/org/traccar/client/MainApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.preference.PreferenceManager;

public class MainApplication extends Application {

Expand All @@ -33,6 +36,8 @@ public void onCreate() {
super.onCreate();
System.setProperty("http.keepAliveDuration", String.valueOf(30 * 60 * 1000));

migrateLegacyPreferences(PreferenceManager.getDefaultSharedPreferences(this));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
registerChannel();
}
Expand All @@ -47,4 +52,22 @@ private void registerChannel() {
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
}

private void migrateLegacyPreferences(SharedPreferences preferences) {
String port = preferences.getString("port", null);
if (port != null) {
String host = preferences.getString("address", getString(R.string.settings_url_default_value));
String scheme = preferences.getBoolean("secure", false) ? "https" : "http";

Uri.Builder builder = new Uri.Builder();
builder.scheme(scheme).encodedAuthority(host + ":" + port).build();
SharedPreferences.Editor editor = preferences.edit();
editor.putString(MainFragment.KEY_URL, builder.toString());

editor.remove("port");
editor.remove("address");
editor.remove("secure");
editor.apply();
}
}

}
21 changes: 0 additions & 21 deletions app/src/main/java/org/traccar/client/MainFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
migrateLegacyPreferences(sharedPreferences);
addPreferencesFromResource(R.xml.preferences);
initPreferences();

Expand Down Expand Up @@ -288,24 +287,4 @@ private boolean validateServerURL(String userUrl) {
return false;
}

private void migrateLegacyPreferences(SharedPreferences preferences) {
String port = preferences.getString("port", null);
if (port != null) {
Log.d(TAG, "Migrating to URL preference");

String host = preferences.getString("address", getString(R.string.settings_url_default_value));
String scheme = preferences.getBoolean("secure", false) ? "https" : "http";

Uri.Builder builder = new Uri.Builder();
builder.scheme(scheme).encodedAuthority(host + ":" + port).build();
SharedPreferences.Editor editor = preferences.edit();
editor.putString(KEY_URL, builder.toString());

editor.remove("port");
editor.remove("address");
editor.remove("secure");
editor.apply();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public TrackingController(Context context) {
networkManager = new NetworkManager(context, this);
isOnline = networkManager.isOnline();

url = preferences.getString(MainFragment.KEY_URL, null);
url = preferences.getString(MainFragment.KEY_URL, context.getString(R.string.settings_url_default_value));

PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<string name="settings_id_title">Device identifier</string>
<string name="settings_url_title">Server URL</string>
<string name="settings_url_summary">Tracking server URL</string>
<string name="settings_url_default_value" translatable="false">http://demo.traccar.org:5055</string>
<string name="settings_interval_title">Frequency</string>
<string name="settings_interval_summary">Reporting interval in seconds</string>
<string name="settings_distance_title">Distance</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/values.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="settings_url_default_value" translatable="false">http://demo.traccar.org:5055</string>

<string-array name="settings_provider_values" translatable="false">
<item>gps</item>
<item>network</item>
Expand Down

0 comments on commit 70d6dfe

Please sign in to comment.