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;
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;
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
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.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;
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());
+ }
}
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();
}
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);