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

Commit b8c4512b authored by Justin Klaassen's avatar Justin Klaassen
Browse files

Set theme for Screensaver

- Set Theme.DeskClock for Screensaver so that app-level theme attributes
  such as ?colorAccent can be resolved succcessfully.
- Also did some minor cleanup of Screensaver and ScreensaverActivity.

Bug: 35330855
Test: manually verified that crash does not occur when using Screensaver
or ScreensaverActivity on API 19 and 25 devices.

Change-Id: I107fdd51af5a97f41464942dc3e2f7730f11b5c2
parent 36d4b8bc
Loading
Loading
Loading
Loading
+29 −18
Original line number Diff line number Diff line
@@ -41,11 +41,14 @@ public final class Screensaver extends DreamService {
    private final OnPreDrawListener mStartPositionUpdater = new StartPositionUpdater();
    private MoveScreensaverRunnable mPositionUpdater;

    private View mContentView, mSaverView;
    private View mAnalogClock, mDigitalClock;
    private String mDateFormat;
    private String mDateFormatForAccessibility;

    private View mContentView;
    private View mMainClockView;
    private TextClock mDigitalClock;
    private AnalogClock mAnalogClock;

    /* Register ContentObserver to see alarm changes for pre-L */
    private final ContentObserver mSettingsContentObserver =
            Utils.isLOrLater() ? null : new ContentObserver(new Handler()) {
@@ -76,6 +79,8 @@ public final class Screensaver extends DreamService {
    @Override
    public void onCreate() {
        LOGGER.v("Screensaver created");

        setTheme(R.style.Theme_DeskClock);
        super.onCreate();

        mDateFormat = getString(R.string.abbrev_wday_month_day_no_year);
@@ -88,29 +93,34 @@ public final class Screensaver extends DreamService {
        super.onAttachedToWindow();

        setContentView(R.layout.desk_clock_saver);
        mDigitalClock = findViewById(R.id.digital_clock);
        mAnalogClock = findViewById(R.id.analog_clock);
        mSaverView = findViewById(R.id.main_clock);
        mContentView = (View) mSaverView.getParent();

        mContentView = findViewById(R.id.saver_container);
        mMainClockView = mContentView.findViewById(R.id.main_clock);
        mDigitalClock = (TextClock) mMainClockView.findViewById(R.id.digital_clock);
        mAnalogClock = (AnalogClock) mMainClockView.findViewById(R.id.analog_clock);

        setClockStyle();
        Utils.setClockIconTypeface(mContentView);
        Utils.setTimeFormat(mDigitalClock, false);
        mAnalogClock.enableSeconds(false);

        mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                | View.SYSTEM_UI_FLAG_IMMERSIVE
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

        setClockStyle();
        Utils.setTimeFormat((TextClock) mDigitalClock, false);

        mPositionUpdater = new MoveScreensaverRunnable(mContentView, mSaverView);
        mPositionUpdater = new MoveScreensaverRunnable(mContentView, mMainClockView);

        // We want the screen saver to exit upon user interaction.
        setInteractive(false);

        setFullscreen(true);

        // Setup handlers for time reference changes and date updates.
        final IntentFilter filter = new IntentFilter(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED);
        registerReceiver(mAlarmChangedReceiver, filter);
        if (Utils.isLOrLater()) {
            registerReceiver(mAlarmChangedReceiver,
                    new IntentFilter(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED));
        }

        if (mSettingsContentObserver != null) {
            @SuppressWarnings("deprecation")
@@ -119,8 +129,7 @@ public final class Screensaver extends DreamService {
        }

        Utils.updateDate(mDateFormat, mDateFormatForAccessibility, mContentView);
        Utils.setClockIconTypeface(mContentView);
        Utils.refreshAlarm(Screensaver.this, mContentView);
        Utils.refreshAlarm(this, mContentView);

        startPositionUpdater();
        UiDataModel.getUiDataModel().addMidnightCallback(mMidnightUpdater, 100);
@@ -139,8 +148,10 @@ public final class Screensaver extends DreamService {
        stopPositionUpdater();

        // Tear down handlers for time reference changes and date updates.
        if (Utils.isLOrLater()) {
            unregisterReceiver(mAlarmChangedReceiver);
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
@@ -153,7 +164,7 @@ public final class Screensaver extends DreamService {
    private void setClockStyle() {
        Utils.setScreensaverClockStyle(mDigitalClock, mAnalogClock);
        final boolean dimNightMode = DataModel.getDataModel().getScreensaverNightModeOn();
        Utils.dimClockView(dimNightMode, mSaverView);
        Utils.dimClockView(dimNightMode, mMainClockView);
        setScreenBright(!dimNightMode);
    }

@@ -181,7 +192,7 @@ public final class Screensaver extends DreamService {
    private final class StartPositionUpdater implements OnPreDrawListener {
        /**
         * This callback occurs after initial layout has completed. It is an appropriate place to
         * select a random position for {@link #mSaverView} and schedule future callbacks to update
         * select a random position for {@link #mMainClockView} and schedule future callbacks to update
         * its position.
         *
         * @return {@code true} to continue with the drawing pass
+32 −26
Original line number Diff line number Diff line
@@ -43,18 +43,12 @@ public class ScreensaverActivity extends BaseActivity {
    private static final LogUtils.Logger LOGGER = new LogUtils.Logger("ScreensaverActivity");

    /** These flags keep the screen on if the device is plugged in. */
    private static final int sWindowFlags = WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
    private static final int WINDOW_FLAGS = WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
            | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;

    private final OnPreDrawListener mStartPositionUpdater = new StartPositionUpdater();
    private MoveScreensaverRunnable mPositionUpdater;

    private View mContentView, mSaverView;

    private String mDateFormat;
    private String mDateFormatForAccessibility;

    private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
@@ -96,29 +90,43 @@ public class ScreensaverActivity extends BaseActivity {
        }
    };

    private String mDateFormat;
    private String mDateFormatForAccessibility;

    private View mContentView;
    private View mMainClockView;

    private MoveScreensaverRunnable mPositionUpdater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mDateFormat = getString(R.string.abbrev_wday_month_day_no_year);
        mDateFormatForAccessibility = getString(R.string.full_wday_month_day_no_year);

        setContentView(R.layout.desk_clock_saver);
        final View digitalClock = findViewById(R.id.digital_clock);
        final View analogClock = findViewById(R.id.analog_clock);
        mSaverView = findViewById(R.id.main_clock);
        mContentView = findViewById(R.id.saver_container);
        mMainClockView = mContentView.findViewById(R.id.main_clock);

        final View digitalClock = mMainClockView.findViewById(R.id.digital_clock);
        final AnalogClock analogClock =
                (AnalogClock) mMainClockView.findViewById(R.id.analog_clock);

        Utils.setClockIconTypeface(mMainClockView);
        Utils.setTimeFormat((TextClock) digitalClock, false);
        Utils.setClockStyle(digitalClock, analogClock);
        Utils.dimClockView(true, mMainClockView);
        analogClock.enableSeconds(false);

        mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                | View.SYSTEM_UI_FLAG_IMMERSIVE
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

        mContentView.setOnSystemUiVisibilityChangeListener(new InteractionListener());

        Utils.setClockIconTypeface(mSaverView);
        Utils.setTimeFormat((TextClock) digitalClock, false);
        Utils.setClockStyle(digitalClock, analogClock);
        Utils.dimClockView(true, mSaverView);

        mPositionUpdater = new MoveScreensaverRunnable(mContentView, mSaverView);
        mPositionUpdater = new MoveScreensaverRunnable(mContentView, mMainClockView);

        final Intent intent = getIntent();
        if (intent != null) {
@@ -135,7 +143,9 @@ public class ScreensaverActivity extends BaseActivity {
        filter.addAction(Intent.ACTION_POWER_CONNECTED);
        filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        if (Utils.isLOrLater()) {
            filter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED);
        }
        registerReceiver(mIntentReceiver, filter);

        if (mSettingsContentObserver != null) {
@@ -149,9 +159,6 @@ public class ScreensaverActivity extends BaseActivity {
    public void onResume() {
        super.onResume();

        mDateFormat = getString(R.string.abbrev_wday_month_day_no_year);
        mDateFormatForAccessibility = getString(R.string.full_wday_month_day_no_year);

        Utils.updateDate(mDateFormat, mDateFormatForAccessibility, mContentView);
        Utils.refreshAlarm(ScreensaverActivity.this, mContentView);

@@ -159,8 +166,7 @@ public class ScreensaverActivity extends BaseActivity {
        UiDataModel.getUiDataModel().addMidnightCallback(mMidnightUpdater, 100);

        final Intent intent = registerReceiver(null, new IntentFilter(ACTION_BATTERY_CHANGED));
        final int plugged = intent.getIntExtra(EXTRA_PLUGGED, 0);
        final boolean pluggedIn = plugged != 0;
        final boolean pluggedIn = intent != null && intent.getIntExtra(EXTRA_PLUGGED, 0) != 0;
        updateWakeLock(pluggedIn);
    }

@@ -194,9 +200,9 @@ public class ScreensaverActivity extends BaseActivity {
        final WindowManager.LayoutParams winParams = win.getAttributes();
        winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        if (pluggedIn) {
            winParams.flags |= sWindowFlags;
            winParams.flags |= WINDOW_FLAGS;
        } else {
            winParams.flags &= (~sWindowFlags);
            winParams.flags &= (~WINDOW_FLAGS);
        }
        win.setAttributes(winParams);
    }
@@ -221,7 +227,7 @@ public class ScreensaverActivity extends BaseActivity {
    private final class StartPositionUpdater implements OnPreDrawListener {
        /**
         * This callback occurs after initial layout has completed. It is an appropriate place to
         * select a random position for {@link #mSaverView} and schedule future callbacks to update
         * select a random position for {@link #mMainClockView} and schedule future callbacks to update
         * its position.
         *
         * @return {@code true} to continue with the drawing pass