Skip to content

Instantly share code, notes, and snippets.

@pjhjohn
Created July 9, 2017 04:02
Show Gist options
  • Save pjhjohn/e5903b7847a3f402763d73f2ee3cb6ee to your computer and use it in GitHub Desktop.
Save pjhjohn/e5903b7847a3f402763d73f2ee3cb6ee to your computer and use it in GitHub Desktop.
package io.bitsound.apitestapp.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import java.util.Locale;
import io.bitsound.receiver.BitsoundContents;
import io.bitsound.receiver.common.Stringify;
import io.bitsound.smarton.BitsoundSmartOn;
import timber.log.Timber;
public class BitsoundSmartOnReceiver extends BroadcastReceiver {
private static BitsoundSmartOnReceiver instance = null;
private static Listener listener = null;
public static BitsoundSmartOnReceiver getInstance() {
if (null == instance) {
synchronized (BitsoundSmartOnReceiver.class) {
if (null == instance) instance = new BitsoundSmartOnReceiver();
}
}
Timber.d("[getInstance] __INSTANCE__%s__LISTENER__%s", instance, listener);
return instance;
}
@Override
public void onReceive(Context context, Intent intent) {
Timber.d("[onReceive] __INSTANCE__%s__LISTENER__%s__THIS__%s", instance, listener, this);
final int code = intent.getIntExtra(BitsoundSmartOn.EXTRA_BITSOUND_SMARTON_CONTENTS_RESULT_CODE, BitsoundContents.Result.SIGNAL_NOT_FOUND);
final BitsoundContents contents = BitsoundContents.fromBytes(intent.getByteArrayExtra(BitsoundSmartOn.EXTRA_BITSOUND_SMARTON_CONTENTS_BYTES));
Timber.d("---> %s %s", Stringify.contents(code), contents);
Toast.makeText(context, String.format(Locale.getDefault(), "%s : %d\n%s", intent, code, contents), Toast.LENGTH_SHORT).show();
// For Notification
if (contents != null) {
Timber.d("Invoke Listener");
if (listener != null) listener.invoke(contents);
else Timber.d("Listener is null");
} else Timber.d("Contents is null");
}
public void setListener(Listener listener) {
BitsoundSmartOnReceiver.listener = listener;
Timber.d("[setListener] __INSTANCE__%s__LISTENER__%s__THIS__%s", instance, listener, this);
}
public interface Listener {
void invoke(BitsoundContents contents);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment