Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Persistent Notification #26

Merged
merged 6 commits into from
Dec 25, 2017
Merged

Conversation

shacharr
Copy link
Contributor

Show a notification when the app is connected and when connection is dropped.

Pull request also refreshes the gradle version and fixes a minor leak

Fixing issue #18

shacharr and others added 4 commits December 24, 2017 11:15
Add google maven repository, to allow importing compatibility libs
TODO: Change icon color/details for connected vs. disconnected status
Update the SDK version to match the version in gradle.
@@ -174,7 +174,7 @@ public void run()
{
final TextView addressText = (TextView) findViewById(R.id.address);

final WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain or provide a link which describes this type of leak? Is the handle to the WifiManager, coming from the Activity's context, causing the WifiManager to not be garbage collected?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue was spotted by Android studio. The following link seems to explain it for another project:
pwittchen/NetworkEvents#106

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, OK. From this bug report against Android 5.1 and below it is possible for a WifiManager to not be garbage collected based on a bug in WifiManager itself. I think that getting an instance via the application context just limits the maximum number of leaked objects to 1, as the application context is fixed for the life of the entire app.

As this app supports down to Android 4.1, it is fine to use this workaround to avoid the bug in WifiManager. Can you add a comment, reminding why using the application context is important?

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(_mParentContext)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Baby Monitor Listener")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these strings be added to the strings.xml file instead? That way others can provide translations for other languages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, hijacked the existing application name and connection status string. Will refresh the pull-request with the fixes.



// Sets an ID for the notification
int mNotificationId = 1;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not make this ID a final and static class member, as it is the same for both notifications. That way it is not a magic number.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -37,6 +40,8 @@
String _address;
int _port;
String _name;
Context _mParentContext = this;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for capturing the context here? For the lifetime of the Activity the context will not change. Is it to allow the Runnables to access the context? Why not use ListenActivity.this instead:

NotificationCompat.Builder mBuilder =
                         new NotificationCompat.Builder(ListenActivity.this)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that is cleaner way for doing this. Updated the code to this.

@brarcher brarcher merged commit e4debe7 into brarcher:master Dec 25, 2017
@brarcher
Copy link
Owner

I took a look at the notifications, and it looks good. Thanks for the improvement!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants