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