Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #272 from matthewsibigtroth/lockscreen_notificatio…
Browse files Browse the repository at this point in the history
…n_remix

Lockscreen notification remix
  • Loading branch information
matthewsibigtroth committed Jan 16, 2015
2 parents 190617c + 899cd6f commit 7dc9021
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
Expand Down Expand Up @@ -99,6 +100,9 @@ public void onScanFailed(int errorCode) {
private static final int NON_LOLLIPOP_NOTIFICATION_URL_COLOR = Color.parseColor("#999999");
private static final int NON_LOLLIPOP_NOTIFICATION_SNIPPET_COLOR = Color.parseColor("#999999");
private static final int NOTIFICATION_PRIORITY = NotificationCompat.PRIORITY_LOW;
private static final long NOTIFICATION_UPDATE_GATE_DURATION = 1000;
private boolean mCanUpdateNotifications = false;
private Handler mHandler;
private ScreenBroadcastReceiver mScreenStateBroadcastReceiver;
private RegionResolver mRegionResolver;
private NotificationManagerCompat mNotificationManager;
Expand Down Expand Up @@ -127,6 +131,14 @@ public int compare(String address, String otherAddress) {
return address.compareTo(otherAddress);
}
};
// TODO: consider a more elegant solution for preventing notification conflicts
private Runnable mNotificationUpdateGateTimeout = new Runnable() {
@Override
public void run() {
mCanUpdateNotifications = true;
updateNotifications();
}
};

public UriBeaconDiscoveryService() {
}
Expand All @@ -143,6 +155,7 @@ private static String generateMockBluetoothAddress(int hashCode) {
private void initialize() {
mNotificationManager = NotificationManagerCompat.from(this);
mMdnsUrlDiscoverer = new MdnsUrlDiscoverer(this, UriBeaconDiscoveryService.this);
mHandler = new Handler();
initializeScreenStateBroadcastReceiver();
}

Expand Down Expand Up @@ -182,6 +195,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
// Start scanning only if the screen is on
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (powerManager.isScreenOn()) {
mCanUpdateNotifications = false;
mHandler.postDelayed(mNotificationUpdateGateTimeout, NOTIFICATION_UPDATE_GATE_DURATION);
startSearchingForUriBeacons();
mMdnsUrlDiscoverer.startScanning();
}
Expand All @@ -199,6 +214,7 @@ public IBinder onBind(Intent intent) {
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy: service exiting");
mHandler.removeCallbacks(mNotificationUpdateGateTimeout);
stopSearchingForUriBeacons();
mMdnsUrlDiscoverer.stopScanning();
unregisterReceiver(mScreenStateBroadcastReceiver);
Expand Down Expand Up @@ -286,6 +302,10 @@ private void handleFoundDevice(ScanResult scanResult) {
* Create a new set of notifications or update those existing
*/
private void updateNotifications() {
if (!mCanUpdateNotifications) {
return;
}

mSortedDevices = getSortedBeaconsWithMetadata();

// If no beacons have been found
Expand Down Expand Up @@ -484,9 +504,12 @@ public void onReceive(Context context, Intent intent) {
initializeLists();
mNotificationManager.cancelAll();
if (isScreenOn) {
mCanUpdateNotifications = false;
mHandler.postDelayed(mNotificationUpdateGateTimeout, NOTIFICATION_UPDATE_GATE_DURATION);
startSearchingForUriBeacons();
mMdnsUrlDiscoverer.startScanning();
} else {
mHandler.removeCallbacks(mNotificationUpdateGateTimeout);
stopSearchingForUriBeacons();
mMdnsUrlDiscoverer.stopScanning();
}
Expand Down

0 comments on commit 7dc9021

Please sign in to comment.