Skip to content

Commit

Permalink
Prevent Android O from blocking intent/notification conversion
Browse files Browse the repository at this point in the history
- use BeaconLocalBroadcastProcessor instead of BeaconIntentProcessor when ScanJob is used
  • Loading branch information
davidgyoung committed Jul 24, 2017
1 parent 956eff4 commit 16fb444
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 84 deletions.
72 changes: 14 additions & 58 deletions src/main/java/org/altbeacon/beacon/BeaconIntentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,19 @@

/**
* Converts internal intents to notifier callbacks
* This IntentService may be running in a different process from the BeaconService.
*
* This is used with the BeaconService and supports scanning in a separate process.
* It is not used with the ScanJob, as an IntentService will not be able to be started in some cases
* where the app is in the background on Android O.
*
* @see BeaconLocalBroadcastProcessor for the equivalent use with ScanJob.
*
* This IntentService may be running in a different process from the BeaconService, which justifies
* its continued existence for multi-process service cases.
*
* Internal library class. Do not use directly from outside the library
*
* @hide
*/
public class BeaconIntentProcessor extends IntentService {
private static final String TAG = "BeaconIntentProcessor";
Expand All @@ -46,62 +58,6 @@ public BeaconIntentProcessor() {

@Override
protected void onHandleIntent(Intent intent) {
LogManager.d(TAG, "got an intent to process");

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(this).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(this).getDataRequestNotifier();
if (dataNotifier != null) {
dataNotifier.didRangeBeaconsInRegion(beacons, rangingData.getRegion());
}
}

if (monitoringData != null) {
LogManager.d(TAG, "got monitoring data");
Set<MonitorNotifier> notifiers = BeaconManager.getInstanceForApplication(this).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(this).updateLocalState(region, state);
if (monitoringData.isInside()) {
notifier.didEnterRegion(monitoringData.getRegion());
} else {
notifier.didExitRegion(monitoringData.getRegion());
}
}
}
}
new IntentHandler().convertIntentsToCallbacks(this.getApplicationContext(), intent);
}
}
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);
}
};
}
3 changes: 1 addition & 2 deletions src/main/java/org/altbeacon/beacon/BeaconManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,7 @@ protected BeaconManager(@NonNull Context context) {
verifyServiceDeclaration();
}
this.beaconParsers.add(new AltBeaconParser());
// TODO: Change this to >= Build.VERSION_CODES.O when the SDK is released
mScheduledScanJobsEnabled = android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.N;
mScheduledScanJobsEnabled = android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
}

/***
Expand Down
85 changes: 85 additions & 0 deletions src/main/java/org/altbeacon/beacon/IntentHandler.java
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());
}
}
}
}

}
}
50 changes: 26 additions & 24 deletions src/main/java/org/altbeacon/beacon/service/Callback.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,20 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;

import org.altbeacon.beacon.BeaconLocalBroadcastProcessor;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.logging.LogManager;

import java.io.IOException;
import java.io.Serializable;

public class Callback implements Serializable {
private static final String TAG = "Callback";
private transient Intent mIntent;
private String mIntentPackageName;

//TODO: Remove this constructor in favor of an empty one, as the packae name is no longer needed
public Callback(String intentPackageName) {
mIntentPackageName = intentPackageName;
initializeIntent();
}

private void initializeIntent() {
if (mIntentPackageName != null) {
mIntent = new Intent();
mIntent.setComponent(new ComponentName(mIntentPackageName, "org.altbeacon.beacon.BeaconIntentProcessor"));
}
}

public Intent getIntent() {
return mIntent;
}

/**
Expand All @@ -63,20 +52,34 @@ public Intent getIntent() {
* @return false if it callback cannot be made
*/
public boolean call(Context context, String dataName, Bundle data) {
if(mIntent == null){
initializeIntent();
}
boolean useLocalBroadcast = BeaconManager.getInstanceForApplication(context).getScheduledScanJobsEnabled();
boolean success = false;
if (mIntent != null) {
LogManager.d(TAG, "attempting callback via intent: %s", mIntent.getComponent());
mIntent.putExtra(dataName, data);

if(useLocalBroadcast) {
String action = null;
if (dataName == "rangingData") {
action = BeaconLocalBroadcastProcessor.RANGE_NOTIFICATION;
}
else {
action = BeaconLocalBroadcastProcessor.MONITOR_NOTIFICATION;
}
Intent intent = new Intent(action);
intent.putExtra(dataName, data);
LogManager.d(TAG, "attempting callback via local broadcast intent: %s",action);
success = LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
else {
Intent intent = new Intent();
intent.setComponent(new ComponentName(context.getPackageName(), "org.altbeacon.beacon.BeaconIntentProcessor"));
intent.putExtra(dataName, data);
LogManager.d(TAG, "attempting callback via global broadcast intent: %s",intent.getComponent());
try {
context.startService(mIntent);
context.startService(intent);
success = true;
} catch (Exception e) {
LogManager.e(
TAG,
"Failed attempting to start service: " + mIntent.getComponent().flattenToString(),
"Failed attempting to start service: " + intent.getComponent().flattenToString(),
e
);
}
Expand All @@ -88,6 +91,5 @@ public boolean call(Context context, String dataName, Bundle data) {
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException {
in.defaultReadObject();
initializeIntent();
}
}
3 changes: 3 additions & 0 deletions src/main/java/org/altbeacon/beacon/service/ScanJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import android.bluetooth.le.ScanResult;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.NonNull;

import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconLocalBroadcastProcessor;
import org.altbeacon.beacon.BuildConfig;
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.distance.ModelSpecificDistanceCalculator;
Expand Down
Loading

0 comments on commit 16fb444

Please sign in to comment.