Skip to content

Instantly share code, notes, and snippets.

@Piasy
Last active October 1, 2015 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Piasy/2bf7d73456c6652db079 to your computer and use it in GitHub Desktop.
Save Piasy/2bf7d73456c6652db079 to your computer and use it in GitHub Desktop.
App launch accelerate source code
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mFragmentManager = getSupportFragmentManager();
mFragmentManager.beginTransaction()
.add(android.R.id.content, new SplashFragment(), SPLASH_FRAGMENT)
.commit();
Observable.create(subscriber -> {
Timber.plant(new Timber.DebugTree());
Fresco.initialize(mApp);
// simulate heavy library initialization
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
Timber.e(Constants.ERROR_LOG_FORMAT, TAG, e);
}
subscriber.onNext(true);
subscriber.onCompleted();
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(success -> {
mFragmentManager.beginTransaction()
.remove(mFragmentManager.findFragmentByTag(SPLASH_FRAGMENT))
.add(android.R.id.content, new GithubSearchFragment(), GITHUB_SEARCH_FRAGMENT)
.commit();
});
}
public class SplashActivity extends BaseActivity implements HasComponent<SplashComponent> {
private static final String SPLASH_FRAGMENT = "SplashFragment";
private static final String GITHUB_SEARCH_FRAGMENT = "GithubSearchFragment";
private static final String RELEASE = "release";
private static final int TIME = 10000;
private static final String TAG = "SplashActivity";
@Inject
TemplateApp mApp;
private SplashComponent mSplashComponent;
private FragmentManager mFragmentManager;
private EasyFlow<StatefulContext> mFlow;
private final StatefulContext mStatefulContext = new StatefulContext();
private boolean mIsPaused;
@Override
protected void initializeInjector() {
mSplashComponent = TemplateApp.get(this)
.visitorComponent()
.plus(getActivityModule(), new SplashModule());
mSplashComponent.inject(this);
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mFragmentManager = getSupportFragmentManager();
mFragmentManager.beginTransaction()
.add(android.R.id.content, new SplashFragment(), SPLASH_FRAGMENT)
.commit();
initFlow();
mFlow/*.trace()*/.start(mStatefulContext);
}
private void initFlow() {
mFlow = FlowBuilder.from(State.Start)
.transit(on(Event.Initialize).to(State.Initializing)
.transit(on(Event.Finish).finish(State.Transaction),
on(Event.Pause).to(State.Wait4InitializedAndResume)
.transit(on(Event.Resume).to(State.Initializing),
on(Event.Finish).to(State.Wait4Resume)
.transit(on(Event.Resume).finish(
State.Transaction)))))
.executor(new UiThreadExecutor());
mFlow.whenEnter(State.Start, context -> {
context.setState(State.Start);
Observable.create(subscriber -> {
Timber.plant(new Timber.DebugTree());
try {
mFlow.trigger(Event.Initialize, mStatefulContext);
} catch (LogicViolationError logicViolationError) {
Timber.e(Constants.ERROR_LOG_FORMAT, TAG, logicViolationError);
}
Fresco.initialize(mApp);
// simulate heavy library initialization
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
Timber.e(Constants.ERROR_LOG_FORMAT, TAG, e);
}
subscriber.onNext(true);
subscriber.onCompleted();
}).subscribeOn(Schedulers.io()).subscribe(success -> {
try {
mFlow.trigger(Event.Finish, mStatefulContext);
} catch (LogicViolationError logicViolationError) {
Timber.e(Constants.ERROR_LOG_FORMAT, TAG, logicViolationError);
}
});
}).whenEnter(State.Transaction, context -> {
context.setState(State.Transaction);
mFragmentManager.beginTransaction()
.remove(mFragmentManager.findFragmentByTag(SPLASH_FRAGMENT))
.add(android.R.id.content, new GithubSearchFragment(), GITHUB_SEARCH_FRAGMENT)
.commit();
});
}
@Override
protected void onResumeFragments() {
super.onResumeFragments();
if (mIsPaused) {
try {
mFlow.trigger(Event.Resume, mStatefulContext);
} catch (LogicViolationError logicViolationError) {
Timber.e(Constants.ERROR_LOG_FORMAT, TAG, logicViolationError);
}
}
}
@Override
protected void onPause() {
super.onPause();
mIsPaused = true;
try {
mFlow.trigger(Event.Pause, mStatefulContext);
} catch (LogicViolationError logicViolationError) {
Timber.e(Constants.ERROR_LOG_FORMAT, TAG, logicViolationError);
}
}
@Override
public SplashComponent getComponent() {
return mSplashComponent;
}
/**
* Init state enum.
* TODO modify EasyFlow to avoid enum.
*/
enum State implements StateEnum {
Start, Initializing, Wait4InitializedAndResume, Wait4Resume, Transaction
}
/**
* Init event enum.
* TODO modify EasyFlow to avoid enum.
*/
enum Event implements EventEnum {
Initialize, Pause, Resume, Finish
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment