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