<commit_before>
package com.battlelancer.seriesguide.appwidget;

import com.battlelancer.seriesguide.R;
import com.battlelancer.seriesguide.enums.WidgetListType;
import com.battlelancer.seriesguide.ui.EpisodeDetailsActivity;
import com.battlelancer.seriesguide.ui.UpcomingFragment.UpcomingQuery;
import com.battlelancer.seriesguide.util.DBUtils;
import com.battlelancer.seriesguide.util.Utils;
import com.battlelancer.thetvdbapi.ImageCache;

import android.annotation.TargetApi;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

@TargetApi(11)
public class ListWidgetService extends RemoteViewsService {

    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new ListRemoteViewsFactory(this.getApplicationContext(), intent);
    }

    class ListRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
        private Context mContext;

        private int mAppWidgetId;

        private Cursor mEpisodeCursor;

        private WidgetListType mType;

        private boolean mIsOnlyUnwatched;

        public ListRemoteViewsFactory(Context context, Intent intent) {
            mContext = context;
            mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        public void onCreate() {
            // In onCreate() you setup any connections / cursors to your data
            // source. Heavy lifting, for example downloading or creating
            // content etc, should be deferred to onDataSetChanged() or
            // getViewAt(). Taking more than 20 seconds in this call will result
            // in an ANR.
            SharedPreferences prefs = getSharedPreferences(ListWidgetConfigure.PREFS_NAME, 0);
            mType = WidgetListType.values()[prefs.getInt(ListWidgetConfigure.PREF_LISTTYPE_KEY
                    + mAppWidgetId, WidgetListType.UPCOMING.index)];
            mIsOnlyUnwatched = prefs.getBoolean(ListWidgetConfigure.PREF_WATCHEDONLY_KEY
                    + mAppWidgetId, false);

            queryForData();
        }

        private void queryForData() {
            switch (mType) {
                case UPCOMING:
                    mEpisodeCursor = DBUtils.getUpcomingEpisodes(mIsOnlyUnwatched, mContext);
                    break;
                case RECENT:
                    mEpisodeCursor = DBUtils.getRecentEpisodes(mIsOnlyUnwatched, mContext);
                    break;
            }
        }

        public void onDestroy() {
            // In onDestroy() you should tear down anything that was setup for
            // your data source, eg. cursors, connections, etc.
            mEpisodeCursor.close();
        }

        public int getCount() {
            return mEpisodeCursor.getCount();
        }

        public RemoteViews getViewAt(int position) {
            // position will always range from 0 to getCount() - 1.
            mEpisodeCursor.moveToPosition(position);

            // We construct a remote views item based on our widget item xml
            // file, and set the text based on the position.
            RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.appwidget_row);

            // episode description
            int seasonNumber = mEpisodeCursor.getInt(UpcomingQuery.SEASON);
            int episodeNumber = mEpisodeCursor.getInt(UpcomingQuery.NUMBER);
            String title = mEpisodeCursor.getString(UpcomingQuery.TITLE);
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);<commit_msg>Prevent widget crash by using default settings if necessary.<commit_after>
package com.battlelancer.seriesguide.appwidget;

import com.battlelancer.seriesguide.R;
import com.battlelancer.seriesguide.enums.WidgetListType;
import com.battlelancer.seriesguide.ui.EpisodeDetailsActivity;
import com.battlelancer.seriesguide.ui.UpcomingFragment.UpcomingQuery;
import com.battlelancer.seriesguide.util.DBUtils;
import com.battlelancer.seriesguide.util.Utils;
import com.battlelancer.thetvdbapi.ImageCache;

import android.annotation.TargetApi;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

@TargetApi(11)
public class ListWidgetService extends RemoteViewsService {

    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new ListRemoteViewsFactory(this.getApplicationContext(), intent);
    }

    class ListRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
        private Context mContext;

        private int mAppWidgetId;

        private Cursor mEpisodeCursor;

        private WidgetListType mType;

        private boolean mIsOnlyUnwatched;

        public ListRemoteViewsFactory(Context context, Intent intent) {
            mContext = context;
            mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        public void onCreate() {
            // In onCreate() you setup any connections / cursors to your data
            // source. Heavy lifting, for example downloading or creating
            // content etc, should be deferred to onDataSetChanged() or
            // getViewAt(). Taking more than 20 seconds in this call will result
            // in an ANR.
            SharedPreferences prefs = getSharedPreferences(ListWidgetConfigure.PREFS_NAME, 0);
            mType = WidgetListType.values()[prefs.getInt(ListWidgetConfigure.PREF_LISTTYPE_KEY
                    + mAppWidgetId, WidgetListType.UPCOMING.index)];
            mIsOnlyUnwatched = prefs.getBoolean(ListWidgetConfigure.PREF_WATCHEDONLY_KEY
                    + mAppWidgetId, false);

            queryForData();
        }

        private void queryForData() {
            switch (mType) {
                case RECENT:
                    mEpisodeCursor = DBUtils.getRecentEpisodes(mIsOnlyUnwatched, mContext);
                    break;
                case UPCOMING:
                default:
                    mEpisodeCursor = DBUtils.getUpcomingEpisodes(mIsOnlyUnwatched, mContext);
                    break;
            }
        }

        public void onDestroy() {
            // In onDestroy() you should tear down anything that was setup for
            // your data source, eg. cursors, connections, etc.
            mEpisodeCursor.close();
        }

        public int getCount() {
            return mEpisodeCursor.getCount();
        }

        public RemoteViews getViewAt(int position) {
            // position will always range from 0 to getCount() - 1.
            mEpisodeCursor.moveToPosition(position);

            // We construct a remote views item based on our widget item xml
            // file, and set the text based on the position.
            RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.appwidget_row);

            // episode description
            int seasonNumber = mEpisodeCursor.getInt(UpcomingQuery.SEASON);
            int episodeNumber = mEpisodeCursor.getInt(UpcomingQuery.NUMBER);
            String title = mEpisodeCursor.getString(UpcomingQuery.TITLE);
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
{'fraction_non_alphanumeric': 0.07019328585961343, 'fraction_numerical': 0.001780264496439471, 'mean_word_length': 2.6735170481083608, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1221948', 'n_tokens_mistral': 2077, 'n_tokens_neox': 2001, 'n_words': 482}