1b6b28a703afdc16b464e9a113fc2f49f1870a24
[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.R;
35
36 public class LocationUpdateService extends IntentService implements
37 LocationListener {
38
39 public static final String TAG = "LocationUpdateService";
40
41 private LocationManager mLocationManager;
42 private LocationProvider mLocationProvider;
43 private SharedPreferences mPreferences;
44
45 public LocationUpdateService() {
46 super(TAG);
47 }
48
49 @Override
50 protected void onHandleIntent(Intent intent) {
51 mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
52 // Determine, how we can track the device
53 Criteria criteria = new Criteria();
54 criteria.setAccuracy(Criteria.ACCURACY_FINE);
55 criteria.setPowerRequirement(Criteria.POWER_LOW);
56 mLocationProvider = mLocationManager.getProvider(mLocationManager
57 .getBestProvider(criteria, true));
58
59 // Notify user if there is no way to track the device
60 if (mLocationProvider == null) {
61 String message = String.format(getString(R.string.location_no_provider), getString(R.string.app_name));
62 Toast.makeText(this,
63 message,
64 Toast.LENGTH_LONG).show();
65 stopSelf();
66 return;
67 }
68
69 // Get preferences for device tracking
70 mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
71 boolean trackDevice = mPreferences.getBoolean("enable_devicetracking",
72 true);
73 int updateIntervall = Integer.parseInt(mPreferences.getString(
74 "devicetracking_update_intervall", "30")) * 60 * 1000;
75 int distanceBetweenLocationChecks = 50;
76
77 // If we do shall track the device -> Stop
78 if (!trackDevice) {
79 Log.d(TAG, "Devicetracking is disabled");
80 stopSelf();
81 return;
82 }
83
84 mLocationManager.requestLocationUpdates(mLocationProvider.getName(),
85 updateIntervall, distanceBetweenLocationChecks, this);
86 }
87
88 @Override
89 public void onLocationChanged(Location location) {
90 Log.d(TAG, "Location changed: " + location);
91
92 }
93
94 @Override
95 public void onProviderDisabled(String arg0) {
96 // TODO Auto-generated method stub
97
98 }
99
100 @Override
101 public void onProviderEnabled(String arg0) {
102 // TODO Auto-generated method stub
103
104 }
105
106 @Override
107 public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
108 // TODO Auto-generated method stub
109
110 }
111
112 }