7ec3c467ee5bc0ea8c31eb8022c8a50732872fc2
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / location / LocationUpdateService.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18 package eu.alefzero.owncloud.location;
19
20 import android.app.IntentService;
21 import android.content.Intent;
22 import android.content.SharedPreferences;
23 import android.location.Criteria;
24 import android.location.Location;
25 import android.location.LocationListener;
26 import android.location.LocationManager;
27 import android.location.LocationProvider;
28 import android.os.Bundle;
29 import android.preference.PreferenceManager;
30 import android.util.Log;
31 import android.widget.Toast;
32
33 public class LocationUpdateService extends IntentService implements LocationListener {
34
35 public static final String TAG = "LocationUpdateService";
36
37 private LocationManager mLocationManager;
38 private LocationProvider mLocationProvider;
39 private SharedPreferences mPreferences;
40
41 public LocationUpdateService() {
42 super(TAG);
43 }
44
45 @Override
46 protected void onHandleIntent(Intent intent) {
47 mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
48 // Determine, how we can track the device
49 Criteria criteria = new Criteria();
50 criteria.setAccuracy(Criteria.ACCURACY_FINE);
51 criteria.setPowerRequirement(Criteria.POWER_LOW);
52 mLocationProvider = mLocationManager.getProvider(mLocationManager.getBestProvider(criteria, true));
53
54 // Notify user if there is no way to track the device
55 if(mLocationProvider == null){
56 Toast.makeText(this, eu.alefzero.owncloud.R.string.location_no_provider, Toast.LENGTH_LONG);
57 stopSelf();
58 return;
59 }
60
61 // Get preferences for device tracking
62 mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
63 boolean trackDevice = mPreferences.getBoolean("enable_devicetracking", true);
64 int updateIntervall = Integer.parseInt(mPreferences.getString("devicetracking_update_intervall", "30")) * 60 * 1000;
65 int distanceBetweenLocationChecks = 50;
66
67 // If we do shall track the device -> Stop
68 if(!trackDevice){
69 Log.d(TAG, "Devicetracking is disabled");
70 stopSelf();
71 return;
72 }
73
74 mLocationManager.requestLocationUpdates(mLocationProvider.getName(), updateIntervall, distanceBetweenLocationChecks, this);
75 }
76
77 @Override
78 public void onLocationChanged(Location location) {
79 Log.d(TAG, "Location changed: " + location);
80
81 }
82
83 @Override
84 public void onProviderDisabled(String arg0) {
85 // TODO Auto-generated method stub
86
87 }
88
89 @Override
90 public void onProviderEnabled(String arg0) {
91 // TODO Auto-generated method stub
92
93 }
94
95 @Override
96 public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
97 // TODO Auto-generated method stub
98
99 }
100
101
102 }