Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e1b23719 authored by Jason Monk's avatar Jason Monk Committed by android-build-merger
Browse files

Have QS fragment keep track of some state on recreate

am: 64b214ea

Change-Id: I6e9bb373a765e7ed04ee6fb055d5aec03ec7d505
parents a55b0217 64b214ea
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.app.Fragment;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.VisibleForTesting;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@@ -39,6 +40,8 @@ import com.android.systemui.statusbar.stack.StackStateAnimator;
public class QSFragment extends Fragment implements QS {
    private static final String TAG = "QS";
    private static final boolean DEBUG = false;
    private static final String EXTRA_EXPANDED = "expanded";
    private static final String EXTRA_LISTENING = "listening";

    private final Rect mQsBounds = new Rect();
    private boolean mQsExpanded;
@@ -85,6 +88,35 @@ public class QSFragment extends Fragment implements QS {

        mQSCustomizer = view.findViewById(R.id.qs_customize);
        mQSCustomizer.setQs(this);
        if (savedInstanceState != null) {
            setExpanded(savedInstanceState.getBoolean(EXTRA_EXPANDED));
            setListening(savedInstanceState.getBoolean(EXTRA_LISTENING));
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mListening) {
            setListening(false);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(EXTRA_EXPANDED, mQsExpanded);
        outState.putBoolean(EXTRA_LISTENING, mListening);
    }

    @VisibleForTesting
    boolean isListening() {
        return mListening;
    }

    @VisibleForTesting
    boolean isExpanded() {
        return mQsExpanded;
    }

    @Override
+23 −0
Original line number Diff line number Diff line
@@ -15,8 +15,11 @@
package com.android.systemui.qs;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

import android.app.FragmentController;
import android.app.FragmentManagerNonConfig;
import android.os.Looper;

import com.android.internal.logging.MetricsLogger;
@@ -24,6 +27,7 @@ import com.android.keyguard.CarrierText;
import com.android.systemui.Dependency;
import com.android.systemui.R;

import android.os.Parcelable;
import android.testing.AndroidTestingRunner;

import com.android.systemui.SysuiBaseFragmentTest;
@@ -90,4 +94,23 @@ public class QSFragmentTest extends SysuiBaseFragmentTest {
        host.destroy();
        processAllMessages();
    }

    @Test
    public void testSaveState() {
        QSFragment qs = (QSFragment) mFragment;

        mFragments.dispatchResume();
        processAllMessages();

        qs.setListening(true);
        qs.setExpanded(true);
        processAllMessages();
        recreateFragment();
        processAllMessages();

        // Get the reference to the new fragment.
        qs = (QSFragment) mFragment;
        assertTrue(qs.isListening());
        assertTrue(qs.isExpanded());
    }
}
+13 −8
Original line number Diff line number Diff line
@@ -133,14 +133,7 @@ public abstract class BaseFragmentTest {
    public void testRecreate() {
        mFragments.dispatchResume();
        processAllMessages();
        mFragments.dispatchPause();
        Parcelable p = mFragments.saveAllState();
        mFragments.dispatchDestroy();

        mFragments = FragmentController.createController(new HostCallbacks());
        mFragments.attachHost(null);
        mFragments.restoreAllState(p, (FragmentManagerNonConfig) null);
        mFragments.dispatchResume();
        recreateFragment();
        processAllMessages();
    }

@@ -154,6 +147,18 @@ public abstract class BaseFragmentTest {
        processAllMessages();
    }

    protected void recreateFragment() {
        mFragments.dispatchPause();
        Parcelable p = mFragments.saveAllState();
        mFragments.dispatchDestroy();

        mFragments = FragmentController.createController(new HostCallbacks());
        mFragments.attachHost(null);
        mFragments.restoreAllState(p, (FragmentManagerNonConfig) null);
        mFragments.dispatchResume();
        mFragment = mFragments.getFragmentManager().findFragmentById(VIEW_ID);
    }

    protected void attachFragmentToWindow() {
        ViewUtils.attachView(mView);
        TestableLooper.get(this).processMessages(1);