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

Commit b3ba5770 authored by Ned Burns's avatar Ned Burns
Browse files

Add Dagger-injectable version of SystemClock

Also adds FakeSystemClock for use in tests.

Test: atest
Change-Id: Ic63012bff96498354c76e43635567b1e02fffe51
parent 89c5098c
Loading
Loading
Loading
Loading
+6 −0
Original line number Original line Diff line number Diff line
@@ -32,6 +32,8 @@ import com.android.systemui.statusbar.notification.people.PeopleHubModule;
import com.android.systemui.statusbar.phone.KeyguardLiftController;
import com.android.systemui.statusbar.phone.KeyguardLiftController;
import com.android.systemui.statusbar.phone.StatusBar;
import com.android.systemui.statusbar.phone.StatusBar;
import com.android.systemui.util.sensors.AsyncSensorManager;
import com.android.systemui.util.sensors.AsyncSensorManager;
import com.android.systemui.util.time.SystemClock;
import com.android.systemui.util.time.SystemClockImpl;


import javax.inject.Singleton;
import javax.inject.Singleton;


@@ -84,4 +86,8 @@ public abstract class SystemUIModule {


    @BindsOptionalOf
    @BindsOptionalOf
    abstract StatusBar optionalStatusBar();
    abstract StatusBar optionalStatusBar();

    @Singleton
    @Binds
    abstract SystemClock bindSystemClock(SystemClockImpl systemClock);
}
}
+46 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.util.time;

/**
 * Testable wrapper around {@link android.os.SystemClock}.
 *
 * Dagger can inject this wrapper into your classes. The implementation just proxies calls to the
 * real SystemClock.
 *
 * In tests, pass an instance of FakeSystemClock, which allows you to control the values returned by
 * the various getters below.
 */
public interface SystemClock {
    /** @see android.os.SystemClock#uptimeMillis() */
    long uptimeMillis();

    /** @see android.os.SystemClock#elapsedRealtime() */
    long elapsedRealtime();

    /** @see android.os.SystemClock#elapsedRealtimeNanos() */
    long elapsedRealtimeNanos();

    /** @see android.os.SystemClock#currentThreadTimeMillis() */
    long currentThreadTimeMillis();

    /** @see android.os.SystemClock#currentThreadTimeMicro() */
    long currentThreadTimeMicro();

    /** @see android.os.SystemClock#currentTimeMicro() */
    long currentTimeMicro();
}
+55 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.util.time;

import javax.inject.Inject;

/** Default implementation of {@link SystemClock}. */
public class SystemClockImpl implements SystemClock {
    @Inject
    public SystemClockImpl() {}

    @Override
    public long uptimeMillis() {
        return android.os.SystemClock.uptimeMillis();
    }

    @Override
    public long elapsedRealtime() {
        return android.os.SystemClock.elapsedRealtime();
    }

    @Override
    public long elapsedRealtimeNanos() {
        return android.os.SystemClock.elapsedRealtimeNanos();
    }

    @Override
    public long currentThreadTimeMillis() {
        return android.os.SystemClock.currentThreadTimeMillis();
    }

    @Override
    public long currentThreadTimeMicro() {
        return android.os.SystemClock.currentThreadTimeMicro();
    }

    @Override
    public long currentTimeMicro() {
        return android.os.SystemClock.currentTimeMicro();
    }
}
+111 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.util.time;

public class FakeSystemClock implements SystemClock {
    private boolean mAutoIncrement = true;

    private long mUptimeMillis;
    private long mElapsedRealtime;
    private long mElapsedRealtimeNanos;
    private long mCurrentThreadTimeMillis;
    private long mCurrentThreadTimeMicro;
    private long mCurrentTimeMicro;

    @Override
    public long uptimeMillis() {
        long value = mUptimeMillis;
        if (mAutoIncrement) {
            mUptimeMillis++;
        }
        return value;
    }

    @Override
    public long elapsedRealtime() {
        long value = mElapsedRealtime;
        if (mAutoIncrement) {
            mElapsedRealtime++;
        }
        return value;
    }

    @Override
    public long elapsedRealtimeNanos() {
        long value = mElapsedRealtimeNanos;
        if (mAutoIncrement) {
            mElapsedRealtimeNanos++;
        }
        return value;
    }

    @Override
    public long currentThreadTimeMillis() {
        long value = mCurrentThreadTimeMillis;
        if (mAutoIncrement) {
            mCurrentThreadTimeMillis++;
        }
        return value;
    }

    @Override
    public long currentThreadTimeMicro() {
        long value = mCurrentThreadTimeMicro;
        if (mAutoIncrement) {
            mCurrentThreadTimeMicro++;
        }
        return value;
    }

    @Override
    public long currentTimeMicro() {
        long value = mCurrentTimeMicro;
        if (mAutoIncrement) {
            mCurrentTimeMicro++;
        }
        return value;
    }

    public void setUptimeMillis(long uptimeMillis) {
        mUptimeMillis = uptimeMillis;
    }

    public void setElapsedRealtime(long elapsedRealtime) {
        mElapsedRealtime = elapsedRealtime;
    }

    public void setElapsedRealtimeNanos(long elapsedRealtimeNanos) {
        mElapsedRealtimeNanos = elapsedRealtimeNanos;
    }

    public void setCurrentThreadTimeMillis(long currentThreadTimeMillis) {
        mCurrentThreadTimeMillis = currentThreadTimeMillis;
    }

    public void setCurrentThreadTimeMicro(long currentThreadTimeMicro) {
        mCurrentThreadTimeMicro = currentThreadTimeMicro;
    }

    public void setCurrentTimeMicro(long currentTimeMicro) {
        mCurrentTimeMicro = currentTimeMicro;
    }

    /** If true, each call to get____ will be one higher than the previous call to that method. */
    public void setAutoIncrement(boolean autoIncrement) {
        mAutoIncrement = autoIncrement;
    }
}