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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ findbugs {

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"

defaultConfig {
applicationId "protect.babymonitor"
Expand All @@ -22,6 +21,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}

dependencies {
compile "com.android.support:support-compat:25.4.0"
}
}

task findbugs(type: FindBugs, dependsOn: 'assembleDebug') {
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/java/protect/babymonitor/ListenActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;

public class ListenActivity extends Activity
{
Expand All @@ -37,6 +40,8 @@ public class ListenActivity extends Activity
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.

NotificationManagerCompat _mNotifyMgr;

Thread _listenThread;
private void streamAudio(final Socket socket) throws IllegalArgumentException, IllegalStateException, IOException
Expand Down Expand Up @@ -93,6 +98,9 @@ protected void onCreate(Bundle savedInstanceState)
_address = b.getString("address");
_port = b.getInt("port");
_name = b.getString("name");
// Gets an instance of the NotificationManager service
_mNotifyMgr =
NotificationManagerCompat.from(_mParentContext);

setContentView(R.layout.activity_listen);

Expand All @@ -101,6 +109,17 @@ protected void onCreate(Bundle savedInstanceState)
@Override
public void run()
{
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.

.setContentText("Listener is running");


// 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.

_mNotifyMgr.notify(mNotificationId, mBuilder.build());

final TextView connectedText = (TextView) findViewById(R.id.connectedTo);
connectedText.setText(_name);

Expand Down Expand Up @@ -146,6 +165,14 @@ public void run()

final TextView statusText = (TextView) findViewById(R.id.textStatus);
statusText.setText(R.string.disconnected);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(_mParentContext)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Baby Monitor Listener")
.setContentText("Disconnected!");
// Sets an ID for the notification
int mNotificationId = 1;
_mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
});
}
Expand Down