Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle (auto)rotation changes during activity lifecycle #2444

Merged
merged 2 commits into from Jul 21, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java
Expand Up @@ -24,11 +24,13 @@
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.database.ContentObserver;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.support.annotation.ColorInt;
Expand Down Expand Up @@ -113,6 +115,8 @@ public final class MainVideoPlayer extends AppCompatActivity
private boolean isInMultiWindow;
private boolean isBackPressed;

private ContentObserver rotationObserver;

/*//////////////////////////////////////////////////////////////////////////
// Activity LifeCycle
//////////////////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -147,6 +151,23 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
Toast.makeText(this, R.string.general_error, Toast.LENGTH_SHORT).show();
finish();
}

rotationObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
if (globalScreenOrientationLocked()) {
final boolean lastOrientationWasLandscape = defaultPreferences.getBoolean(
getString(R.string.last_orientation_landscape_key), false);
setLandscape(lastOrientationWasLandscape);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}
};
getContentResolver().registerContentObserver(
Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION),
false, rotationObserver);
}

@Override
Expand Down Expand Up @@ -239,6 +260,9 @@ protected void onStop() {
playerState = createPlayerState();
playerImpl.destroy();

if (rotationObserver != null)
getContentResolver().unregisterContentObserver(rotationObserver);

isInMultiWindow = false;
isBackPressed = false;
}
Expand Down