Этот коммит содержится в:
BrianBlade
2017-06-14 08:40:40 +02:00
родитель 6e398d12fe
Коммит 149eee530a
4 изменённых файлов: 28 добавлений и 26 удалений
+6 -12
Просмотреть файл
@@ -39,8 +39,6 @@ import android.view.MenuItem;
import android.webkit.URLUtil; import android.webkit.URLUtil;
import android.widget.Toast; import android.widget.Toast;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet; import java.util.HashSet;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
@@ -73,7 +71,7 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
} }
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
migratePreferencesIfNecessary(sharedPreferences); migrateLegacyPreferences(sharedPreferences);
addPreferencesFromResource(R.xml.preferences); addPreferencesFromResource(R.xml.preferences);
initPreferences(); initPreferences();
@@ -304,17 +302,17 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
preference.setSummary(R.string.settings_url_summary); preference.setSummary(R.string.settings_url_summary);
findPreference(KEY_STATUS).setEnabled(true); findPreference(KEY_STATUS).setEnabled(true);
} else { } else {
preference.setSummary(R.string.settings_invalid_url_summary); preference.setSummary(R.string.error_invalid_url_summary);
findPreference(KEY_STATUS).setEnabled(false); findPreference(KEY_STATUS).setEnabled(false);
Toast.makeText(MainActivity.this, R.string.msg_invalid_url, Toast.LENGTH_LONG).show(); Toast.makeText(MainActivity.this, R.string.error_msg_invalid_url, Toast.LENGTH_LONG).show();
} }
return true; return true;
} }
private void migratePreferencesIfNecessary(SharedPreferences preferences) { private void migrateLegacyPreferences(SharedPreferences preferences) {
String port = preferences.getString("port", null); String port = preferences.getString("port", null);
if (port != null) { if (port != null) {
Log.d(TAG, "migratePreferencesIfNecessary: migrating to URL preference"); Log.d(TAG, "migrateLegacyPreferences: migrating to URL preference");
String host = preferences.getString("address", getString(R.string.settings_url_default_value)); String host = preferences.getString("address", getString(R.string.settings_url_default_value));
String scheme = preferences.getBoolean("secure", false) ? "https" : "http"; String scheme = preferences.getBoolean("secure", false) ? "https" : "http";
@@ -327,11 +325,7 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
editor.remove("port"); editor.remove("port");
editor.remove("address"); editor.remove("address");
editor.remove("secure"); editor.remove("secure");
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD) { editor.commit();
editor.commit();
} else {
editor.apply();
}
} }
} }
} }
+8 -10
Просмотреть файл
@@ -24,7 +24,8 @@ public class ProtocolFormatter {
} }
public static String formatRequest(String url, Position position, String alarm) { public static String formatRequest(String url, Position position, String alarm) {
Uri serverUrl = Uri.parse(url).buildUpon() Uri serverUrl = Uri.parse(url);
Uri.Builder builder = serverUrl.buildUpon()
.appendQueryParameter("id", position.getDeviceId()) .appendQueryParameter("id", position.getDeviceId())
.appendQueryParameter("timestamp", String.valueOf(position.getTime().getTime() / 1000)) .appendQueryParameter("timestamp", String.valueOf(position.getTime().getTime() / 1000))
.appendQueryParameter("lat", String.valueOf(position.getLatitude())) .appendQueryParameter("lat", String.valueOf(position.getLatitude()))
@@ -32,19 +33,16 @@ public class ProtocolFormatter {
.appendQueryParameter("speed", String.valueOf(position.getSpeed())) .appendQueryParameter("speed", String.valueOf(position.getSpeed()))
.appendQueryParameter("bearing", String.valueOf(position.getCourse())) .appendQueryParameter("bearing", String.valueOf(position.getCourse()))
.appendQueryParameter("altitude", String.valueOf(position.getAltitude())) .appendQueryParameter("altitude", String.valueOf(position.getAltitude()))
.appendQueryParameter("batt", String.valueOf(position.getBattery())) .appendQueryParameter("batt", String.valueOf(position.getBattery()));
.build();
if (alarm != null && alarm.trim().length() != 0) { if (alarm != null) {
serverUrl = serverUrl.buildUpon().appendQueryParameter("alarm", alarm).build(); builder.appendQueryParameter("alarm", alarm);
} }
int port = serverUrl.getPort(); int port = serverUrl.getPort();
if (port < 0 || port > 65535) { if (port < 0 || port > 65535) {
port = 5055; builder.encodedAuthority(serverUrl.getHost() + ":" + 5055);
String host = serverUrl.getHost();
serverUrl = serverUrl.buildUpon().encodedAuthority(host + ":" + port).build();
} }
return serverUrl.toString(); return builder.build().toString();
} }
} }
+2 -2
Просмотреть файл
@@ -27,7 +27,6 @@
<string name="settings_provider_mixed">Mixed provider</string> <string name="settings_provider_mixed">Mixed provider</string>
<string name="settings_foreground_title">Foreground service</string> <string name="settings_foreground_title">Foreground service</string>
<string name="settings_foreground_summary">Increase service priority</string> <string name="settings_foreground_summary">Increase service priority</string>
<string name="settings_invalid_url_summary">Invalid server URL</string>
<string name="menu_status">Status</string> <string name="menu_status">Status</string>
<string name="menu_about">About</string> <string name="menu_about">About</string>
<string name="menu_shortcuts">Add shortcuts</string> <string name="menu_shortcuts">Add shortcuts</string>
@@ -43,5 +42,6 @@
<string name="status_connectivity_change">Connectivity change</string> <string name="status_connectivity_change">Connectivity change</string>
<string name="hidden_app_name">Device Settings</string> <string name="hidden_app_name">Device Settings</string>
<string name="hidden_alert">The app has been hidden. To open it again please dial 8722227 (TRACCAR).</string> <string name="hidden_alert">The app has been hidden. To open it again please dial 8722227 (TRACCAR).</string>
<string name="msg_invalid_url">Please enter a valid http:// or https:// URL</string> <string name="error_msg_invalid_url">Please enter a valid http:// or https:// URL</string>
<string name="error_invalid_url_summary">Invalid server URL</string>
</resources> </resources>
+12 -2
Просмотреть файл
@@ -22,8 +22,18 @@ public class ProtocolFormatterTest {
Position position = new Position("123456789012345", new Location("gps"), 0); Position position = new Position("123456789012345", new Location("gps"), 0);
position.setTime(new Date(0)); position.setTime(new Date(0));
String url = ProtocolFormatter.formatRequest("http://localhost:5055/path", position); String url = ProtocolFormatter.formatRequest("http://localhost", position);
assertEquals("http://localhost:5055/path?id=123456789012345&timestamp=0&lat=0.0&lon=0.0&speed=0.0&bearing=0.0&altitude=0.0&batt=0.0", url); assertEquals("http://localhost:5055?id=123456789012345&timestamp=0&lat=0.0&lon=0.0&speed=0.0&bearing=0.0&altitude=0.0&batt=0.0", url);
}
@Test
public void testFormatPathPortRequest() throws Exception {
Position position = new Position("123456789012345", new Location("gps"), 0);
position.setTime(new Date(0));
String url = ProtocolFormatter.formatRequest("http://localhost:8888/path", position);
assertEquals("http://localhost:8888/path?id=123456789012345&timestamp=0&lat=0.0&lon=0.0&speed=0.0&bearing=0.0&altitude=0.0&batt=0.0", url);
} }
@Test @Test