Skip to content
This repository has been archived by the owner on Dec 25, 2019. It is now read-only.

Commit

Permalink
revert back to version 1
Browse files Browse the repository at this point in the history
  • Loading branch information
beworker committed Nov 5, 2014
1 parent 34400b3 commit e096d5d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 438 deletions.
111 changes: 30 additions & 81 deletions tinybus/src/com/halfbit/tinybus/BusDepot.java
Original file line number Diff line number Diff line change
@@ -1,85 +1,34 @@
/*
* Copyright (C) 2014 Sergej Shafarenka, halfbit.de
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.halfbit.tinybus;

import java.util.WeakHashMap;

import android.app.Activity;
import android.app.Application;
import android.app.Application.ActivityLifecycleCallbacks;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

class BusDepot implements ActivityLifecycleCallbacks {

private static final String TAG = BusDepot.class.getSimpleName();
private static final boolean DEBUG = true;

private static BusDepot INSTANCE;
/**
* Implement this interface to provide an instance of {@link com.halfbit.tinybus.Bus}.
*
* <p>If you want a global bus instance, then let {@link android.app.Application} to
* implement this interface. In case you want to have a separate bus per
* {@link android.app.Activity}, then implement this interface in your activity.
*
* @author Sergej Shafarenka
*/
public interface BusDepot {
/**
* @return instance of bus
*/
Bus getBus();

public static BusDepot get(Context context) {
if (INSTANCE == null) {
INSTANCE = new BusDepot(context);
}
return INSTANCE;
}

private final WeakHashMap<Context, TinyBus> mBuses;

public BusDepot(Context context) {
mBuses = new WeakHashMap<Context, TinyBus>();
final Application app = (Application) context.getApplicationContext();
app.registerActivityLifecycleCallbacks(this);
}

public TinyBus create(Context context) {
TinyBus bus = mBuses.get(context);
if (bus != null) {
throw new IllegalArgumentException("Bus has already been created with the context. "
+ "Use TinyBus.from(Context) method to access created bus instance. "
+ "Context: " + context);
}
bus = new TinyBus();
mBuses.put(context, bus);
return bus;
}

public TinyBus getBus(Context context) {
final TinyBus bus = mBuses.get(context);
if (bus == null) {
throw new IllegalArgumentException("Bus has not yet been created in the context. "
+ "Use TinyBus.create(Context) method inside Activity.onCreate() method "
+ "to create bus instance first. Context: " + context);
}
return bus;
}

@Override
public void onActivityStarted(Activity activity) {
TinyBus bus = mBuses.get(activity);
if (bus != null) {
bus.dispatchOnStart(activity);
}
if (DEBUG) Log.d(TAG, " #### STARTED, bus count: " + mBuses.size());
}

@Override
public void onActivityStopped(Activity activity) {
TinyBus bus = mBuses.get(activity);
if (bus != null) {
bus.dispatchOnStop(activity);
}
if (DEBUG) Log.d(TAG, " #### STOPPED, bus count: " + mBuses.size());
}

@Override
public void onActivityDestroyed(Activity activity) {
mBuses.remove(activity);
if (DEBUG) Log.d(TAG, " #### DESTROYED, bus count: " + mBuses.size());
}

@Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { }
@Override public void onActivityResumed(Activity activity) { }
@Override public void onActivityPaused(Activity activity) { }
@Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { }

}
12 changes: 0 additions & 12 deletions tinybus/src/com/halfbit/tinybus/Events.java

This file was deleted.

109 changes: 26 additions & 83 deletions tinybus/src/com/halfbit/tinybus/TinyBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,68 +15,53 @@
*/
package com.halfbit.tinybus;

import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;

import android.app.Activity;
import android.content.Context;
import android.os.Looper;

public class TinyBus implements Bus {

//-- static public methods

public static TinyBus create(Activity activity) {
if (activity == null) {
throw new NullPointerException("context must not be null");
}
return BusDepot.get(activity).create(activity);
}

public static TinyBus createAndAttach(Activity activity) {
if (activity == null) {
throw new NullPointerException("context must not be null");
}
final TinyBus bus = BusDepot.get(activity).create(activity);
bus.subscribeFor(new ObjectEvents(activity));
return bus;
}

public TinyBus attach(Object object) {
subscribeFor(new ObjectEvents(object));
return this;
}

public TinyBus subscribeFor(Events events) {
if (mEvents == null) {
mEvents = new ArrayList<Events>();
/**
* Use this method to get a bus instance available in current context. Do not forget to
* implement {@link com.halfbit.tinybus.BusDepot} in your activity or application to make
* this method working.
*
* @see BusDepot
*
* @param context
* @return event bus instance, never null
*/
public static Bus from(Context context) {
if (context instanceof BusDepot) {
return ((BusDepot)context).getBus();
} else {
context = context.getApplicationContext();
if (context instanceof BusDepot) {
return ((BusDepot)context).getBus();
}
}
mEvents.add(events);
events.bus = this;
return this;
throw new IllegalArgumentException("Make sure Activity or Application implements BusDepot interface.");
}


public static TinyBus from(Activity activity) {
return BusDepot.get(activity).getBus(activity);
}

//-- implementation

//-- static members

// set it to true, if you want the bus to check whether it is called from the main thread
private static final boolean ASSERT_ACCESS = false;

private static final int QUEUE_SIZE = 12;
private static final AccessAssertion MAIN_THREAD_CHECKER = new MainThreadAssertion();

// cached objects meta data
private static final HashMap<Class<?> /*receivers or producer*/, ObjectMeta>
OBJECTS_META = new HashMap<Class<?>, ObjectMeta>();

//-- fields

private final HashMap<Class<?>/*event class*/, HashSet<Object>/*multiple receiver objects*/>
mEventReceivers = new HashMap<Class<?>, HashSet<Object>>();

Expand All @@ -89,6 +74,8 @@ public static TinyBus from(Activity activity) {
private Task tail;
private boolean mProcessing;

//-- public api

public TinyBus() {
this(MAIN_THREAD_CHECKER);
}
Expand Down Expand Up @@ -512,48 +499,4 @@ public boolean release(T instance) {
}
}

//-- dynamic producers

private ArrayList<Events> mEvents;

void dispatchOnStart(Activity activity) {
if (mEvents != null) {
for (Events producer : mEvents) {
//register(producer);
producer.onStarted(activity);
}
}
}

void dispatchOnStop(Activity activity) {
if (mEvents != null) {
for (Events producer : mEvents) {
//unregister(producer);
producer.onStopped(activity);
}
}
}

static class ObjectEvents extends Events {

private final WeakReference<Object> mReference;

public ObjectEvents(Object object) {
mReference = new WeakReference<Object>(object);
}

@Override
protected void onStarted(Context context) {
final Object object = mReference.get();
if (object != null) bus.register(object);
}

@Override
protected void onStopped(Context context) {
final Object object = mReference.get();
if (object != null) bus.unregister(object);
}

}

}
114 changes: 0 additions & 114 deletions tinybus/src/com/halfbit/tinybus/events/BatteryEvents.java

This file was deleted.

Loading

0 comments on commit e096d5d

Please sign in to comment.