<commit_before>/**
 *
 * @author <NAME>
 *
 */
package be.ugent.zeus.hydra.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.view.View;
import android.widget.RemoteViews;
import be.ugent.zeus.hydra.R;
import be.ugent.zeus.hydra.RestoMenu;
import be.ugent.zeus.hydra.data.Menu;
import be.ugent.zeus.hydra.data.services.MenuService;
import be.ugent.zeus.hydra.ui.menu.MenuView;
import java.util.Calendar;

public class RestoMenuWidgetProvider extends AppWidgetProvider {

    private RemoteViews views;
    private Context context;
    private AppWidgetManager appWidgetManager;
    private int[] appWidgetIds;
    // These two are mutually exclusive, if they both are enabled it's FCFS.
    private static final boolean OPENAPPONCLICK = true;
    private static final boolean REFRESHONCLICK = false;

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        this.context = context;
        this.appWidgetManager = appWidgetManager;
        this.appWidgetIds = appWidgetIds;

        // Get the layout for the App Widget
        views = new RemoteViews(context.getPackageName(), R.layout.widget_restomenu);

        // Send intent to retrieve Menu data for today
        Intent menuIntent = new Intent(context, MenuService.class);
        menuIntent.putExtra(MenuService.RESULT_RECEIVER_EXTRA, new RestoMenuWidgetResultReceiver());
        menuIntent.putExtra(MenuService.DATE_EXTRA, Calendar.getInstance());
        context.startService(menuIntent);

        // Add action to widget to start the RestoMenu activity
        if (OPENAPPONCLICK) {
            Intent intent = new Intent(context, RestoMenu.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            views.setOnClickPendingIntent(R.id.widget_image, pendingIntent);
        }

        // Add action to widget to refresh onClick
        if (REFRESHONCLICK) {
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, menuIntent, 0);
            views.setOnClickPendingIntent(R.id.widget_image, pendingIntent);
        }
    }

    private class RestoMenuWidgetResultReceiver extends ResultReceiver {

        public RestoMenuWidgetResultReceiver() {
            super(null);
        }

        @Override
        protected void onReceiveResult(int code, Bundle data) {
            if (code == MenuService.STATUS_FINISHED) {
                // Read today's Menu
                Menu menu = (be.ugent.zeus.hydra.data.Menu) data.getSerializable(MenuService.MENU);

                final int N = appWidgetIds.length;
                // Perform this loop procedure for each App Widget that belongs to this provider
                for (int i = 0; i < N; i++) {
                    int appWidgetId = appWidgetIds[i];

                    // Since the widget only contains static content, it can be made with this
                    // simple trick without having to remake the complete View.
                    // Re-using the same bitmap for each bitmap might trigger some bitmap cache
                    // exceptions.

                    // First set the theme to avoid the ugly default textcolor
                    context.setTheme(R.style.Theme_Hydra_Light);//Sherlock___TextAppearance_Small

                    // Make a MenuView for the received Menu and resize it
                    // If no menu is available we'll show the closed image
                    if(menu == null || !menu.open) {
                    	views.setImageViewResource(R.id.widget_image, R.drawable.closed);
                    }
                    else {
	                    View view = new MenuView(context, menu);
	                    view.measure(350, 450);
	                    view.layout(0, 0, 350, 450);
	
	                    // Get a bitmap from the View and add it to the ImageView of our Widget
	                    view.setDrawingCacheEnabled(true);
	                    Bitmap bitmap = view.getDrawingCache();<commit_msg>Widget size is now based on device resolution.<commit_after>/**
 *
 * @author <NAME>
 *
 */
package be.ugent.zeus.hydra.widget;

import java.util.Calendar;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.RemoteViews;
import be.ugent.zeus.hydra.R;
import be.ugent.zeus.hydra.RestoMenu;
import be.ugent.zeus.hydra.data.Menu;
import be.ugent.zeus.hydra.data.services.MenuService;
import be.ugent.zeus.hydra.ui.menu.MenuView;

public class RestoMenuWidgetProvider extends AppWidgetProvider {

    private RemoteViews views;
    private Context context;
    private AppWidgetManager appWidgetManager;
    private int[] appWidgetIds;
    // These two are mutually exclusive, if they both are enabled it's FCFS.
    private static final boolean OPENAPPONCLICK = true;
    private static final boolean REFRESHONCLICK = false;
    
    private int width=350;
    private int height=450;

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        this.context = context;
        this.appWidgetManager = appWidgetManager;
        this.appWidgetIds = appWidgetIds;

        // Get the layout for the App Widget
        views = new RemoteViews(context.getPackageName(), R.layout.widget_restomenu);
        
        // Calculate a fitting size for the user's screensize
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        width = (int) (metrics.widthPixels * 0.65);
        height = (int) (metrics.heightPixels * 0.65);

        // Send intent to retrieve Menu data for today
        Intent menuIntent = new Intent(context, MenuService.class);
        menuIntent.putExtra(MenuService.RESULT_RECEIVER_EXTRA, new RestoMenuWidgetResultReceiver());
        menuIntent.putExtra(MenuService.DATE_EXTRA, Calendar.getInstance());
        context.startService(menuIntent);

        // Add action to widget to start the RestoMenu activity
        if (OPENAPPONCLICK) {
            Intent intent = new Intent(context, RestoMenu.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            views.setOnClickPendingIntent(R.id.widget_image, pendingIntent);
        }

        // Add action to widget to refresh onClick
        if (REFRESHONCLICK) {
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, menuIntent, 0);
            views.setOnClickPendingIntent(R.id.widget_image, pendingIntent);
        }
    }

    private class RestoMenuWidgetResultReceiver extends ResultReceiver {

        public RestoMenuWidgetResultReceiver() {
            super(null);
        }

        @Override
        protected void onReceiveResult(int code, Bundle data) {
            if (code == MenuService.STATUS_FINISHED) {
                // Read today's Menu
                Menu menu = (be.ugent.zeus.hydra.data.Menu) data.getSerializable(MenuService.MENU);

                final int N = appWidgetIds.length;
                // Perform this loop procedure for each App Widget that belongs to this provider
                for (int i = 0; i < N; i++) {
                    int appWidgetId = appWidgetIds[i];

                    // Since the widget only contains static content, it can be made with this
                    // simple trick without having to remake the complete View.
                    // Re-using the same bitmap for each bitmap might trigger some bitmap cache
                    // exceptions.

                    // First set the theme to avoid the ugly default textcolor
                    context.setTheme(R.style.Theme_Hydra_Light);//Sherlock___TextAppearance_Small

                    // Make a MenuView for the received Menu and resize it
                    // If no menu is available we'll show the closed image
                    if(menu == null || !menu.open) {
                    	views.setImageViewResource(R.id.widget_image, R.drawable.closed);
                    }
                    else {
	                    View view = new MenuView(context, menu);
	                    view.measure(width, height);
	                    view.layout(0, 0, width, height);
	
	                    // Get a bitmap from the View and add it to the ImageView of our Widget
	                    view.setDrawingCacheEnabled(true);
	                    Bitmap bitmap = view.getDrawingCache();
{'fraction_non_alphanumeric': 0.07881165919282511, 'fraction_numerical': 0.004260089686098654, 'mean_word_length': 2.3424503559385537, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5936594', 'n_tokens_mistral': 2407, 'n_tokens_neox': 2259, 'n_words': 669}