package com.android.settings.gestures;
import android.content.Context;
+import android.os.Bundle;
+import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.support.v7.preference.TwoStatePreference;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
+import com.android.settingslib.core.lifecycle.events.OnCreate;
+import com.android.settingslib.core.lifecycle.events.OnSaveInstanceState;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
public abstract class GesturePreferenceController extends AbstractPreferenceController
implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
- LifecycleObserver, OnStart, OnStop {
+ LifecycleObserver, OnStart, OnStop, OnCreate, OnSaveInstanceState {
+
+ @VisibleForTesting
+ static final String KEY_VIDEO_PAUSED = "key_video_paused";
private VideoPreference mVideoPreference;
+ private boolean mVideoPaused;
public GesturePreferenceController(Context context, Lifecycle lifecycle) {
super(context);
}
@Override
+ public void onCreate(Bundle savedInstanceState) {
+ if (savedInstanceState != null) {
+ mVideoPaused = savedInstanceState.getBoolean(KEY_VIDEO_PAUSED, false);
+ }
+ }
+
+ @Override
+ public void onSaveInstanceState(Bundle outState) {
+ if (mVideoPreference != null) {
+ mVideoPaused = mVideoPreference.isVideoPaused();
+ }
+ outState.putBoolean(KEY_VIDEO_PAUSED, mVideoPaused);
+ }
+
+ @Override
public void onStop() {
if (mVideoPreference != null) {
mVideoPreference.onViewInvisible();
@Override
public void onStart() {
if (mVideoPreference != null) {
- mVideoPreference.onViewVisible();
+ mVideoPreference.onViewVisible(mVideoPaused);
}
}
private MediaPlayer mMediaPlayer;
private boolean mAnimationAvailable;
private boolean mVideoReady;
+ private boolean mVideoPaused;
private int mPreviewResource;
public VideoPreference(Context context, AttributeSet attrs) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
playButton.setVisibility(View.VISIBLE);
+ mVideoPaused = true;
} else {
mMediaPlayer.start();
playButton.setVisibility(View.GONE);
+ mVideoPaused = false;
}
}
});
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
if (mVideoReady && imageView.getVisibility() == View.VISIBLE) {
imageView.setVisibility(View.GONE);
+ if (!mVideoPaused && mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
+ mMediaPlayer.start();
+ playButton.setVisibility(View.GONE);
+ }
}
if (mMediaPlayer != null && !mMediaPlayer.isPlaying() &&
playButton.getVisibility() != View.VISIBLE) {
super.onDetached();
}
- public void onViewVisible() {
+ public void onViewVisible(boolean videoPaused) {
+ mVideoPaused = videoPaused;
if (mVideoReady && mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
mMediaPlayer.seekTo(0);
}
mMediaPlayer.pause();
}
}
+
+ public boolean isVideoPaused() {
+ return mVideoPaused;
+ }
+
}
package com.android.settings.gestures;
import android.content.Context;
+import android.os.Bundle;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.support.v7.preference.TwoStatePreference;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
}
@Test
- public void onStart_shouldStartVideoPreference() {
+ public void onStart_shouldStartVideoPreferenceWithVideoPauseState() {
final VideoPreference videoPreference = mock(VideoPreference.class);
when(mScreen.findPreference(mController.getVideoPrefKey())).thenReturn(videoPreference);
mController.mIsPrefAvailable = true;
mController.displayPreference(mScreen);
+ final Bundle savedState = new Bundle();
+
+ mController.onCreate(null);
+ mController.onStart();
+ verify(videoPreference).onViewVisible(false);
+
+ reset(videoPreference);
+ savedState.putBoolean(mController.KEY_VIDEO_PAUSED, true);
+ mController.onCreate(savedState);
mController.onStart();
+ verify(videoPreference).onViewVisible(true);
- verify(videoPreference).onViewVisible();
+ reset(videoPreference);
+ savedState.putBoolean(mController.KEY_VIDEO_PAUSED, false);
+ mController.onCreate(savedState);
+ mController.onStart();
+ verify(videoPreference).onViewVisible(false);
}
@Test
}
@Test
+ public void onSaveInstanceState_shouldSaveVideoPauseState() {
+ final VideoPreference videoPreference = mock(VideoPreference.class);
+ when(mScreen.findPreference(mController.getVideoPrefKey())).thenReturn(videoPreference);
+ mController.mIsPrefAvailable = true;
+ mController.displayPreference(mScreen);
+ final Bundle outState = mock(Bundle.class);
+
+ when(videoPreference.isVideoPaused()).thenReturn(true);
+ mController.onSaveInstanceState(outState);
+ verify(outState).putBoolean(mController.KEY_VIDEO_PAUSED, true);
+
+ reset(outState);
+ when(videoPreference.isVideoPaused()).thenReturn(false);
+ mController.onSaveInstanceState(outState);
+ verify(outState).putBoolean(mController.KEY_VIDEO_PAUSED, false);
+ }
+
+ @Test
public void updateState_preferenceSetCheckedWhenSettingIsOn() {
// Mock a TwoStatePreference
final TwoStatePreference preference = mock(TwoStatePreference.class);