-
Notifications
You must be signed in to change notification settings - Fork 838
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent Android O from blocking intent/notification conversion
- use BeaconLocalBroadcastProcessor instead of BeaconIntentProcessor when ScanJob is used
- Loading branch information
1 parent
956eff4
commit 16fb444
Showing
7 changed files
with
233 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/main/java/org/altbeacon/beacon/BeaconLocalBroadcastProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* Radius Networks, Inc. | ||
* http://www.radiusnetworks.com | ||
* | ||
* @author David G. Young | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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.altbeacon.beacon; | ||
|
||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.IntentFilter; | ||
import android.support.annotation.NonNull; | ||
import android.support.v4.content.LocalBroadcastManager; | ||
|
||
import org.altbeacon.beacon.logging.LogManager; | ||
|
||
import java.util.Set; | ||
|
||
/** | ||
* Converts internal intents to notifier callbacks | ||
* | ||
* This is used with ScanJob and supports delivering intents even under Android O background | ||
* restrictions preventing starting a new IntentService. | ||
* | ||
* It is not used with the BeaconService, as local broadcast intents cannot be deliverd across | ||
* different processes which the BeaconService supports. | ||
* | ||
* @see BeaconIntentProcessor for the equivalent use with BeaconService. | ||
** | ||
* Internal library class. Do not use directly from outside the library | ||
* | ||
* @hide | ||
*/ | ||
public class BeaconLocalBroadcastProcessor { | ||
private static final String TAG = "BeaconLocalBroadcastProcessor"; | ||
|
||
public static final String RANGE_NOTIFICATION = "org.altbeacon.beacon.range_notification"; | ||
public static final String MONITOR_NOTIFICATION = "org.altbeacon.beacon.monitor_notification"; | ||
|
||
@NonNull | ||
private Context mContext; | ||
private BeaconLocalBroadcastProcessor() { | ||
|
||
} | ||
public BeaconLocalBroadcastProcessor(Context context) { | ||
mContext = context; | ||
|
||
} | ||
|
||
static int registerCallCount = 0; | ||
int registerCallCountForInstnace = 0; | ||
public void register() { | ||
registerCallCount += 1; | ||
registerCallCountForInstnace += 1; | ||
LogManager.d(TAG, "Register calls: global="+registerCallCount+" instance="+registerCallCountForInstnace); | ||
unregister(); | ||
LocalBroadcastManager.getInstance(mContext).registerReceiver(mLocalBroadcastReceiver, | ||
new IntentFilter(RANGE_NOTIFICATION)); | ||
LocalBroadcastManager.getInstance(mContext).registerReceiver(mLocalBroadcastReceiver, | ||
new IntentFilter(MONITOR_NOTIFICATION)); | ||
} | ||
|
||
public void unregister() { | ||
LocalBroadcastManager.getInstance(mContext).unregisterReceiver(mLocalBroadcastReceiver); | ||
} | ||
|
||
|
||
private BroadcastReceiver mLocalBroadcastReceiver = new BroadcastReceiver() { | ||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
new IntentHandler().convertIntentsToCallbacks(context, intent); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.altbeacon.beacon; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
|
||
import org.altbeacon.beacon.logging.LogManager; | ||
import org.altbeacon.beacon.service.MonitoringData; | ||
import org.altbeacon.beacon.service.MonitoringStatus; | ||
import org.altbeacon.beacon.service.RangingData; | ||
|
||
import java.util.Set; | ||
|
||
/** | ||
* Converts internal Intents for ranging/monitoring to notifier callbacks. | ||
* These may be local broadcast intents from BeaconLocalBroadcastProcessor or | ||
* global broadcast intents fro BeaconIntentProcessor | ||
* | ||
* Internal library class. Do not use directly from outside the library | ||
* | ||
* @hide | ||
* Created by dyoung on 7/20/17. | ||
*/ | ||
|
||
/* package private*/ | ||
class IntentHandler { | ||
private static final String TAG = IntentHandler.class.getSimpleName(); | ||
public void convertIntentsToCallbacks(Context context, Intent intent) { | ||
MonitoringData monitoringData = null; | ||
RangingData rangingData = null; | ||
|
||
if (intent != null && intent.getExtras() != null) { | ||
if (intent.getExtras().getBundle("monitoringData") != null) { | ||
monitoringData = MonitoringData.fromBundle(intent.getExtras().getBundle("monitoringData")); | ||
} | ||
if (intent.getExtras().getBundle("rangingData") != null) { | ||
rangingData = RangingData.fromBundle(intent.getExtras().getBundle("rangingData")); | ||
} | ||
} | ||
|
||
if (rangingData != null) { | ||
LogManager.d(TAG, "got ranging data"); | ||
if (rangingData.getBeacons() == null) { | ||
LogManager.w(TAG, "Ranging data has a null beacons collection"); | ||
} | ||
Set<RangeNotifier> notifiers = BeaconManager.getInstanceForApplication(context).getRangingNotifiers(); | ||
java.util.Collection<Beacon> beacons = rangingData.getBeacons(); | ||
if (notifiers != null) { | ||
for(RangeNotifier notifier : notifiers){ | ||
notifier.didRangeBeaconsInRegion(beacons, rangingData.getRegion()); | ||
} | ||
} | ||
else { | ||
LogManager.d(TAG, "but ranging notifier is null, so we're dropping it."); | ||
} | ||
RangeNotifier dataNotifier = BeaconManager.getInstanceForApplication(context).getDataRequestNotifier(); | ||
if (dataNotifier != null) { | ||
dataNotifier.didRangeBeaconsInRegion(beacons, rangingData.getRegion()); | ||
} | ||
} | ||
|
||
if (monitoringData != null) { | ||
LogManager.d(TAG, "got monitoring data"); | ||
Set<MonitorNotifier> notifiers = BeaconManager.getInstanceForApplication(context).getMonitoringNotifiers(); | ||
if (notifiers != null) { | ||
for(MonitorNotifier notifier : notifiers) { | ||
LogManager.d(TAG, "Calling monitoring notifier: %s", notifier); | ||
Region region = monitoringData.getRegion(); | ||
Integer state = monitoringData.isInside() ? MonitorNotifier.INSIDE : | ||
MonitorNotifier.OUTSIDE; | ||
notifier.didDetermineStateForRegion(state, region); | ||
// In case the beacon scanner is running in a separate process, the monitoring | ||
// status in this process will not have been updated yet as a result of this | ||
// region state change. We make a call here to keep it in sync. | ||
MonitoringStatus.getInstanceForApplication(context).updateLocalState(region, state); | ||
if (monitoringData.isInside()) { | ||
notifier.didEnterRegion(monitoringData.getRegion()); | ||
} else { | ||
notifier.didExitRegion(monitoringData.getRegion()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.