Skip to content

Instantly share code, notes, and snippets.

@philipesteiff
Last active September 10, 2015 16:45
Show Gist options
  • Save philipesteiff/2b9e057243e9d62e47fb to your computer and use it in GitHub Desktop.
Save philipesteiff/2b9e057243e9d62e47fb to your computer and use it in GitHub Desktop.
LocalBroadcast um pouco mais prático
public interface Command<T extends Parcelable> {
void execute(Context context, T data);
}
public final class DataBroadcastReceiver<T extends Parcelable> extends BroadcastReceiver {
private final Command<T> command;
public DataBroadcastReceiver(Command<T> command) {
this.command = command;
}
@Override
public void onReceive(Context context, Intent intent) {
T payload = getPayload(intent);
this.command.execute(context, payload);
}
public static <T extends Parcelable> DataBroadcastReceiver<T> newDataBroadcastReceiver(Command<T> command) {
return new DataBroadcastReceiver<T>(command);
}
}
public final class IntBroadcastReceiver extends BroadcastReceiver {
private final IntCommand command;
public IntBroadcastReceiver(IntCommand command) {
this.command = command;
}
@Override
public void onReceive(Context context, Intent intent) {
this.command.execute(context, getPayloadAsInt(intent));
}
public static IntBroadcastReceiver newIntBroadcastReceiver(IntCommand command) {
return new IntBroadcastReceiver(command);
}
}
public interface IntCommand {
void execute(Context context, int data);
}
public final class ListBroadcastReceiver<T extends Parcelable> extends BroadcastReceiver {
private final ListCommand<T> command;
public ListBroadcastReceiver(ListCommand<T> command) {
this.command = command;
}
@Override
public void onReceive(Context context, Intent intent) {
List<T> list = getPayloadAsList(intent);
this.command.execute(context, list);
}
public static <T extends Parcelable> ListBroadcastReceiver<T> newListBroadcastReceiver(ListCommand<T> command) {
return new ListBroadcastReceiver<T>(command);
}
}
public interface ListCommand<T extends Parcelable> {
void execute(Context context, List<T> list);
}
public class MyLocalBroadcastManager {
public static final String LIST = "list";
public static final String DATA = "data";
public static final int NO_DATA = -1;
private final MyLocalBroadcastManager localBroadcastManager;
public LocalBroadcastManager(final Application application) {
localBroadcastManager = LocalBroadcastManager.getInstance(application);
}
public enum Action {
ERROR(String.class, "error"),
// .
// .
// .
// Action list
private Class<?> clazz;
private final String action;
private Action(final Class<?> clazz, final String action) {
this.clazz = clazz;
this.action = action;
}
public Class<?> getClazz() {
return clazz;
}
public String getAction() {
return action;
}
public static Action valueOf(final Class<? extends Parcelable> clazz) {
if (clazz == null) {
throw new IllegalArgumentException("Valor desconhecido " + clazz);
}
for (final Action action : values()) {
if (action.getClazz().equals(clazz)) {
return action;
}
}
throw new IllegalArgumentException("Valor desconhecido " + clazz);
}
}
/**
* Registers a {@link BroadcastReceiver} to a given <b>action</b>.
*
* @param broadcastReceiver
* @param action
*/
public void registerLocalBroadcastReceiver(final BroadcastReceiver broadcastReceiver, final Action action) {
localBroadcastManager.registerReceiver(broadcastReceiver, new IntentFilter(action.getAction()));
}
/**
* Unregisters a {@link BroadcastReceiver} from the
* {@link LocalBroadcastManager}.
*
* @param broadcastReceiver
*/
public void unregisterLocalBroadcastReceiver(final BroadcastReceiver broadcastReceiver) {
localBroadcastManager.unregisterReceiver(broadcastReceiver);
}
/**
* Sends a broadcast {@link Intent}
* {@link BroadcastReceiver}s registered to <b>action</b>.
*
* @param action
*/
public <T extends Parcelable> void sendLocalBroadcast(final Action action) {
final Intent intent = new Intent(action.getAction());
localBroadcastManager.sendBroadcast(intent);
}
/**
* Sends a broadcast {@link Intent} containing a <b>data</b> to all
* {@link BroadcastReceiver}s registered to <b>action</b>.
*
* @param action
* @param data
*/
public <T extends Parcelable> void sendLocalBroadcast(final Action action, final List<T> data) {
final Intent intent = new Intent(action.getAction());
intent.putParcelableArrayListExtra(LIST, (ArrayList<T>) data);
localBroadcastManager.sendBroadcast(intent);
}
/**
* Sends a broadcast {@link Intent} containing a <b>data</b> to all
* {@link BroadcastReceiver}s registered to <b>action</b>.
*
* @param action
* @param data
*/
public <T extends Parcelable> void sendLocalBroadcast(final Action action, final T data) {
final Intent intent = new Intent(action.getAction());
intent.putExtra(DATA, data);
localBroadcastManager.sendBroadcast(intent);
}
/**
* Sends a broadcast {@link Intent} containing a <b>data</b> to all
* {@link BroadcastReceiver}s registered to <b>action</b>.
*
* @param action
* @param data
*/
public void sendLocalBroadcast(final Action action, final String data) {
final Intent intent = new Intent(action.getAction());
intent.putExtra(DATA, data);
localBroadcastManager.sendBroadcast(intent);
}
/**
* Sends a broadcast {@link Intent} containing a <b>data</b> to all
* {@link BroadcastReceiver}s registered to <b>action</b>.
*
* @param action
* @param data
*/
public void sendLocalBroadcast(final Action action, final int data) {
final Intent intent = new Intent(action.getAction());
intent.putExtra(DATA, data);
localBroadcastManager.sendBroadcast(intent);
}
/**
* Sends a broadcast {@link Intent} containing a <b>data</b> to all
* {@link BroadcastReceiver}s registered to <b>action</b>.
*
* @param action
* @param data
*/
public void sendLocalBroadcast(final Action action, final boolean data) {
final Intent intent = new Intent(action.getAction());
intent.putExtra(DATA, data);
localBroadcastManager.sendBroadcast(intent);
}
public class SampleActivity extends Activity {
MyLocalBroadcastManager myLocalBroadcastManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
buttonX.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myLocalBroadcastManager.sendLocalBroadcast(Action.ERROR, "Deu merlin");
}
});
}
@Override
protected void onResume() {
super.onResume();
myLocalBroadcastManager.registerLocalBroadcastReceiver(errorReceiver, MyLocalBroadcastManager.Action.ERROR);
}
@Override
protected void onPause() {
super.onPause();
myLocalBroadcastManager.unregisterLocalBroadcastReceiver(errorReceiver);
}
private final BroadcastReceiver errorReceiver = newDataBroadcastReceiver(new StringCommand() {
@Override
public void execute(final Context context, final String message) {
// Do your stuff
}
});
}
public final class StringBroadcastReceiver extends BroadcastReceiver {
private final StringCommand command;
public StringBroadcastReceiver(StringCommand command) {
this.command = command;
}
@Override
public void onReceive(Context context, Intent intent) {
this.command.execute(context, getPayloadAsString(intent));
}
public static StringBroadcastReceiver newStringBroadcastReceiver(StringCommand command) {
return new StringBroadcastReceiver(command);
}
}
public interface StringCommand {
void execute(Context context, String data);
}
public class VoidBroadcastReceiver extends BroadcastReceiver {
private final VoidCommand command;
public VoidBroadcastReceiver(final VoidCommand command) {
this.command = command;
}
@Override
public void onReceive(final Context context, final Intent intent) {
this.command.execute(context);
}
public static VoidBroadcastReceiver newVoidBroadcastReceiver(final VoidCommand command) {
return new VoidBroadcastReceiver(command);
}
}
public interface VoidCommand {
void execute(Context context);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment