<commit_before>package org.catinthedark.wallpepper.service;

import android.app.IntentService;
import android.app.WallpaperManager;
import android.content.Intent;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;

import com.google.gson.Gson;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.catinthedark.wallpepper.MyActivity;
import org.catinthedark.wallpepper.R;
import org.catinthedark.wallpepper.WallpepperNotification;
import org.catinthedark.wallpepper.json.JsonHelpers;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class WallpaperService extends IntentService {
    public static final String ACTION_CHANGE_WALLPAPER = "org.catinthedark.wallpepper.service.action.CHANGE_WALLPAPER";
    public static final String EXTRA_TAGS = "org.catinthedark.wallpepper.service.extra.TAGS";
    public static final String EXTRA_LOWRES = "org.catinthedark.wallpepper.service.extra.LOWRES";
    public static final String EXTRA_RANDOM_RANGE = "org.catinthedark.wallpepper.service.extra.RANDOM_RANGE";

    private final Uri.Builder getImageIdsUriBuilder = new Uri.Builder()
            .scheme("https")
            .authority("api.flickr.com")
            .appendPath("services")
            .appendPath("rest")
            .appendQueryParameter("method", "flickr.photos.search")
            .appendQueryParameter("tag_mode", "all")
            .appendQueryParameter("sort", "interestingness-desc")
            .appendQueryParameter("page", "1")
            .appendQueryParameter("format", "json")
            .appendQueryParameter("nojsoncallback", "1");

    private final Uri.Builder getImageSizesUriBuilder = new Uri.Builder()
            .scheme("https")
            .authority("api.flickr.com")
            .appendPath("services")
            .appendPath("rest")
            .appendQueryParameter("method", "flickr.photos.getSizes")
            .appendQueryParameter("format", "json")
            .appendQueryParameter("nojsoncallback", "1");

    private final int RANDOM_RANGE = 15;
    private WallpepperNotification wallpepperNotification;

    private static final String TAG = MyActivity.TAG;

    /**
     * Starts this service to find image on Flickr service with the given tags,
     * download this image and set this as wallpaper
     *
     * @see IntentService
     */
    public static void startChangeWallpaper(Context context, String tags, int randomRange, boolean lowRes) {
        Intent intent = new Intent(context, WallpaperService.class);
        intent.setAction(ACTION_CHANGE_WALLPAPER);
        intent.putExtra(EXTRA_TAGS, tags);
        intent.putExtra(EXTRA_RANDOM_RANGE, randomRange);
        intent.putExtra(EXTRA_LOWRES, lowRes);
        context.startService(intent);
    }

    public WallpaperService() {
        super("WallpaperService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Context context = getApplicationContext();
        wallpepperNotification = new WallpepperNotification(context, getText(R.string.app_name));
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        startForeground(WallpepperNotification.NOTIFICATION_ID, wallpepperNotification.getNotification());
        return START_NOT_STICKY;
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_CHANGE_WALLPAPER.equals(action)) {

                SharedPreferences preferences = getSharedPreferences(MyActivity.SHARED_PREFS_NAME, MODE_PRIVATE);

                String tags = "";
                if (intent.hasExtra(EXTRA_TAGS)) {
                    tags = intent.getStringExtra(EXTRA_TAGS);
                } else if (preferences.contains(MyActivity.TAGS_KEY)) {
                    tags = preferences.getString(MyActivity.TAGS_KEY, "");
                }

                int randomRange = RANDOM_RANGE;
                if (intent.hasExtra(EXTRA_RANDOM_RANGE)) {
                    randomRange = intent.getIntExtra(EXTRA_RANDOM_RANGE, RANDOM_RANGE);
                } else if (preferences.contains(MyActivity.RANDOM_RANGE_KEY)) {
                    randomRange = preferences.getInt(MyActivity.RANDOM_RANGE_KEY, RANDOM_RANGE);
                }

                boolean lowRes = false;
                if (intent.hasExtra(EXTRA_LOWRES)) {<commit_msg>Launch activity if user clicked on widget, but tags are empty for some reason.<commit_after>package org.catinthedark.wallpepper.service;

import android.app.IntentService;
import android.app.WallpaperManager;
import android.content.Intent;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;

import com.google.gson.Gson;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.catinthedark.wallpepper.MyActivity;
import org.catinthedark.wallpepper.R;
import org.catinthedark.wallpepper.WallpepperNotification;
import org.catinthedark.wallpepper.json.JsonHelpers;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class WallpaperService extends IntentService {
    public static final String ACTION_CHANGE_WALLPAPER = "org.catinthedark.wallpepper.service.action.CHANGE_WALLPAPER";
    public static final String EXTRA_TAGS = "org.catinthedark.wallpepper.service.extra.TAGS";
    public static final String EXTRA_LOWRES = "org.catinthedark.wallpepper.service.extra.LOWRES";
    public static final String EXTRA_RANDOM_RANGE = "org.catinthedark.wallpepper.service.extra.RANDOM_RANGE";

    private final Uri.Builder getImageIdsUriBuilder = new Uri.Builder()
            .scheme("https")
            .authority("api.flickr.com")
            .appendPath("services")
            .appendPath("rest")
            .appendQueryParameter("method", "flickr.photos.search")
            .appendQueryParameter("tag_mode", "all")
            .appendQueryParameter("sort", "interestingness-desc")
            .appendQueryParameter("page", "1")
            .appendQueryParameter("format", "json")
            .appendQueryParameter("nojsoncallback", "1");

    private final Uri.Builder getImageSizesUriBuilder = new Uri.Builder()
            .scheme("https")
            .authority("api.flickr.com")
            .appendPath("services")
            .appendPath("rest")
            .appendQueryParameter("method", "flickr.photos.getSizes")
            .appendQueryParameter("format", "json")
            .appendQueryParameter("nojsoncallback", "1");

    private final int RANDOM_RANGE = 15;
    private WallpepperNotification wallpepperNotification;

    private static final String TAG = MyActivity.TAG;

    /**
     * Starts this service to find image on Flickr service with the given tags,
     * download this image and set this as wallpaper
     *
     * @see IntentService
     */
    public static void startChangeWallpaper(Context context, String tags, int randomRange, boolean lowRes) {
        Intent intent = new Intent(context, WallpaperService.class);
        intent.setAction(ACTION_CHANGE_WALLPAPER);
        intent.putExtra(EXTRA_TAGS, tags);
        intent.putExtra(EXTRA_RANDOM_RANGE, randomRange);
        intent.putExtra(EXTRA_LOWRES, lowRes);
        context.startService(intent);
    }

    public WallpaperService() {
        super("WallpaperService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Context context = getApplicationContext();
        wallpepperNotification = new WallpepperNotification(context, getText(R.string.app_name));
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        startForeground(WallpepperNotification.NOTIFICATION_ID, wallpepperNotification.getNotification());
        return START_NOT_STICKY;
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_CHANGE_WALLPAPER.equals(action)) {

                SharedPreferences preferences = getSharedPreferences(MyActivity.SHARED_PREFS_NAME, MODE_PRIVATE);

                String tags = "";
                if (intent.hasExtra(EXTRA_TAGS)) {
                    tags = intent.getStringExtra(EXTRA_TAGS);
                } else if (preferences.contains(MyActivity.TAGS_KEY)) {
                    tags = preferences.getString(MyActivity.TAGS_KEY, "");
                }

                if (tags == null || tags.isEmpty()) {
                    Context context = getApplicationContext();
                    getApplicationContext().startActivity(
                            new Intent(context, MyActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                    return;
                }

                int randomRange = RANDOM_RANGE;
                if (intent.hasExtra(EXTRA_RANDOM_RANGE)) {
                    randomRange = intent.getIntExtra(EXTRA_RANDOM_RANGE, RANDOM_RANGE);
                } else if (preferences.contains(MyActivity.RANDOM_RANGE_KEY)) {
                    randomRange = preferences.getInt(MyActivity.RANDOM_RANGE_KEY, RANDOM_RANGE);
                }

                boolean lowRes = false;
                if (intent.hasExtra(EXTRA_LOWRES)) {
{'fraction_non_alphanumeric': 0.09593080401022214, 'fraction_numerical': 0.000982897582071948, 'mean_word_length': 3.0185624012638232, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5109346', 'n_tokens_mistral': 2876, 'n_tokens_neox': 2799, 'n_words': 543}