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

Commit 86689039 authored by Lucas Silva's avatar Lucas Silva
Browse files

Create dream preview complication

Test: locally on device
Bug: 219747041

Change-Id: I93f6dd4b4e025b5929d272e23ea887792efb0b33
parent f084f35b
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ 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.
  -->
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dream_preview_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="@dimen/dream_overlay_complication_preview_text_size"
    android:textColor="@android:color/white"
    android:shadowColor="@color/keyguard_shadow_color"
    android:shadowRadius="?attr/shadowRadius"
    android:gravity="center_vertical"
    android:drawableStart="@drawable/ic_arrow_back"
    android:drawablePadding="@dimen/dream_overlay_complication_preview_icon_padding"
    android:drawableTint="@android:color/white"/>
+2 −0
Original line number Diff line number Diff line
@@ -1347,6 +1347,8 @@
    <dimen name="dream_overlay_complication_clock_time_text_size">72sp</dimen>
    <dimen name="dream_overlay_complication_clock_date_text_size">18sp</dimen>
    <dimen name="dream_overlay_complication_weather_text_size">18sp</dimen>
    <dimen name="dream_overlay_complication_preview_text_size">36sp</dimen>
    <dimen name="dream_overlay_complication_preview_icon_padding">28dp</dimen>

    <!-- The position of the end guide, which dream overlay complications can align their start with
         if their end is aligned with the parent end. Represented as the percentage over from the
+11 −1
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import com.android.keyguard.KeyguardUpdateMonitor;
import com.android.keyguard.KeyguardUpdateMonitorCallback;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.dreams.complication.Complication;
import com.android.systemui.dreams.complication.DreamPreviewComplication;
import com.android.systemui.dreams.dagger.DreamOverlayComponent;
import com.android.systemui.dreams.touch.DreamOverlayTouchMonitor;

@@ -57,6 +58,7 @@ public class DreamOverlayService extends android.service.dreams.DreamOverlayServ
    // content area).
    private final DreamOverlayContainerViewController mDreamOverlayContainerViewController;
    private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
    private final DreamPreviewComplication mPreviewComplication;

    // A reference to the {@link Window} used to hold the dream overlay.
    private Window mWindow;
@@ -96,12 +98,14 @@ public class DreamOverlayService extends android.service.dreams.DreamOverlayServ
            @Main Executor executor,
            DreamOverlayComponent.Factory dreamOverlayComponentFactory,
            DreamOverlayStateController stateController,
            KeyguardUpdateMonitor keyguardUpdateMonitor) {
            KeyguardUpdateMonitor keyguardUpdateMonitor,
            DreamPreviewComplication previewComplication) {
        mContext = context;
        mExecutor = executor;
        mKeyguardUpdateMonitor = keyguardUpdateMonitor;
        mKeyguardUpdateMonitor.registerCallback(mKeyguardCallback);
        mStateController = stateController;
        mPreviewComplication = previewComplication;

        final DreamOverlayComponent component =
                dreamOverlayComponentFactory.create(mViewModelStore, mHost);
@@ -125,6 +129,8 @@ public class DreamOverlayService extends android.service.dreams.DreamOverlayServ
            windowManager.removeView(mWindow.getDecorView());
        }
        mStateController.setOverlayActive(false);
        mPreviewComplication.setDreamLabel(null);
        mStateController.removeComplication(mPreviewComplication);
        super.onDestroy();
    }

@@ -133,6 +139,10 @@ public class DreamOverlayService extends android.service.dreams.DreamOverlayServ
        setCurrentState(Lifecycle.State.STARTED);
        mExecutor.execute(() -> {
            mStateController.setShouldShowComplications(shouldShowComplications());
            if (isPreviewMode()) {
                mPreviewComplication.setDreamLabel(getDreamLabel());
                mStateController.addComplication(mPreviewComplication);
            }
            addOverlayWindowLocked(layoutParams);
            setCurrentState(Lifecycle.State.RESUMED);
            mStateController.setOverlayActive(true);
+130 −0
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.dreams.complication;

import static com.android.systemui.dreams.complication.dagger.DreamPreviewComplicationComponent.DREAM_LABEL;
import static com.android.systemui.dreams.complication.dagger.DreamPreviewComplicationComponent.DreamPreviewComplicationModule.DREAM_PREVIEW_COMPLICATION_LAYOUT_PARAMS;
import static com.android.systemui.dreams.complication.dagger.DreamPreviewComplicationComponent.DreamPreviewComplicationModule.DREAM_PREVIEW_COMPLICATION_VIEW;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.android.systemui.dreams.complication.dagger.DreamPreviewComplicationComponent;
import com.android.systemui.util.ViewController;

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

/**
 * Preview complication shown when user is previewing a dream.
 */
public class DreamPreviewComplication implements Complication {
    DreamPreviewComplicationComponent.Factory mComponentFactory;
    @Nullable
    private CharSequence mDreamLabel;

    /**
     * Default constructor for {@link DreamPreviewComplication}.
     */
    @Inject
    public DreamPreviewComplication(
            DreamPreviewComplicationComponent.Factory componentFactory) {
        mComponentFactory = componentFactory;
    }

    /**
     * Create {@link DreamPreviewViewHolder}.
     */
    @Override
    public ViewHolder createView(ComplicationViewModel model) {
        return mComponentFactory.create(model, mDreamLabel).getViewHolder();
    }

    /**
     * Sets the user-facing label for the current dream.
     */
    public void setDreamLabel(@Nullable CharSequence dreamLabel) {
        mDreamLabel = dreamLabel;
    }

    /**
     * ViewHolder to contain value/logic associated with a Preview Complication View.
     */
    public static class DreamPreviewViewHolder implements ViewHolder {
        private final TextView mView;
        private final ComplicationLayoutParams mLayoutParams;
        private final DreamPreviewViewController mViewController;

        @Inject
        DreamPreviewViewHolder(@Named(DREAM_PREVIEW_COMPLICATION_VIEW) TextView view,
                DreamPreviewViewController controller,
                @Named(DREAM_PREVIEW_COMPLICATION_LAYOUT_PARAMS)
                        ComplicationLayoutParams layoutParams,
                @Named(DREAM_LABEL) @Nullable CharSequence dreamLabel) {
            mView = view;
            mLayoutParams = layoutParams;
            mViewController = controller;
            mViewController.init();

            if (!TextUtils.isEmpty(dreamLabel)) {
                mView.setText(dreamLabel);
            }
        }

        @Override
        public View getView() {
            return mView;
        }

        @Override
        public ComplicationLayoutParams getLayoutParams() {
            return mLayoutParams;
        }

        @Override
        public int getCategory() {
            return CATEGORY_SYSTEM;
        }
    }

    /**
     * ViewController to contain value/logic associated with a Preview Complication View.
     */
    static class DreamPreviewViewController extends ViewController<TextView> {
        private final ComplicationViewModel mViewModel;

        @Inject
        DreamPreviewViewController(@Named(DREAM_PREVIEW_COMPLICATION_VIEW) TextView view,
                ComplicationViewModel viewModel) {
            super(view);
            mViewModel = viewModel;
        }

        @Override
        protected void onViewAttached() {
            mView.setOnClickListener(v -> mViewModel.exitDream());
        }

        @Override
        protected void onViewDetached() {
            mView.setOnClickListener(null);
        }
    }
}
+117 −0
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.dreams.complication.dagger;


import static java.lang.annotation.RetentionPolicy.RUNTIME;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.android.internal.util.Preconditions;
import com.android.systemui.R;
import com.android.systemui.dreams.complication.ComplicationLayoutParams;
import com.android.systemui.dreams.complication.ComplicationViewModel;
import com.android.systemui.dreams.complication.DreamPreviewComplication.DreamPreviewViewHolder;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;

import javax.inject.Named;
import javax.inject.Scope;

import dagger.BindsInstance;
import dagger.Module;
import dagger.Provides;
import dagger.Subcomponent;

/**
 * {@link DreamPreviewComplicationComponent} is responsible for generating dependencies
 * surrounding the
 * Preview {@link com.android.systemui.dreams.complication.Complication}, such as the layout
 * details.
 */
@Subcomponent(modules = {
        DreamPreviewComplicationComponent.DreamPreviewComplicationModule.class,
})
@DreamPreviewComplicationComponent.DreamPreviewComplicationScope
public interface DreamPreviewComplicationComponent {
    String DREAM_LABEL = "dream_label";

    /**
     * Creates {@link DreamPreviewViewHolder}.
     */
    DreamPreviewViewHolder getViewHolder();

    @Documented
    @Retention(RUNTIME)
    @Scope
    @interface DreamPreviewComplicationScope {
    }

    /**
     * Generates {@link DreamPreviewComplicationComponent}.
     */
    @Subcomponent.Factory
    interface Factory {
        DreamPreviewComplicationComponent create(
                @BindsInstance ComplicationViewModel viewModel,
                @Named(DREAM_LABEL) @BindsInstance @Nullable CharSequence dreamLabel);
    }

    /**
     * Scoped values for {@link DreamPreviewComplicationComponent}.
     */
    @Module
    interface DreamPreviewComplicationModule {
        String DREAM_PREVIEW_COMPLICATION_VIEW = "preview_complication_view";
        String DREAM_PREVIEW_COMPLICATION_LAYOUT_PARAMS = "preview_complication_layout_params";
        // Order weight of insert into parent container
        int INSERT_ORDER_WEIGHT = 1000;

        /**
         * Provides the complication view.
         */
        @Provides
        @DreamPreviewComplicationScope
        @Named(DREAM_PREVIEW_COMPLICATION_VIEW)
        static TextView provideComplicationView(LayoutInflater layoutInflater) {
            return Preconditions.checkNotNull((TextView)
                            layoutInflater.inflate(R.layout.dream_overlay_complication_preview,
                                    null, false),
                    "R.layout.dream_overlay_complication_preview did not properly inflated");
        }

        /**
         * Provides the layout parameters for the complication view.
         */
        @Provides
        @DreamPreviewComplicationScope
        @Named(DREAM_PREVIEW_COMPLICATION_LAYOUT_PARAMS)
        static ComplicationLayoutParams provideLayoutParams() {
            return new ComplicationLayoutParams(0,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ComplicationLayoutParams.POSITION_TOP
                            | ComplicationLayoutParams.POSITION_START,
                    ComplicationLayoutParams.DIRECTION_DOWN,
                    INSERT_ORDER_WEIGHT, /* snapToGuide= */ true);
        }
    }
}
Loading