1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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.
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/>.
18 package com
.owncloud
.android
.location
;
20 import com
.owncloud
.android
.Log_OC
;
22 import android
.app
.ActivityManager
;
23 import android
.app
.ActivityManager
.RunningServiceInfo
;
24 import android
.content
.BroadcastReceiver
;
25 import android
.content
.Context
;
26 import android
.content
.Intent
;
27 import android
.content
.SharedPreferences
;
28 import android
.preference
.PreferenceManager
;
29 import android
.util
.Log
;
31 public class LocationServiceLauncherReciever
extends BroadcastReceiver
{
33 private final String TAG
= getClass().getSimpleName();
36 public void onReceive(Context context
, Intent intent
) {
37 Intent deviceTrackingIntent
= new Intent();
39 .setAction("com.owncloud.android.location.LocationUpdateService");
40 SharedPreferences preferences
= PreferenceManager
41 .getDefaultSharedPreferences(context
);
42 boolean trackDevice
= preferences
.getBoolean("enable_devicetracking",
45 // Used in Preferences activity so that tracking is disabled or
47 if (intent
.hasExtra("TRACKING_SETTING")) {
48 trackDevice
= intent
.getBooleanExtra("TRACKING_SETTING", true
);
51 startOrStopDeviceTracking(context
, trackDevice
);
55 * Used internally. Starts or stops the device tracking service
57 * @param trackDevice true to start the service, false to stop it
59 private void startOrStopDeviceTracking(Context context
, boolean trackDevice
) {
60 Intent deviceTrackingIntent
= new Intent();
62 .setAction("com.owncloud.android.location.LocationUpdateService");
63 if (!isDeviceTrackingServiceRunning(context
) && trackDevice
) {
64 Log_OC
.d(TAG
, "Starting device tracker service");
65 context
.startService(deviceTrackingIntent
);
66 } else if (isDeviceTrackingServiceRunning(context
) && !trackDevice
) {
67 Log_OC
.d(TAG
, "Stopping device tracker service");
68 context
.stopService(deviceTrackingIntent
);
73 * Checks to see whether or not the LocationUpdateService is running
75 * @return true, if it is. Otherwise false
77 private boolean isDeviceTrackingServiceRunning(Context context
) {
78 ActivityManager manager
= (ActivityManager
) context
79 .getSystemService(Context
.ACTIVITY_SERVICE
);
80 for (RunningServiceInfo service
: manager
81 .getRunningServices(Integer
.MAX_VALUE
)) {
82 if (getClass().getName().equals(service
.service
.getClassName())) {