Started working on the "Where is my device?" feature
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / location / LocationServiceLauncherReciever.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.ActivityManager;
21 import android.app.ActivityManager.RunningServiceInfo;
22 import android.content.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.SharedPreferences;
26 import android.preference.PreferenceManager;
27 import android.util.Log;
28
29 public class LocationServiceLauncherReciever extends BroadcastReceiver {
30
31 private final String TAG = getClass().getSimpleName();
32
33 @Override
34 public void onReceive(Context context, Intent intent) {
35 Intent deviceTrackingIntent = new Intent();
36 deviceTrackingIntent.setAction("eu.alefzero.owncloud.location.LocationUpdateService");
37 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
38 boolean trackDevice = preferences.getBoolean("enable_devicetracking", true);
39
40 // Used in Preferences activity so that tracking is disabled or reenabled
41 if(intent.hasExtra("TRACKING_SETTING")){
42 trackDevice = intent.getBooleanExtra("TRACKING_SETTING", true);
43 }
44
45 startOrStopDeviceTracking(context, trackDevice);
46 }
47
48 /**
49 * Used internally. Starts or stops the device tracking service
50 *
51 * @param trackDevice
52 * true to start the service, false to stop it
53 */
54 private void startOrStopDeviceTracking(Context context, boolean trackDevice) {
55 Intent deviceTrackingIntent = new Intent();
56 deviceTrackingIntent
57 .setAction("eu.alefzero.owncloud.location.LocationUpdateService");
58 if (!isDeviceTrackingServiceRunning(context) && trackDevice) {
59 Log.d(TAG, "Starting device tracker service");
60 context.startService(deviceTrackingIntent);
61 } else if (isDeviceTrackingServiceRunning(context) && !trackDevice) {
62 Log.d(TAG, "Stopping device tracker service");
63 context.stopService(deviceTrackingIntent);
64 }
65 }
66
67 /**
68 * Checks to see whether or not the LocationUpdateService is running
69 *
70 * @return true, if it is. Otherwise false
71 */
72 private boolean isDeviceTrackingServiceRunning(Context context) {
73 ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
74 for (RunningServiceInfo service : manager
75 .getRunningServices(Integer.MAX_VALUE)) {
76 if (getClass().getName().equals(service.service.getClassName())) {
77 return true;
78 }
79 }
80 return false;
81 }
82
83 }