From c098ecaf8fb49860efde7b9375a0b3c7ae502345 Mon Sep 17 00:00:00 2001 From: Vitaliy Ryumshyn Date: Sun, 29 Jul 2018 13:07:39 +0200 Subject: [PATCH 1/5] fix TLM layout selection. Added experimental foreground scan for Android 8+ --- app/build.gradle | 36 +++++++-------- app/src/main/AndroidManifest.xml | 4 ++ .../beacon/locator/BeaconLocatorApp.java | 45 ++++++++++++++++--- .../locator/ui/activity/SettingsActivity.java | 2 + .../beacon/locator/util/PreferencesUtil.java | 12 +++++ app/src/main/res/values/strings.xml | 3 ++ app/src/main/res/xml/pref_scan.xml | 8 +++- build.gradle | 2 +- 8 files changed, 87 insertions(+), 25 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 980b813..5d4b985 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,13 +2,13 @@ apply plugin: 'com.android.application' android { compileSdkVersion 27 - buildToolsVersion "27.0.3" + buildToolsVersion "28.0.1" defaultConfig { applicationId 'com.samebits.beacon.locator' minSdkVersion 18 - targetSdkVersion 22 - versionCode 119 - versionName '1.1.9' + targetSdkVersion 26 + versionCode 120 + versionName '1.2.0' testApplicationId 'com.samebits.beacon.locator.test' } buildTypes { @@ -51,21 +51,21 @@ android { dependencies { final SUPPORT_LIBRARY_VERSION = '27.1.0' final DAGGER_VERSION = '2.11' - final ALTBEACON_VERSION = '2.13.1' - compile fileTree(include: ['*.jar'], dir: 'libs') - compile "com.android.support:support-v4:$SUPPORT_LIBRARY_VERSION" - compile "com.android.support:support-v13:$SUPPORT_LIBRARY_VERSION" - compile "com.android.support:appcompat-v7:$SUPPORT_LIBRARY_VERSION" - compile "com.android.support:preference-v7:$SUPPORT_LIBRARY_VERSION" - compile "com.android.support:cardview-v7:$SUPPORT_LIBRARY_VERSION" - compile "com.android.support:design:$SUPPORT_LIBRARY_VERSION" - compile "com.android.support:recyclerview-v7:$SUPPORT_LIBRARY_VERSION" - compile "com.android.support:support-annotations:$SUPPORT_LIBRARY_VERSION" - compile "org.altbeacon:android-beacon-library:$ALTBEACON_VERSION" - compile "com.google.dagger:dagger:$DAGGER_VERSION" + implementation fileTree(include: ['*.jar'], dir: 'libs') + api 'org.altbeacon:android-beacon-library:2.15.1' + 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" + implementation "com.android.support:preference-v7:$SUPPORT_LIBRARY_VERSION" + implementation "com.android.support:cardview-v7:$SUPPORT_LIBRARY_VERSION" + implementation "com.android.support:design:$SUPPORT_LIBRARY_VERSION" + implementation "com.android.support:recyclerview-v7:$SUPPORT_LIBRARY_VERSION" + implementation "com.android.support:support-annotations:$SUPPORT_LIBRARY_VERSION" + implementation "com.google.dagger:dagger:$DAGGER_VERSION" annotationProcessor "com.google.dagger:dagger-compiler:$DAGGER_VERSION" - provided 'org.glassfish:javax.annotation:10.0-b28' - compile 'com.jakewharton:butterknife:8.8.0' + compileOnly 'org.glassfish:javax.annotation:10.0-b28' + implementation 'com.jakewharton:butterknife:8.8.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.0' + } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 318bf81..1720fb0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ @@ -53,6 +54,9 @@ + + + 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 519a0c4..5ef9146 100644 --- a/app/src/main/java/com/samebits/beacon/locator/BeaconLocatorApp.java +++ b/app/src/main/java/com/samebits/beacon/locator/BeaconLocatorApp.java @@ -19,10 +19,13 @@ package com.samebits.beacon.locator; import android.app.Application; +import android.app.Notification; +import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Parcelable; import android.os.RemoteException; +import android.preference.Preference; import android.support.annotation.NonNull; import android.util.Log; @@ -35,6 +38,7 @@ import com.samebits.beacon.locator.model.IManagedBeacon; import com.samebits.beacon.locator.model.RegionName; import com.samebits.beacon.locator.model.TrackedBeacon; +import com.samebits.beacon.locator.ui.activity.MainNavigationActivity; import com.samebits.beacon.locator.util.BeaconUtil; import com.samebits.beacon.locator.util.Constants; import com.samebits.beacon.locator.util.PreferencesUtil; @@ -88,6 +92,24 @@ public void onCreate() { mDataManager = BeaconLocatorApp.from(this).getComponent().dataManager(); initBeaconManager(); + + // the ability to continually scan for long periods of time in the background on Andorid 8+ + // in exchange for showing an icon at the top of the screen and a always-on notification to + // communicate to users that your app is using resources in the background. + + if (PreferencesUtil.isForegroundScan(this)) { + Notification.Builder builder = new Notification.Builder(this); + builder.setSmallIcon(R.mipmap.ic_launcher); + builder.setContentTitle(getText(R.string.text_scanning)); + Intent intent = new Intent(this, MainNavigationActivity.class); + PendingIntent pendingIntent = PendingIntent.getActivity( + this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT + ); + builder.setContentIntent(pendingIntent); + mBeaconManager.enableForegroundServiceScanning(builder.build(), 456); + } else { + mBeaconManager.disableForegroundServiceScanning(); + } enableBackgroundScan(PreferencesUtil.isBackgroundScan(this)); } @@ -101,6 +123,9 @@ private void initBeaconManager() { if (PreferencesUtil.isEddystoneLayoutURL(this)) { mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT)); } + if (PreferencesUtil.isEddystoneLayoutTLM(this)) { + mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT)); + } mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.ALTBEACON_LAYOUT)); //konkakt? @@ -110,12 +135,22 @@ private void initBeaconManager() { mBeaconManager.setBackgroundBetweenScanPeriod(PreferencesUtil.getBackgroundScanInterval(this)); - mBeaconManager.setBackgroundScanPeriod(10000L); // default is 10000L - mBeaconManager.setForegroundBetweenScanPeriod(0L); // default is 0L - mBeaconManager.setForegroundScanPeriod(1100L); // Default is 1100L - //mBeaconManager.setMaxTrackingAge(10000); - //mBeaconManager.setRegionExitPeriod(18000L); + if (PreferencesUtil.isForegroundScan(this)) { + + mBeaconManager.setEnableScheduledScanJobs(false); + mBeaconManager.setBackgroundBetweenScanPeriod(0L); + mBeaconManager.setBackgroundScanPeriod(1100L); + + } else { + mBeaconManager.setEnableScheduledScanJobs(true); + mBeaconManager.setBackgroundScanPeriod(10000L); // default is 10000L + mBeaconManager.setForegroundBetweenScanPeriod(0L); // default is 0L + mBeaconManager.setForegroundScanPeriod(1100L); // Default is 1100L + + //mBeaconManager.setMaxTrackingAge(10000); + //mBeaconManager.setRegionExitPeriod(18000L); + } /* RangedBeacon.setMaxTrackingAge() only controls the period of time ranged beacons will continue to be diff --git a/app/src/main/java/com/samebits/beacon/locator/ui/activity/SettingsActivity.java b/app/src/main/java/com/samebits/beacon/locator/ui/activity/SettingsActivity.java index f74e6fe..8774724 100644 --- a/app/src/main/java/com/samebits/beacon/locator/ui/activity/SettingsActivity.java +++ b/app/src/main/java/com/samebits/beacon/locator/ui/activity/SettingsActivity.java @@ -200,6 +200,8 @@ public void onCreate(Bundle savedInstanceState) { bindPreferenceSummaryToValue(findPreference("scan_default_region_text")); bindPreferenceSummaryToValue(findPreference("scan_manual_timeout_list")); bindPreferenceSummaryToValue(findPreference("scan_sorting_order_list")); + Preference fgScanPref = findPreference("scan_foreground_switch"); + fgScanPref.setEnabled(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O); } diff --git a/app/src/main/java/com/samebits/beacon/locator/util/PreferencesUtil.java b/app/src/main/java/com/samebits/beacon/locator/util/PreferencesUtil.java index 387a031..4671c23 100644 --- a/app/src/main/java/com/samebits/beacon/locator/util/PreferencesUtil.java +++ b/app/src/main/java/com/samebits/beacon/locator/util/PreferencesUtil.java @@ -20,6 +20,7 @@ import android.content.Context; import android.content.SharedPreferences; +import android.os.Build; import android.preference.PreferenceManager; @@ -60,6 +61,13 @@ public static boolean isBackgroundScan(Context context) { return getSharedPreferences(context).getBoolean("scan_background_switch", true); } + public static boolean isForegroundScan(Context context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + return getSharedPreferences(context).getBoolean("scan_foreground_switch", false); + } + return false; + } + public static int getBackgroundScanInterval(Context context) { return Integer.parseInt(getSharedPreferences(context).getString("scan_background_timeout_list", "120000")); } @@ -72,6 +80,10 @@ public static boolean isEddystoneLayoutURL(Context context) { return getSharedPreferences(context).getBoolean("scan_parser_layout_eddystone_url", true); } + public static boolean isEddystoneLayoutTLM(Context context) { + return getSharedPreferences(context).getBoolean("scan_parser_layout_eddystone_tlm", false); + } + public static int getSilentModeProfile(Context context) { return getSharedPreferences(context).getInt("silent_profile_mode", 2); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7bbe4f7..d5b02b6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -34,6 +34,9 @@ Enables to run scanning in background and trigger actions. + Enable foreground scan (Android 8+) + Foreground service to be able run scan more often than every 15 min (only Android 8+). + Background scan interval (only Android 4.X) Region name diff --git a/app/src/main/res/xml/pref_scan.xml b/app/src/main/res/xml/pref_scan.xml index 48fc5b4..04844ac 100644 --- a/app/src/main/res/xml/pref_scan.xml +++ b/app/src/main/res/xml/pref_scan.xml @@ -7,6 +7,12 @@ android:summary="@string/pref_description_background_scan" android:title="@string/pref_title_background_scan" /> + + diff --git a/build.gradle b/build.gradle index 72ab166..e691196 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { google() } dependencies { - classpath 'com.android.tools.build:gradle:3.1.0' + classpath 'com.android.tools.build:gradle:3.1.4' } } From 00c095f6d6b3589538a3acab4d37830ba6591e23 Mon Sep 17 00:00:00 2001 From: Vitaliy Ryumshyn Date: Sun, 29 Jul 2018 13:07:39 +0200 Subject: [PATCH 2/5] fix TLM layout selection. Added experimental foreground scan for Android 8+ --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index 5d4b985..5eaaef1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -49,7 +49,7 @@ android { } dependencies { - final SUPPORT_LIBRARY_VERSION = '27.1.0' + final SUPPORT_LIBRARY_VERSION = '27.1.1' final DAGGER_VERSION = '2.11' implementation fileTree(include: ['*.jar'], dir: 'libs') From 40fc8e2c7dc364299279818b1334bebb0f1539da Mon Sep 17 00:00:00 2001 From: Vitaliy Ryumshyn Date: Fri, 28 Sep 2018 21:40:27 +0200 Subject: [PATCH 3/5] fix notifications for API>26 --- app/build.gradle | 8 ++--- .../locator/receiver/BeaconAlertReceiver.java | 2 +- .../locator/receiver/LocationReceiver.java | 25 ++++++++++++--- .../locator/util/NotificationBuilder.java | 32 +++++++++++++++++-- 4 files changed, 55 insertions(+), 12 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 5eaaef1..ad04a8b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,13 +2,13 @@ apply plugin: 'com.android.application' android { compileSdkVersion 27 - buildToolsVersion "28.0.1" + buildToolsVersion "28.0.3" defaultConfig { applicationId 'com.samebits.beacon.locator' minSdkVersion 18 - targetSdkVersion 26 - versionCode 120 - versionName '1.2.0' + targetSdkVersion 27 + versionCode 121 + versionName '1.2.1' testApplicationId 'com.samebits.beacon.locator.test' } buildTypes { diff --git a/app/src/main/java/com/samebits/beacon/locator/receiver/BeaconAlertReceiver.java b/app/src/main/java/com/samebits/beacon/locator/receiver/BeaconAlertReceiver.java index 6530849..1fac1e7 100644 --- a/app/src/main/java/com/samebits/beacon/locator/receiver/BeaconAlertReceiver.java +++ b/app/src/main/java/com/samebits/beacon/locator/receiver/BeaconAlertReceiver.java @@ -49,7 +49,7 @@ private void createNotification(Context context, String title, String msgText, S PendingIntent notificationIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainNavigationActivity.class), 0); NotificationBuilder notificationBuilder = new NotificationBuilder(context); - notificationBuilder.createNotification(R.mipmap.ic_launcher, title, notificationIntent); + notificationBuilder.createNotification(R.mipmap.ic_launcher, title, isVibrate, notificationIntent); notificationBuilder.setMessage(msgText); notificationBuilder.setTicker(msgAlert); diff --git a/app/src/main/java/com/samebits/beacon/locator/receiver/LocationReceiver.java b/app/src/main/java/com/samebits/beacon/locator/receiver/LocationReceiver.java index fdf440f..ec89e00 100644 --- a/app/src/main/java/com/samebits/beacon/locator/receiver/LocationReceiver.java +++ b/app/src/main/java/com/samebits/beacon/locator/receiver/LocationReceiver.java @@ -18,16 +18,21 @@ package com.samebits.beacon.locator.receiver; +import android.Manifest; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; import android.location.Criteria; import android.location.Location; import android.location.LocationManager; import android.net.Uri; +import android.support.v4.app.ActivityCompat; +import android.util.Log; import com.samebits.beacon.locator.R; +import com.samebits.beacon.locator.util.Constants; import com.samebits.beacon.locator.util.NotificationBuilder; @@ -39,13 +44,19 @@ public class LocationReceiver extends BroadcastReceiver { private int retryCount = 0; @Override - public void onReceive(Context context, Intent intent) - { + public void onReceive(Context context, Intent intent) { retryCount = intent.getIntExtra("RETRY_COUNT", 0); Location bestLocation = null; final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); for (final String provider : locationManager.getAllProviders()) { if (provider != null) { + if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED + && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + + Log.w(Constants.TAG, "No permissions to use GPS "); + + return; + } final Location location = locationManager.getLastKnownLocation(provider); final long now = System.currentTimeMillis(); if (location != null @@ -64,7 +75,7 @@ public void onReceive(Context context, Intent intent) PendingIntent notificationIntent = PendingIntent.getActivity(context, 0, mapIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationBuilder notificationBuilder = new NotificationBuilder(context); - notificationBuilder.createNotification(R.mipmap.ic_launcher, context.getString(R.string.action_alarm_text_title), notificationIntent); + notificationBuilder.createNotification(R.mipmap.ic_launcher, context.getString(R.string.action_alarm_text_title), true, notificationIntent); notificationBuilder.setMessage(context.getString(R.string.notification_display_last_position)); notificationBuilder.show(1); @@ -77,9 +88,15 @@ public void onReceive(Context context, Intent intent) retryCount++; intent.putExtra("RETRY_COUNT", retryCount); final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); + if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED + && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + + Log.w(Constants.TAG, "No permissions to use GPS "); + + return; + } locationManager.requestSingleUpdate(criteria, pendingIntent); } } } - } 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 36f21ef..27ea005 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 @@ -19,6 +19,7 @@ package com.samebits.beacon.locator.util; import android.app.Notification; +import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; @@ -52,9 +53,34 @@ public NotificationBuilder(Context mContext) { /** * Creation of notification on operations completed */ - public NotificationBuilder createNotification(int smallIcon, String title, PendingIntent notifyIntent) { - mBuilder = new NotificationCompat.Builder(mContext).setSmallIcon(smallIcon).setContentTitle(title) - .setAutoCancel(true).setColor(ContextCompat.getColor(mContext, R.color.hn_orange)); + public NotificationBuilder createNotification(int smallIcon, String title, boolean isVibrate, PendingIntent notifyIntent) { + + NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); + String NOTIFICATION_CHANNEL_ID = "blocator_channel_01"; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, + title, NotificationManager.IMPORTANCE_HIGH); + + // Configure the notification channel. + notificationChannel.setDescription("Channel description"); + notificationChannel.enableLights(true); + notificationChannel.setLightColor(Color.RED); + if (isVibrate) { + notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000}); + notificationChannel.enableVibration(true); + } + notificationManager.createNotificationChannel(notificationChannel); + } + + mBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID); + + mBuilder.setAutoCancel(true) + .setDefaults(Notification.DEFAULT_ALL) + .setSmallIcon(smallIcon) + .setContentTitle(title) + .setColor(ContextCompat.getColor(mContext, R.color.hn_orange)); + if (notifyIntent != null) { mBuilder.setContentIntent(notifyIntent); } From 5db3cd09aa98ebf8bc11759c1f524c76f5033abf Mon Sep 17 00:00:00 2001 From: Vitaliy Ryumshyn Date: Sun, 28 Oct 2018 18:09:38 +0100 Subject: [PATCH 4/5] 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 From 0a756cf4a3e3c5c96b9fe59dcf3e066cf5d77202 Mon Sep 17 00:00:00 2001 From: Vitaliy Ryumshyn Date: Sun, 28 Oct 2018 18:27:00 +0100 Subject: [PATCH 5/5] set API 27 --- app/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 6a21bde..66cffe2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,12 +1,12 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 28 + compileSdkVersion 27 buildToolsVersion "28.0.3" defaultConfig { applicationId 'com.samebits.beacon.locator' minSdkVersion 18 - targetSdkVersion 28 + targetSdkVersion 27 versionCode 121 versionName '1.2.1' testApplicationId 'com.samebits.beacon.locator.test'