<commit_before>package fr.free.nrw.commons.widget;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.net.Uri;
import androidx.annotation.Nullable;
import android.widget.RemoteViews;

import com.facebook.common.executors.CallerThreadExecutor;
import com.facebook.common.references.CloseableReference;
import com.facebook.datasource.DataSource;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

import javax.inject.Inject;

import fr.free.nrw.commons.R;
import fr.free.nrw.commons.di.ApplicationlessInjection;
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;

/**
 * Implementation of App Widget functionality.
 */
public class PicOfDayAppWidget extends AppWidgetProvider {

    private CompositeDisposable compositeDisposable = new CompositeDisposable();

    @Inject OkHttpJsonApiClient okHttpJsonApiClient;

    void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.pic_of_day_app_widget);
        loadPictureOfTheDay(context, views, appWidgetManager, appWidgetId);
    }

    /**
     * Loads the picture of the day using media wiki API
     * @param context
     * @param views
     * @param appWidgetManager
     * @param appWidgetId
     */
    private void loadPictureOfTheDay(Context context,
                                     RemoteViews views,
                                     AppWidgetManager appWidgetManager,
                                     int appWidgetId) {
        compositeDisposable.add(okHttpJsonApiClient.getPictureOfTheDay()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        response -> {
                            if (response != null) {
                                views.setTextViewText(R.id.appwidget_title, response.getDisplayTitle());
                                loadImageFromUrl(response.getImageUrl(), context, views, appWidgetManager, appWidgetId);
                            }
                        },
                        t -> Timber.e(t, "Fetching picture of the day failed")
                ));
    }

    /**
     * Uses Fresco to load an image from Url
     * @param imageUrl
     * @param context
     * @param views
     * @param appWidgetManager
     * @param appWidgetId
     */
    private void loadImageFromUrl(String imageUrl,
                                  Context context,
                                  RemoteViews views,
                                  AppWidgetManager appWidgetManager,
                                  int appWidgetId) {
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(imageUrl)).build();<commit_msg>Added Click To View Photo on Home Screen Widget (#2768)<commit_after>package fr.free.nrw.commons.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.graphics.Canvas;
import android.graphics.Paint;
import android.net.Uri;
import android.widget.RemoteViews;

import com.facebook.common.executors.CallerThreadExecutor;
import com.facebook.common.references.CloseableReference;
import com.facebook.datasource.DataSource;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

import javax.inject.Inject;

import androidx.annotation.Nullable;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.di.ApplicationlessInjection;
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;

import static android.content.Intent.ACTION_VIEW;

/**
 * Implementation of App Widget functionality.
 */
public class PicOfDayAppWidget extends AppWidgetProvider {

    private CompositeDisposable compositeDisposable = new CompositeDisposable();

    @Inject OkHttpJsonApiClient okHttpJsonApiClient;

    void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.pic_of_day_app_widget);
        loadPictureOfTheDay(context, views, appWidgetManager, appWidgetId);
    }

    /**
     * Loads the picture of the day using media wiki API
     * @param context
     * @param views
     * @param appWidgetManager
     * @param appWidgetId
     */
    private void loadPictureOfTheDay(Context context,
                                     RemoteViews views,
                                     AppWidgetManager appWidgetManager,
                                     int appWidgetId) {
        compositeDisposable.add(okHttpJsonApiClient.getPictureOfTheDay()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        response -> {
                            if (response != null) {
                                views.setTextViewText(R.id.appwidget_title, response.getDisplayTitle());

                                // View in browser
                                Intent viewIntent = new Intent();
                                viewIntent.setAction(ACTION_VIEW);
                                viewIntent.setData(response.getFilePageTitle().getMobileUri());
                                PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, viewIntent, 0);
                                views.setOnClickPendingIntent(R.id.appwidget_image, pendingIntent);

                                loadImageFromUrl(response.getImageUrl(), context, views, appWidgetManager, appWidgetId);
                            }
                        },
                        t -> Timber.e(t, "Fetching picture of the day failed")
                ));
    }

    /**
     * Uses Fresco to load an image from Url
     * @param imageUrl
     * @param context
     * @param views
     * @param appWidgetManager
     * @param appWidgetId
     */
    private void loadImageFromUrl(String imageUrl,
                                  Context context,
                                  RemoteViews views,
                                  AppWidgetManager appWidgetManager,
                                  int appWidgetId) {
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(imageUrl)).build();
{'fraction_non_alphanumeric': 0.0777422790202343, 'fraction_numerical': 0.0007987220447284345, 'mean_word_length': 2.4384439359267733, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1890570', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1871, 'n_words': 376}