blob: 8f0d160ed7b18b10c04f3a5327a06d2a636f254d [file] [log] [blame]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chromecast.shell;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import org.chromium.base.ContextUtils;
import org.chromium.chromecast.base.Scope;
/**
* Registers a BroadcastReceiver in the constructor, and unregisters it in the close() method.
*
* This can be used to react to Observables to properly control the lifetimes of BroadcastReceivers.
*/
public class LocalBroadcastReceiverScope implements Scope {
private final LocalBroadcastManager mBroadcastManager;
private final BroadcastReceiver mReceiver;
public LocalBroadcastReceiverScope(IntentFilter filter, IntentReceivedCallback callback) {
this(LocalBroadcastManager.getInstance(ContextUtils.getApplicationContext()), filter,
callback);
}
public LocalBroadcastReceiverScope(LocalBroadcastManager broadcastManager, IntentFilter filter,
IntentReceivedCallback callback) {
mBroadcastManager = broadcastManager;
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
callback.onReceive(intent);
}
};
mBroadcastManager.registerReceiver(mReceiver, filter);
}
@Override
public void close() {
mBroadcastManager.unregisterReceiver(mReceiver);
}
/**
* Functional interface to handle received Intents.
*/
public interface IntentReceivedCallback { public void onReceive(Intent intent); }
}