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

Commit 29007e6d authored by Kunhung Li's avatar Kunhung Li
Browse files

Add clock plugin function

Use clock plugin interface to replace current TextClock in keyguard

Bug: 111971817
Test: atest SystemUITests
Change-Id: Ib6920844700445d9cd3ffa4159cd7f630eaa853b
parent 1b35253f
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
**
** Copyright 2018, 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.
*/
-->

<!-- This is a view that shows clock information in Keyguard. -->
<com.android.keyguard.KeyguardClockSwitch
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_alignParentTop="true">
    <TextClock
        android:id="@+id/default_clock_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:letterSpacing="0.03"
        android:textColor="?attr/wallpaperTextColor"
        android:singleLine="true"
        style="@style/widget_big_thin"
        android:format12Hour="@string/keyguard_widget_12_hours_format"
        android:format24Hour="@string/keyguard_widget_24_hours_format" />
</com.android.keyguard.KeyguardClockSwitch>
+4 −13
Original line number Diff line number Diff line
@@ -38,19 +38,10 @@
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|top">
                <TextClock
                <include layout="@layout/keyguard_clock_switch"
                         android:id="@+id/clock_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_centerHorizontal="true"
                    android:layout_alignParentTop="true"
                    android:letterSpacing="0.03"
                    android:textColor="?attr/wallpaperTextColor"
                    android:singleLine="true"
                    style="@style/widget_big_thin"
                    android:format12Hour="@string/keyguard_widget_12_hours_format"
                    android:format24Hour="@string/keyguard_widget_24_hours_format" />
                         android:layout_width="match_parent"
                         android:layout_height="wrap_content" />
                <View
                    android:id="@+id/clock_separator"
                    android:layout_width="@dimen/widget_separator_width"
+4 −13
Original line number Diff line number Diff line
@@ -55,19 +55,10 @@
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|top">
            <TextClock
            <include layout="@layout/keyguard_clock_switch"
                 android:id="@+id/clock_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_centerHorizontal="true"
                android:layout_alignParentTop="true"
                android:letterSpacing="0.03"
                android:textColor="?attr/wallpaperTextColor"
                android:singleLine="true"
                style="@style/widget_big_thin"
                android:format12Hour="@string/keyguard_widget_12_hours_format"
                android:format24Hour="@string/keyguard_widget_24_hours_format" />
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content" />
            <View
                android:id="@+id/clock_separator"
                android:layout_width="@dimen/widget_separator_width"
+155 −0
Original line number Diff line number Diff line
package com.android.keyguard;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextClock;

import androidx.annotation.VisibleForTesting;

import com.android.systemui.Dependency;
import com.android.systemui.plugins.ClockPlugin;
import com.android.systemui.plugins.PluginListener;
import com.android.systemui.plugins.PluginManager;

/**
 * Switch to show plugin clock when plugin is connected, otherwise it will show default clock.
 */
public class KeyguardClockSwitch extends FrameLayout {
    /**
     * Optional/alternative clock injected via plugin.
     */
    private ClockPlugin mClockPlugin;
    /**
     * Default clock.
     */
    private TextClock mClockView;

    private final PluginListener<ClockPlugin> mClockPluginListener =
            new PluginListener<ClockPlugin>() {
                @Override
                public void onPluginConnected(ClockPlugin plugin, Context pluginContext) {
                    View view = plugin.getView();
                    if (view != null) {
                        mClockPlugin = plugin;
                        addView(view, -1,
                                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                        ViewGroup.LayoutParams.WRAP_CONTENT));
                        initPluginParams();
                        mClockView.setVisibility(View.GONE);
                    }
                }

                @Override
                public void onPluginDisconnected(ClockPlugin plugin) {
                    View view = plugin.getView();
                    if (view != null) {
                        mClockPlugin = null;
                        removeView(view);
                        mClockView.setVisibility(View.VISIBLE);
                    }
                }
            };

    public KeyguardClockSwitch(Context context) {
        this(context, null);
    }

    public KeyguardClockSwitch(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mClockView = findViewById(R.id.default_clock_view);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Dependency.get(PluginManager.class).addPluginListener(mClockPluginListener,
                ClockPlugin.class);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Dependency.get(PluginManager.class).removePluginListener(mClockPluginListener);
    }

    /**
     * It will also update plugin setStyle if plugin is connected.
     */
    public void setStyle(Style style) {
        mClockView.getPaint().setStyle(style);
        if (mClockPlugin != null) {
            mClockPlugin.setStyle(style);
        }
    }

    /**
     * It will also update plugin setTextColor if plugin is connected.
     */
    public void setTextColor(int color) {
        mClockView.setTextColor(color);
        if (mClockPlugin != null) {
            mClockPlugin.setTextColor(color);
        }
    }

    public void setShowCurrentUserTime(boolean showCurrentUserTime) {
        mClockView.setShowCurrentUserTime(showCurrentUserTime);
    }

    public void setElegantTextHeight(boolean elegant) {
        mClockView.setElegantTextHeight(elegant);
    }

    public void setTextSize(int unit, float size) {
        mClockView.setTextSize(unit, size);
    }

    public void setFormat12Hour(CharSequence format) {
        mClockView.setFormat12Hour(format);
    }

    public void setFormat24Hour(CharSequence format) {
        mClockView.setFormat24Hour(format);
    }

    public Paint getPaint() {
        return mClockView.getPaint();
    }

    public int getCurrentTextColor() {
        return mClockView.getCurrentTextColor();
    }

    public float getTextSize() {
        return mClockView.getTextSize();
    }

    public void refresh() {
        mClockView.refresh();
    }

    /**
     * When plugin changes, set all kept parameters into newer plugin.
     */
    private void initPluginParams() {
        if (mClockPlugin != null) {
            mClockPlugin.setStyle(getPaint().getStyle());
            mClockPlugin.setTextColor(getCurrentTextColor());
        }
    }

    @VisibleForTesting (otherwise = VisibleForTesting.NONE)
    PluginListener getClockPluginListener() {
        return mClockPluginListener;
    }
}
+3 −4
Original line number Diff line number Diff line
@@ -39,7 +39,6 @@ import android.util.TypedValue;
import android.view.View;
import android.widget.GridLayout;
import android.widget.RelativeLayout;
import android.widget.TextClock;
import android.widget.TextView;

import com.android.internal.widget.LockPatternUtils;
@@ -64,7 +63,7 @@ public class KeyguardStatusView extends GridLayout implements
    private final float mSmallClockScale;

    private TextView mLogoutView;
    private TextClock mClockView;
    private KeyguardClockSwitch mClockView;
    private View mClockSeparator;
    private TextView mOwnerInfo;
    private KeyguardSliceView mKeyguardSlice;
@@ -248,7 +247,7 @@ public class KeyguardStatusView extends GridLayout implements
                        .scaleX(clockScale)
                        .scaleY(clockScale)
                        .withEndAction(() -> {
                            mClockView.getPaint().setStyle(style);
                            mClockView.setStyle(style);
                            mClockView.invalidate();
                        })
                        .start();
@@ -256,7 +255,7 @@ public class KeyguardStatusView extends GridLayout implements
                mClockView.setY(top);
                mClockView.setScaleX(clockScale);
                mClockView.setScaleY(clockScale);
                mClockView.getPaint().setStyle(style);
                mClockView.setStyle(style);
                mClockView.invalidate();
            }
        } else if (view == mClockSeparator) {
Loading