From 979963f6a248bc106fdd2167421399ebcd5721c7 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 7 Sep 2017 19:04:10 +1200 Subject: [PATCH] Fix auto-start issue on Android O --- .../org/traccar/client/AutostartReceiver.java | 1 - .../client/WakefulBroadcastReceiver.java | 62 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/org/traccar/client/WakefulBroadcastReceiver.java diff --git a/app/src/main/java/org/traccar/client/AutostartReceiver.java b/app/src/main/java/org/traccar/client/AutostartReceiver.java index af57fad..f394b04 100644 --- a/app/src/main/java/org/traccar/client/AutostartReceiver.java +++ b/app/src/main/java/org/traccar/client/AutostartReceiver.java @@ -19,7 +19,6 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; -import android.support.v4.content.WakefulBroadcastReceiver; public class AutostartReceiver extends WakefulBroadcastReceiver { diff --git a/app/src/main/java/org/traccar/client/WakefulBroadcastReceiver.java b/app/src/main/java/org/traccar/client/WakefulBroadcastReceiver.java new file mode 100644 index 0000000..ef1c262 --- /dev/null +++ b/app/src/main/java/org/traccar/client/WakefulBroadcastReceiver.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.client; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.PowerManager; +import android.support.v4.content.ContextCompat; +import android.util.SparseArray; + +public abstract class WakefulBroadcastReceiver extends BroadcastReceiver { + private static final String EXTRA_WAKE_LOCK_ID = "android.support.content.wakelockid"; + private static final SparseArray mActiveWakeLocks = new SparseArray<>(); + private static int mNextId = 1; + + public static void startWakefulService(Context context, Intent intent) { + synchronized (mActiveWakeLocks) { + int id = mNextId; + mNextId++; + if (mNextId <= 0) { + mNextId = 1; + } + intent.putExtra(EXTRA_WAKE_LOCK_ID, id); + ContextCompat.startForegroundService(context, intent); + PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); + PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, + WakefulBroadcastReceiver.class.getSimpleName()); + wl.setReferenceCounted(false); + wl.acquire(60*1000); + mActiveWakeLocks.put(id, wl); + } + } + + public static boolean completeWakefulIntent(Intent intent) { + final int id = intent.getIntExtra(EXTRA_WAKE_LOCK_ID, 0); + if (id == 0) { + return false; + } + synchronized (mActiveWakeLocks) { + PowerManager.WakeLock wl = mActiveWakeLocks.get(id); + if (wl != null) { + wl.release(); + mActiveWakeLocks.remove(id); + return true; + } + return true; + } + } +}