FairEmail/app/src/main/java/eu/faircode/email/TwoStateOwner.java

134 lines
4.5 KiB
Java
Raw Normal View History

2019-03-06 16:36:20 +00:00
package eu.faircode.email;
2019-05-04 20:49:22 +00:00
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2021-01-01 07:56:36 +00:00
Copyright 2018-2021 by Marcel Bokhorst (M66B)
2019-05-04 20:49:22 +00:00
*/
2019-03-06 16:36:20 +00:00
import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
2019-03-17 20:31:24 +00:00
import androidx.lifecycle.LifecycleObserver;
2019-03-06 16:36:20 +00:00
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;
2019-03-17 20:31:24 +00:00
import androidx.lifecycle.OnLifecycleEvent;
2019-03-06 16:36:20 +00:00
2020-11-12 07:07:30 +00:00
import java.util.Objects;
// This class can be used as an externally controlled standalone or child life cycle owner
2019-03-06 16:36:20 +00:00
public class TwoStateOwner implements LifecycleOwner {
2019-03-27 13:24:58 +00:00
private String name;
2020-11-12 07:07:30 +00:00
private Object condition;
2019-03-06 16:36:20 +00:00
private LifecycleRegistry registry;
2019-03-27 12:27:51 +00:00
// https://developer.android.com/topic/libraries/architecture/lifecycle#lc
2019-03-27 13:24:58 +00:00
TwoStateOwner(String aname) {
name = aname;
2019-04-13 06:02:01 +00:00
create();
2019-03-06 16:36:20 +00:00
}
2019-03-27 13:24:58 +00:00
TwoStateOwner(LifecycleOwner owner, String aname) {
this(aname);
// Destroy when parent destroyed
2019-03-17 20:31:24 +00:00
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() {
2019-12-07 16:02:42 +00:00
Log.d("LifeCycle " + name + " parent destroyed");
2019-03-25 19:20:23 +00:00
destroy();
2019-03-17 20:31:24 +00:00
}
});
}
2019-04-13 06:02:01 +00:00
private void create() {
// Initialize
registry = new LifecycleRegistry(this);
registry.addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
public void onAny() {
2019-12-17 09:08:42 +00:00
Log.d("LifeCycle " + name + " state=" + registry.getCurrentState() + " " + registry);
2019-04-13 06:02:01 +00:00
}
});
2020-03-12 12:40:07 +00:00
setState(Lifecycle.State.CREATED);
2019-04-13 06:02:01 +00:00
}
2019-03-06 16:36:20 +00:00
void start() {
2020-04-23 10:58:27 +00:00
Lifecycle.State state = registry.getCurrentState();
if (!state.equals(Lifecycle.State.STARTED) && !state.equals(Lifecycle.State.DESTROYED))
2020-03-12 12:40:07 +00:00
setState(Lifecycle.State.STARTED);
}
2019-03-06 16:36:20 +00:00
void stop() {
2020-04-23 10:58:27 +00:00
Lifecycle.State state = registry.getCurrentState();
if (!state.equals(Lifecycle.State.CREATED) && !state.equals(Lifecycle.State.DESTROYED))
2020-03-12 12:40:07 +00:00
setState(Lifecycle.State.CREATED);
2019-03-06 16:36:20 +00:00
}
2019-03-17 20:31:24 +00:00
void restart() {
2019-03-25 19:20:23 +00:00
stop();
2019-03-17 20:31:24 +00:00
start();
}
2019-04-13 06:02:01 +00:00
void recreate() {
destroy();
create();
}
2020-11-12 07:07:30 +00:00
void recreate(Object condition) {
if (!Objects.equals(this.condition, condition)) {
this.condition = condition;
recreate();
}
}
2019-03-25 19:20:23 +00:00
void destroy() {
Lifecycle.State state = registry.getCurrentState();
2020-07-26 19:33:12 +00:00
if (!state.equals(Lifecycle.State.DESTROYED)) {
if (!state.equals(Lifecycle.State.CREATED))
setState(Lifecycle.State.CREATED);
2020-03-12 12:40:07 +00:00
setState(Lifecycle.State.DESTROYED);
2020-07-26 19:33:12 +00:00
}
2019-03-30 15:09:06 +00:00
}
2019-03-06 16:36:20 +00:00
@NonNull
@Override
public Lifecycle getLifecycle() {
return registry;
}
2020-03-12 12:40:07 +00:00
private void setState(@NonNull Lifecycle.State state) {
try {
registry.setCurrentState(state);
} catch (Throwable ex) {
Log.e(ex);
/*
java.lang.RuntimeException: Failed to call observer method
at androidx.lifecycle.ClassesInfoCache$MethodReference.invokeCallback(SourceFile:226)
at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeMethodsForEvent(SourceFile:194)
at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeCallbacks(SourceFile:186)
at androidx.lifecycle.ReflectiveGenericLifecycleObserver.onStateChanged(SourceFile:37)
at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(SourceFile:361)
at androidx.lifecycle.LifecycleRegistry.backwardPass(SourceFile:316)
at androidx.lifecycle.LifecycleRegistry.sync(SourceFile:334)
at androidx.lifecycle.LifecycleRegistry.moveToState(SourceFile:145)
at androidx.lifecycle.LifecycleRegistry.setCurrentState(SourceFile:118)
*/
}
}
2019-03-06 16:36:20 +00:00
}