This repository has been archived by the owner on Dec 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
beworker
committed
Nov 5, 2014
1 parent
34400b3
commit e096d5d
Showing
5 changed files
with
56 additions
and
438 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { } | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 0 additions & 114 deletions
114
tinybus/src/com/halfbit/tinybus/events/BatteryEvents.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.