Android Open Source - FRCAndroidWidget F R C Scroll App Widget Renderer






From Project

Back to project page FRCAndroidWidget.

License

The source code is released under:

GNU General Public License

If you think the Android project FRCAndroidWidget listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * French Revolutionary Calendar Android Widget
 * Copyright (C) 2011 - 2014 Carmen Alvarez
 * /*from   w w  w .j  a  v a 2  s.  c om*/
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.

 */
package ca.rmen.android.frcwidget.render;

import java.util.Locale;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.LinearLayout;
import android.widget.RemoteViews;
import android.widget.TextView;
import ca.rmen.android.frccommon.Constants;
import ca.rmen.android.frccommon.FRCDateUtils;
import ca.rmen.android.frccommon.prefs.FRCPreferences;
import ca.rmen.android.frccommon.prefs.FRCPreferences.DetailedView;
import ca.rmen.android.frenchcalendar.R;
import ca.rmen.lfrc.FrenchRevolutionaryCalendarDate;

/**
 * Responsible for drawing the scroll widgets.
 * TODO try to share as much as logic as possible between the scroll widgets and the minimalist widgets.
 * 
 * @author calvarez
 * 
 */
class FRCScrollAppWidgetRenderer implements FRCAppWidgetRenderer {
    private static final String TAG = Constants.TAG + FRCScrollAppWidgetRenderer.class.getSimpleName();
    private final FRCScrollAppWidgetRenderParams mParams;

    FRCScrollAppWidgetRenderer(FRCScrollAppWidgetRenderParams params) {
      mParams = params;
    }

    public RemoteViews render(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
        Log.v(TAG, "render");

        float defaultWidgetWidth = context.getResources().getDimension(mParams.widthResourceId);
        float defaultWidgetHeight= context.getResources().getDimension(mParams.heightResourceId);

        float scaleFactor = FRCRender.getScaleFactor(context, appWidgetManager, appWidgetId, defaultWidgetWidth, defaultWidgetHeight);

        FrenchRevolutionaryCalendarDate frenchDate = FRCDateUtils.getToday(context);

        // Create a view with the right scroll image as the background.
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(mParams.layoutResourceId, null, false);
        int color = FRCDateUtils.getColor(context, frenchDate);
        view.setBackgroundResource(mParams.scrollResourceId);
        Drawable background = view.getBackground();
        background.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
        //noinspection deprecation
        view.setBackgroundDrawable(background);

        int width = (int) (scaleFactor * context.getResources().getDimensionPixelSize(mParams.widthResourceId));
        int height = (int) (scaleFactor * context.getResources().getDimensionPixelSize(mParams.heightResourceId));
        Log.v(TAG, "Creating widget of size " + width + "x" + height);

        // Set all the text fields for the date.
        // Add a space before and after the text: this font sometimes cuts off the last letter.
        ((TextView) view.findViewById(R.id.text_year)).setText(" " + frenchDate.year + " ");
        ((TextView) view.findViewById(R.id.text_dayofmonth)).setText(String.valueOf(frenchDate.dayOfMonth));
        ((TextView) view.findViewById(R.id.text_weekday)).setText(frenchDate.getWeekdayName());
        ((TextView) view.findViewById(R.id.text_month)).setText(frenchDate.getMonthName());

        // Set the text fields for the time.
        final String timestamp;
        DetailedView detailedView = FRCPreferences.getInstance(context).getDetailedView();

        TextView timeView = (TextView) view.findViewById(R.id.text_time);
        if (detailedView == DetailedView.NONE) {
            timeView.setVisibility(View.GONE);
            timestamp = "";
        } else {
            timeView.setVisibility(View.VISIBLE);
            if (detailedView == DetailedView.TIME) timestamp = String.format(Locale.US, "%d:%02d", frenchDate.hour, frenchDate.minute);
            else
                timestamp = frenchDate.getDayOfYear();
        }
        ((TextView) view.findViewById(R.id.text_time)).setText(" " + timestamp + " ");

        FRCRender.scaleViews(view, scaleFactor);
        Font.applyFont(context, view);
        int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
        int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        view.measure(widthSpec, heightSpec);
        view.layout(0, 0, width - 1, height - 1);

        // Just in case the line with the month name is too long for the widget, we'll squeeze it so it fits.
        int textViewableWidth = (int) (scaleFactor * context.getResources().getDimensionPixelSize(mParams.textViewableWidthResourceId));
        squeezeMonthLine(view, textViewableWidth);
        squeezeTextView(timeView, textViewableWidth);
        return FRCRender.createRemoteViews(context, view, width, height);
    }

    private static void squeezeMonthLine(View view, int textViewableWidth) {
        TextView dateView = (TextView) view.findViewById(R.id.text_dayofmonth);
        TextView monthView = (TextView) view.findViewById(R.id.text_month);
        LinearLayout monthLine = (LinearLayout) monthView.getParent();
        TextView yearView = (TextView) monthLine.findViewById(R.id.text_year);

        int textWidth = dateView.getWidth() + monthView.getWidth() + (yearView == null ? 0 : yearView.getWidth());

        if (textWidth > textViewableWidth) {
            float squeezeFactor = (float) textViewableWidth / textWidth;

            Log.v(TAG, "SqueezeFactor: " + squeezeFactor);
            dateView.setTextScaleX(squeezeFactor);
            monthView.setTextScaleX(squeezeFactor);
            if (yearView != null) yearView.setTextScaleX(squeezeFactor);
        }
    }

    private static void squeezeTextView(TextView view, int textViewableWidth) {
        int textWidth = view.getWidth();
        if (textWidth > textViewableWidth) {
            float squeezeFactor = (float) textViewableWidth / textWidth;
            view.setTextScaleX(squeezeFactor);
        }
    }


}




Java Source Code List

ca.rmen.android.frccommon.Constants.java
ca.rmen.android.frccommon.FRCAboutActivity.java
ca.rmen.android.frccommon.FRCDateUtils.java
ca.rmen.android.frccommon.prefs.FRCPreferenceActivity.java
ca.rmen.android.frccommon.prefs.FRCPreferences.java
ca.rmen.android.frcwear.FRCAndroidWearService.java
ca.rmen.android.frcwear.FRCBootCompletedBroadcastReceiver.java
ca.rmen.android.frcwear.FRCWearCommHelper.java
ca.rmen.android.frcwear.FRCWearPreferenceListener.java
ca.rmen.android.frcwear.FRCWearPreferenceListener.java
ca.rmen.android.frcwear.FRCWearScheduler.java
ca.rmen.android.frcwidget.FRCAppWidgetManager.java
ca.rmen.android.frcwidget.FRCAppWidgetProvider.java
ca.rmen.android.frcwidget.FRCPopupActivity.java
ca.rmen.android.frcwidget.FRCWidgetScheduler.java
ca.rmen.android.frcwidget.render.FRCAppWidgetRendererFactory.java
ca.rmen.android.frcwidget.render.FRCAppWidgetRenderer.java
ca.rmen.android.frcwidget.render.FRCMinimalistAppWidgetRenderer.java
ca.rmen.android.frcwidget.render.FRCRenderApi13.java
ca.rmen.android.frcwidget.render.FRCRenderApi16.java
ca.rmen.android.frcwidget.render.FRCRender.java
ca.rmen.android.frcwidget.render.FRCScrollAppWidgetRenderParams.java
ca.rmen.android.frcwidget.render.FRCScrollAppWidgetRenderer.java
ca.rmen.android.frcwidget.render.Font.java
ca.rmen.android.frenchcalendar.FrenchCalendarAppWidgetMinimalist.java
ca.rmen.android.frenchcalendar.FrenchCalendarAppWidgetNarrow.java
ca.rmen.android.frenchcalendar.FrenchCalendarAppWidgetWide.java
ca.rmen.android.frenchcalendar.common.WearCommConstants.java
ca.rmen.android.frenchcalendar.wearable.app.notif.NotificationService.java