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

Commit d6c13497 authored by Dylan Phan's avatar Dylan Phan
Browse files

Dynamically size fonts in the digital widget

Bug: 22690473

Also introduced DataModel.run() for accessing data on the correct thread.

Change-Id: I6c21b9293882e6aa502e6930bd039d342d9cbfd4
parent 19a6a8fb
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -94,7 +94,8 @@ public class DigitalAppWidgetProvider extends AppWidgetProvider {
                    RemoteViews widget = new RemoteViews(context.getPackageName(),
                            R.layout.digital_appwidget);
                    float ratio = WidgetUtils.getScaleRatio(context, null, appWidgetId);
                    WidgetUtils.setTimeFormat(context, widget, 0/*no am/pm*/, R.id.the_clock);
                    WidgetUtils.setTimeFormat(context, widget, false /* showAmPm */,
                            R.id.the_clock);
                    WidgetUtils.setClockSize(context, widget, ratio);
                    refreshAlarm(context, widget);
                    appWidgetManager.partiallyUpdateAppWidget(appWidgetId, widget);
@@ -125,6 +126,11 @@ public class DigitalAppWidgetProvider extends AppWidgetProvider {
                    appWidgetManager.
                            notifyAppWidgetViewDataChanged(appWidgetId,
                                    R.id.digital_appwidget_listview);
                    final RemoteViews widget = new RemoteViews(context.getPackageName(),
                            R.layout.digital_appwidget);
                    final float ratio = WidgetUtils.getScaleRatio(context, null, appWidgetId);
                    WidgetUtils.setClockSize(context, widget, ratio);
                    appWidgetManager.partiallyUpdateAppWidget(appWidgetId, widget);
                }
            }
        }
@@ -183,7 +189,7 @@ public class DigitalAppWidgetProvider extends AppWidgetProvider {

        // Setup alarm text clock's format and font sizes
        refreshAlarm(context, widget);
        WidgetUtils.setTimeFormat(context, widget, 0/*no am/pm*/, R.id.the_clock);
        WidgetUtils.setTimeFormat(context, widget, false /* showAmPm */, R.id.the_clock);
        WidgetUtils.setClockSize(context, widget, ratio);

        // Set today's date format
+6 −28
Original line number Diff line number Diff line
@@ -19,8 +19,6 @@ package com.android.alarmclock;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Looper;
import android.text.format.DateFormat;
import android.util.Log;
import android.util.TypedValue;
@@ -32,6 +30,7 @@ import com.android.deskclock.R;
import com.android.deskclock.data.City;
import com.android.deskclock.data.DataModel;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
@@ -40,7 +39,6 @@ import java.util.TimeZone;
import static android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_ID;
import static android.appwidget.AppWidgetManager.INVALID_APPWIDGET_ID;
import static com.android.deskclock.Utils.enforceMainLooper;
import static com.android.deskclock.Utils.enforceNotMainLooper;
import static java.util.Calendar.DAY_OF_WEEK;

public class DigitalWidgetViewsFactory implements RemoteViewsFactory {
@@ -49,8 +47,6 @@ public class DigitalWidgetViewsFactory implements RemoteViewsFactory {

    private final Intent mFillInIntent = new Intent();

    private final Handler mHandler = new Handler(Looper.getMainLooper());

    private final Context mContext;
    private final Resources mResources;
    private final float mFontSize;
@@ -173,22 +169,9 @@ public class DigitalWidgetViewsFactory implements RemoteViewsFactory {
     */
    @Override
    public synchronized void onDataSetChanged() {
        enforceNotMainLooper();

        // Fetch the data on the main Looper.
        final RefreshRunnable refreshRunnable = new RefreshRunnable();
        mHandler.post(refreshRunnable);

        // Wait for the data to arrive, if it has not.
        synchronized (refreshRunnable) {
            if (refreshRunnable.mCities == null) {
                try {
                    refreshRunnable.wait();
                } catch (InterruptedException ie) {
                    // ignore
                }
            }
        }
        DataModel.getDataModel().run(refreshRunnable);

        // Store the data in local variables.
        mFontScale = WidgetUtils.getScaleRatio(mContext, null, mWidgetId);
@@ -198,8 +181,7 @@ public class DigitalWidgetViewsFactory implements RemoteViewsFactory {
    }

    private void update(RemoteViews clock, City city, int clockId, int labelId, int dayId) {
        final int labelSize = mResources.getDimensionPixelSize(R.dimen.widget_label_font_size);
        WidgetUtils.setTimeFormat(mContext, clock, labelSize, clockId);
        WidgetUtils.setTimeFormat(mContext, clock, true /* showAmPm */, clockId);

        final float fontSize = DateFormat.is24HourFormat(mContext) ? mFont24Size : mFontSize;
        clock.setTextViewTextSize(clockId, TypedValue.COMPLEX_UNIT_PX, fontSize * mFontScale);
@@ -244,13 +226,9 @@ public class DigitalWidgetViewsFactory implements RemoteViewsFactory {
        public void run() {
            enforceMainLooper();

            synchronized (this) {
            mHomeCity = DataModel.getDataModel().getHomeCity();
                mCities = DataModel.getDataModel().getSelectedCities();
            mCities = new ArrayList<>(DataModel.getDataModel().getSelectedCities());
            mShowHomeClock = DataModel.getDataModel().getShowHomeClock();

                notifyAll();
            }
        }
    }
}
 No newline at end of file
+26 −5
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.widget.RemoteViews;

import com.android.deskclock.R;
import com.android.deskclock.Utils;
import com.android.deskclock.data.DataModel;

public class WidgetUtils {
    static final String TAG = "WidgetUtils";
@@ -61,9 +62,18 @@ public class WidgetUtils {
                    < res.getDimension(R.dimen.min_digital_widget_height)) {
                ratio = Math.min(ratio, getHeightScaleRatio(context, options, id));
            }
            return (ratio > 1) ? 1 : ratio;

            final SelectedCitiesRunnable selectedCitiesRunnable = new SelectedCitiesRunnable();
            DataModel.getDataModel().run(selectedCitiesRunnable);
            if (selectedCitiesRunnable.mAnyCitiesSelected) {
                return (ratio > 1f) ? 1f : ratio;
            }
        return 1;

            ratio *= .83f;
            ratio = Math.min(ratio, 1.7f);
            return ratio;
        }
        return 1f;
    }

    // Calculate the scale factor of the fonts in the list of  the widget using the widget height
@@ -133,17 +143,28 @@ public class WidgetUtils {
     * Set the format of the time on the clock according to the locale
     * @param context - Context used to get user's locale and time preferences
     * @param clock - view to format
     * @param amPmFontSize - size of am/pm label, zero size means no am/om label
     * @param showAmPm - show am/pm label if true
     * @param clockId - id of TextClock view as defined in the clock's layout.
     */
    public static void setTimeFormat(Context context, RemoteViews clock, int amPmFontSize,
    public static void setTimeFormat(Context context, RemoteViews clock, boolean showAmPm,
            int clockId) {
        if (clock != null) {
            // Set the best format for 12 hours mode according to the locale
            clock.setCharSequence(clockId, "setFormat12Hour", Utils.get12ModeFormat(context));
            clock.setCharSequence(clockId, "setFormat12Hour",
                    Utils.get12ModeFormat(context, showAmPm));
            // Set the best format for 24 hours mode according to the locale
            clock.setCharSequence(clockId, "setFormat24Hour", Utils.get24ModeFormat());
        }
    }

    private static class SelectedCitiesRunnable implements Runnable {

        private boolean mAnyCitiesSelected;

        @Override
        public void run() {
            mAnyCitiesSelected = !DataModel.getDataModel().getSelectedCities().isEmpty();
        }
    }
}
+6 −2
Original line number Diff line number Diff line
@@ -527,7 +527,7 @@ public class Utils {
    public static void setTimeFormat(Context context, TextClock clock) {
        if (clock != null) {
            // Get the best format for 12 hours mode according to the locale
            clock.setFormat12Hour(get12ModeFormat(context));
            clock.setFormat12Hour(get12ModeFormat(context, true /* showAmPm */));
            // Get the best format for 24 hours mode according to the locale
            clock.setFormat24Hour(get24ModeFormat());
        }
@@ -551,10 +551,14 @@ public class Utils {

    /**
     * @param context - context used to get time format string resource
     * @param showAmPm - include the am/pm string if true
     * @return format string for 12 hours mode time
     */
    public static CharSequence get12ModeFormat(Context context) {
    public static CharSequence get12ModeFormat(Context context, boolean showAmPm) {
        String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), "hma");
        if (!showAmPm) {
            pattern = pattern.replaceAll("a", "").trim();
        }

        // Replace spaces with "Hair Space"
        pattern = pattern.replaceAll(" ", "\u200A");
+1 −2
Original line number Diff line number Diff line
@@ -95,8 +95,7 @@ public abstract class AlarmTimeViewHolder extends RecyclerView.ViewHolder {

    protected void bindClock(Context context, Alarm alarm) {
        clock.setAlpha(alarm.enabled ? CLOCK_ENABLED_ALPHA : CLOCK_DISABLED_ALPHA);
        clock.setFormat(context,
                context.getResources().getDimensionPixelSize(R.dimen.alarm_label_size));
        clock.setFormat(context);
        clock.setTime(alarm.hour, alarm.minutes);
    }

Loading