/*
 * Copyright 2015 Priyesh Patel
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.chromium.fontinstaller.ui.settings;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;

import com.chromium.fontinstaller.BuildConfig;
import com.chromium.fontinstaller.R;
import com.chromium.fontinstaller.util.Licenses;
import com.chromium.fontinstaller.util.PreferencesManager;

import de.psdev.licensesdialog.LicensesDialog;

public class SettingsFragment extends PreferenceFragment {

    private PreferencesManager prefs;

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);

        prefs = PreferencesManager.getInstance(getActivity());

        CheckBoxPreference trueFont = (CheckBoxPreference) findPreference("trueFont");
        trueFont.setOnPreferenceChangeListener((pref, newValue) -> handleTrueFont(newValue));

        Preference source = findPreference("viewSource");
        source.setOnPreferenceClickListener(pref -> viewSource());

        Preference licenses = findPreference("licenses");
        licenses.setOnPreferenceClickListener(pref -> openLicensesDialog());

        Preference appVersion = findPreference("appVersion");
        appVersion.setSummary(BuildConfig.VERSION_NAME + " - " + BuildConfig.BUILD_TYPE);
    }

    private boolean handleTrueFont(Object newValue) {
        prefs.setBoolean(PreferencesManager.KEY_ENABLE_TRUEFONT, (boolean) newValue);
        return true;
    }

    private boolean viewSource() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_BROWSABLE);
        intent.setData(Uri.parse("https://github.com/ItsPriyesh/FontInstaller"));
        startActivity(intent);
        return true;
    }

    private boolean openLicensesDialog() {
        new LicensesDialog.Builder(getActivity())
                .setNotices(Licenses.getNotices())
                .build().show();
        return true;
    }
}


@@ -640,16 +640,48 @@
 tings;%0A%0A
+import android.app.AlertDialog;%0A
 import a
@@ -703,16 +703,16 @@
 Intent;%0A
-
 import a
@@ -882,16 +882,62 @@
 ragment;
+%0Aimport android.support.v4.app.ActivityCompat;
 %0A%0Aimport
@@ -1014,16 +1014,72 @@
 ller.R;%0A
+import com.chromium.fontinstaller.ui.main.MainActivity;%0A
 import c
@@ -2300,24 +2300,53 @@
  newValue);%0A
+        showRestartDialog();%0A
         retu
@@ -2849,24 +2849,24 @@
 d().show();%0A
-
         retu
@@ -2879,12 +2879,456 @@
 e;%0A    %7D
+%0A%0A    private void showRestartDialog() %7B%0A        new AlertDialog.Builder(getActivity())%0A                .setMessage(%22Restart the app for the change to take effect.%22)%0A                .setPositiveButton(%22Restart%22, (dialog, id) -%3E restartApp())%0A                .create().show();%0A    %7D%0A%0A    private void restartApp() %7B%0A        ActivityCompat.finishAffinity(getActivity());%0A        startActivity(new Intent(getActivity(), MainActivity.class));%0A    %7D
 %0A%7D%0A%0A

Add restart prompt after toggling TrueFont