<commit_before>package com.carlosefonseca.common.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.carlosefonseca.common.CFApp;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings("UnusedDeclaration")
public final class AppUpdater {
    private static final String TAG = CodeUtils.getTag(AppUpdater.class);

    @Nullable
    private static String managerUrl = null;
    private static String appName;
    private static String channel;

    private static String existsNewVersion;

    private static LatestVersionJSON latestVersion;
    private static Map<String, LatestVersionJSON> latestVersions;

    private AppUpdater() {}

    static String currentChannelUrl() {
        if (managerUrl == null) throw new RuntimeException("URL must be set!");
        return managerUrl + "/" + appName + "/" + channel;
    }

    public static String urlForLatest() {
        if (managerUrl == null) throw new RuntimeException("URL must be set!");
        return managerUrl + "/" + appName + "/releases/latest";
    }


    public static void checkForUpdates(final CheckForUpdatesDelegate delegate) {
        checkForUpdates(currentChannelUrl(), delegate);
    }

    private static void checkForUpdates(final String url, final CheckForUpdatesDelegate delegate) {
        assert appName != null;
        assert channel != null;
        if (!NetworkingUtils.hasInternet()) return;
        final DownloadURL.DownloadResult aaa = new DownloadURL.DownloadResult() {
            @Override
            public void onString(String string) {
                if (string == null) return;

                try {
                    latestVersion = new Gson().fromJson(string, LatestVersionJSON.class);

                    Log.v(TAG, "Current Version: " + CodeUtils.getAppVersionName() + " Latest Version " + latestVersion.version);

                    existsNewVersion = latestVersion.version;

                    if (CodeUtils.getAppVersionCode() != latestVersion.getVersionCode()) {
                        delegate.newUpdateExists(existsNewVersion);
                    }
                } catch (JsonSyntaxException e) {
                    Log.e(TAG, "" + e.getMessage() + " - " + string, e);
                }
            }
        };
        new DownloadURL(null, false, aaa).execute(url);
    }

    public static void getServerVersions(final CheckForUpdatesDelegate delegate) {
        if (!NetworkingUtils.hasInternet()) return;
        DownloadURL.DownloadResult aaa = new DownloadURL.DownloadResult() {
            @Override
            public void onString(String string) {
                if (string == null) return;

                latestVersions = new Gson().fromJson(string, new TypeToken<Map<String, LatestVersionJSON>>() {}.getType());

                String list = "";

                for (Map.Entry<String, LatestVersionJSON> version : latestVersions.entrySet()) {
                    list += StringUtils.capitalize(version.getKey()) + ": " + version.getValue().version + "\n";
                }

                Log.v(TAG, "Server Versions: " + list.replace("\n", " ; "));

                delegate.newUpdateExists(list.trim());
            }
        };
        new DownloadURL(null, false, aaa).execute(urlForLatest());
    }

    public static void updateApp(final Context activity) {
        if (latestVersion == null) {
            if (latestVersions == null) {
                throw new RuntimeException("No known update checked");
            } else if (latestVersions.containsKey(channel)) {
                latestVersion = latestVersions.get(channel);
            } else {
                throw new RuntimeException("No known update checked");
            }
        }

        ProgressDialog mProgressDialog = new ProgressDialog(activity);
        mProgressDialog.setTitle("Downloading update…");
        if (StringUtils.isNotBlank(latestVersion.release_notes)) {
            mProgressDialog.setMessage("Release notes:\n\n" + latestVersion.release_notes);
        } else {
            mProgressDialog.setMessage("Please wait…");
        }
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setCancelable(true);
        mProgressDialog.setMax(100);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

        DownloadURL.DownloadResult aaa = new DownloadURL.DownloadResult() {
            @Override
            public void onFail() {
                Toast.makeText(CFApp.getContext(),
                               "Download failed :( Check your internet connection and try again…",
                               Toast.LENGTH_SHORT).show();<commit_msg>crash fix<commit_after>package com.carlosefonseca.common.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.carlosefonseca.common.CFApp;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings("UnusedDeclaration")
public final class AppUpdater {
    private static final String TAG = CodeUtils.getTag(AppUpdater.class);

    @Nullable
    private static String managerUrl = null;
    private static String appName;
    private static String channel;

    private static String existsNewVersion;

    private static LatestVersionJSON latestVersion;
    private static Map<String, LatestVersionJSON> latestVersions;

    private AppUpdater() {}

    static String currentChannelUrl() {
        if (managerUrl == null) throw new RuntimeException("URL must be set!");
        return managerUrl + "/" + appName + "/" + channel;
    }

    public static String urlForLatest() {
        if (managerUrl == null) throw new RuntimeException("URL must be set!");
        return managerUrl + "/" + appName + "/releases/latest";
    }


    public static void checkForUpdates(final CheckForUpdatesDelegate delegate) {
        checkForUpdates(currentChannelUrl(), delegate);
    }

    private static void checkForUpdates(final String url, final CheckForUpdatesDelegate delegate) {
        assert appName != null;
        assert channel != null;
        if (!NetworkingUtils.hasInternet()) return;
        final DownloadURL.DownloadResult aaa = new DownloadURL.DownloadResult() {
            @Override
            public void onString(String string) {
                if (string == null) return;

                try {
                    latestVersion = new Gson().fromJson(string, LatestVersionJSON.class);

                    Log.v(TAG, "Current Version: " + CodeUtils.getAppVersionName() + " Latest Version " + latestVersion.version);

                    existsNewVersion = latestVersion.version;

                    if (CodeUtils.getAppVersionCode() != latestVersion.getVersionCode()) {
                        delegate.newUpdateExists(existsNewVersion);
                    }
                } catch (JsonSyntaxException e) {
                    Log.e(TAG, "" + e.getMessage() + " - " + string, e);
                }
            }
        };
        new DownloadURL(null, false, aaa).execute(url);
    }

    public static void getServerVersions(final CheckForUpdatesDelegate delegate) {
        if (!NetworkingUtils.hasInternet()) return;
        DownloadURL.DownloadResult aaa = new DownloadURL.DownloadResult() {
            @Override
            public void onString(String string) {
                if (string == null) return;

                latestVersions = new Gson().fromJson(string, new TypeToken<Map<String, LatestVersionJSON>>() {}.getType());

                String list = "";

                for (Map.Entry<String, LatestVersionJSON> version : latestVersions.entrySet()) {
                    list += StringUtils.capitalize(version.getKey()) + ": " + version.getValue().version + "\n";
                }

                Log.v(TAG, "Server Versions: " + list.replace("\n", " ; "));

                delegate.newUpdateExists(list.trim());
            }
        };
        new DownloadURL(null, false, aaa).execute(urlForLatest());
    }

    public static void updateApp(final Context activity) {
        if (latestVersion == null) {
            if (latestVersions == null) {
                Toast.makeText(activity, "Error :(", Toast.LENGTH_SHORT).show();
                Log.w(TAG, new RuntimeException("No known update checked"));
            } else if (latestVersions.containsKey(channel)) {
                latestVersion = latestVersions.get(channel);
            } else {
                Toast.makeText(activity, "Error :(", Toast.LENGTH_SHORT).show();
                Log.w(TAG, new RuntimeException("No known update checked."));
            }
        }

        ProgressDialog mProgressDialog = new ProgressDialog(activity);
        mProgressDialog.setTitle("Downloading update…");
        if (StringUtils.isNotBlank(latestVersion.release_notes)) {
            mProgressDialog.setMessage("Release notes:\n\n" + latestVersion.release_notes);
        } else {
            mProgressDialog.setMessage("Please wait…");
        }
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setCancelable(true);
        mProgressDialog.setMax(100);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

        DownloadURL.DownloadResult aaa = new DownloadURL.DownloadResult() {
            @Override
            public void onFail() {
                Toast.makeText(CFApp.getContext(),
                               "Download failed :( Check your internet connection and try again…",
                               Toast.LENGTH_SHORT).show();
{'fraction_non_alphanumeric': 0.10289729521609395, 'fraction_numerical': 0.0007700452401578592, 'mean_word_length': 2.492436974789916, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4668599', 'n_tokens_mistral': 2722, 'n_tokens_neox': 2617, 'n_words': 598}