Skip to content

Commit

Permalink
fix notifications, rework broadcast receivers for Android 8.0+
Browse files Browse the repository at this point in the history
  • Loading branch information
vitas committed Oct 28, 2018
1 parent ab8195d commit fac41e3
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 17 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 27
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
applicationId 'com.samebits.beacon.locator'
minSdkVersion 18
targetSdkVersion 27
targetSdkVersion 28
versionCode 121
versionName '1.2.1'
testApplicationId 'com.samebits.beacon.locator.test'
Expand Down Expand Up @@ -53,7 +53,7 @@ dependencies {
final DAGGER_VERSION = '2.11'

implementation fileTree(include: ['*.jar'], dir: 'libs')
api 'org.altbeacon:android-beacon-library:2.15.1'
api 'org.altbeacon:android-beacon-library:2.15.2'
implementation "com.android.support:support-v4:$SUPPORT_LIBRARY_VERSION"
implementation "com.android.support:support-v13:$SUPPORT_LIBRARY_VERSION"
implementation "com.android.support:appcompat-v7:$SUPPORT_LIBRARY_VERSION"
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
android:label="@string/title_activity_settings" />
<activity
android:name=".ui.activity.BeaconActionActivity"
android:label="@string/title_activity_beacon"
/>
android:label="@string/title_activity_beacon" />
<receiver android:name=".receiver.BeaconRegionReceiver">
<intent-filter>
<action android:name="${applicationId}.action.NOTIFY_BEACON_ENTERS_REGION" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.os.RemoteException;
import android.preference.Preference;
import android.support.annotation.NonNull;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.samebits.beacon.locator.data.DataManager;
Expand Down Expand Up @@ -70,6 +71,7 @@ public class BeaconLocatorApp extends Application implements BootstrapNotifier,
private BeaconManager mBeaconManager;
private DataManager mDataManager;
private RegionBootstrap mRegionBootstrap;
private LocalBroadcastManager mBroadcaster;

public static BeaconLocatorApp from(@NonNull Context context) {
return (BeaconLocatorApp) context.getApplicationContext();
Expand All @@ -91,6 +93,8 @@ public void onCreate() {
mBeaconManager = BeaconLocatorApp.from(this).getComponent().beaconManager();
mDataManager = BeaconLocatorApp.from(this).getComponent().dataManager();

mBroadcaster = LocalBroadcastManager.getInstance(this);

initBeaconManager();

// the ability to continually scan for long periods of time in the background on Andorid 8+
Expand Down Expand Up @@ -240,7 +244,7 @@ public void didEnterRegion(Region region) {
Intent intent = new Intent();
intent.setAction(Constants.NOTIFY_BEACON_ENTERS_REGION);
intent.putExtra("REGION", (Parcelable)region);
getApplicationContext().sendOrderedBroadcast(intent, null);
mBroadcaster.sendBroadcast(intent);
}
}
}
Expand All @@ -266,8 +270,7 @@ public void didExitRegion(Region region) {
Intent intent = new Intent();
intent.setAction(Constants.NOTIFY_BEACON_LEAVES_REGION);
intent.putExtra("REGION", (Parcelable) region);
getApplicationContext().sendOrderedBroadcast(intent, null);
}
mBroadcaster.sendBroadcast(intent); }
}
}

Expand Down Expand Up @@ -297,8 +300,7 @@ public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
Intent intent = new Intent();
intent.setAction(Constants.NOTIFY_BEACON_NEAR_YOU_REGION);
intent.putExtra("REGION", (Parcelable)region);
getApplicationContext().sendOrderedBroadcast(intent, null);
}
mBroadcaster.sendBroadcast(intent); }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.samebits.beacon.locator.model.NotificationAction;
import com.samebits.beacon.locator.util.Constants;
Expand All @@ -37,7 +38,8 @@ public LocationAction(String param, NotificationAction notification) {
@Override
public String execute(Context context) {
Intent newIntent = new Intent(Constants.GET_CURRENT_LOCATION);
context.sendBroadcast(newIntent);
LocalBroadcastManager.getInstance(context).sendBroadcast(newIntent);

return super.execute(context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.samebits.beacon.locator.R;
import com.samebits.beacon.locator.model.NotificationAction;
Expand All @@ -33,6 +34,7 @@ public class NoneAction extends Action {
protected final String param;
protected final NotificationAction notification;


public NoneAction(String param, NotificationAction notification) {
this.param = param;
this.notification = notification;
Expand All @@ -44,6 +46,7 @@ public String execute(Context context) {
if (isParamRequired() && isParamEmpty()) {
return context.getString(R.string.action_action_param_is_required);
}

//empty
sendAlarm(context);
return null;
Expand All @@ -58,7 +61,7 @@ protected void sendAlarm(Context context) {
if (isNotificationRequired()) {
Intent newIntent = new Intent(Constants.ALARM_NOTIFICATION_SHOW);
newIntent.putExtra("NOTIFICATION", notification);
context.sendBroadcast(newIntent);
LocalBroadcastManager.getInstance(context).sendBroadcast(newIntent);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,4 @@ public void onReceive(Context context, Intent intent) {
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -34,6 +35,7 @@
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
Expand All @@ -49,6 +51,9 @@
import com.samebits.beacon.locator.BeaconLocatorApp;
import com.samebits.beacon.locator.R;
import com.samebits.beacon.locator.model.TrackedBeacon;
import com.samebits.beacon.locator.receiver.BeaconAlertReceiver;
import com.samebits.beacon.locator.receiver.BeaconRegionReceiver;
import com.samebits.beacon.locator.receiver.LocationReceiver;
import com.samebits.beacon.locator.ui.fragment.BeaconFragment;
import com.samebits.beacon.locator.ui.fragment.DetectedBeaconsFragment;
import com.samebits.beacon.locator.ui.fragment.ScanFragment;
Expand Down Expand Up @@ -83,6 +88,9 @@ public class MainNavigationActivity extends BaseActivity

BeaconManager mBeaconManager;

BeaconRegionReceiver mRegionReceiver;
BeaconAlertReceiver mAlertReceiver;
LocationReceiver mLocationReceiver;

TrackedBeacon mSelectedBeacon;
private Unbinder unbinder;
Expand Down Expand Up @@ -123,6 +131,8 @@ protected void onCreate(Bundle savedInstanceState) {

readExtras();

registerReceivers();

if (null == savedInstanceState) {
if (mSelectedBeacon != null) {
launchTrackedListView();
Expand All @@ -133,8 +143,34 @@ protected void onCreate(Bundle savedInstanceState) {

}

void registerReceivers() {

mRegionReceiver = new BeaconRegionReceiver();
mLocationReceiver = new LocationReceiver();
mAlertReceiver = new BeaconAlertReceiver();

LocalBroadcastManager.getInstance(this).registerReceiver(
mRegionReceiver, new IntentFilter( Constants.NOTIFY_BEACON_ENTERS_REGION));
LocalBroadcastManager.getInstance(this).registerReceiver(
mRegionReceiver, new IntentFilter( Constants.NOTIFY_BEACON_LEAVES_REGION));
LocalBroadcastManager.getInstance(this).registerReceiver(
mRegionReceiver, new IntentFilter( Constants.NOTIFY_BEACON_NEAR_YOU_REGION));

LocalBroadcastManager.getInstance(this).registerReceiver(
mLocationReceiver, new IntentFilter( Constants.GET_CURRENT_LOCATION));
LocalBroadcastManager.getInstance(this).registerReceiver(
mAlertReceiver, new IntentFilter( Constants.ALARM_NOTIFICATION_SHOW));
}

void unregisterReceivers() {
LocalBroadcastManager.getInstance(this).unregisterReceiver( mRegionReceiver );
LocalBroadcastManager.getInstance(this).unregisterReceiver( mLocationReceiver );
LocalBroadcastManager.getInstance(this).unregisterReceiver( mAlertReceiver );
}

@Override
protected void onDestroy() {
unregisterReceivers();
unbinder.unbind();
super.onDestroy();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void onCreate(Bundle savedInstanceState) {
@Override
public void onDestroyView() {
super.onDestroyView();
if (mBeaconManager.isBound(this)) {
if (mBeaconManager != null && mBeaconManager.isBound(this)) {
mBeaconManager.unbind(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public NotificationBuilder createNotification(int smallIcon, String title, boole
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

if (isVibrate) {
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.android.tools.build:gradle:3.2.1'
}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Apr 01 12:23:37 CEST 2018
#Sun Oct 21 22:02:00 CEST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip

0 comments on commit fac41e3

Please sign in to comment.