Этот коммит содержится в:
Anton Tananaev
2017-01-15 17:16:41 +13:00
родитель c717da9c6a
Коммит 4a8b66694c
5 изменённых файлов: 101 добавлений и 28 удалений
+14 -11
Просмотреть файл
@@ -1,5 +1,5 @@
/* /*
* Copyright 2012 - 2016 Anton Tananaev (anton.tananaev@gmail.com) * Copyright 2012 - 2017 Anton Tananaev (anton.tananaev@gmail.com)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@ import android.preference.Preference;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.preference.TwoStatePreference; import android.preference.TwoStatePreference;
import android.util.Log;
import android.util.Patterns; import android.util.Patterns;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@@ -43,6 +44,8 @@ import java.util.Set;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class MainActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener { public class MainActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
private static final String TAG = MainActivity.class.getSimpleName();
public static final String KEY_DEVICE = "id"; public static final String KEY_DEVICE = "id";
public static final String KEY_ADDRESS = "address"; public static final String KEY_ADDRESS = "address";
public static final String KEY_PORT = "port"; public static final String KEY_PORT = "port";
@@ -92,10 +95,9 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
if (newValue != null) { if (newValue != null) {
try { try {
int value = Integer.parseInt((String) newValue); int value = Integer.parseInt((String) newValue);
if (value > 0 && value <= 65536) { return value > 0 && value <= 65536;
return true;
}
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
Log.w(TAG, e);
} }
} }
return false; return false;
@@ -106,9 +108,10 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
if (newValue != null) { if (newValue != null) {
try { try {
Integer.parseInt((String) newValue); int value = Integer.parseInt((String) newValue);
return true; return value > 0;
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
Log.w(TAG, e);
} }
} }
return false; return false;
@@ -139,11 +142,10 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
} }
} }
private void addShortcuts(boolean start, int name) { private void addShortcuts(String action, int name) {
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN); Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setComponent(new ComponentName(getPackageName(), ShortcutActivity.class.getCanonicalName())); shortcutIntent.setComponent(new ComponentName(getPackageName(), ShortcutActivity.class.getCanonicalName()));
shortcutIntent.putExtra(ShortcutActivity.EXTRA_ACTION, start); shortcutIntent.putExtra(ShortcutActivity.EXTRA_ACTION, action);
Intent installShortCutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); Intent installShortCutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
installShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); installShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
installShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(name)); installShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(name));
@@ -206,8 +208,9 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
startActivity(new Intent(this, StatusActivity.class)); startActivity(new Intent(this, StatusActivity.class));
return true; return true;
} else if (item.getItemId() == R.id.shortcuts) { } else if (item.getItemId() == R.id.shortcuts) {
addShortcuts(true, R.string.shortcut_start); addShortcuts(ShortcutActivity.EXTRA_ACTION_START, R.string.shortcut_start);
addShortcuts(false, R.string.shortcut_stop); addShortcuts(ShortcutActivity.EXTRA_ACTION_STOP, R.string.shortcut_stop);
addShortcuts(ShortcutActivity.EXTRA_ACTION_SOS, R.string.shortcut_sos);
return true; return true;
} else if (item.getItemId() == R.id.about) { } else if (item.getItemId() == R.id.about) {
startActivity(new Intent(this, AboutActivity.class)); startActivity(new Intent(this, AboutActivity.class));
+2 -2
Просмотреть файл
@@ -68,14 +68,14 @@ public abstract class PositionProvider {
if (location != null && location.getTime() - lastUpdateTime >= period) { if (location != null && location.getTime() - lastUpdateTime >= period) {
Log.i(TAG, "location new"); Log.i(TAG, "location new");
lastUpdateTime = location.getTime(); lastUpdateTime = location.getTime();
listener.onPositionUpdate(new Position(deviceId, location, getBatteryLevel())); listener.onPositionUpdate(new Position(deviceId, location, getBatteryLevel(context)));
} else { } else {
Log.i(TAG, location != null ? "location old" : "location nil"); Log.i(TAG, location != null ? "location old" : "location nil");
} }
} }
@TargetApi(Build.VERSION_CODES.ECLAIR) @TargetApi(Build.VERSION_CODES.ECLAIR)
private double getBatteryLevel() { public static double getBatteryLevel(Context context) {
if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.ECLAIR) { if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.ECLAIR) {
Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
+8
Просмотреть файл
@@ -20,6 +20,10 @@ import android.net.Uri;
public class ProtocolFormatter { public class ProtocolFormatter {
public static String formatRequest(String address, int port, boolean secure, Position position) { public static String formatRequest(String address, int port, boolean secure, Position position) {
return formatRequest(address, port, secure, position, null);
}
public static String formatRequest(String address, int port, boolean secure, Position position, String alarm) {
Uri.Builder builder = new Uri.Builder(); Uri.Builder builder = new Uri.Builder();
builder.scheme(secure ? "https" : "http").encodedAuthority(address + ':' + port) builder.scheme(secure ? "https" : "http").encodedAuthority(address + ':' + port)
@@ -32,6 +36,10 @@ public class ProtocolFormatter {
.appendQueryParameter("altitude", String.valueOf(position.getAltitude())) .appendQueryParameter("altitude", String.valueOf(position.getAltitude()))
.appendQueryParameter("batt", String.valueOf(position.getBattery())); .appendQueryParameter("batt", String.valueOf(position.getBattery()));
if (alarm != null) {
builder.appendQueryParameter("alarm", alarm);
}
return builder.build().toString(); return builder.build().toString();
} }
+75 -15
Просмотреть файл
@@ -1,5 +1,5 @@
/* /*
* Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) * Copyright 2016 - 2017 Anton Tananaev (anton.tananaev@gmail.com)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,14 +16,23 @@
package org.traccar.client; package org.traccar.client;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.widget.Toast; import android.widget.Toast;
public class ShortcutActivity extends Activity { public class ShortcutActivity extends Activity {
public static final String EXTRA_ACTION = "shortcutAction"; public static final String EXTRA_ACTION = "action";
public static final String EXTRA_ACTION_START = "start";
public static final String EXTRA_ACTION_STOP = "stop";
public static final String EXTRA_ACTION_SOS = "sos";
private static final String ALARM_SOS = "sos";
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@@ -37,20 +46,71 @@ public class ShortcutActivity extends Activity {
checkShortcutAction(intent); checkShortcutAction(intent);
} }
private void checkShortcutAction(Intent intent) { @SuppressWarnings("MissingPermission")
if (intent.hasExtra(EXTRA_ACTION)) { private void sendAlarm() {
boolean start = intent.getBooleanExtra(EXTRA_ACTION, false); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
PreferenceManager.getDefaultSharedPreferences(this)
.edit().putBoolean(MainActivity.KEY_STATUS, start).commit(); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (start) {
startService(new Intent(this, TrackingService.class)); Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Toast.makeText(this, R.string.status_service_create, Toast.LENGTH_SHORT).show(); if (location == null) {
} else { location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
stopService(new Intent(this, TrackingService.class)); }
Toast.makeText(this, R.string.status_service_destroy, Toast.LENGTH_SHORT).show();
} if (location != null) {
finish();
Position position = new Position(
preferences.getString(MainActivity.KEY_DEVICE, null),
location, PositionProvider.getBatteryLevel(this));
String request = ProtocolFormatter.formatRequest(
preferences.getString(MainActivity.KEY_ADDRESS, null),
Integer.parseInt(preferences.getString(MainActivity.KEY_PORT, null)),
preferences.getBoolean(MainActivity.KEY_SECURE, false),
position, ALARM_SOS);
RequestManager.sendRequestAsync(request, new RequestManager.RequestHandler() {
@Override
public void onComplete(boolean success) {
if (success) {
Toast.makeText(ShortcutActivity.this, R.string.status_send_success, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ShortcutActivity.this, R.string.status_send_fail, Toast.LENGTH_SHORT).show();
}
}
});
} else {
Toast.makeText(this, R.string.status_send_fail, Toast.LENGTH_SHORT).show();
} }
} }
private void checkShortcutAction(Intent intent) {
String action;
if (intent.hasExtra("shortcutAction")) {
action = intent.getBooleanExtra("shortcutAction", false)
? EXTRA_ACTION_START : EXTRA_ACTION_STOP;
} else {
action = intent.getStringExtra(EXTRA_ACTION);
}
switch (action) {
case EXTRA_ACTION_START:
PreferenceManager.getDefaultSharedPreferences(this)
.edit().putBoolean(MainActivity.KEY_STATUS, true).commit();
startService(new Intent(this, TrackingService.class));
Toast.makeText(this, R.string.status_service_create, Toast.LENGTH_SHORT).show();
break;
case EXTRA_ACTION_STOP:
PreferenceManager.getDefaultSharedPreferences(this)
.edit().putBoolean(MainActivity.KEY_STATUS, false).commit();
stopService(new Intent(this, TrackingService.class));
Toast.makeText(this, R.string.status_service_destroy, Toast.LENGTH_SHORT).show();
break;
case EXTRA_ACTION_SOS:
sendAlarm();
break;
}
finish();
}
} }
+2
Просмотреть файл
@@ -4,6 +4,7 @@
<string name="app_logo">Traccar</string> <string name="app_logo">Traccar</string>
<string name="shortcut_start">Traccar - Start</string> <string name="shortcut_start">Traccar - Start</string>
<string name="shortcut_stop">Traccar - Stop</string> <string name="shortcut_stop">Traccar - Stop</string>
<string name="shortcut_sos">Traccar - SOS</string>
<string name="settings_id_title">Device identifier</string> <string name="settings_id_title">Device identifier</string>
<string name="settings_address_title">Server address</string> <string name="settings_address_title">Server address</string>
<string name="settings_address_summary">Domain name or IP address</string> <string name="settings_address_summary">Domain name or IP address</string>
@@ -37,6 +38,7 @@
<string name="about_web">For more information visit\nwww.traccar.org/client</string> <string name="about_web">For more information visit\nwww.traccar.org/client</string>
<string name="status_service_create">Service created</string> <string name="status_service_create">Service created</string>
<string name="status_service_destroy">Service destroyed</string> <string name="status_service_destroy">Service destroyed</string>
<string name="status_send_success">Send successfully</string>
<string name="status_send_fail">Send failed</string> <string name="status_send_fail">Send failed</string>
<string name="status_location_update">Location update</string> <string name="status_location_update">Location update</string>
<string name="status_connectivity_change">Connectivity change</string> <string name="status_connectivity_change">Connectivity change</string>