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

Commit 90dc136d authored by John Spurlock's avatar John Spurlock
Browse files

Re-enable dreams: Clock

Convert old Screensaver activity to a dream service. Handle new lifecycle
events and configuration changes.

Change-Id: I02c1eabb9a26d21a61c3b2eac33f834d1e2fb5e3
Bug: 6921930
parent f8cd8650
Loading
Loading
Loading
Loading
+4 −6
Original line number Diff line number Diff line
@@ -131,17 +131,15 @@
            <meta-data android:name="android.appwidget.provider" android:resource="@xml/analog_appwidget" />
        </receiver>

        <activity android:name=".Screensaver"
            android:label="@string/app_label"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
            android:hardwareAccelerated="true"
            >
        <service android:name="Screensaver"
            android:exported="true"
            android:label="@string/app_label">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.DREAM" />
            </intent-filter>
        </activity>
        </service>
    </application>
</manifest>
+62 −48
Original line number Diff line number Diff line
@@ -16,29 +16,25 @@

package com.android.deskclock;

import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.animation.TimeInterpolator;
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.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.AttributeSet;
import android.content.res.Configuration;
import android.os.BatteryManager;
import android.os.Handler;
import android.service.dreams.Dream;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;
import java.lang.Runnable;
import android.util.Log;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

public class Screensaver extends Activity {
public class Screensaver extends Dream {
    static final boolean DEBUG = false;
    static final String TAG = "DeskClock/Screensaver";

@@ -87,11 +83,11 @@ public class Screensaver extends Activity {
        public void run() {
            long delay = MOVE_DELAY;

//            Log.d("DeskClock/Screensaver",
//                    String.format("mContentView=(%d x %d) container=(%d x %d)",
//                        mContentView.getWidth(), mContentView.getHeight(),
//                        mSaverView.getWidth(), mSaverView.getHeight()
//                        ));
            if (DEBUG) Log.d(TAG,
                    String.format("mContentView=(%d x %d) container=(%d x %d)",
                        mContentView.getWidth(), mContentView.getHeight(),
                        mSaverView.getWidth(), mSaverView.getHeight()
                        ));
            final float xrange = mContentView.getWidth() - mSaverView.getWidth();
            final float yrange = mContentView.getHeight() - mSaverView.getHeight();

@@ -167,21 +163,24 @@ public class Screensaver extends Activity {
        }
    };

    public Screensaver() {
        if (DEBUG) Log.d(TAG, "Screensaver allocated");
    }

    @Override
    public void onCreate() {
        if (DEBUG) Log.d(TAG, "Screensaver created");
        super.onCreate();
        setInteractive(false);
    }

    @Override
    public void onStart() {
        if (DEBUG) Log.d(TAG, "Screensaver started");
        super.onStart();
        CLOCK_COLOR = getResources().getColor(R.color.screen_saver_color);
        setContentView(R.layout.desk_clock_saver);
        mSaverView = findViewById(R.id.saver_view);
        mContentView = (View) mSaverView.getParent();
        mSaverView.setAlpha(0);

        AndroidClockTextView timeDisplay = (AndroidClockTextView) findViewById(R.id.timeDisplay);
        if (timeDisplay != null) {
            timeDisplay.setTextColor(CLOCK_COLOR);
            AndroidClockTextView amPm = (AndroidClockTextView)findViewById(R.id.am_pm);
            if (amPm != null) amPm.setTextColor(CLOCK_COLOR);
        }
        CLOCK_COLOR = getResources().getColor(R.color.screen_saver_color);
        layoutClockSaver();

        final IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
@@ -189,35 +188,50 @@ public class Screensaver extends Activity {
    }

    @Override
    public void onAttachedToWindow() {
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
              | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
              );
        mSaverView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
    public void onConfigurationChanged(Configuration newConfig) {
        if (DEBUG) Log.d(TAG, "Screensaver configuration changed");
        super.onConfigurationChanged(newConfig);
        mHandler.removeCallbacks(mMoveSaverRunnable);
        layoutClockSaver();
        mHandler.post(mMoveSaverRunnable);
    }


    @Override
    public void onStop() {
        unregisterReceiver(mPowerIntentReceiver);
        super.onStop();
    }
    public void onAttachedToWindow() {
        if (DEBUG) Log.d(TAG, "Screensaver attached to window");
        super.onAttachedToWindow();

    @Override
    public void onResume() {
        super.onResume();
        mHandler.post(mMoveSaverRunnable);
    }

    @Override
    public void onPause() {
        super.onPause();
    public void onDetachedFromWindow() {
        if (DEBUG) Log.d(TAG, "Screensaver detached from window");
        super.onDetachedFromWindow();

        mHandler.removeCallbacks(mMoveSaverRunnable);
    }

    @Override
    public void onUserInteraction() {
        finish();
    public void onDestroy() {
        if (DEBUG) Log.d(TAG, "Screensaver destroyed");
        super.onDestroy();

        unregisterReceiver(mPowerIntentReceiver);
    }

    private void layoutClockSaver() {
        setContentView(R.layout.desk_clock_saver);
        mSaverView = findViewById(R.id.saver_view);
        mContentView = (View) mSaverView.getParent();
        mSaverView.setAlpha(0);

        AndroidClockTextView timeDisplay = (AndroidClockTextView) findViewById(R.id.timeDisplay);
        if (timeDisplay != null) {
            timeDisplay.setTextColor(CLOCK_COLOR);
            AndroidClockTextView amPm = (AndroidClockTextView)findViewById(R.id.am_pm);
            if (amPm != null) amPm.setTextColor(CLOCK_COLOR);
        }
    }

}