From 5db3cd09aa98ebf8bc11759c1f524c76f5033abf Mon Sep 17 00:00:00 2001 From: Vitaliy Ryumshyn Date: Sun, 28 Oct 2018 18:09:38 +0100 Subject: [PATCH] fix notifications, rework broadcast receivers for Android 8.0+ --- app/build.gradle | 6 ++-- app/src/main/AndroidManifest.xml | 3 +- .../beacon/locator/BeaconLocatorApp.java | 12 ++++--- .../beacon/locator/action/LocationAction.java | 4 ++- .../beacon/locator/action/NoneAction.java | 5 ++- .../receiver/BeaconRegionReceiver.java | 1 - .../ui/activity/MainNavigationActivity.java | 36 +++++++++++++++++++ .../locator/ui/fragment/ScanFragment.java | 2 +- .../locator/util/NotificationBuilder.java | 2 ++ build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +-- 11 files changed, 60 insertions(+), 17 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index ad04a8b..6a21bde 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' @@ -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" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1720fb0..1a3c118 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -35,8 +35,7 @@ android:label="@string/title_activity_settings" /> + android:label="@string/title_activity_beacon" /> diff --git a/app/src/main/java/com/samebits/beacon/locator/BeaconLocatorApp.java b/app/src/main/java/com/samebits/beacon/locator/BeaconLocatorApp.java index 5ef9146..f601ed7 100644 --- a/app/src/main/java/com/samebits/beacon/locator/BeaconLocatorApp.java +++ b/app/src/main/java/com/samebits/beacon/locator/BeaconLocatorApp.java @@ -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; @@ -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(); @@ -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+ @@ -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); } } } @@ -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); } } } @@ -297,8 +300,7 @@ public void didRangeBeaconsInRegion(Collection 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); } } } } diff --git a/app/src/main/java/com/samebits/beacon/locator/action/LocationAction.java b/app/src/main/java/com/samebits/beacon/locator/action/LocationAction.java index ac0b91a..34099e3 100644 --- a/app/src/main/java/com/samebits/beacon/locator/action/LocationAction.java +++ b/app/src/main/java/com/samebits/beacon/locator/action/LocationAction.java @@ -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; @@ -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); } diff --git a/app/src/main/java/com/samebits/beacon/locator/action/NoneAction.java b/app/src/main/java/com/samebits/beacon/locator/action/NoneAction.java index 912d20c..7638fdd 100644 --- a/app/src/main/java/com/samebits/beacon/locator/action/NoneAction.java +++ b/app/src/main/java/com/samebits/beacon/locator/action/NoneAction.java @@ -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; @@ -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; @@ -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; @@ -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); } } diff --git a/app/src/main/java/com/samebits/beacon/locator/receiver/BeaconRegionReceiver.java b/app/src/main/java/com/samebits/beacon/locator/receiver/BeaconRegionReceiver.java index 33bdb1d..fad1c82 100644 --- a/app/src/main/java/com/samebits/beacon/locator/receiver/BeaconRegionReceiver.java +++ b/app/src/main/java/com/samebits/beacon/locator/receiver/BeaconRegionReceiver.java @@ -75,5 +75,4 @@ public void onReceive(Context context, Intent intent) { } } } - } diff --git a/app/src/main/java/com/samebits/beacon/locator/ui/activity/MainNavigationActivity.java b/app/src/main/java/com/samebits/beacon/locator/ui/activity/MainNavigationActivity.java index 9e7ef1c..8a10ab8 100644 --- a/app/src/main/java/com/samebits/beacon/locator/ui/activity/MainNavigationActivity.java +++ b/app/src/main/java/com/samebits/beacon/locator/ui/activity/MainNavigationActivity.java @@ -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; @@ -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; @@ -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; @@ -83,6 +88,9 @@ public class MainNavigationActivity extends BaseActivity BeaconManager mBeaconManager; + BeaconRegionReceiver mRegionReceiver; + BeaconAlertReceiver mAlertReceiver; + LocationReceiver mLocationReceiver; TrackedBeacon mSelectedBeacon; private Unbinder unbinder; @@ -123,6 +131,8 @@ protected void onCreate(Bundle savedInstanceState) { readExtras(); + registerReceivers(); + if (null == savedInstanceState) { if (mSelectedBeacon != null) { launchTrackedListView(); @@ -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(); } diff --git a/app/src/main/java/com/samebits/beacon/locator/ui/fragment/ScanFragment.java b/app/src/main/java/com/samebits/beacon/locator/ui/fragment/ScanFragment.java index c9c86ae..64aab55 100644 --- a/app/src/main/java/com/samebits/beacon/locator/ui/fragment/ScanFragment.java +++ b/app/src/main/java/com/samebits/beacon/locator/ui/fragment/ScanFragment.java @@ -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); } } diff --git a/app/src/main/java/com/samebits/beacon/locator/util/NotificationBuilder.java b/app/src/main/java/com/samebits/beacon/locator/util/NotificationBuilder.java index 27ea005..1ec6cff 100644 --- a/app/src/main/java/com/samebits/beacon/locator/util/NotificationBuilder.java +++ b/app/src/main/java/com/samebits/beacon/locator/util/NotificationBuilder.java @@ -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); diff --git a/build.gradle b/build.gradle index e691196..e390824 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 66e4ab6..bce060e 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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