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

Commit e269bd86 authored by Daniel Sandler's avatar Daniel Sandler
Browse files

Fix bugs in the DeskClock screensaver:

 - release wakelock when power removed
 - wait to add window flags until attached to window

These tweaks will be factored into the updated BasicDream
(will be BasicCandy) support lib class.

Change-Id: I80d9162524d3b0e69d053c60585b49d9cce1b3ff
parent ee0df40c
Loading
Loading
Loading
Loading
+54 −14
Original line number Diff line number Diff line
@@ -23,8 +23,12 @@ import android.animation.ObjectAnimator;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.app.Activity;
import android.os.BatteryManager;
import android.os.Handler;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
@@ -35,7 +39,8 @@ import java.lang.Runnable;
import android.util.Log;

public class Screensaver extends Activity {
    View mContentView, mSaverView;
    static final boolean DEBUG = false;
    static final String TAG = "DeskClock/Screensaver";

    static int CLOCK_COLOR = 0xFF66AAFF;

@@ -45,6 +50,8 @@ public class Screensaver extends Activity {

    static final boolean SLIDE = false;

    private View mContentView, mSaverView;

    private static TimeInterpolator mSlowStartWithBrakes =
        new TimeInterpolator() {
            public float getInterpolation(float x) {
@@ -54,7 +61,28 @@ public class Screensaver extends Activity {

    private Handler mHandler = new Handler();

    private Runnable mMoveSaverRunnable = new Runnable() {
    private boolean mPlugged = false;
    private final BroadcastReceiver mPowerIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
                // Only keep the screen on if we're plugged in.
                boolean plugged = (0 != intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0));
                if (plugged != mPlugged) {
                    if (DEBUG) Log.v(TAG, plugged ? "plugged in" : "unplugged");
                    mPlugged = plugged;
                    if (mPlugged) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                    } else {
                        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                    }
                }
            }
        }
    };

    private final Runnable mMoveSaverRunnable = new Runnable() {
        @Override
        public void run() {
            long delay = MOVE_DELAY;
@@ -87,11 +115,11 @@ public class Screensaver extends Activity {
                    Animator yMove   = ObjectAnimator.ofFloat(mSaverView,
                                         "y", mSaverView.getY(), nexty);

                    Animator xShrink = ObjectAnimator.ofFloat(mSaverView, "scaleX", 1f, 0.75f);
                    Animator xGrow   = ObjectAnimator.ofFloat(mSaverView, "scaleX", 0.75f, 1f);
                    Animator xShrink = ObjectAnimator.ofFloat(mSaverView, "scaleX", 1f, 0.85f);
                    Animator xGrow   = ObjectAnimator.ofFloat(mSaverView, "scaleX", 0.85f, 1f);

                    Animator yShrink = ObjectAnimator.ofFloat(mSaverView, "scaleY", 1f, 0.75f);
                    Animator yGrow   = ObjectAnimator.ofFloat(mSaverView, "scaleY", 0.75f, 1f);
                    Animator yShrink = ObjectAnimator.ofFloat(mSaverView, "scaleY", 1f, 0.85f);
                    Animator yGrow   = ObjectAnimator.ofFloat(mSaverView, "scaleY", 0.85f, 1f);
                    AnimatorSet shrink = new AnimatorSet(); shrink.play(xShrink).with(yShrink);
                    AnimatorSet grow = new AnimatorSet(); grow.play(xGrow).with(yGrow);

@@ -130,7 +158,8 @@ public class Screensaver extends Activity {
                        + (MOVE_DELAY - adjust) // minute aligned
                        - (SLIDE ? 0 : FADE_TIME) // start moving before the fade
                        ;
//                Log.d("DeskClock/Screensaver", "will move again in " + delay + " now=" + now + " adjusted by " + adjust);
                if (DEBUG) Log.d(TAG, 
                        "will move again in " + delay + " now=" + now + " adjusted by " + adjust);
            }

            mHandler.removeCallbacks(this);
@@ -146,7 +175,6 @@ public class Screensaver extends Activity {
        mSaverView = findViewById(R.id.saver_view);
        mContentView = (View) mSaverView.getParent();
        mSaverView.setAlpha(0);
        mSaverView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);

        AndroidClockTextView timeDisplay = (AndroidClockTextView) findViewById(R.id.timeDisplay);
        if (timeDisplay != null) {
@@ -155,24 +183,36 @@ public class Screensaver extends Activity {
            if (amPm != null) amPm.setTextColor(CLOCK_COLOR);
        }

        final IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(mPowerIntentReceiver, filter);
    }

    @Override
    public void onAttachedToWindow() {
        getWindow().addFlags(
                  WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
                WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
              | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
              );
        mSaverView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
    }


    @Override
    public void onStop() {
        unregisterReceiver(mPowerIntentReceiver);
        super.onStop();
    }

    @Override
    public void onResume() {
        super.onResume();
//        Log.d("DeskClock/Screensaver", "resume");
        mHandler.post(mMoveSaverRunnable);
    }

    @Override
    public void onPause() {
        super.onPause();

        mHandler.removeCallbacks(mMoveSaverRunnable);
    }