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

Commit c74dc994 authored by Darrell Shi's avatar Darrell Shi
Browse files

Add dagger scope to clock complication

This change scopes the injected classes to the clock complication, for
example, the clock complication view, to the scope of the complication.
This aligns with other dream complication, like home controls and
weather.

Bug: 284028343
Test: atest DreamClockTimeComplicationTest
Test: verify in logs that the clock complication view is inflated and
attached to view each time the dream overlay starts

Change-Id: I53241da013c5fe95283d167f9c4fbedc85cd2c46
parent bcec6f8c
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -16,12 +16,13 @@

package com.android.systemui.complication;

import static com.android.systemui.complication.dagger.DreamClockTimeComplicationModule.DREAM_CLOCK_TIME_COMPLICATION_VIEW;
import static com.android.systemui.complication.dagger.DreamClockTimeComplicationComponent.DreamClockTimeComplicationModule.DREAM_CLOCK_TIME_COMPLICATION_VIEW;
import static com.android.systemui.complication.dagger.RegisteredComplicationsModule.DREAM_CLOCK_TIME_COMPLICATION_LAYOUT_PARAMS;

import android.view.View;

import com.android.systemui.CoreStartable;
import com.android.systemui.complication.dagger.DreamClockTimeComplicationComponent;
import com.android.systemui.dagger.qualifiers.SystemUser;
import com.android.systemui.dreams.DreamOverlayStateController;
import com.android.systemui.shared.condition.Monitor;
@@ -29,21 +30,20 @@ import com.android.systemui.util.condition.ConditionalCoreStartable;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;

/**
 * Clock Time Complication that produce Clock Time view holder.
 */
public class DreamClockTimeComplication implements Complication {
    private final Provider<DreamClockTimeViewHolder> mDreamClockTimeViewHolderProvider;
    private final DreamClockTimeComplicationComponent.Factory mComponentFactory;

    /**
     * Default constructor for {@link DreamClockTimeComplication}.
     */
    @Inject
    public DreamClockTimeComplication(
            Provider<DreamClockTimeViewHolder> dreamClockTimeViewHolderProvider) {
        mDreamClockTimeViewHolderProvider = dreamClockTimeViewHolderProvider;
            DreamClockTimeComplicationComponent.Factory componentFactory) {
        mComponentFactory = componentFactory;
    }

    @Override
@@ -56,7 +56,7 @@ public class DreamClockTimeComplication implements Complication {
     */
    @Override
    public ViewHolder createView(ComplicationViewModel model) {
        return mDreamClockTimeViewHolderProvider.get();
        return mComponentFactory.create().getViewHolder();
    }

    /**
+81 −0
Original line number Diff line number Diff line
/*
 *  Copyright (C) 2023 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.complication.dagger

import android.view.LayoutInflater
import android.view.View
import android.widget.TextClock
import com.android.internal.util.Preconditions
import com.android.systemui.R
import com.android.systemui.complication.DreamClockTimeComplication
import com.android.systemui.complication.DreamClockTimeComplication.DreamClockTimeViewHolder
import dagger.Module
import dagger.Provides
import dagger.Subcomponent
import javax.inject.Named
import javax.inject.Scope

/** Responsible for generating dependencies for the [DreamClockTimeComplication]. */
@Subcomponent(
    modules = [DreamClockTimeComplicationComponent.DreamClockTimeComplicationModule::class]
)
@DreamClockTimeComplicationComponent.DreamClockTimeComplicationScope
interface DreamClockTimeComplicationComponent {
    /** Scope of the clock complication. */
    @MustBeDocumented
    @Retention(AnnotationRetention.RUNTIME)
    @Scope
    annotation class DreamClockTimeComplicationScope

    /** Factory that generates a component for the clock complication. */
    @Subcomponent.Factory
    interface Factory {
        fun create(): DreamClockTimeComplicationComponent
    }

    /** Creates a view holder for the clock complication. */
    fun getViewHolder(): DreamClockTimeViewHolder

    /** Module for providing injected values within the clock complication scope. */
    @Module
    interface DreamClockTimeComplicationModule {
        companion object {
            const val DREAM_CLOCK_TIME_COMPLICATION_VIEW = "clock_time_complication_view"
            private const val TAG_WEIGHT = "'wght' "
            private const val WEIGHT = 400

            /** Provides the complication view. */
            @Provides
            @DreamClockTimeComplicationScope
            @Named(DREAM_CLOCK_TIME_COMPLICATION_VIEW)
            fun provideComplicationView(layoutInflater: LayoutInflater): View {
                val view =
                    Preconditions.checkNotNull(
                        layoutInflater.inflate(
                            R.layout.dream_overlay_complication_clock_time,
                            /* root = */ null,
                            /* attachToRoot = */ false,
                        ) as TextClock,
                        "R.layout.dream_overlay_complication_clock_time did not properly inflate"
                    )
                view.setFontVariationSettings(TAG_WEIGHT + WEIGHT)
                return view
            }
        }
    }
}
+0 −55
Original line number 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.systemui.complication.dagger;


import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextClock;

import com.android.internal.util.Preconditions;
import com.android.systemui.R;
import com.android.systemui.complication.DreamClockTimeComplication;

import dagger.Module;
import dagger.Provides;

import javax.inject.Named;

/**
 * Module for providing {@link DreamClockTimeComplication}.
 */
@Module
public interface DreamClockTimeComplicationModule {
    String DREAM_CLOCK_TIME_COMPLICATION_VIEW = "clock_time_complication_view";
    String TAG_WEIGHT = "'wght' ";
    int WEIGHT = 400;

    /**
     * Provides the complication view.
     */
    @Provides
    @Named(DREAM_CLOCK_TIME_COMPLICATION_VIEW)
    static View provideComplicationView(LayoutInflater layoutInflater) {
        final TextClock view = (TextClock) Preconditions.checkNotNull(
                        layoutInflater.inflate(R.layout.dream_overlay_complication_clock_time,
                                null, false),
                "R.layout.dream_overlay_complication_clock_time did not properly inflated");
        view.setFontVariationSettings(TAG_WEIGHT + WEIGHT);
        return view;
    }
}
+2 −3
Original line number Diff line number Diff line
@@ -35,10 +35,9 @@ import javax.inject.Named;
 * Module for all components with corresponding dream layer complications registered in
 * {@link SystemUIBinder}.
 */
@Module(includes = {
                DreamClockTimeComplicationModule.class,
        },
@Module(
        subcomponents = {
                DreamClockTimeComplicationComponent.class,
                DreamHomeControlsComplicationComponent.class,
                DreamMediaEntryComplicationComponent.class
        })
+12 −10
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.view.View;
import androidx.test.filters.SmallTest;

import com.android.systemui.SysuiTestCase;
import com.android.systemui.complication.dagger.DreamClockTimeComplicationComponent;
import com.android.systemui.condition.SelfExecutingMonitor;
import com.android.systemui.dreams.DreamOverlayStateController;
import com.android.systemui.shared.condition.Monitor;
@@ -39,8 +40,6 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import javax.inject.Provider;

@SmallTest
@RunWith(AndroidTestingRunner.class)
public class DreamClockTimeComplicationTest extends SysuiTestCase {
@@ -55,8 +54,10 @@ public class DreamClockTimeComplicationTest extends SysuiTestCase {
    private DreamClockTimeComplication mComplication;

    @Mock
    private Provider<DreamClockTimeComplication.DreamClockTimeViewHolder>
            mDreamClockTimeViewHolderProvider;
    private DreamClockTimeComplicationComponent.Factory mComponentFactory;

    @Mock
    private DreamClockTimeComplicationComponent mComponent;

    @Mock
    private DreamClockTimeComplication.DreamClockTimeViewHolder
@@ -76,7 +77,8 @@ public class DreamClockTimeComplicationTest extends SysuiTestCase {
    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        when(mDreamClockTimeViewHolderProvider.get()).thenReturn(mDreamClockTimeViewHolder);
        when(mComponentFactory.create()).thenReturn(mComponent);
        when(mComponent.getViewHolder()).thenReturn(mDreamClockTimeViewHolder);
        mMonitor = SelfExecutingMonitor.createInstance();
    }

@@ -100,21 +102,21 @@ public class DreamClockTimeComplicationTest extends SysuiTestCase {
    @Test
    public void testComplicationRequiredTypeAvailability() {
        final DreamClockTimeComplication complication =
                new DreamClockTimeComplication(mDreamClockTimeViewHolderProvider);
                new DreamClockTimeComplication(mComponentFactory);
        assertEquals(Complication.COMPLICATION_TYPE_TIME,
                complication.getRequiredTypeAvailability());
    }

    /**
     * Verifies {@link DreamClockTimeComplication.DreamClockTimeViewHolder} is obtainable from its
     * provider when the complication creates view.
     * component when the complication creates view.
     */
    @Test
    public void testComplicationViewHolderProviderOnCreateView() {
    public void testComplicationViewHolderComponentOnCreateView() {
        final DreamClockTimeComplication complication =
                new DreamClockTimeComplication(mDreamClockTimeViewHolderProvider);
                new DreamClockTimeComplication(mComponentFactory);
        final Complication.ViewHolder viewHolder = complication.createView(mComplicationViewModel);
        verify(mDreamClockTimeViewHolderProvider).get();
        verify(mComponent).getViewHolder();
        assertThat(viewHolder).isEqualTo(mDreamClockTimeViewHolder);
    }