Этот коммит содержится в:
Anton Tananaev
2012-07-29 19:47:00 +04:00
Коммит c87cf5b08d
19 изменённых файлов: 999 добавлений и 0 удалений
+22
Просмотреть файл
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.traccar.client"
android:versionCode="1"
android:versionName="1.0">
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="TraccarActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".TraccarService" />
</application>
<uses-sdk android:minSdkVersion="4"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
</manifest>
+17
Просмотреть файл
@@ -0,0 +1,17 @@
# This file is used to override default values used by the Ant build system.
#
# This file must be checked into Version Control Systems, as it is
# integral to the build system of your project.
# This file is only used by the Ant script.
# You can use this to override default values such as
# 'source.dir' for the location of your java source folder and
# 'out.dir' for the location of your output folder.
# You can also use it define how the release builds are signed by declaring
# the following properties:
# 'key.store' for the location of your keystore and
# 'key.alias' for the name of the key to use.
# The password will be asked during the build when you use the 'release' target.
+92
Просмотреть файл
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="TraccarClient" default="help">
<!-- The local.properties file is created and updated by the 'android' tool.
It contains the path to the SDK. It should *NOT* be checked into
Version Control Systems. -->
<property file="local.properties" />
<!-- The ant.properties file can be created by you. It is only edited by the
'android' tool to add properties to it.
This is the place to change some Ant specific build properties.
Here are some properties you may want to change/update:
source.dir
The name of the source directory. Default is 'src'.
out.dir
The name of the output directory. Default is 'bin'.
For other overridable properties, look at the beginning of the rules
files in the SDK, at tools/ant/build.xml
Properties related to the SDK location or the project target should
be updated using the 'android' tool with the 'update' action.
This file is an integral part of the build system for your
application and should be checked into Version Control Systems.
-->
<property file="ant.properties" />
<!-- if sdk.dir was not set from one of the property file, then
get it from the ANDROID_HOME env var.
This must be done before we load project.properties since
the proguard config can use sdk.dir -->
<property environment="env" />
<condition property="sdk.dir" value="${env.ANDROID_HOME}">
<isset property="env.ANDROID_HOME" />
</condition>
<!-- The project.properties file is created and updated by the 'android'
tool, as well as ADT.
This contains project specific properties such as project target, and library
dependencies. Lower level build properties are stored in ant.properties
(or in .classpath for Eclipse projects).
This file is an integral part of the build system for your
application and should be checked into Version Control Systems. -->
<loadproperties srcFile="project.properties" />
<!-- quick check on sdk.dir -->
<fail
message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
unless="sdk.dir"
/>
<!--
Import per project custom build rules if present at the root of the project.
This is the place to put custom intermediary targets such as:
-pre-build
-pre-compile
-post-compile (This is typically used for code obfuscation.
Compiled code location: ${out.classes.absolute.dir}
If this is not done in place, override ${out.dex.input.absolute.dir})
-post-package
-post-build
-pre-clean
-->
<import file="custom_rules.xml" optional="true" />
<!-- Import the actual build file.
To customize existing targets, there are two options:
- Customize only one target:
- copy/paste the target into this file, *before* the
<import> task.
- customize it to your needs.
- Customize the whole content of build.xml
- copy/paste the content of the rules files (minus the top node)
into this file, replacing the <import> task.
- customize to your needs.
***********************
****** IMPORTANT ******
***********************
In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
in order to avoid having your file be overridden by tools such as "android update project"
-->
<!-- version-tag: 1 -->
<import file="${sdk.dir}/tools/ant/build.xml" />
</project>
+135
Просмотреть файл
@@ -0,0 +1,135 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="512"
height="512"
id="svg4101"
version="1.1"
inkscape:version="0.48.3.1 r9886"
sodipodi:docname="icon.svg"
inkscape:export-filename="/home/user/Documents/traccar-client/res/drawable-xhdpi/ic_launcher.png"
inkscape:export-xdpi="16.875"
inkscape:export-ydpi="16.875">
<defs
id="defs4103" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="1.4"
inkscape:cx="402.73478"
inkscape:cy="279.68584"
inkscape:document-units="px"
inkscape:current-layer="layer1"
showgrid="false"
fit-margin-top="0"
fit-margin-left="0"
fit-margin-right="0"
fit-margin-bottom="0"
inkscape:window-width="1680"
inkscape:window-height="1026"
inkscape:window-x="0"
inkscape:window-y="24"
inkscape:window-maximized="1"
width="512px" />
<metadata
id="metadata4106">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Ebene 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(-317.72197,34.03834)">
<g
id="g3057">
<rect
ry="99.363052"
rx="99.36306"
y="-11.541533"
x="340.21878"
height="467.00638"
width="467.00635"
id="rect3055"
style="fill:#ffffff;stroke:#ffffff;stroke-width:0.99363058999999998" />
<g
transform="matrix(3.9153612,2.053283,-2.053283,3.9153612,92.187853,-3064.4694)"
id="g4068">
<g
id="g4046"
transform="matrix(0.83109018,-0.84335752,0.84335752,0.83109018,-425.72131,478.52845)">
<path
inkscape:connector-curvature="0"
id="path3194"
d="m 443.65385,563.57293 c -1.05193,-0.0129 -2.10701,0.0598 -3.12744,0.18048 l 0.06,2.75094 c 0.99036,-0.12733 2.00989,-0.19417 3.0337,-0.18162 12.83055,0.15709 23.11001,11.57671 22.93914,25.53274 -0.002,0.17643 -0.001,0.35569 -0.007,0.53121 l 2.50057,-0.0319 c 0.004,-0.15699 0.004,-0.31113 0.006,-0.46872 0.18944,-15.473 -11.18011,-28.13898 -25.40528,-28.31315 z"
style="fill:#000000;fill-opacity:1;stroke:#000000;stroke-width:0;stroke-miterlimit:4;stroke-dasharray:none" />
<path
sodipodi:nodetypes="cccscccccc"
inkscape:connector-curvature="0"
id="path3194-4"
d="m 442.89836,570.53348 c -0.79783,-0.01 -1.59802,0.0454 -2.37195,0.13688 l 0.0455,2.52833 c 0.75111,-0.0966 1.52436,-0.14725 2.30084,-0.13774 9.73108,0.11913 16.95911,8.33817 16.82952,18.92284 -0.002,0.1338 -10e-4,0.26976 -0.005,0.40289 l 2.46472,-0.0242 c 0.003,-0.11907 0.003,-0.23598 0.004,-0.35547 0.14368,-11.73519 -8.47933,-21.34145 -19.26811,-21.47354 z"
style="fill:#000000;fill-opacity:1;stroke:#000000;stroke-width:0;stroke-miterlimit:4;stroke-dasharray:none" />
<path
sodipodi:nodetypes="cccccccccc"
inkscape:connector-curvature="0"
id="path3194-0"
d="m 442.20847,576.88946 c -0.56577,-0.007 -1.13323,0.0322 -1.68206,0.0971 l 0.0323,2.42658 c 0.53265,-0.0685 1.081,-0.10442 1.63165,-0.0977 6.90079,0.46331 10.91426,5.09 11.13804,12.78552 -10e-4,0.0949 -7.7e-4,0.19129 -0.003,0.28572 l 2.54446,-0.0171 c 0.002,-0.0845 0.002,-0.16735 0.003,-0.25208 0.10189,-8.32201 -6.01312,-15.13429 -13.66399,-15.22797 z"
style="fill:#000000;fill-opacity:1;stroke:#000000;stroke-width:0;stroke-miterlimit:4;stroke-dasharray:none" />
</g>
<g
id="g4061"
transform="translate(4.3272904,0)">
<path
id="path4401"
d="m 428.82958,632.50381 c 1.51739,8.39514 3.18843,16.70366 3.25039,25.86381 13.43389,-7.07008 16.24573,-8.60045 29.67957,-15.67062 -4.84405,-3.37683 -9.59166,-7.39353 -13.56307,-11.22903 -6.14118,1.98174 -12.37667,2.63171 -19.36689,1.03584 z m 3.03559,3.5588 2.90054,0.34654 c 1.65734,7.68989 1.24429,15.11973 -0.19508,18.63703 -0.5999,-6.11993 -0.9108,-10.44519 -2.70546,-18.98357 z"
style="fill:#000000;fill-opacity:1;stroke:none"
inkscape:connector-curvature="0" />
<path
id="path3423-9-5"
d="m 398.11826,602.8218 c 5.46403,15.3042 20.05405,26.26585 37.2409,26.32057 17.18753,0.0547 31.81943,-10.80285 37.38029,-26.07291 l -74.62119,-0.24766 z m 6.11343,4.37701 6.55964,0.18844 c 4.52018,8.23085 9.05564,11.77782 15.40988,16.82483 -9.13544,-2.75636 -16.94711,-7.80772 -21.96952,-17.01327 z"
style="fill:#000000;fill-opacity:1;stroke:none"
inkscape:connector-curvature="0" />
<g
transform="translate(2.1996971,2)"
id="g4055">
<rect
style="fill:#000000;fill-opacity:1;stroke:#000000;stroke-width:0;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none"
id="rect4051"
width="3.2829957"
height="8.0812206"
x="431.58765"
y="594.76306" />
<path
sodipodi:type="arc"
style="fill:#000000;fill-opacity:1;stroke:#000000;stroke-width:0;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none"
id="path4053"
sodipodi:cx="433.22916"
sodipodi:cy="594.00549"
sodipodi:rx="3.4092648"
sodipodi:ry="3.7880721"
d="m 436.63842,594.00549 c 0,2.0921 -1.52638,3.78808 -3.40926,3.78808 -1.88289,0 -3.40927,-1.69598 -3.40927,-3.78808 0,-2.09209 1.52638,-3.78807 3.40927,-3.78807 1.88288,0 3.40926,1.69598 3.40926,3.78807 z" />
</g>
</g>
</g>
</g>
</g>
</svg>

После

Ширина:  |  Высота:  |  Размер: 6.4 KiB

Двоичные данные
Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 25 KiB

+10
Просмотреть файл
@@ -0,0 +1,10 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
# location of the SDK. This is only used by Ant
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=/home/user/Documents/android-sdk-linux
+20
Просмотреть файл
@@ -0,0 +1,20 @@
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
+14
Просмотреть файл
@@ -0,0 +1,14 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
# Project target.
target=android-16
Двоичные данные
Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 2.7 KiB

Двоичные данные
Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 1.2 KiB

Двоичные данные
Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 1.7 KiB

Двоичные данные
Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 3.6 KiB

+101
Просмотреть файл
@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20sp"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Server address"
/>
<EditText
android:id="@+id/edit_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textUri"
/>
<View
android:layout_width="fill_parent"
android:layout_height="10sp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Server port"
/>
<EditText
android:id="@+id/edit_port"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
/>
<View
android:layout_width="fill_parent"
android:layout_height="10sp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Frequency in seconds"
/>
<EditText
android:id="@+id/edit_period"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
/>
<View
android:layout_width="fill_parent"
android:layout_height="10sp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Service status"
/>
<EditText
android:id="@+id/edit_service"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:editable="false"
android:focusable="false"
/>
<View
android:layout_width="fill_parent"
android:layout_height="10sp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Connection status"
/>
<EditText
android:id="@+id/edit_connection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:editable="false"
android:focusable="false"
/>
<View
android:layout_width="fill_parent"
android:layout_height="20sp"
/>
<ToggleButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="ON"
android:textOff="OFF"
android:onClick="onButtonClick"
/>
</LinearLayout>
</ScrollView>
+7
Просмотреть файл
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/about"
android:title="@string/about_option"
android:showAsAction="ifRoom" />
</menu>
+8
Просмотреть файл
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Traccar Client</string>
<string name="error_input">Invalid input parameters.</string>
<string name="about_option">About</string>
<string name="about_text">Android software GPS tracker. For more information visit:\n\nhttp://www.traccar.org</string>
<string name="about_close">Close</string>
</resources>
+153
Просмотреть файл
@@ -0,0 +1,153 @@
/*
* Copyright 2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.client;
import java.io.*;
import java.net.*;
import java.util.*;
import android.os.Handler;
import android.content.Intent;
import android.content.Context;
/**
* Connection with autoreconnect
*/
public class Connection {
private String address;
private int port;
private String connectMessage;
private Socket socket;
private OutputStream socketStream;
private String status;
private Context context;
/**
* Initialize connection parameters
*/
public Connection(String address, int port, String message) {
this.address = address;
this.port = port;
connectMessage = message;
setStatus(Traccar.STATUS_DISCONNECTED);
}
/**
* Get address
*/
public String getAddress() {
return address;
}
/**
* Get port
*/
public int getPort() {
return port;
}
/**
* Set context
*/
public void setContext(Context context) {
this.context = context;
}
/**
* Set connection status
*/
private void setStatus(String status) {
if (!status.equals(this.status)) {
this.status = status;
if (context != null) {
Intent intent = new Intent(Traccar.MSG_CONNECTION_STATUS);
intent.putExtra(Traccar.EXTRA_STATUS, status);
context.sendBroadcast(intent);
}
}
}
/**
* Get connection status
*/
public String getStatus() {
return status;
}
/**
* Establish connection
*/
public void connect() {
try {
socket = new Socket(address, port);
socket.setSoTimeout(Traccar.SOCKET_TIMEOUT);
socketStream = socket.getOutputStream();
setStatus(Traccar.STATUS_CONNECTED);
send(connectMessage);
} catch (Exception e) {
reconnect();
}
}
/**
* Send message
*/
public void send(String message) {
try {
if (socketStream != null) {
socketStream.write(message.getBytes());
socketStream.flush();
}
} catch (Exception e) {
reconnect();
}
}
/**
* Close connection
*/
public void close() {
handler.removeCallbacks(task);
if (socket != null) {
try {
socket.close();
} catch (Exception e) {
}
socket = null;
}
socketStream = null;
setStatus(Traccar.STATUS_DISCONNECTED);
}
private void reconnect() {
close();
handler.postDelayed(task, Traccar.RECONNECT_DELAY);
}
private Handler handler = new Handler();
private ReconnectTask task = new ReconnectTask();
class ReconnectTask implements Runnable {
@Override
public void run() {
connect();
}
}
}
+70
Просмотреть файл
@@ -0,0 +1,70 @@
/*
* Copyright 2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.client;
import java.util.Formatter;
import android.location.Location;
/**
* Protocol formatting
*/
public class Protocol {
/**
* Format device id message
*/
public static String createId(String id) {
StringBuilder s = new StringBuilder("$PGID,");
Formatter f = new Formatter(s);
s.append(id);
byte checksum = 0;
for (byte b : s.substring(1).getBytes()) {
checksum ^= b;
}
f.format("*%02x\r\n", (int) checksum);
return s.toString();
}
/**
* Format location message
*/
public static String createNmea(Location l) {
StringBuilder s = new StringBuilder("$GPRMC,");
Formatter f = new Formatter(s);
f.format("%1$tH%1$tM%1$tS.%1$tL,A,", l.getTime());
double lat = l.getLatitude();
double lon = l.getLongitude();
f.format("%02d%07.4f,%c,", (int) Math.abs(lat), Math.abs(lat) % 1 * 60, lat < 0 ? 'S' : 'N' );
f.format("%03d%07.4f,%c,", (int) Math.abs(lon), Math.abs(lon) % 1 * 60, lon < 0 ? 'W' : 'E' );
f.format("%.2f,%.2f,", l.getSpeed(), l.getBearing());
f.format("%1$td%1$tm%1$ty,,", l.getTime());
byte checksum = 0;
for (byte b : s.substring(1).getBytes()) {
checksum ^= b;
}
f.format("*%02x\r\n", (int) checksum);
return s.toString();
}
}
+209
Просмотреть файл
@@ -0,0 +1,209 @@
/*
* Copyright 2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.client;
import java.util.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.text.*;
import android.text.method.*;
import android.text.util.*;
import android.view.*;
import android.widget.*;
/**
* Global parameters
*/
class Traccar {
public static final String EXTRA_ADDRESS = "address";
public static final String EXTRA_PORT = "port";
public static final String EXTRA_PERIOD = "period";
public static final String EXTRA_STATUS = "status";
public static final String DEFAULT_ADDRESS = "193.193.165.166";
public static final String DEFAULT_PORT = "20100";
public static final String DEFAULT_PERIOD = "60";
public static final String TRACCAR_SETTINGS = "traccar_settings";
public static final long RECONNECT_DELAY = 10 * 1000;
public static final int SOCKET_TIMEOUT = 2000;
public static final String MSG_UPDATE = "org.traccar.client.MSG_UPDATE";
public static final String MSG_SERVICE_CONFIG = "org.traccar.client.MSG_SERVICE_CONFIG";
public static final String MSG_SERVICE_STATUS = "org.traccar.client.SERVICE_STATUS";
public static final String MSG_CONNECTION_STATUS = "org.traccar.client.CONNECTION_STATUS";
public static final String STATUS_STOPPED = "Stopped";
public static final String STATUS_RUNNING = "Running";
public static final String STATUS_CONNECTED = "Connected";
public static final String STATUS_DISCONNECTED = "Disconnected";
}
/**
* Main user interface
*/
public class TraccarActivity extends Activity {
private EditText editAddress;
private EditText editPort;
private EditText editPeriod;
private EditText editService;
private EditText editConnection;
private ToggleButton button;
public boolean isServiceRunning() {
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
for (ActivityManager.RunningServiceInfo runningServiceInfo : services) {
if (runningServiceInfo.service.getClassName().equals(TraccarService.class.getName())){
return true;
}
}
return false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Bind elements
editAddress = (EditText) findViewById(R.id.edit_address);
editPort = (EditText) findViewById(R.id.edit_port);
editPeriod = (EditText) findViewById(R.id.edit_period);
editService = (EditText) findViewById(R.id.edit_service);
editConnection = (EditText) findViewById(R.id.edit_connection);
button = (ToggleButton) findViewById(R.id.button);
editService.setText(Traccar.STATUS_STOPPED);
editConnection.setText(Traccar.STATUS_DISCONNECTED);
button.setChecked(isServiceRunning());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.about){
showAboutDialog();
return true;
}
return false;
}
public void showAboutDialog() {
TextView message = new TextView(this);
SpannableString str = new SpannableString(getString(R.string.about_text));
Linkify.addLinks(str, Linkify.WEB_URLS);
message.setText(str);
message.setMovementMethod(LinkMovementMethod.getInstance());
int padding = (int) (10 * getResources().getDisplayMetrics().scaledDensity);
message.setPadding(padding, padding, padding, padding);
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(R.string.about_option);
dialog.setView(message);
dialog.setPositiveButton(R.string.about_close, null);
dialog.show();
}
private BroadcastReceiver serviceConfigReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
editAddress.setText(intent.getStringExtra(Traccar.EXTRA_ADDRESS));
editPort.setText(String.valueOf(intent.getIntExtra(Traccar.EXTRA_PORT, 0)));
editPeriod.setText(String.valueOf(intent.getIntExtra(Traccar.EXTRA_PERIOD, 0)));
}
};
private BroadcastReceiver serviceStatusReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
editService.setText(intent.getStringExtra(Traccar.EXTRA_STATUS));
}
};
private BroadcastReceiver connectionStatusReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
editConnection.setText(intent.getStringExtra(Traccar.EXTRA_STATUS));
}
};
@Override
protected void onStart() {
super.onStart();
loadSettings();
// Register receivers
registerReceiver(serviceConfigReceiver, new IntentFilter(Traccar.MSG_SERVICE_CONFIG));
registerReceiver(serviceStatusReceiver, new IntentFilter(Traccar.MSG_SERVICE_STATUS));
registerReceiver(connectionStatusReceiver, new IntentFilter(Traccar.MSG_CONNECTION_STATUS));
// Request update
sendBroadcast(new Intent(Traccar.MSG_UPDATE));
}
@Override
protected void onStop() {
super.onStop();
saveSettings();
// Unregister receivers
unregisterReceiver(serviceConfigReceiver);
unregisterReceiver(serviceStatusReceiver);
unregisterReceiver(connectionStatusReceiver);
}
public void onButtonClick(View v) {
if (button.isChecked()) {
try {
Intent serviceIntent = new Intent(this, TraccarService.class);
serviceIntent.putExtra(Traccar.EXTRA_ADDRESS, editAddress.getText().toString());
serviceIntent.putExtra(Traccar.EXTRA_PORT, Integer.parseInt(editPort.getText().toString()));
serviceIntent.putExtra(Traccar.EXTRA_PERIOD, Integer.parseInt(editPeriod.getText().toString()));
startService(serviceIntent);
} catch (NumberFormatException e) {
Toast.makeText(this, R.string.error_input, Toast.LENGTH_SHORT).show();
button.setChecked(false);
}
} else {
stopService(new Intent(this, TraccarService.class));
}
}
public void saveSettings() {
SharedPreferences settings = getSharedPreferences(Traccar.TRACCAR_SETTINGS, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(Traccar.EXTRA_ADDRESS, editAddress.getText().toString());
editor.putString(Traccar.EXTRA_PORT, editPort.getText().toString());
editor.putString(Traccar.EXTRA_PERIOD, editPeriod.getText().toString());
editor.commit();
}
public void loadSettings() {
SharedPreferences settings = getSharedPreferences(Traccar.TRACCAR_SETTINGS, 0);
editAddress.setText(settings.getString(Traccar.EXTRA_ADDRESS, Traccar.DEFAULT_ADDRESS));
editPort.setText(settings.getString(Traccar.EXTRA_PORT, Traccar.DEFAULT_PORT));
editPeriod.setText(settings.getString(Traccar.EXTRA_PERIOD, Traccar.DEFAULT_PERIOD));
}
}
+141
Просмотреть файл
@@ -0,0 +1,141 @@
/*
* Copyright 2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.client;
import java.io.*;
import java.net.*;
import java.util.*;
import android.app.Service;
import android.content.*;
import android.location.*;
import android.os.*;
import android.telephony.TelephonyManager;
import android.widget.Toast;
/**
* Background service
*/
public class TraccarService extends Service implements LocationListener {
private int period;
private String deviceId;
private Connection connection;
private LocationManager locationManager;
/*
* Location methods
*/
@Override
public void onLocationChanged(Location location) {
connection.send(Protocol.createNmea(location));
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
/*
* Status methods
*/
private String status;
private void setStatus(String status) {
if (!status.equals(this.status)) {
this.status = status;
Intent intent = new Intent(Traccar.MSG_SERVICE_STATUS);
intent.putExtra(Traccar.EXTRA_STATUS, status);
sendBroadcast(intent);
}
}
private BroadcastReceiver updateReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
// Send config
intent = new Intent(Traccar.MSG_SERVICE_CONFIG);
intent.putExtra(Traccar.EXTRA_ADDRESS, connection.getAddress());
intent.putExtra(Traccar.EXTRA_PORT, connection.getPort());
intent.putExtra(Traccar.EXTRA_PERIOD, period);
sendBroadcast(intent);
// Send service status
intent = new Intent(Traccar.MSG_SERVICE_STATUS);
intent.putExtra(Traccar.EXTRA_STATUS, status);
sendBroadcast(intent);
// Send connection status
intent = new Intent(Traccar.MSG_CONNECTION_STATUS);
intent.putExtra(Traccar.EXTRA_STATUS, connection.getStatus());
sendBroadcast(intent);
}
};
/*
* Service methods
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Read device id
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
deviceId = telephonyManager.getDeviceId();
// Create connection
connection = new Connection(
intent.getStringExtra(Traccar.EXTRA_ADDRESS),
intent.getIntExtra(Traccar.EXTRA_PORT, 0),
Protocol.createId(deviceId));
connection.setContext(this);
connection.connect();
// Request location updates
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
period = intent.getIntExtra(Traccar.EXTRA_PERIOD, 0);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, period * 1000, 0, this);
// Report status
registerReceiver(updateReceiver, new IntentFilter(Traccar.MSG_UPDATE));
setStatus(Traccar.STATUS_RUNNING);
return START_STICKY;
}
@Override
public void onDestroy() {
// Close connection
connection.close();
// Stop location updates
locationManager.removeUpdates(this);
// Report status
unregisterReceiver(updateReceiver);
setStatus(Traccar.STATUS_STOPPED);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}