<commit_before>/*
 * Copyright (C) 2015 <NAME>
 *
 * This file is part of GGVertretungsplan.
 *
 * GGVertretungsplan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GGVertretungsplan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GGVertretungsplan.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.gebatzens.ggvertretungsplan;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.PowerManager;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceFragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Scanner;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class SettingsActivity extends Activity {

    Toolbar mToolBar;
    private static boolean changed;
    static String version;


    public static class GGPFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

        SharedPreferences prefs;

        @Override
        public void onCreate(Bundle s) {
            super.onCreate(s);
            GGApp gg = GGApp.GG_APP;
            prefs = getActivity().getSharedPreferences("gguser", Context.MODE_PRIVATE);
            String sessId = prefs.getString("sessid", null);

            addPreferencesFromResource(R.xml.preferences);
            SharedPreferences sp = getPreferenceScreen().getSharedPreferences();
            sp.registerOnSharedPreferenceChangeListener(this);
            String pref_schule_content = gg.provider.getFullName();
            String pref_klasse_content = gg.getSelectedClass();
            if(pref_klasse_content.equals(""))
                pref_klasse_content = "Keine ausgewählt";

            Preference pref_schule = findPreference("schule");
            pref_schule.setSummary(pref_schule_content);

            Preference pref_klasse = findPreference("klasse");
            pref_klasse.setSummary(pref_klasse_content);

            Preference update = findPreference("appupdates");
            update.setSummary(gg.translateUpdateType(gg.getUpdateType()));

            Preference pref_buildversion = findPreference("buildversion");
            String versionName = BuildConfig.VERSION_NAME;
            pref_buildversion.setSummary("Version: " + versionName + " (" + BuildConfig.BUILD_TYPE + ") (Zum aktualisieren berühren)");
            pref_buildversion.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    new AsyncTask<Object, Void, Void>() {

                        @Override
                        protected Void doInBackground(Object... params) {
                            try {
                                int update_build_number = BuildConfig.VERSION_CODE;

                                HttpsURLConnection con = (HttpsURLConnection) new URL("https://gymnasium-glinde.logoip.de/infoapp/update.php?version").openConnection();
                                con.setRequestMethod("POST");
                                con.setSSLSocketFactory(GGProvider.sslSocketFactory);

                                if (con.getResponseCode() == 200) {
                                    BufferedInputStream in = new BufferedInputStream(con.getInputStream());
                                    Scanner scan = new Scanner(in);
                                    String resp = "";
                                    while (scan.hasNextLine())
                                        resp += scan.nextLine();
                                    scan.close();
                                    if (update_build_number < Integer.valueOf(resp)) {
                                        getActivity().runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                                                builder.setTitle("Aktualisierung verfügbar");
                                                builder.setMessage("Soll die SchulinfoAPP aktualisiert werden?");
                                                builder.setPositiveButton("Ja", new DialogInterface.OnClickListener() {
                                                    @Override
                                                    public void onClick(DialogInterface dialog, int which) {
                                                        UpdateActivity ua = new UpdateActivity(getActivity(), getActivity());
                                                        ua.execute();
                                                        dialog.dismiss();
                                                    }
                                                });
                                                builder.setNegativeButton("Nein", new DialogInterface.OnClickListener() {
                                                    @Override
                                                    public void onClick(DialogInterface dialog, int which) {
                                                        dialog.dismiss();
                                                    }
                                                });
                                                builder.create().show();
                                            }
                                        });
                                    } else {
                                        getActivity().runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Toast.makeText(getActivity().getApplication(), "Keine neue Version verfügbar.", Toast.LENGTH_SHORT).show();
                                            }
                                        });<commit_msg>Improved update function, added change log.<commit_after>/*
 * Copyright (C) 2015 <NAME>
 *
 * This file is part of GGVertretungsplan.
 *
 * GGVertretungsplan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GGVertretungsplan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GGVertretungsplan.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.gebatzens.ggvertretungsplan;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.PowerManager;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceFragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Scanner;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class SettingsActivity extends Activity {

    Toolbar mToolBar;
    private static boolean changed;
    static String version;


    public static class GGPFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

        SharedPreferences prefs;

        @Override
        public void onCreate(Bundle s) {
            super.onCreate(s);
            GGApp gg = GGApp.GG_APP;
            prefs = getActivity().getSharedPreferences("gguser", Context.MODE_PRIVATE);
            String sessId = prefs.getString("sessid", null);

            addPreferencesFromResource(R.xml.preferences);
            SharedPreferences sp = getPreferenceScreen().getSharedPreferences();
            sp.registerOnSharedPreferenceChangeListener(this);
            String pref_schule_content = gg.provider.getFullName();
            String pref_klasse_content = gg.getSelectedClass();
            if(pref_klasse_content.equals(""))
                pref_klasse_content = "Keine ausgewählt";

            Preference pref_schule = findPreference("schule");
            pref_schule.setSummary(pref_schule_content);

            Preference pref_klasse = findPreference("klasse");
            pref_klasse.setSummary(pref_klasse_content);

            Preference update = findPreference("appupdates");
            update.setSummary(gg.translateUpdateType(gg.getUpdateType()));

            Preference pref_buildversion = findPreference("buildversion");
            String versionName = BuildConfig.VERSION_NAME;
            pref_buildversion.setSummary("Version: " + versionName + " (" + BuildConfig.BUILD_TYPE + ") (Zum aktualisieren berühren)");
            pref_buildversion.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    new AsyncTask<Object, Void, Void>() {

                        @Override
                        protected Void doInBackground(Object... params) {
                            try {
                                int update_build_number = BuildConfig.VERSION_CODE;

                                HttpsURLConnection con = (HttpsURLConnection) new URL("https://gymnasium-glinde.logoip.de/infoapp/update.php?version").openConnection();
                                con.setRequestMethod("POST");
                                con.setSSLSocketFactory(GGProvider.sslSocketFactory);

                                if (con.getResponseCode() == 200) {
                                    BufferedInputStream in = new BufferedInputStream(con.getInputStream());
                                    Scanner scan = new Scanner(in);
                                    String resp = "";
                                    while (scan.hasNextLine())
                                        resp += scan.nextLine();
                                    scan.close();
                                    if (update_build_number-1 < Integer.valueOf(resp)) {
                                        HttpsURLConnection con_changelog = (HttpsURLConnection) new URL("https://gymnasium-glinde.logoip.de/infoapp/update.php?changelog="+resp).openConnection();
                                        con_changelog.setRequestMethod("GET");
                                        con_changelog.setSSLSocketFactory(GGProvider.sslSocketFactory);

                                        if(con_changelog.getResponseCode() == 200) {
                                            BufferedInputStream in_changelog = new BufferedInputStream(con_changelog.getInputStream());
                                            Scanner scan_changelog = new Scanner(in_changelog);
                                            String resp_changelog = "";
                                            while (scan_changelog.hasNextLine())
                                                resp_changelog += scan_changelog.nextLine();
                                            scan_changelog.close();
                                            final String final_resp_changelog = resp_changelog;
                                            getActivity().runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                                                    builder.setTitle("Aktualisierung verfügbar");
                                                    builder.setMessage("Soll die SchulinfoAPP aktualisiert werden?\n\nChangelog:\n"+final_resp_changelog.replace("|","\n"));
                                                    builder.setPositiveButton("Ja", new DialogInterface.OnClickListener() {
                                                        @Override
                                                        public void onClick(DialogInterface dialog, int which) {
                                                            UpdateActivity ua = new UpdateActivity(getActivity(), getActivity());
                                                            ua.execute();
                                                            dialog.dismiss();
                                                        }
                                                    });
                                                    builder.setNegativeButton("Nein", new DialogInterface.OnClickListener() {
                                                        @Override
                                                        public void onClick(DialogInterface dialog, int which) {
                                                            dialog.dismiss();
                                                        }
                                                    });
                                                    builder.create().show();
                                                }
                                            });
                                        }
                                    } else {
                                        getActivity().runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Toast.makeText(getActivity().getApplication(), "Keine neue Version verfügbar.", Toast.LENGTH_SHORT).show();
                                            }
                                        });
{'fraction_non_alphanumeric': 0.07433703501426057, 'fraction_numerical': 0.0020632319922325383, 'mean_word_length': 1.521805661820964, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 151, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7392886', 'n_tokens_mistral': 3479, 'n_tokens_neox': 3342, 'n_words': 787}