LocationServiceLauncherReciever.java 3.4 KB

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