package com.creativetrends.app.simplicity.fragments;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.creativetrends.app.simplicity.SimplicityApplication;
import com.creativetrends.app.simplicity.utils.StaticUtils;
import com.creativetrends.simplicity.app.R;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

/**
 * Created by Creative Trends Apps.
 */

public class AboutFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener {
    public boolean mListStyled;
    Context context;
    SharedPreferences preferences;
    private static Elements eName;
    private static Document dName;
    TextView etUsername;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = SimplicityApplication.getContextOfApplication();
        addPreferencesFromResource(R.xml.about);
        preferences = PreferenceManager.getDefaultSharedPreferences(context);
        Preference versionnumber = findPreference("version_simple");
        versionnumber.setSummary(getResources().getString(R.string.app_name) + " " + StaticUtils.getAppVersionName(context));
        Preference changeLog = findPreference("change");
        changeLog.setOnPreferenceClickListener(this);

    }


    @Override
    public void onStart() {
        super.onStart();

    }


    @Override
    public void onResume() {
        super.onResume();
        getActivity().setTitle(R.string.about_app);
        if (!mListStyled) {
            View rootView = getView();
            if (rootView != null) {
                ListView list = rootView.findViewById(android.R.id.list);
                list.setPadding(0, 0, 0, 0);
                list.setDivider(null);
                mListStyled = true;
            }
        }

    }


    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        String key = preference.getKey();
        switch (key) {
            //top preferences
            case "change":
                changeDialog();
                break;

            default:
                break;


        }

        return false;


    }



    @SuppressLint({"StaticFieldLeak", "SetTextI18n"})
    private void updateChangelog() {
        // Set the user name
        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                try {
                    dName = Jsoup.connect("https://play.google.com/store/apps/details?id=com.creativetrends.simplicity.app").get();
                    eName = dName.select("div.recent-change").next();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(String string) {
                try {
                    if (eName != null) {
                        etUsername.setText(Html.fromHtml(eName.toString().replace("&lt;b&gt;", " ")));
                        Log.i("Changelog from Play", eName.text());
                    }else{
                        etUsername.setText("Bug fixes");
                    }
                } catch (NullPointerException ignored) {
                } catch (Exception i) {
                    i.printStackTrace();
                }

            }

        }.execute();

    }

    private void changeDialog() {
        updateChangelog();
        LayoutInflater inflater = getActivity().getLayoutInflater();
        @SuppressLint("InflateParams") View alertLayout = inflater.inflate(R.layout.changelog_layout, null);
        etUsername = alertLayout.findViewById(R.id.change_text);
        //etUsername.setText(Html.fromHtml(preferences.getString("new_change", "")));
        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
        alert.setTitle(getString(R.string.whats_new));
        alert.setView(alertLayout);
        // disallow cancel of AlertDialog on click of back button and outside touch
        alert.setCancelable(false);
        alert.setPositiveButton(getString(R.string.ok), (dialog, which) -> {

        });
        try {
            AlertDialog dialog = alert.create();
            dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
            dialog.show();
        }catch(NullPointerException ignored){
        }catch (Exception i){
            i.printStackTrace();
        }
    }


}
@@ -165,37 +165,8 @@
 es;%0A
-import android.os.AsyncTask;%0A
 impo
@@ -187,16 +187,16 @@
 Bundle;%0A
+
 import a
@@ -363,59 +363,8 @@
 og;%0A
-import android.text.Html;%0Aimport android.util.Log;%0A
 impo
@@ -659,100 +659,8 @@
 R;%0A%0A
-import org.jsoup.Jsoup;%0Aimport org.jsoup.nodes.Document;%0Aimport org.jsoup.select.Elements;%0A%0A
 /**%0A
@@ -896,78 +896,8 @@
 es;%0A
-    private static Elements eName;%0A    private static Document dName;%0A
     
@@ -2144,79 +2144,8 @@
  %7D%0A%0A
-    @Override%0A    public void onStop() %7B%0A        super.onStop();%0A    %7D%0A
 %0A   
@@ -2443,33 +2443,32 @@
 ak;%0A%0A%0A        %7D%0A
-%0A
         return f
@@ -2486,1248 +2486,8 @@
  %7D%0A%0A
-%0A%0A    @SuppressLint(%7B%22StaticFieldLeak%22, %22SetTextI18n%22%7D)%0A    private void updateChangelog() %7B%0A        // Set the user name%0A        new AsyncTask%3CVoid, Void, String%3E() %7B%0A            @Override%0A            protected String doInBackground(Void... params) %7B%0A                try %7B%0A                    dName = Jsoup.connect(%22https://play.google.com/store/apps/details?id=com.creativetrends.simplicity.app%22).get();%0A                    eName = dName.select(%22div.recent-change%22).next();%0A                %7D catch (Exception e) %7B%0A                    e.printStackTrace();%0A                %7D%0A                return null;%0A            %7D%0A%0A            @Override%0A            protected void onPostExecute(String string) %7B%0A                try %7B%0A                    if (eName != null) %7B%0A                        etUsername.setText(Html.fromHtml(eName.toString().replace(%22&lt;b&gt;%22, %22 %22)));%0A                        Log.i(%22Changelog from Play%22, eName.text());%0A                    %7Delse%7B%0A                        etUsername.setText(%22Bug fixes%22);%0A                    %7D%0A                %7D catch (NullPointerException ignored) %7B%0A                %7D catch (Exception i) %7B%0A                    i.printStackTrace();%0A                %7D%0A%0A            %7D%0A%0A        %7D.execute();%0A%0A    %7D%0A%0A
     
@@ -2520,35 +2520,8 @@
 ) %7B%0A
-        updateChangelog();%0A
     
@@ -2767,18 +2767,16 @@
         
-//
 etUserna
@@ -2790,62 +2790,19 @@
 ext(
-Html.fromHtml(preferences.getString(%22new_change%22, %22%22))
+%22Bug fixes%22
 );%0A 
@@ -2971,92 +2971,8 @@
 t);%0A
-        // disallow cancel of AlertDialog on click of back button and outside touch%0A
     
@@ -3080,17 +3080,16 @@
 h) -%3E %7B%0A
-%0A
         
@@ -3104,286 +3104,90 @@
     
-try %7B%0A            AlertDialog dialog = alert.create();%0A            dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;%0A            dialog.show();%0A        %7Dcatch(NullPointerException ignored)%7B%0A        %7Dcatch (Exception i)%7B%0A            i.printStackTrace
+alert.create();%0A        if (!getActivity().isFinishing()) %7B%0A            alert.show
 ();%0A
@@ -3200,13 +3200,14 @@
   %7D%0A
+%0A
     %7D%0A%0A
-%0A
 %7D
+%0A

Update AboutFragment.java