130 строки
4.7 KiB
Java
130 строки
4.7 KiB
Java
/*
|
|
* 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 android.app.ActivityManager;
|
|
import android.app.ActivityManager.RunningServiceInfo;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.SharedPreferences;
|
|
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
|
import android.os.Bundle;
|
|
import android.preference.PreferenceActivity;
|
|
import android.preference.PreferenceManager;
|
|
import android.telephony.TelephonyManager;
|
|
import android.view.Menu;
|
|
import android.view.MenuInflater;
|
|
import android.view.MenuItem;
|
|
|
|
/**
|
|
* Main user interface
|
|
*/
|
|
@SuppressWarnings("deprecation")
|
|
public class TraccarActivity extends PreferenceActivity {
|
|
|
|
public static final String LOG_TAG = "traccar";
|
|
|
|
public static final String KEY_ID = "id";
|
|
public static final String KEY_ADDRESS = "address";
|
|
public static final String KEY_PORT = "port";
|
|
public static final String KEY_INTERVAL = "interval";
|
|
public static final String KEY_PROVIDER = "provider";
|
|
public static final String KEY_DEBUG = "debug";
|
|
public static final String KEY_STATUS = "status";
|
|
|
|
@Override
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
addPreferencesFromResource(R.xml.preferences);
|
|
initPreferences();
|
|
}
|
|
|
|
@Override
|
|
protected void onResume() {
|
|
super.onResume();
|
|
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(
|
|
preferenceChangeListener);
|
|
}
|
|
|
|
@Override
|
|
protected void onPause() {
|
|
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(
|
|
preferenceChangeListener);
|
|
super.onPause();
|
|
}
|
|
|
|
OnSharedPreferenceChangeListener preferenceChangeListener = new OnSharedPreferenceChangeListener() {
|
|
@Override
|
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
|
if (key.equals(KEY_STATUS)) {
|
|
if (sharedPreferences.getBoolean(KEY_STATUS, false)) {
|
|
startService(new Intent(TraccarActivity.this, TraccarService.class));
|
|
} else {
|
|
stopService(new Intent(TraccarActivity.this, TraccarService.class));
|
|
}
|
|
} else if (key.equals(KEY_ID)) {
|
|
findPreference(KEY_ID).setSummary(sharedPreferences.getString(KEY_ID, null));
|
|
}
|
|
}
|
|
};
|
|
|
|
@Override
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
MenuInflater inflater = getMenuInflater();
|
|
inflater.inflate(R.menu.main, menu);
|
|
return super.onCreateOptionsMenu(menu);
|
|
}
|
|
|
|
@Override
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
if (item.getItemId() == R.id.status) {
|
|
startActivity(new Intent(this, StatusActivity.class));
|
|
return true;
|
|
} else if (item.getItemId() == R.id.about) {
|
|
startActivity(new Intent(this, AboutActivity.class));
|
|
return true;
|
|
}
|
|
return super.onOptionsItemSelected(item);
|
|
}
|
|
|
|
private boolean isServiceRunning() {
|
|
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
|
|
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
|
|
if (TraccarService.class.getName().equals(service.service.getClassName())) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private void initPreferences() {
|
|
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
|
|
|
|
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
|
|
String id = telephonyManager.getDeviceId();
|
|
|
|
SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
|
|
|
|
if (!sharedPreferences.contains(KEY_ID)) {
|
|
sharedPreferences.edit().putString(KEY_ID, id).commit();
|
|
}
|
|
findPreference(KEY_ID).setSummary(sharedPreferences.getString(KEY_ID, id));
|
|
|
|
sharedPreferences.edit().putBoolean(KEY_STATUS, isServiceRunning()).commit();
|
|
}
|
|
|
|
}
|