package jp.hazuki.yuzubrowser.webencode;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;

import jp.hazuki.yuzubrowser.R;

import static android.app.Activity.RESULT_OK;

/**
 * Created by hazuki on 17/01/19.
 */

public class WebTextEncodeListDialog extends DialogFragment {

    private static final String ENCODING = "enc";

    public static WebTextEncodeListDialog newInstance(String webTextEncode) {
        WebTextEncodeListDialog dialog = new WebTextEncodeListDialog();
        Bundle bundle = new Bundle();
        bundle.putString(ENCODING, webTextEncode);
        dialog.setArguments(bundle);
        return dialog;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        WebTextEncodeList encodes = new WebTextEncodeList();
        encodes.read(getActivity());

        final String[] entries = new String[encodes.size()];

        String now = getArguments().getString(ENCODING);

        if (now == null) now = "";

        int pos = -1;

        WebTextEncode encode;
        for (int i = 0; encodes.size() > i; i++) {
            encode = encodes.get(i);
            entries[i] = encode.encoding;
            if (now.equals(encode.encoding)) {
                pos = i;
            }
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.web_encode)
                .setSingleChoiceItems(entries, pos, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent();
                        intent.putExtra(Intent.EXTRA_TEXT, entries[which]);
                        getActivity().setResult(RESULT_OK, intent);
                        dismiss();
                    }
                })
                .setNegativeButton(R.string.cancel, null);
        return builder.create();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        getActivity().finish();
    }
}

@@ -1,8 +1,603 @@
+/*%0A * Copyright (C) 2017 Hazuki%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0A
 package 
@@ -630,16 +630,45 @@
 ncode;%0A%0A
+import android.app.Activity;%0A
 import a
@@ -938,16 +938,16 @@
 ser.R;%0A%0A
+
 import s
@@ -989,51 +989,8 @@
 K;%0A%0A
-/**%0A * Created by hazuki on 17/01/19.%0A */%0A%0A
 publ
@@ -2832,29 +2832,101 @@
         
-getActivity()
+Activity activity = getActivity();%0A        if (activity != null)%0A            activity
 .finish(

fix NPE