Implement alarm shortcut (fix #244)
Этот коммит содержится в:
@@ -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");
|
||||
* 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.PreferenceManager;
|
||||
import android.preference.TwoStatePreference;
|
||||
import android.util.Log;
|
||||
import android.util.Patterns;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
@@ -43,6 +44,8 @@ import java.util.Set;
|
||||
@SuppressWarnings("deprecation")
|
||||
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_ADDRESS = "address";
|
||||
public static final String KEY_PORT = "port";
|
||||
@@ -92,10 +95,9 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
|
||||
if (newValue != null) {
|
||||
try {
|
||||
int value = Integer.parseInt((String) newValue);
|
||||
if (value > 0 && value <= 65536) {
|
||||
return true;
|
||||
}
|
||||
return value > 0 && value <= 65536;
|
||||
} catch (NumberFormatException e) {
|
||||
Log.w(TAG, e);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@@ -106,9 +108,10 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
if (newValue != null) {
|
||||
try {
|
||||
Integer.parseInt((String) newValue);
|
||||
return true;
|
||||
int value = Integer.parseInt((String) newValue);
|
||||
return value > 0;
|
||||
} catch (NumberFormatException e) {
|
||||
Log.w(TAG, e);
|
||||
}
|
||||
}
|
||||
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);
|
||||
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");
|
||||
installShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
|
||||
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));
|
||||
return true;
|
||||
} else if (item.getItemId() == R.id.shortcuts) {
|
||||
addShortcuts(true, R.string.shortcut_start);
|
||||
addShortcuts(false, R.string.shortcut_stop);
|
||||
addShortcuts(ShortcutActivity.EXTRA_ACTION_START, R.string.shortcut_start);
|
||||
addShortcuts(ShortcutActivity.EXTRA_ACTION_STOP, R.string.shortcut_stop);
|
||||
addShortcuts(ShortcutActivity.EXTRA_ACTION_SOS, R.string.shortcut_sos);
|
||||
return true;
|
||||
} else if (item.getItemId() == R.id.about) {
|
||||
startActivity(new Intent(this, AboutActivity.class));
|
||||
|
||||
@@ -68,14 +68,14 @@ public abstract class PositionProvider {
|
||||
if (location != null && location.getTime() - lastUpdateTime >= period) {
|
||||
Log.i(TAG, "location new");
|
||||
lastUpdateTime = location.getTime();
|
||||
listener.onPositionUpdate(new Position(deviceId, location, getBatteryLevel()));
|
||||
listener.onPositionUpdate(new Position(deviceId, location, getBatteryLevel(context)));
|
||||
} else {
|
||||
Log.i(TAG, location != null ? "location old" : "location nil");
|
||||
}
|
||||
}
|
||||
|
||||
@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) {
|
||||
Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
|
||||
|
||||
@@ -20,6 +20,10 @@ import android.net.Uri;
|
||||
public class ProtocolFormatter {
|
||||
|
||||
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();
|
||||
builder.scheme(secure ? "https" : "http").encodedAuthority(address + ':' + port)
|
||||
@@ -32,6 +36,10 @@ public class ProtocolFormatter {
|
||||
.appendQueryParameter("altitude", String.valueOf(position.getAltitude()))
|
||||
.appendQueryParameter("batt", String.valueOf(position.getBattery()));
|
||||
|
||||
if (alarm != null) {
|
||||
builder.appendQueryParameter("alarm", alarm);
|
||||
}
|
||||
|
||||
return builder.build().toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -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");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,14 +16,23 @@
|
||||
package org.traccar.client;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.location.Location;
|
||||
import android.location.LocationManager;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.widget.Toast;
|
||||
|
||||
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
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
@@ -37,20 +46,71 @@ public class ShortcutActivity extends Activity {
|
||||
checkShortcutAction(intent);
|
||||
}
|
||||
|
||||
private void checkShortcutAction(Intent intent) {
|
||||
if (intent.hasExtra(EXTRA_ACTION)) {
|
||||
boolean start = intent.getBooleanExtra(EXTRA_ACTION, false);
|
||||
PreferenceManager.getDefaultSharedPreferences(this)
|
||||
.edit().putBoolean(MainActivity.KEY_STATUS, start).commit();
|
||||
if (start) {
|
||||
startService(new Intent(this, TrackingService.class));
|
||||
Toast.makeText(this, R.string.status_service_create, Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
stopService(new Intent(this, TrackingService.class));
|
||||
Toast.makeText(this, R.string.status_service_destroy, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
finish();
|
||||
@SuppressWarnings("MissingPermission")
|
||||
private void sendAlarm() {
|
||||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
|
||||
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
||||
|
||||
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
||||
if (location == null) {
|
||||
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
|
||||
}
|
||||
|
||||
if (location != null) {
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user