Add lifecycle observers for future mixin structures.

- Converted VisibilityLoggerMixin into a LifecyclerObservable so we
  don't have to call logger.onResume/onPause manually in most fragments.
- Observable will be useful when we provide logics across all
  fragment/activity, eg log lifecycle event latencies.
- Also added new tests for lifecycle component.

Bug: 30681529
Test: RunSettingsRoboTests

Change-Id: Ida39300aeb42f71b2e0bbfaebd0c51dc468cb5e8
This commit is contained in:
Fan Zhang
2016-08-22 15:20:22 -07:00
parent 141c20c5e3
commit ea024155fb
19 changed files with 583 additions and 53 deletions

View File

@@ -16,27 +16,18 @@
package com.android.settings.core;
import android.app.Activity;
import com.android.settings.core.instrumentation.Instrumentable;
import com.android.settings.core.instrumentation.VisibilityLoggerMixin;
import com.android.settings.core.lifecycle.ObservableActivity;
/**
* Instrumented activity that logs visibility state.
*/
public abstract class InstrumentedActivity extends Activity implements Instrumentable {
public abstract class InstrumentedActivity extends ObservableActivity implements Instrumentable {
private final VisibilityLoggerMixin mVisibilityLoggerMixin = new VisibilityLoggerMixin(this);
@Override
public void onResume() {
super.onResume();
mVisibilityLoggerMixin.onResume(this);
public InstrumentedActivity() {
// Mixin that logs visibility change for activity.
getLifecycle().addObserver(new VisibilityLoggerMixin(getMetricsCategory()));
}
@Override
public void onPause() {
super.onPause();
mVisibilityLoggerMixin.onPause(this);
}
}

View File

@@ -17,31 +17,23 @@
package com.android.settings.core;
import android.os.Bundle;
import android.support.v14.preference.PreferenceFragment;
import com.android.settings.core.instrumentation.VisibilityLoggerMixin;
import com.android.settings.core.instrumentation.Instrumentable;
import com.android.settings.core.instrumentation.VisibilityLoggerMixin;
import com.android.settings.core.lifecycle.ObservablePreferenceFragment;
/**
* Instrumented fragment that logs visibility state.
*/
public abstract class InstrumentedFragment extends PreferenceFragment implements Instrumentable {
public abstract class InstrumentedFragment extends ObservablePreferenceFragment
implements Instrumentable {
private final VisibilityLoggerMixin mVisibilityLoggerMixin = new VisibilityLoggerMixin(this);
public InstrumentedFragment() {
// Mixin that logs visibility change for activity.
getLifecycle().addObserver(new VisibilityLoggerMixin(getMetricsCategory()));
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
}
@Override
public void onResume() {
super.onResume();
mVisibilityLoggerMixin.onResume(getActivity());
}
@Override
public void onPause() {
super.onPause();
mVisibilityLoggerMixin.onPause(getActivity());
}
}

View File

@@ -16,30 +16,34 @@
package com.android.settings.core.instrumentation;
import android.content.Context;
import com.android.settings.core.lifecycle.LifecycleObserver;
import com.android.settings.core.lifecycle.events.OnPause;
import com.android.settings.core.lifecycle.events.OnResume;
/**
* Logs visibility change of a fragment.
*/
public class VisibilityLoggerMixin {
public class VisibilityLoggerMixin implements LifecycleObserver, OnResume, OnPause {
private final Instrumentable mInstrumentable;
private final int mMetricsCategory;
private final LogWriter mLogWriter;
public VisibilityLoggerMixin(Instrumentable instrumentable) {
this(instrumentable, MetricsFactory.get().getLogger());
public VisibilityLoggerMixin(int metricsCategory) {
this(metricsCategory, MetricsFactory.get().getLogger());
}
public VisibilityLoggerMixin(Instrumentable instrumentable, LogWriter logWriter) {
mInstrumentable = instrumentable;
public VisibilityLoggerMixin(int metricsCategory, LogWriter logWriter) {
mMetricsCategory = metricsCategory;
mLogWriter = logWriter;
}
public void onResume(Context context) {
mLogWriter.visible(context, mInstrumentable.getMetricsCategory());
@Override
public void onResume() {
mLogWriter.visible(null /* context */, mMetricsCategory);
}
public void onPause(Context context) {
mLogWriter.hidden(context, mInstrumentable.getMetricsCategory());
@Override
public void onPause() {
mLogWriter.hidden(null /* context */, mMetricsCategory);
}
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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.android.settings.core.lifecycle;
import android.annotation.UiThread;
import com.android.settings.core.lifecycle.events.OnDestroy;
import com.android.settings.core.lifecycle.events.OnPause;
import com.android.settings.core.lifecycle.events.OnResume;
import com.android.settings.core.lifecycle.events.OnStart;
import com.android.settings.core.lifecycle.events.OnStop;
import com.android.settings.utils.ThreadUtils;
import java.util.ArrayList;
import java.util.List;
/**
* Dispatcher for lifecycle events.
*/
public class Lifecycle {
protected final List<LifecycleObserver> mObservers = new ArrayList<>();
/**
* Registers a new observer of lifecycle events.
*/
@UiThread
public <T extends LifecycleObserver> T addObserver(T observer) {
ThreadUtils.ensureMainThread();
mObservers.add(observer);
return observer;
}
public void onStart() {
for (LifecycleObserver observer : mObservers) {
if (observer instanceof OnStart) {
((OnStart) observer).onStart();
}
}
}
public void onResume() {
for (LifecycleObserver observer : mObservers) {
if (observer instanceof OnResume) {
((OnResume) observer).onResume();
}
}
}
public void onPause() {
for (LifecycleObserver observer : mObservers) {
if (observer instanceof OnPause) {
((OnPause) observer).onPause();
}
}
}
public void onStop() {
for (LifecycleObserver observer : mObservers) {
if (observer instanceof OnStop) {
((OnStop) observer).onStop();
}
}
}
public void onDestroy() {
for (LifecycleObserver observer : mObservers) {
if (observer instanceof OnDestroy) {
((OnDestroy) observer).onDestroy();
}
}
}
}

View File

@@ -0,0 +1,22 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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.android.settings.core.lifecycle;
/**
* Observer of lifecycle events.
*/
public interface LifecycleObserver {
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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.android.settings.core.lifecycle;
import android.app.Activity;
/**
* {@link Activity} that has hooks to observe activity lifecycle events.
*/
public class ObservableActivity extends Activity {
private final Lifecycle mLifecycle = new Lifecycle();
protected Lifecycle getLifecycle() {
return mLifecycle;
}
@Override
protected void onStart() {
mLifecycle.onStart();
super.onStart();
}
@Override
protected void onResume() {
mLifecycle.onResume();
super.onResume();
}
@Override
protected void onPause() {
mLifecycle.onPause();
super.onPause();
}
@Override
protected void onStop() {
mLifecycle.onStop();
super.onStop();
}
@Override
protected void onDestroy() {
mLifecycle.onDestroy();
super.onDestroy();
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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.android.settings.core.lifecycle;
import android.annotation.CallSuper;
import android.support.v14.preference.PreferenceFragment;
/**
* {@link PreferenceFragment} that has hooks to observe fragment lifecycle events.
*/
public abstract class ObservablePreferenceFragment extends PreferenceFragment {
private final Lifecycle mLifecycle = new Lifecycle();
protected Lifecycle getLifecycle() {
return mLifecycle;
}
@CallSuper
@Override
public void onStart() {
mLifecycle.onStart();
super.onStart();
}
@CallSuper
@Override
public void onResume() {
mLifecycle.onResume();
super.onResume();
}
@CallSuper
@Override
public void onPause() {
mLifecycle.onPause();
super.onPause();
}
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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.android.settings.core.lifecycle.events;
public interface OnDestroy {
void onDestroy();
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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.android.settings.core.lifecycle.events;
public interface OnPause {
void onPause();
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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.android.settings.core.lifecycle.events;
public interface OnResume {
void onResume();
}

View File

@@ -0,0 +1,21 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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.android.settings.core.lifecycle.events;
public interface OnStart {
void onStart();
}

View File

@@ -0,0 +1,21 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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.android.settings.core.lifecycle.events;
public interface OnStop {
void onStop();
}