123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package com.owncloud.android.location;
- import com.owncloud.android.Log_OC;
- import android.app.ActivityManager;
- import android.app.ActivityManager.RunningServiceInfo;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.preference.PreferenceManager;
- import android.util.Log;
- public class LocationServiceLauncherReciever extends BroadcastReceiver {
- private final String TAG = getClass().getSimpleName();
- @Override
- public void onReceive(Context context, Intent intent) {
- Intent deviceTrackingIntent = new Intent();
- deviceTrackingIntent
- .setAction("com.owncloud.android.location.LocationUpdateService");
- SharedPreferences preferences = PreferenceManager
- .getDefaultSharedPreferences(context);
- boolean trackDevice = preferences.getBoolean("enable_devicetracking",
- true);
-
-
- if (intent.hasExtra("TRACKING_SETTING")) {
- trackDevice = intent.getBooleanExtra("TRACKING_SETTING", true);
- }
- startOrStopDeviceTracking(context, trackDevice);
- }
-
- private void startOrStopDeviceTracking(Context context, boolean trackDevice) {
- Intent deviceTrackingIntent = new Intent();
- deviceTrackingIntent
- .setAction("com.owncloud.android.location.LocationUpdateService");
- if (!isDeviceTrackingServiceRunning(context) && trackDevice) {
- Log_OC.d(TAG, "Starting device tracker service");
- context.startService(deviceTrackingIntent);
- } else if (isDeviceTrackingServiceRunning(context) && !trackDevice) {
- Log_OC.d(TAG, "Stopping device tracker service");
- context.stopService(deviceTrackingIntent);
- }
- }
-
- private boolean isDeviceTrackingServiceRunning(Context context) {
- ActivityManager manager = (ActivityManager) context
- .getSystemService(Context.ACTIVITY_SERVICE);
- for (RunningServiceInfo service : manager
- .getRunningServices(Integer.MAX_VALUE)) {
- if (getClass().getName().equals(service.service.getClassName())) {
- return true;
- }
- }
- return false;
- }
- }
|