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