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

Commit b2b4d519 authored by Neil Fuller's avatar Neil Fuller Committed by Android (Google) Code Review
Browse files

Merge "Add infra for an internal API for time_detector"

parents 33e893f9 286769a0
Loading
Loading
Loading
Loading
+28 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2022 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.server.timedetector;

/**
 * The internal (in-process) system server API for the {@link
 * com.android.server.timedetector.TimeDetectorService}.
 *
 * <p>The methods on this class can be called from any thread.
 * @hide
 */
public interface TimeDetectorInternal {

}
+42 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2022 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.server.timedetector;

import android.annotation.NonNull;
import android.content.Context;
import android.os.Handler;

import java.util.Objects;

/**
 * The real {@link TimeDetectorInternal} local service implementation.
 *
 * @hide
 */
public class TimeDetectorInternalImpl implements TimeDetectorInternal {

    @NonNull private final Context mContext;
    @NonNull private final Handler mHandler;
    @NonNull private final TimeDetectorStrategy mTimeDetectorStrategy;

    public TimeDetectorInternalImpl(@NonNull Context context, @NonNull Handler handler,
            @NonNull TimeDetectorStrategy timeDetectorStrategy) {
        mContext = Objects.requireNonNull(context);
        mHandler = Objects.requireNonNull(handler);
        mTimeDetectorStrategy = Objects.requireNonNull(timeDetectorStrategy);
    }
}
+5 −0
Original line number Original line Diff line number Diff line
@@ -83,6 +83,11 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub
            TimeDetectorStrategy timeDetectorStrategy =
            TimeDetectorStrategy timeDetectorStrategy =
                    TimeDetectorStrategyImpl.create(context, handler, serviceConfigAccessor);
                    TimeDetectorStrategyImpl.create(context, handler, serviceConfigAccessor);


            // Create and publish the local service for use by internal callers.
            TimeDetectorInternal internal =
                    new TimeDetectorInternalImpl(context, handler, timeDetectorStrategy);
            publishLocalService(TimeDetectorInternal.class, internal);

            TimeDetectorService service = new TimeDetectorService(
            TimeDetectorService service = new TimeDetectorService(
                    context, handler, serviceConfigAccessor, timeDetectorStrategy);
                    context, handler, serviceConfigAccessor, timeDetectorStrategy);


+112 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2022 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.server.timedetector;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import android.annotation.UserIdInt;
import android.app.time.ExternalTimeSuggestion;
import android.app.timedetector.GnssTimeSuggestion;
import android.app.timedetector.ManualTimeSuggestion;
import android.app.timedetector.NetworkTimeSuggestion;
import android.app.timedetector.TelephonyTimeSuggestion;
import android.util.IndentingPrintWriter;

/**
 * A fake implementation of {@link com.android.server.timedetector.TimeDetectorStrategy} for use
 * in tests.
 */
class FakeTimeDetectorStrategy implements TimeDetectorStrategy {

    // Call tracking.
    private TelephonyTimeSuggestion mLastTelephonySuggestion;
    private @UserIdInt Integer mLastManualSuggestionUserId;
    private ManualTimeSuggestion mLastManualSuggestion;
    private NetworkTimeSuggestion mLastNetworkSuggestion;
    private GnssTimeSuggestion mLastGnssSuggestion;
    private ExternalTimeSuggestion mLastExternalSuggestion;
    private boolean mDumpCalled;

    @Override
    public void suggestTelephonyTime(TelephonyTimeSuggestion timeSuggestion) {
        mLastTelephonySuggestion = timeSuggestion;
    }

    @Override
    public boolean suggestManualTime(@UserIdInt int userId, ManualTimeSuggestion timeSuggestion) {
        mLastManualSuggestionUserId = userId;
        mLastManualSuggestion = timeSuggestion;
        return true;
    }

    @Override
    public void suggestNetworkTime(NetworkTimeSuggestion timeSuggestion) {
        mLastNetworkSuggestion = timeSuggestion;
    }

    @Override
    public void suggestGnssTime(GnssTimeSuggestion timeSuggestion) {
        mLastGnssSuggestion = timeSuggestion;
    }

    @Override
    public void suggestExternalTime(ExternalTimeSuggestion timeSuggestion) {
        mLastExternalSuggestion = timeSuggestion;
    }

    @Override
    public void dump(IndentingPrintWriter pw, String[] args) {
        mDumpCalled = true;
    }

    void resetCallTracking() {
        mLastTelephonySuggestion = null;
        mLastManualSuggestionUserId = null;
        mLastManualSuggestion = null;
        mLastNetworkSuggestion = null;
        mLastGnssSuggestion = null;
        mLastExternalSuggestion = null;
        mDumpCalled = false;
    }

    void verifySuggestTelephonyTimeCalled(TelephonyTimeSuggestion expectedSuggestion) {
        assertEquals(expectedSuggestion, mLastTelephonySuggestion);
    }

    void verifySuggestManualTimeCalled(
            @UserIdInt int expectedUserId, ManualTimeSuggestion expectedSuggestion) {
        assertEquals((Integer) expectedUserId, mLastManualSuggestionUserId);
        assertEquals(expectedSuggestion, mLastManualSuggestion);
    }

    void verifySuggestNetworkTimeCalled(NetworkTimeSuggestion expectedSuggestion) {
        assertEquals(expectedSuggestion, mLastNetworkSuggestion);
    }

    void verifySuggestGnssTimeCalled(GnssTimeSuggestion expectedSuggestion) {
        assertEquals(expectedSuggestion, mLastGnssSuggestion);
    }

    void verifySuggestExternalTimeCalled(ExternalTimeSuggestion expectedSuggestion) {
        assertEquals(expectedSuggestion, mLastExternalSuggestion);
    }

    void verifyDumpCalled() {
        assertTrue(mDumpCalled);
    }
}
+68 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2022 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.server.timedetector;

import static org.mockito.Mockito.mock;

import android.content.Context;
import android.os.HandlerThread;

import androidx.test.runner.AndroidJUnit4;

import com.android.server.timezonedetector.TestHandler;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class TimeDetectorInternalImplTest {

    private Context mMockContext;
    private FakeTimeDetectorStrategy mFakeTimeDetectorStrategy;

    private TimeDetectorInternalImpl mTimeDetectorInternal;
    private HandlerThread mHandlerThread;
    private TestHandler mTestHandler;

    @Before
    public void setUp() {
        mMockContext = mock(Context.class);

        // Create a thread + handler for processing the work that the service posts.
        mHandlerThread = new HandlerThread("TimeDetectorInternalTest");
        mHandlerThread.start();
        mTestHandler = new TestHandler(mHandlerThread.getLooper());

        mFakeTimeDetectorStrategy = new FakeTimeDetectorStrategy();

        mTimeDetectorInternal = new TimeDetectorInternalImpl(
                mMockContext, mTestHandler, mFakeTimeDetectorStrategy);
    }

    @Test
    public void placeholder() {
      // A placeholder test until there are real methods to test.
    }

    @After
    public void tearDown() throws Exception {
        mHandlerThread.quit();
        mHandlerThread.join();
    }
}
Loading