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

Commit 1f33a02b authored by Alon Albert's avatar Alon Albert
Browse files

Replace DigitialClock with TextTime

Bug: 10823373
Change-Id: I67a5baf46fb7273130eb2ad3fdcac497f96a1ea5
parent 564e58db
Loading
Loading
Loading
Loading
+11 −36
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:dc="http://schemas.android.com/apk/res-auto"

    android:id="@+id/alarm_item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
@@ -33,45 +35,18 @@
        android:layout_marginEnd="12dp"
        android:layout_marginTop="12dp"
        android:orientation="horizontal">
        <com.android.deskclock.DigitalClock
        <com.android.deskclock.widget.TextTime
            android:id="@+id/digital_clock"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="top"
            android:clickable="true"
            android:layoutDirection="ltr"
            android:background="@drawable/item_background">
            <TextView
                android:id="@+id/timeDisplayHours"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="@dimen/mini_time_margin_right"
                android:paddingEnd="@dimen/mini_time_margin_right"
                android:singleLine="true"
                android:ellipsize="none"
                style="@style/small_light"
                android:textSize="@dimen/alarm_time_font_size"
                android:textColor="@color/clock_white"/>
            <TextView
                android:id="@+id/timeDisplayMinutes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/small_light"
            style="@style/medium_light"
            android:textSize="@dimen/alarm_time_font_size"
                android:singleLine="true"
                android:ellipsize="none"
                android:textColor="@color/clock_white" />
            <TextView
                android:id="@+id/am_pm"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                style="@style/label"
                android:paddingLeft="6dp"
                android:paddingStart="6dp"
                android:singleLine="true"
                android:ellipsize="none"
                android:textColor="@color/clock_white" />
        </com.android.deskclock.DigitalClock>
            android:textColor="@color/clock_white"
            android:baselineAligned="true"
            android:layout_gravity="center"
            android:gravity="center"
            dc:format12Hour="@string/clock_12_hours_format_with_ampm"
            dc:format24Hour="@string/widget_24_hours_format"/>
        <Space
            android:layout_height="match_parent"
            android:layout_width="0dip"
+13 −0
Original line number Diff line number Diff line
@@ -73,4 +73,17 @@
        <attr name="jewelColor" format="color"/>
    </declare-styleable>

    <declare-styleable name="TextTime">
        <!-- Specifies the formatting pattern used to show the time and/or date
             in 12-hour mode. Please refer to {@link android.text.format.DateFormat}
             for a complete description of accepted formatting patterns.
             The default pattern is "h:mm a". -->
        <attr name="format12Hour" format="string"/>
        <!-- Specifies the formatting pattern used to show the time and/or date
             in 24-hour mode. Please refer to {@link android.text.format.DateFormat}
             for a complete description of accepted formatting patterns.
             The default pattern is "H:mm". -->
        <attr name="format24Hour" format="string"/>
   </declare-styleable>

</resources>
+4 −4
Original line number Diff line number Diff line
@@ -71,6 +71,7 @@ import com.android.deskclock.provider.Alarm;
import com.android.deskclock.provider.AlarmInstance;
import com.android.deskclock.provider.DaysOfWeek;
import com.android.deskclock.widget.ActionableToastBar;
import com.android.deskclock.widget.TextTime;

import java.text.DateFormatSymbols;
import java.util.Calendar;
@@ -725,7 +726,7 @@ public class AlarmClockFragment extends DeskClockFragment implements

            // views for optimization
            LinearLayout alarmItem;
            DigitalClock clock;
            TextTime clock;
            Switch onoff;
            TextView daysOfWeek;
            TextView label;
@@ -993,8 +994,7 @@ public class AlarmClockFragment extends DeskClockFragment implements
            // standard view holder optimization
            final ItemHolder holder = new ItemHolder();
            holder.alarmItem = (LinearLayout) view.findViewById(R.id.alarm_item);
            holder.clock = (DigitalClock) view.findViewById(R.id.digital_clock);
            holder.clock.setLive(false);
            holder.clock = (TextTime) view.findViewById(R.id.digital_clock);
            holder.onoff = (Switch) view.findViewById(R.id.onoff);
            holder.onoff.setTypeface(mRobotoNormal);
            holder.daysOfWeek = (TextView) view.findViewById(R.id.daysOfWeek);
@@ -1063,7 +1063,7 @@ public class AlarmClockFragment extends DeskClockFragment implements
                setItemAlpha(itemHolder, itemHolder.onoff.isChecked());
            }

            itemHolder.clock.updateTime(alarm.hour, alarm.minutes);
            itemHolder.clock.setTime(alarm.hour, alarm.minutes);
            itemHolder.clock.setClickable(true);
            itemHolder.clock.setOnClickListener(new View.OnClickListener() {
                @Override
+0 −229
Original line number Diff line number Diff line
/*
 * Copyright (C) 2008 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.deskclock;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.graphics.Typeface;
import android.os.Handler;
import android.provider.Settings;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.TimeZone;

/**
 * Displays the time
 */
public class DigitalClock extends LinearLayout {

    private final static String HOURS_24 = "kk";
    private final static String HOURS = "h";
    private final static String MINUTES = "mm";

    private Calendar mCalendar;
    private String mHoursFormat;
    private TextView mTimeDisplayHours, mTimeDisplayMinutes;
    private AmPm mAmPm;
    private ContentObserver mFormatChangeObserver;
    private boolean mLive = true;
    private boolean mAttached;
    private final Typeface mRobotoThin;
    private String mTimeZoneId;


    /* called by system on minute ticks */
    private final Handler mHandler = new Handler();
    private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (mLive && intent.getAction().equals(
                            Intent.ACTION_TIMEZONE_CHANGED)) {
                    mCalendar = Calendar.getInstance();
                }
                // Post a runnable to avoid blocking the broadcast.
                mHandler.post(new Runnable() {
                        public void run() {
                            updateTime();
                        }
                });
            }
        };

    static class AmPm {
        private final TextView mAmPm;
        private final String mAmString, mPmString;

        AmPm(View parent) {
            mAmPm = (TextView) parent.findViewById(R.id.am_pm);

            String[] ampm = new DateFormatSymbols().getAmPmStrings();
            mAmString = ampm[0];
            mPmString = ampm[1];
        }

        void setShowAmPm(boolean show) {
            mAmPm.setVisibility(show ? View.VISIBLE : View.GONE);
        }

        void setIsMorning(boolean isMorning) {
            mAmPm.setText(isMorning ? mAmString : mPmString);
        }

        CharSequence getAmPmText() {
            return mAmPm.getText();
        }
    }

    private class FormatChangeObserver extends ContentObserver {
        public FormatChangeObserver() {
            super(new Handler());
        }
        @Override
        public void onChange(boolean selfChange) {
            setDateFormat();
            updateTime();
        }
    }

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

    public DigitalClock(Context context, AttributeSet attrs) {
        super(context, attrs);
        mRobotoThin = Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Thin.ttf");
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mTimeDisplayHours = (TextView)findViewById(R.id.timeDisplayHours);
        mTimeDisplayHours.setTypeface(mRobotoThin);
        mTimeDisplayMinutes = (TextView)findViewById(R.id.timeDisplayMinutes);
        mTimeDisplayMinutes.setTypeface(mRobotoThin);
        mAmPm = new AmPm(this);
        mCalendar = Calendar.getInstance();

        setDateFormat();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        if (Log.LOGV) Log.v("onAttachedToWindow " + this);

        if (mAttached) return;
        mAttached = true;

        if (mLive) {
            /* monitor time ticks, time changed, timezone */
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_TIME_TICK);
            filter.addAction(Intent.ACTION_TIME_CHANGED);
            filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
            getContext().registerReceiver(mIntentReceiver, filter);
        }

        /* monitor 12/24-hour display preference */
        mFormatChangeObserver = new FormatChangeObserver();
        getContext().getContentResolver().registerContentObserver(
                Settings.System.CONTENT_URI, true, mFormatChangeObserver);

        updateTime();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();

        if (!mAttached) return;
        mAttached = false;

        if (mLive) {
            getContext().unregisterReceiver(mIntentReceiver);
        }
        getContext().getContentResolver().unregisterContentObserver(
                mFormatChangeObserver);
    }


    void updateTime(Calendar c) {
        mCalendar = c;
        updateTime();
    }

    public void updateTime(int hour, int minute) {
        // set the alarm text
        final Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        mCalendar = c;
        updateTime();
    }

    private void updateTime() {
        if (mLive) {
            mCalendar.setTimeInMillis(System.currentTimeMillis());
        }
        if (mTimeZoneId != null) {
            mCalendar.setTimeZone(TimeZone.getTimeZone(mTimeZoneId));
        }

        StringBuilder fullTimeStr = new StringBuilder();
        CharSequence newTime = DateFormat.format(mHoursFormat, mCalendar);
        mTimeDisplayHours.setText(newTime);
        fullTimeStr.append(newTime);
        newTime = DateFormat.format(MINUTES, mCalendar);
        fullTimeStr.append(newTime);
        mTimeDisplayMinutes.setText(newTime);

        boolean isMorning = mCalendar.get(Calendar.AM_PM) == 0;
        mAmPm.setIsMorning(isMorning);
        if (!DateFormat.is24HourFormat(getContext())) {
            fullTimeStr.append(mAmPm.getAmPmText());
        }

        // Update accessibility string.
        setContentDescription(fullTimeStr);
    }

    private void setDateFormat() {
        boolean is24HourMode = DateFormat.is24HourFormat(getContext());
        mHoursFormat = is24HourMode ? HOURS_24 : HOURS;
        mAmPm.setShowAmPm(!is24HourMode);
    }

    void setLive(boolean live) {
        mLive = live;
    }

    public void setTimeZone(String id) {
        mTimeZoneId = id;
        updateTime();
    }
}
+152 −0
Original line number Diff line number Diff line
// Copyright 2013 Google Inc. All Rights Reserved.

package com.android.deskclock.widget;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.TypedArray;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.provider.Settings;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.widget.TextView;

import com.android.deskclock.R;

import java.util.Calendar;

/**
 * Based on {@link android.widget.TextClock}, This widget displays a constant time of day using
 * format specifiers. {@link android.widget.TextClock} Doesn't support a non ticking clock.
 */
public class TextTime extends TextView {
    public static final CharSequence DEFAULT_FORMAT_12_HOUR = "h:mm a";

    public static final CharSequence DEFAULT_FORMAT_24_HOUR = "H:mm";

    private CharSequence mFormat12;
    private CharSequence mFormat24;
    private CharSequence mFormat;

    private boolean mAttached;

    private int mHour;
    private int mMinute;

    private final ContentObserver mFormatChangeObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            chooseFormat();
            updateTime();
        }

        @Override
        public void onChange(boolean selfChange, Uri uri) {
            chooseFormat();
            updateTime();
        }
    };

    @SuppressWarnings("UnusedDeclaration")
    public TextTime(Context context) {
        this(context, null);
    }

    @SuppressWarnings("UnusedDeclaration")
    public TextTime(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TextTime(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        final TypedArray styledAttributes = context.obtainStyledAttributes(
                attrs, R.styleable.TextTime, defStyle, 0);
        try {
            mFormat12 = styledAttributes.getText(R.styleable.TextTime_format12Hour);
            mFormat24 = styledAttributes.getText(R.styleable.TextTime_format24Hour);
        } finally {
            styledAttributes.recycle();
        }
        chooseFormat();
    }

    @SuppressWarnings("UnusedDeclaration")
    public CharSequence getFormat12Hour() {
        return mFormat12;
    }

    @SuppressWarnings("UnusedDeclaration")
    public void setFormat12Hour(CharSequence format) {
        mFormat12 = format;

        chooseFormat();
        updateTime();
    }

    @SuppressWarnings("UnusedDeclaration")
    public CharSequence getFormat24Hour() {
        return mFormat24;
    }

    @SuppressWarnings("UnusedDeclaration")
    public void setFormat24Hour(CharSequence format) {
        mFormat24 = format;

        chooseFormat();
        updateTime();
    }

    private void chooseFormat() {
        final boolean format24Requested = DateFormat.is24HourFormat(getContext());
        if (format24Requested) {
            mFormat = mFormat24 == null ? DEFAULT_FORMAT_24_HOUR : mFormat24;
        } else {
            mFormat = mFormat12 == null ? DEFAULT_FORMAT_12_HOUR : mFormat12;
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (!mAttached) {
            mAttached = true;
            registerObserver();
            updateTime();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mAttached) {
            unregisterObserver();
            mAttached = false;
        }
    }

    private void registerObserver() {
        final ContentResolver resolver = getContext().getContentResolver();
        resolver.registerContentObserver(Settings.System.CONTENT_URI, true, mFormatChangeObserver);
    }

    private void unregisterObserver() {
        final ContentResolver resolver = getContext().getContentResolver();
        resolver.unregisterContentObserver(mFormatChangeObserver);
    }

    public void setTime(int hour, int minute) {
        mHour = hour;
        mMinute = minute;
        updateTime();
    }

    private void updateTime() {
        final Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, mHour);
        calendar.set(Calendar.MINUTE, mMinute);
        setText(DateFormat.format(mFormat, calendar));
    }
}