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

import com.battlelancer.seriesguide.R;
import com.battlelancer.seriesguide.SeriesGuideData;
import com.battlelancer.seriesguide.SeriesGuidePreferences;
import com.battlelancer.seriesguide.provider.SeriesContract.Shows;
import com.battlelancer.seriesguide.ui.ShowsActivity;
import com.battlelancer.thetvdbapi.TheTVDB;

import org.xml.sax.SAXException;

import android.content.ContentResolver;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.ViewStub;
import android.widget.ProgressBar;
import android.widget.Toast;

import java.util.concurrent.atomic.AtomicInteger;

public class UpdateTask extends AsyncTask<Void, Integer, Integer> {

    private static final int UPDATE_SUCCESS = 100;

    private static final int UPDATE_SAXERROR = 102;

    private static final int UPDATE_OFFLINE = 103;

    private static final int UPDATE_INCOMPLETE = 104;

    public String[] mShows = null;

    public String mFailedShows = "";

    private final ShowsActivity mContext;

    public final AtomicInteger mUpdateCount = new AtomicInteger();

    private boolean mIsFullUpdate = false;

    public UpdateTask(boolean isFullUpdate, ShowsActivity context) {
        mContext = context;
        mIsFullUpdate = isFullUpdate;
    }

    public UpdateTask(String[] shows, int index, String failedShows, ShowsActivity context) {
        mContext = context;
        mShows = shows;
        mUpdateCount.set(index);
        mFailedShows = failedShows;
    }

    @Override
    protected void onPreExecute() {
        if (mContext.mProgressOverlay == null) {
            mContext.mProgressOverlay = ((ViewStub) mContext.findViewById(R.id.stub_update))
                    .inflate();
            mContext.mUpdateProgress = (ProgressBar) mContext
                    .findViewById(R.id.ProgressBarShowListDet);

            final View cancelButton = mContext.mProgressOverlay.findViewById(R.id.overlayCancel);
            cancelButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    mContext.onCancelTasks();
                }
            });
        }

        mContext.mUpdateProgress.setIndeterminate(false);
        mContext.mUpdateProgress.setProgress(0);
        mContext.showOverlay(mContext.mProgressOverlay);
    }

    @Override
    protected Integer doInBackground(Void... params) {
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext
                .getApplicationContext());
        final ContentResolver resolver = mContext.getContentResolver();
        final AtomicInteger updateCount = mUpdateCount;

        if (mShows == null) {
            final long currentServerTime;
            try {
                currentServerTime = TheTVDB.getServerTime(mContext);
            } catch (SAXException e1) {
                return UPDATE_SAXERROR;
            }
            final long previousUpdateTime = Long.valueOf(prefs.getString(
                    SeriesGuidePreferences.KEY_LASTUPDATETIME, "0"));

            // new update task
            if (mIsFullUpdate || isFullUpdateNeeded(currentServerTime, previousUpdateTime)) {
                final Cursor shows = resolver.query(Shows.CONTENT_URI, new String[] {
                    Shows._ID
                }, null, null, null);
                mShows = new String[shows.getCount()];
                int i = 0;
                while (shows.moveToNext()) {
                    mShows[i] = shows.getString(0);
                    i++;
                }
                shows.close();
            } else {
                try {
                    mShows = TheTVDB.deltaUpdateShows(previousUpdateTime, mContext);
                } catch (SAXException e) {
                    return UPDATE_SAXERROR;
                }
            }

            prefs.edit()
                    .putString(SeriesGuidePreferences.KEY_LASTUPDATETIME,
                            String.valueOf(currentServerTime)).commit();
        }

        int resultCode = UPDATE_SUCCESS;
        String id;

        for (int i = updateCount.get(); i < mShows.length; i++) {
            // fail early if cancelled or network connection is lost
            if (isCancelled()) {
                resultCode = UPDATE_INCOMPLETE;
                break;
            }
            if (!SeriesGuideData.isNetworkAvailable(mContext)) {
                resultCode = UPDATE_OFFLINE;
                break;
            }

            publishProgress(i, mShows.length + 1);

            id = mShows[i];
            for (int itry = 0; itry < 2; itry++) {
                try {
                    TheTVDB.updateShow(id, mContext);
                    break;
                } catch (SAXException saxe) {
                    // failed twice
                    if (itry == 1) {
                        resultCode = UPDATE_SAXERROR;
                        Cursor show = resolver.query(Shows.buildShowUri(id), new String[] {
                            Shows.TITLE
                        }, null, null, null);
                        if (show.moveToFirst()) {
                            String name = show.getString(0);
                            addFailedShow(name);
                        }
                        show.close();
                    }
                }
            }
            updateCount.incrementAndGet();
        }

        publishProgress(mShows.length, mShows.length + 1);

        // renew FTS3 table
        TheTVDB.onRenewFTSTable(mContext);

        publishProgress(mShows.length + 1, mShows.length + 1);

        return resultCode;
    }
<commit_msg>Only store new update time if update was acutally successful.<commit_after>
package com.battlelancer.seriesguide.util;

import com.battlelancer.seriesguide.R;
import com.battlelancer.seriesguide.SeriesGuideData;
import com.battlelancer.seriesguide.SeriesGuidePreferences;
import com.battlelancer.seriesguide.provider.SeriesContract.Shows;
import com.battlelancer.seriesguide.ui.ShowsActivity;
import com.battlelancer.thetvdbapi.TheTVDB;

import org.xml.sax.SAXException;

import android.content.ContentResolver;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.ViewStub;
import android.widget.ProgressBar;
import android.widget.Toast;

import java.util.concurrent.atomic.AtomicInteger;

public class UpdateTask extends AsyncTask<Void, Integer, Integer> {

    private static final int UPDATE_SUCCESS = 100;

    private static final int UPDATE_SAXERROR = 102;

    private static final int UPDATE_OFFLINE = 103;

    private static final int UPDATE_INCOMPLETE = 104;

    public String[] mShows = null;

    public String mFailedShows = "";

    private final ShowsActivity mContext;

    public final AtomicInteger mUpdateCount = new AtomicInteger();

    private boolean mIsFullUpdate = false;

    public UpdateTask(boolean isFullUpdate, ShowsActivity context) {
        mContext = context;
        mIsFullUpdate = isFullUpdate;
    }

    public UpdateTask(String[] shows, int index, String failedShows, ShowsActivity context) {
        mContext = context;
        mShows = shows;
        mUpdateCount.set(index);
        mFailedShows = failedShows;
    }

    @Override
    protected void onPreExecute() {
        if (mContext.mProgressOverlay == null) {
            mContext.mProgressOverlay = ((ViewStub) mContext.findViewById(R.id.stub_update))
                    .inflate();
            mContext.mUpdateProgress = (ProgressBar) mContext
                    .findViewById(R.id.ProgressBarShowListDet);

            final View cancelButton = mContext.mProgressOverlay.findViewById(R.id.overlayCancel);
            cancelButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    mContext.onCancelTasks();
                }
            });
        }

        mContext.mUpdateProgress.setIndeterminate(false);
        mContext.mUpdateProgress.setProgress(0);
        mContext.showOverlay(mContext.mProgressOverlay);
    }

    @Override
    protected Integer doInBackground(Void... params) {
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext
                .getApplicationContext());
        final ContentResolver resolver = mContext.getContentResolver();
        final AtomicInteger updateCount = mUpdateCount;
        long currentServerTime = 0;
        if (mShows == null) {
            
            try {
                currentServerTime = TheTVDB.getServerTime(mContext);
            } catch (SAXException e1) {
                return UPDATE_SAXERROR;
            }
            final long previousUpdateTime = Long.valueOf(prefs.getString(
                    SeriesGuidePreferences.KEY_LASTUPDATETIME, "0"));

            // new update task
            if (mIsFullUpdate || isFullUpdateNeeded(currentServerTime, previousUpdateTime)) {
                final Cursor shows = resolver.query(Shows.CONTENT_URI, new String[] {
                    Shows._ID
                }, null, null, null);
                mShows = new String[shows.getCount()];
                int i = 0;
                while (shows.moveToNext()) {
                    mShows[i] = shows.getString(0);
                    i++;
                }
                shows.close();
            } else {
                try {
                    mShows = TheTVDB.deltaUpdateShows(previousUpdateTime, mContext);
                } catch (SAXException e) {
                    return UPDATE_SAXERROR;
                }
            }
        }

        int resultCode = UPDATE_SUCCESS;
        String id;

        for (int i = updateCount.get(); i < mShows.length; i++) {
            // fail early if cancelled or network connection is lost
            if (isCancelled()) {
                resultCode = UPDATE_INCOMPLETE;
                break;
            }
            if (!SeriesGuideData.isNetworkAvailable(mContext)) {
                resultCode = UPDATE_OFFLINE;
                break;
            }

            publishProgress(i, mShows.length + 1);

            id = mShows[i];
            for (int itry = 0; itry < 2; itry++) {
                try {
                    TheTVDB.updateShow(id, mContext);
                    break;
                } catch (SAXException saxe) {
                    // failed twice
                    if (itry == 1) {
                        resultCode = UPDATE_SAXERROR;
                        Cursor show = resolver.query(Shows.buildShowUri(id), new String[] {
                            Shows.TITLE
                        }, null, null, null);
                        if (show.moveToFirst()) {
                            String name = show.getString(0);
                            addFailedShow(name);
                        }
                        show.close();
                    }
                }
            }
            updateCount.incrementAndGet();
        }

        publishProgress(mShows.length, mShows.length + 1);

        // renew FTS3 table
        TheTVDB.onRenewFTSTable(mContext);

        publishProgress(mShows.length + 1, mShows.length + 1);
        
        // store time of update if it was successful
        if (currentServerTime != 0 && resultCode == UPDATE_SUCCESS) {
            prefs.edit()
            .putString(SeriesGuidePreferences.KEY_LASTUPDATETIME,
                    String.valueOf(currentServerTime)).commit();
        }

        return resultCode;
    }

{'fraction_non_alphanumeric': 0.08380790480237994, 'fraction_numerical': 0.004589885252868678, 'mean_word_length': 1.8344977113948446, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '5155803', 'n_tokens_mistral': 3224, 'n_tokens_neox': 2997, 'n_words': 642}