<commit_before>/*
 * Copyright 2013 Google Inc.
 *
 * 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.google.android.apps.dashclock.render;

import com.google.android.apps.dashclock.ExtensionManager;
import com.google.android.apps.dashclock.LogUtils;
import com.google.android.apps.dashclock.Utils;
import com.google.android.apps.dashclock.WidgetClickProxyActivity;
import com.google.android.apps.dashclock.configuration.AppChooserPreference;
import com.google.android.apps.dashclock.configuration.AppearanceConfig;
import com.google.android.apps.dashclock.configuration.ConfigurationActivity;

import net.nurik.roman.dashclock.R;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;

import java.util.List;
import java.util.Locale;

import static com.google.android.apps.dashclock.ExtensionManager.ExtensionWithData;

/**
 * Abstract helper class in charge of core rendering logic for DashClock widgets.
 */
public abstract class DashClockRenderer {
    private static final String TAG = LogUtils.makeLogTag(DashClockRenderer.class);

    private static final int MAX_COLLAPSED_EXTENSIONS = 3;
    private static final int MIN_NORMAL_FONTSIZE_WIDTH_DP = 300;

    public static final String PREF_CLOCK_SHORTCUT = "pref_clock_shortcut";

    protected Context mContext;
    protected ExtensionManager mExtensionManager;

    protected Options mOptions;

    protected DashClockRenderer(Context context) {
        mContext = context;
        mExtensionManager = ExtensionManager.getInstance(context);
    }

    public void setOptions(Options options) {
        mOptions = options;
    }

    public Object renderWidget(Object container) {
        ViewBuilder vb = onCreateViewBuilder();
        Resources res = mContext.getResources();

        // Load data from extensions
        List<ExtensionWithData> extensions = mExtensionManager.getActiveExtensionsWithData();
        int activeExtensions = extensions.size();
        int visibleExtensions = 0;
        for (ExtensionWithData ewd : extensions) {
            if (!ewd.latestData.visible()) {
                continue;
            }
            ++visibleExtensions;
        }

        // Determine if we're on a tablet or not (lock screen widgets can't be collapsed on
        // tablets).
        boolean isTablet = res.getConfiguration().smallestScreenWidthDp >= 600;

        // Pull high-level user-defined appearance options.
        boolean hideSettings = AppearanceConfig.isSettingsButtonHidden(mContext);
        int shadeColor = AppearanceConfig.getHomescreenBackgroundColor(mContext);

        int minExpandedHeight = res.getDimensionPixelSize(
                mOptions.target == Options.TARGET_LOCK_SCREEN
                        ? R.dimen.min_expanded_height_lock_screen
                        : R.dimen.min_expanded_height);
        boolean isExpanded = (mOptions.minHeightDp
                >= minExpandedHeight / res.getDisplayMetrics().density);

        // Step 1. Load the root layout
        vb.loadRootLayout(container, isExpanded
                ? R.layout.widget_main_expanded
                : (mOptions.target == Options.TARGET_LOCK_SCREEN)
                        ? R.layout.widget_main_collapsed_lockscreen
                        : R.layout.widget_main_collapsed);

        // Step 2. Configure the shade, if it should exist
        vb.setViewBackgroundColor(R.id.shade, shadeColor);
        vb.setViewVisibility(R.id.shade,
                (mOptions.target != Options.TARGET_HOME_SCREEN || shadeColor == 0)
                        ? View.GONE : View.VISIBLE);

        // Step 3. Draw the basic clock face
        boolean hideClock =
                (mOptions.target == Options.TARGET_HOME_SCREEN
                        && AppearanceConfig.isClockHiddenOnHomeScreen(mContext))
                || (mOptions.target == Options.TARGET_LOCK_SCREEN
                        && AppearanceConfig.isClockHiddenOnLockScreen(mContext));
        vb.setViewVisibility(R.id.clock_target, hideClock ? View.GONE : View.VISIBLE);
        if (!hideClock) {
            renderClockFace(vb);
        }

        // Step 4. Align the clock face and settings button (if shown)

        // Conditionally displace the settings button to keep the clock centered.
        vb.setViewVisibility(R.id.settings_button_center_displacement,
                hideSettings ? View.GONE : View.VISIBLE);

        // Settings button
        if (isExpanded) {
            vb.setViewVisibility(R.id.settings_button, hideSettings ? View.GONE : View.VISIBLE);
            vb.setViewClickIntent(R.id.settings_button,
                    new Intent(mContext, ConfigurationActivity.class)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                    | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS));
            vb.setImageViewBitmap(R.id.settings_button,
                    Utils.recolorBitmap(
                            (BitmapDrawable) res.getDrawable(R.drawable.ic_widget_action_settings),<commit_msg>Hide settings if the clock is hidden<commit_after>/*
 * Copyright 2013 Google Inc.
 *
 * 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.google.android.apps.dashclock.render;

import com.google.android.apps.dashclock.ExtensionManager;
import com.google.android.apps.dashclock.LogUtils;
import com.google.android.apps.dashclock.Utils;
import com.google.android.apps.dashclock.WidgetClickProxyActivity;
import com.google.android.apps.dashclock.configuration.AppChooserPreference;
import com.google.android.apps.dashclock.configuration.AppearanceConfig;
import com.google.android.apps.dashclock.configuration.ConfigurationActivity;

import net.nurik.roman.dashclock.R;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;

import java.util.List;
import java.util.Locale;

import static com.google.android.apps.dashclock.ExtensionManager.ExtensionWithData;

/**
 * Abstract helper class in charge of core rendering logic for DashClock widgets.
 */
public abstract class DashClockRenderer {
    private static final String TAG = LogUtils.makeLogTag(DashClockRenderer.class);

    private static final int MAX_COLLAPSED_EXTENSIONS = 3;
    private static final int MIN_NORMAL_FONTSIZE_WIDTH_DP = 300;

    public static final String PREF_CLOCK_SHORTCUT = "pref_clock_shortcut";

    protected Context mContext;
    protected ExtensionManager mExtensionManager;

    protected Options mOptions;

    protected DashClockRenderer(Context context) {
        mContext = context;
        mExtensionManager = ExtensionManager.getInstance(context);
    }

    public void setOptions(Options options) {
        mOptions = options;
    }

    public Object renderWidget(Object container) {
        ViewBuilder vb = onCreateViewBuilder();
        Resources res = mContext.getResources();

        // Load data from extensions
        List<ExtensionWithData> extensions = mExtensionManager.getActiveExtensionsWithData();
        int activeExtensions = extensions.size();
        int visibleExtensions = 0;
        for (ExtensionWithData ewd : extensions) {
            if (!ewd.latestData.visible()) {
                continue;
            }
            ++visibleExtensions;
        }

        // Determine if we're on a tablet or not (lock screen widgets can't be collapsed on
        // tablets).
        boolean isTablet = res.getConfiguration().smallestScreenWidthDp >= 600;

        int shadeColor = AppearanceConfig.getHomescreenBackgroundColor(mContext);

        int minExpandedHeight = res.getDimensionPixelSize(
                mOptions.target == Options.TARGET_LOCK_SCREEN
                        ? R.dimen.min_expanded_height_lock_screen
                        : R.dimen.min_expanded_height);
        boolean isExpanded = (mOptions.minHeightDp
                >= minExpandedHeight / res.getDisplayMetrics().density);

        // Step 1. Load the root layout
        vb.loadRootLayout(container, isExpanded
                ? R.layout.widget_main_expanded
                : (mOptions.target == Options.TARGET_LOCK_SCREEN)
                        ? R.layout.widget_main_collapsed_lockscreen
                        : R.layout.widget_main_collapsed);

        // Step 2. Configure the shade, if it should exist
        vb.setViewBackgroundColor(R.id.shade, shadeColor);
        vb.setViewVisibility(R.id.shade,
                (mOptions.target != Options.TARGET_HOME_SCREEN || shadeColor == 0)
                        ? View.GONE : View.VISIBLE);

        // Step 3. Draw the basic clock face
        boolean hideSettings = false;
        boolean hideClock =
                (mOptions.target == Options.TARGET_HOME_SCREEN
                        && AppearanceConfig.isClockHiddenOnHomeScreen(mContext))
                || (mOptions.target == Options.TARGET_LOCK_SCREEN
                        && AppearanceConfig.isClockHiddenOnLockScreen(mContext));
        vb.setViewVisibility(R.id.clock_target, hideClock ? View.GONE : View.VISIBLE);
        if (hideClock) {
            hideSettings = true;
        } else {
            renderClockFace(vb);
            hideSettings = AppearanceConfig.isSettingsButtonHidden(mContext);
        }

        // Step 4. Align the clock face and settings button (if shown)

        // Conditionally displace the settings button to keep the clock centered.
        vb.setViewVisibility(R.id.settings_button_center_displacement,
                hideSettings ? View.GONE : View.VISIBLE);

        // Settings button
        if (isExpanded) {
            vb.setViewVisibility(R.id.settings_button, hideSettings ? View.GONE : View.VISIBLE);
            vb.setViewClickIntent(R.id.settings_button,
                    new Intent(mContext, ConfigurationActivity.class)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                    | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS));
            vb.setImageViewBitmap(R.id.settings_button,
                    Utils.recolorBitmap(
                            (BitmapDrawable) res.getDrawable(R.drawable.ic_widget_action_settings),
{'fraction_non_alphanumeric': 0.07957425240750127, 'fraction_numerical': 0.0035478966041561076, 'mean_word_length': 2.78363694471077, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2773392', 'n_tokens_mistral': 3169, 'n_tokens_neox': 3018, 'n_words': 733}