<commit_before>package sk.henrichg.phoneprofilesplus;

import android.app.Activity;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.core.content.ContextCompat;

public class ApplicationEditorIntentActivityX extends AppCompatActivity {

    private Application application = null;
    private PPIntent ppIntent = null;

    private int startApplicationDelay;

    private Button okButton;

    //private ScrollView intentScrollView;
    private EditText intentNameEditText;
    private AppCompatSpinner intentIntentTypeSpinner;
    private EditText intentPackageName;
    private EditText intentClassName;
    private AppCompatSpinner intentActionSpinner;
    private EditText intentActionEdit;
    private EditText intentData;
    private EditText intentMimeType;
    private TextView categoryTextView;
    private TextView flagsTextView;

    private EditText intentExtraKeyName1;
    private EditText intentExtraKeyValue1;
    private AppCompatSpinner intentExtraSpinner1;
    private EditText intentExtraKeyName2;
    private EditText intentExtraKeyValue2;
    private AppCompatSpinner intentExtraSpinner2;
    private EditText intentExtraKeyName3;
    private EditText intentExtraKeyValue3;
    private AppCompatSpinner intentExtraSpinner3;
    private EditText intentExtraKeyName4;
    private EditText intentExtraKeyValue4;
    private AppCompatSpinner intentExtraSpinner4;
    private EditText intentExtraKeyName5;
    private EditText intentExtraKeyValue5;
    private AppCompatSpinner intentExtraSpinner5;

    private String[] actionsArray;
    private String[] categoryArray;
    private boolean[] categoryIndices;
    private String[] flagArray;
    private boolean[] flagIndices;

    public static final String EXTRA_DIALOG_PREFERENCE_START_APPLICATION_DELAY = "dialogPreferenceStartApplicationDelay";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        GlobalGUIRoutines.setTheme(this, false, false/*, false*/); // must by called before super.onCreate()
        GlobalGUIRoutines.setLanguage(this);

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_application_editor_intent);

        /*
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            Window w = getWindow(); // in Activity's onCreate() for instance
            //w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            // create our manager instance after the content view is set
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            // enable status bar tint
            tintManager.setStatusBarTintEnabled(true);
            // set a custom tint color for status bar
            switch (ApplicationPreferences.applicationTheme(getApplicationContext(), true)) {
                case "color":
                    tintManager.setStatusBarTintColor(ContextCompat.getColor(getBaseContext(), R.color.primary));
                    break;
                case "white":
                    tintManager.setStatusBarTintColor(ContextCompat.getColor(getBaseContext(), R.color.primaryDark19_white));
                    break;
                default:
                    tintManager.setStatusBarTintColor(ContextCompat.getColor(getBaseContext(), R.color.primary_dark));
                    break;
            }
        }
        */

        if (getSupportActionBar() != null) {
            //getSupportActionBar().setHomeButtonEnabled(true);
            //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle(R.string.intent_editor_title);
            getSupportActionBar().setElevation(0/*GlobalGUIRoutines.dpToPx(1)*/);
        }

        Intent intent = getIntent();
        application = intent.getParcelableExtra(ApplicationEditorDialogX.EXTRA_APPLICATION);
        ppIntent = intent.getParcelableExtra(ApplicationEditorDialogX.EXTRA_PP_INTENT);
        if (ppIntent == null)
            PPApplication.logE("ApplicationEditorIntentActivity.onCreate", "ppIntent=null");
        else
            PPApplication.logE("ApplicationEditorIntentActivity.onCreate", "ppIntent._id="+ppIntent._id);

        startApplicationDelay = getIntent().getIntExtra(EXTRA_DIALOG_PREFERENCE_START_APPLICATION_DELAY, 0);

        okButton = findViewById(R.id.application_editor_intent_ok);
        //intentScrollView = findViewById(R.id.application_editor_intent_scroll_view);

        intentNameEditText = findViewById(R.id.application_editor_intent_intent_name);
        intentNameEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                enableOKButton();
            }
        });

        intentIntentTypeSpinner = findViewById(R.id.application_editor_intent_intent_type_spinner);
        GlobalGUIRoutines.HighlightedSpinnerAdapter spinnerAdapter = new GlobalGUIRoutines.HighlightedSpinnerAdapter(
                this,
                R.layout.highlighted_spinner,
                getResources().getStringArray(R.array.applicationEditorIntentIntentTypeArray));
        spinnerAdapter.setDropDownViewResource(R.layout.highlighted_spinner_dropdown);
        intentIntentTypeSpinner.setAdapter(spinnerAdapter);
        intentIntentTypeSpinner.setPopupBackgroundResource(R.drawable.popupmenu_background);
        intentIntentTypeSpinner.setSupportBackgroundTintList(ContextCompat.getColorStateList(this/*getBaseContext()*/, R.color.highlighted_spinner));
        intentIntentTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ((GlobalGUIRoutines.HighlightedSpinnerAdapter)intentIntentTypeSpinner.getAdapter()).setSelection(position);
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        intentPackageName = findViewById(R.id.application_editor_intent_package_name);

        intentClassName = findViewById(R.id.application_editor_intent_class_name);
        intentData = findViewById(R.id.application_editor_intent_data);
        intentMimeType = findViewById(R.id.application_editor_intent_mime_type);

        intentActionSpinner = findViewById(R.id.application_editor_intent_action_spinner);
        spinnerAdapter = new GlobalGUIRoutines.HighlightedSpinnerAdapter(
                this,
                R.layout.highlighted_spinner,
                getResources().getStringArray(R.array.applicationEditorIntentActionArray));
        spinnerAdapter.setDropDownViewResource(R.layout.highlighted_spinner_dropdown);
        intentActionSpinner.setAdapter(spinnerAdapter);
        intentActionSpinner.setPopupBackgroundResource(R.drawable.popupmenu_background);
        intentActionSpinner.setSupportBackgroundTintList(ContextCompat.getColorStateList(this/*getBaseContext()*/, R.color.highlighted_spinner));
        intentActionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ((GlobalGUIRoutines.HighlightedSpinnerAdapter)intentActionSpinner.getAdapter()).setSelection(position);

                if (position == 0) {
                    intentActionEdit.setText(R.string.empty_string);
                    intentActionEdit.setEnabled(false);
                }
                else
                if (position == 1) {
                    intentActionEdit.setEnabled(true);
                }
                else {
                    intentActionEdit.setText(R.string.empty_string);
                    intentActionEdit.setEnabled(false);
                }
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        intentActionEdit = findViewById(R.id.application_editor_intent_action_edit);

        final Activity activity = this;

        categoryTextView = findViewById(R.id.application_editor_intent_category_value);
        categoryArray = getResources().getStringArray(R.array.applicationEditorIntentCategoryArray);
        categoryIndices = new boolean[categoryArray.length];
        AppCompatImageButton intentCategoryButton = findViewById(R.id.application_editor_intent_category_btn);
        intentCategoryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder =
                        new AlertDialog.Builder(activity)
                                .setTitle(R.string.application_editor_intent_categories_dlg_title)
                                //.setIcon(getDialogIcon())
                                .setMultiChoiceItems(R.array.applicationEditorIntentCategoryArray, categoryIndices,
                                        new DialogInterface.OnMultiChoiceClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                        categoryIndices[which] = isChecked;
                                    }
                                });
                builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        intentNameEditText.clearFocus();
                        intentPackageName.clearFocus();
                        intentClassName.clearFocus();
                        intentActionEdit.clearFocus();
                        intentData.clearFocus();
                        intentMimeType.clearFocus();

                        String categoryValue = "";
                        int i = 0;
                        for (boolean selected : categoryIndices) {
                            if (selected) {
                                if (!categoryValue.isEmpty())
                                    //noinspection StringConcatenationInLoop
                                    categoryValue = categoryValue + "\n";
                                //noinspection StringConcatenationInLoop
                                categoryValue = categoryValue + categoryArray[i];
                            }
                            ++i;
                        }
                        categoryTextView.setText(categoryValue);
                        /*intentScrollView.post(new Runnable() {
                            public void run() {
                                intentScrollView.scrollTo(0, categoryTextView.getBottom());
                            }
                        });*/
                    }
                });
                builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });

                //AlertDialog mDialog = builder.create();
                if (!isFinishing())
                    builder.show();
            }
        });

        flagsTextView = findViewById(R.id.application_editor_intent_flags_value);
        flagArray = getResources().getStringArray(R.array.applicationEditorIntentFlagArray);
        flagIndices = new boolean[flagArray.length];
        AppCompatImageButton intentFlagsButton = findViewById(R.id.application_editor_intent_flags_btn);
        intentFlagsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder =
                        new AlertDialog.Builder(activity)
                                .setTitle(R.string.application_editor_intent_flags_dlg_title)
                                //.setIcon(getDialogIcon())
                                .setMultiChoiceItems(R.array.applicationEditorIntentFlagArray, flagIndices, new DialogInterface.OnMultiChoiceClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                        flagIndices[which] = isChecked;
                                    }
                                });
                builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        intentNameEditText.clearFocus();
                        intentPackageName.clearFocus();
                        intentClassName.clearFocus();
                        intentActionEdit.clearFocus();
                        intentData.clearFocus();
                        intentMimeType.clearFocus();

                        String flagsValue = "";
                        int i = 0;
                        for (boolean selected : flagIndices) {
                            if (selected) {
                                if (!flagsValue.isEmpty())
                                    //noinspection StringConcatenationInLoop
                                    flagsValue = flagsValue + "\n";
                                //noinspection StringConcatenationInLoop
                                flagsValue = flagsValue + flagArray[i];
                            }
                            ++i;
                        }
                        flagsTextView.setText(flagsValue);
                        /*intentScrollView.post(new Runnable() {
                            public void run() {
                                intentScrollView.scrollTo(0, flagsTextView.getBottom());
                            }
                        });*/
                    }
                });
                builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });

                AlertDialog mDialog = builder.create();
                if (!isFinishing())
                    mDialog.show();
            }
        });

        intentExtraKeyName1 = findViewById(R.id.application_editor_intent_extra_key_1);
        intentExtraKeyValue1 = findViewById(R.id.application_editor_intent_extra_value_1);

        intentExtraSpinner1  = findViewById(R.id.application_editor_intent_extra_type_spinner_1);
        spinnerAdapter = new GlobalGUIRoutines.HighlightedSpinnerAdapter(
                this,
                R.layout.highlighted_spinner,
                getResources().getStringArray(R.array.applicationEditorIntentExtraTypeArray));
        spinnerAdapter.setDropDownViewResource(R.layout.highlighted_spinner_dropdown);
        intentExtraSpinner1.setAdapter(spinnerAdapter);
        intentExtraSpinner1.setPopupBackgroundResource(R.drawable.popupmenu_background);
        intentExtraSpinner1.setSupportBackgroundTintList(ContextCompat.getColorStateList(this/*getBaseContext()*/, R.color.highlighted_spinner));
        intentExtraSpinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ((GlobalGUIRoutines.HighlightedSpinnerAdapter)intentExtraSpinner1.getAdapter()).setSelection(position);
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        intentExtraKeyName2 = findViewById(R.id.application_editor_intent_extra_key_2);
        intentExtraKeyValue2 = findViewById(R.id.application_editor_intent_extra_value_2);

        intentExtraSpinner2  = findViewById(R.id.application_editor_intent_extra_type_spinner_2);
        spinnerAdapter = new GlobalGUIRoutines.HighlightedSpinnerAdapter(
                this,
                R.layout.highlighted_spinner,
                getResources().getStringArray(R.array.applicationEditorIntentExtraTypeArray));
        spinnerAdapter.setDropDownViewResource(R.layout.highlighted_spinner_dropdown);
        intentExtraSpinner2.setAdapter(spinnerAdapter);
        intentExtraSpinner2.setPopupBackgroundResource(R.drawable.popupmenu_background);
        intentExtraSpinner2.setSupportBackgroundTintList(ContextCompat.getColorStateList(this/*getBaseContext()*/, R.color.highlighted_spinner));
        intentExtraSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ((GlobalGUIRoutines.HighlightedSpinnerAdapter)intentExtraSpinner2.getAdapter()).setSelection(position);
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        intentExtraKeyName3 = findViewById(R.id.application_editor_intent_extra_key_3);
        intentExtraKeyValue3 = findViewById(R.id.application_editor_intent_extra_value_3);

        intentExtraSpinner3  = findViewById(R.id.application_editor_intent_extra_type_spinner_3);
        spinnerAdapter = new GlobalGUIRoutines.HighlightedSpinnerAdapter(
                this,
                R.layout.highlighted_spinner,
                getResources().getStringArray(R.array.applicationEditorIntentExtraTypeArray));
        spinnerAdapter.setDropDownViewResource(R.layout.highlighted_spinner_dropdown);
        intentExtraSpinner3.setAdapter(spinnerAdapter);
        intentExtraSpinner3.setPopupBackgroundResource(R.drawable.popupmenu_background);
        intentExtraSpinner3.setSupportBackgroundTintList(ContextCompat.getColorStateList(this/*getBaseContext()*/, R.color.highlighted_spinner));
        intentExtraSpinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ((GlobalGUIRoutines.HighlightedSpinnerAdapter)intentExtraSpinner3.getAdapter()).setSelection(position);
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        intentExtraKeyName4 = findViewById(R.id.application_editor_intent_extra_key_4);
        intentExtraKeyValue4 = findViewById(R.id.application_editor_intent_extra_value_4);

        intentExtraSpinner4  = findViewById(R.id.application_editor_intent_extra_type_spinner_4);
        spinnerAdapter = new GlobalGUIRoutines.HighlightedSpinnerAdapter(
                this,
                R.layout.highlighted_spinner,
                getResources().getStringArray(R.array.applicationEditorIntentExtraTypeArray));
        spinnerAdapter.setDropDownViewResource(R.layout.highlighted_spinner_dropdown);
        intentExtraSpinner4.setAdapter(spinnerAdapter);
        intentExtraSpinner4.setPopupBackgroundResource(R.drawable.popupmenu_background);
        intentExtraSpinner4.setSupportBackgroundTintList(ContextCompat.getColorStateList(this/*getBaseContext()*/, R.color.highlighted_spinner));
        intentExtraSpinner4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ((GlobalGUIRoutines.HighlightedSpinnerAdapter)intentExtraSpinner4.getAdapter()).setSelection(position);
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        intentExtraKeyName5 = findViewById(R.id.application_editor_intent_extra_key_5);
        intentExtraKeyValue5 = findViewById(R.id.application_editor_intent_extra_value_5);

        intentExtraSpinner5  = findViewById(R.id.application_editor_intent_extra_type_spinner_5);
        spinnerAdapter = new GlobalGUIRoutines.HighlightedSpinnerAdapter(
                this,
                R.layout.highlighted_spinner,
                getResources().getStringArray(R.array.applicationEditorIntentExtraTypeArray));
        spinnerAdapter.setDropDownViewResource(R.layout.highlighted_spinner_dropdown);
        intentExtraSpinner5.setAdapter(spinnerAdapter);
        intentExtraSpinner5.setPopupBackgroundResource(R.drawable.popupmenu_background);
        intentExtraSpinner5.setSupportBackgroundTintList(ContextCompat.getColorStateList(this/*getBaseContext()*/, R.color.highlighted_spinner));
        intentExtraSpinner5.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ((GlobalGUIRoutines.HighlightedSpinnerAdapter)intentExtraSpinner5.getAdapter()).setSelection(position);
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        actionsArray = getResources().getStringArray(R.array.applicationEditorIntentActionArray);
        if (ppIntent != null) {
            intentNameEditText.setText(ppIntent._name);
            intentIntentTypeSpinner.setSelection(ppIntent._intentType);
            intentPackageName.setText(ppIntent._packageName);
            intentClassName.setText(ppIntent._className);
            intentData.setText(ppIntent._data);
            intentMimeType.setText(ppIntent._mimeType);

            if ((ppIntent._action == null) || ppIntent._action.isEmpty()) {
                intentActionSpinner.setSelection(0);
                intentActionEdit.setText(R.string.empty_string);
                intentActionEdit.setEnabled(false);
            } else {
                boolean custom = true;
                for (String action : actionsArray) {
                    if (action.equals(ppIntent._action)) {
                        custom = false;
                        break;
                    }
                }
                if (custom) {
                    intentActionSpinner.setSelection(1);
                    intentActionEdit.setText(ppIntent._action);
                    intentActionEdit.setEnabled(false);
                } else {
                    intentActionSpinner.setSelection(Arrays.asList(actionsArray).indexOf(ppIntent._action));
                    intentActionEdit.setText(R.string.empty_string);
                    intentActionEdit.setEnabled(false);
                }
            }

            if (ppIntent._categories != null) {
                String categoryValue = ppIntent._categories.replaceAll("\\|", "\n");
                categoryTextView.setText(categoryValue);
                List<String> stringList = new ArrayList<>(Arrays.asList(categoryArray));
                String[] splits = ppIntent._categories.split("\\|");
                for (String category : splits) {
                    int i = stringList.indexOf(category);
                    if (i != -1) {
                        categoryIndices[i] = true;
                    }
                }
            }

            if (ppIntent._flags != null) {
                String flagsValue = ppIntent._flags.replaceAll("\\|", "\n");
                flagsTextView.setText(flagsValue);
                List<String> stringList = new ArrayList<>(Arrays.asList(flagArray));
                String[] splits = ppIntent._flags.split("\\|");
                for (String flag : splits) {
                    int i = stringList.indexOf(flag);
                    if (i != -1)
                        flagIndices[i] = true;
                }
            }

            if (ppIntent._extraKey1 != null) {
                intentExtraKeyName1.setText(ppIntent._extraKey1);
                intentExtraKeyValue1.setText(ppIntent._extraValue1);
                intentExtraSpinner1.setSelection(ppIntent._extraType1);
            }
            if (ppIntent._extraKey2 != null) {
                intentExtraKeyName2.setText(ppIntent._extraKey2);
                intentExtraKeyValue2.setText(ppIntent._extraValue2);
                intentExtraSpinner2.setSelection(ppIntent._extraType2);
            }
            if (ppIntent._extraKey3 != null) {
                intentExtraKeyName3.setText(ppIntent._extraKey3);
                intentExtraKeyValue3.setText(ppIntent._extraValue3);
                intentExtraSpinner3.setSelection(ppIntent._extraType3);
            }
            if (ppIntent._extraKey4 != null) {
                intentExtraKeyName4.setText(ppIntent._extraKey4);
                intentExtraKeyValue4.setText(ppIntent._extraValue4);
                intentExtraSpinner4.setSelection(ppIntent._extraType4);
            }
            if (ppIntent._extraKey5 != null) {
                intentExtraKeyName5.setText(ppIntent._extraKey5);
                intentExtraKeyValue5.setText(ppIntent._extraValue5);
                intentExtraSpinner5.setSelection(ppIntent._extraType5);
            }

        }
        else {
            intentActionEdit.setEnabled(false);
        }

        enableOKButton();
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveIntent();
                Intent returnIntent = new Intent();
                returnIntent.putExtra(ApplicationEditorDialogX.EXTRA_PP_INTENT, ppIntent);
                returnIntent.putExtra(ApplicationEditorDialogX.EXTRA_APPLICATION, application);
                returnIntent.putExtra(EXTRA_DIALOG_PREFERENCE_START_APPLICATION_DELAY, startApplicationDelay);
                setResult(Activity.RESULT_OK, returnIntent);
                finish();
            }
        });

        Button cancelButton = findViewById(R.id.application_editor_intent_cancel);
        //cancelButton.setAllCaps(false);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent returnIntent = new Intent();
                setResult(Activity.RESULT_CANCELED, returnIntent);
                finish();
            }
        });

        Button testButton = findViewById(R.id.application_editor_intent_test);
        testButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveIntent();
                Intent testIntent = createIntent(ppIntent);
                boolean ok = false;
                if (testIntent != null) {
                    if (ppIntent._intentType == 0) {
                        try {
                            testIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(testIntent);
                            ok = true;
                        } catch (Exception ignored) {
                        }
                    }
                    else {
                        try {
                            sendBroadcast(testIntent);
                            ok = true;
                        } catch (Exception ignored) {
                        }
                    }
                }
                if (!ok) {
                    AlertDialog.Builder builder;
                    if (ppIntent._intentType == 0) {
                        builder =
                                new AlertDialog.Builder(activity)
                                        .setTitle(R.string.application_editor_intent_test_title)
                                        //.setIcon(getDialogIcon())
                                        .setMessage(R.string.application_editor_intent_test_activity_bad_data);
                    }
                    else {
                        builder =
                                new AlertDialog.Builder(activity)
                                        .setTitle(R.string.application_editor_intent_test_title)
                                        //.setIcon(getDialogIcon())
                                        .setMessage(R.string.application_editor_intent_test_broadcast_bad_data);
                    }
                    builder.setPositiveButton(android.R.string.ok, null);

                    AlertDialog mDialog = builder.create();
                    if (!isFinishing())
                        mDialog.show();
                }
            }
        });
    }

    private void saveIntent() {
        ppIntent._name = intentNameEditText.getText().toString();
        if (application != null)
            application.appLabel = ppIntent._name;
        ppIntent._intentType = intentIntentTypeSpinner.getSelectedItemPosition();
        ppIntent._packageName = intentPackageName.getText().toString().replaceAll(" ","");
        ppIntent._className = intentClassName.getText().toString().replaceAll(" ","");
        ppIntent._data = intentData.getText().toString();
        ppIntent._mimeType = intentMimeType.getText().toString().replaceAll(" ","");

        int actionSpinnerId = intentActionSpinner.getSelectedItemPosition();
        if (actionSpinnerId == 0)
            ppIntent._action = "";
        else
        if (actionSpinnerId == 1)
            ppIntent._action = intentActionEdit.getText().toString();
        else {
            ppIntent._action = actionsArray[actionSpinnerId];
        }

        ppIntent._categories = "";
        int i = 0;
        for (boolean selected : categoryIndices) {
            if (selected) {
                if (!ppIntent._categories.isEmpty())
                    //noinspection StringConcatenationInLoop
                    ppIntent._categories = ppIntent._categories + "|";
                //noinspection StringConcatenationInLoop
                ppIntent._categories = ppIntent._categories + categoryArray[i];
            }
            ++i;
        }

        ppIntent._flags = "";
        i = 0;
        for (boolean selected : flagIndices) {
            if (selected) {
                if (!ppIntent._flags.isEmpty())
                    //noinspection StringConcatenationInLoop
                    ppIntent._flags = ppIntent._flags + "|";
                //noinspection StringConcatenationInLoop
                ppIntent._flags = ppIntent._flags + flagArray[i];
            }
            ++i;
        }

        ppIntent._extraKey1 = intentExtraKeyName1.getText().toString();
        ppIntent._extraValue1 = intentExtraKeyValue1.getText().toString();
        ppIntent._extraType1 = intentExtraSpinner1.getSelectedItemPosition();
        ppIntent._extraKey2 = intentExtraKeyName2.getText().toString();
        ppIntent._extraValue2 = intentExtraKeyValue2.getText().toString();
        ppIntent._extraType2 = intentExtraSpinner2.getSelectedItemPosition();
        ppIntent._extraKey3 = intentExtraKeyName3.getText().toString();
        ppIntent._extraValue3 = intentExtraKeyValue3.getText().toString();
        ppIntent._extraType3 = intentExtraSpinner3.getSelectedItemPosition();
        ppIntent._extraKey4 = intentExtraKeyName4.getText().toString();
        ppIntent._extraValue4 = intentExtraKeyValue4.getText().toString();
        ppIntent._extraType4 = intentExtraSpinner4.getSelectedItemPosition();
        ppIntent._extraKey5 = intentExtraKeyName5.getText().toString();
        ppIntent._extraValue5 = intentExtraKeyValue5.getText().toString();
        ppIntent._extraType5 = intentExtraSpinner5.getSelectedItemPosition();
    }

    private void enableOKButton() {
        boolean enableOK = (!intentNameEditText.getText().toString().isEmpty());
        okButton.setEnabled(enableOK);
    }

    static Intent createIntent(PPIntent ppIntent) {
        /*
        intent = new Intent("android.intent.action.MAIN");
        intent.setComponent(new ComponentName("com.kuma.smartnotify","com.kuma.smartnotify.SmartNotifyMain"));
        intent.putExtra("CARMODEON",true);
        intent.putExtra("CARMODEOFF",true);
        */

        Intent intent;
        try {
            intent = new Intent();
            if ((ppIntent._packageName != null) && (!ppIntent._packageName.isEmpty()) &&
                    (ppIntent._className != null) && (!ppIntent._className.isEmpty()))
                intent.setComponent(new ComponentName(ppIntent._packageName, ppIntent._className));
<commit_msg>Fixed FC: java.lang.NullPointerException: Attempt to write to field 'java.lang.String sk.henrichg.phoneprofiles.PPIntent._name' on a null object reference                  at sk.henrichg.phoneprofiles.ApplicationEditorIntentActivityX.saveIntent + 473(ApplicationEditorIntentActivityX.java:473)<commit_after>package sk.henrichg.phoneprofilesplus;

import android.app.Activity;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.core.content.ContextCompat;

public class ApplicationEditorIntentActivityX extends AppCompatActivity {

    private Application application = null;
    private PPIntent ppIntent = null;

    private int startApplicationDelay;

    private Button okButton;

    //private ScrollView intentScrollView;
    private EditText intentNameEditText;
    private AppCompatSpinner intentIntentTypeSpinner;
    private EditText intentPackageName;
    private EditText intentClassName;
    private AppCompatSpinner intentActionSpinner;
    private EditText intentActionEdit;
    private EditText intentData;
    private EditText intentMimeType;
    private TextView categoryTextView;
    private TextView flagsTextView;

    private EditText intentExtraKeyName1;
    private EditText intentExtraKeyValue1;
    private AppCompatSpinner intentExtraSpinner1;
    private EditText intentExtraKeyName2;
    private EditText intentExtraKeyValue2;
    private AppCompatSpinner intentExtraSpinner2;
    private EditText intentExtraKeyName3;
    private EditText intentExtraKeyValue3;
    private AppCompatSpinner intentExtraSpinner3;
    private EditText intentExtraKeyName4;
    private EditText intentExtraKeyValue4;
    private AppCompatSpinner intentExtraSpinner4;
    private EditText intentExtraKeyName5;
    private EditText intentExtraKeyValue5;
    private AppCompatSpinner intentExtraSpinner5;

    private String[] actionsArray;
    private String[] categoryArray;
    private boolean[] categoryIndices;
    private String[] flagArray;
    private boolean[] flagIndices;

    public static final String EXTRA_DIALOG_PREFERENCE_START_APPLICATION_DELAY = "dialogPreferenceStartApplicationDelay";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        GlobalGUIRoutines.setTheme(this, false, false/*, false*/); // must by called before super.onCreate()
        GlobalGUIRoutines.setLanguage(this);

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_application_editor_intent);

        /*
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            Window w = getWindow(); // in Activity's onCreate() for instance
            //w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            // create our manager instance after the content view is set
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            // enable status bar tint
            tintManager.setStatusBarTintEnabled(true);
            // set a custom tint color for status bar
            switch (ApplicationPreferences.applicationTheme(getApplicationContext(), true)) {
                case "color":
                    tintManager.setStatusBarTintColor(ContextCompat.getColor(getBaseContext(), R.color.primary));
                    break;
                case "white":
                    tintManager.setStatusBarTintColor(ContextCompat.getColor(getBaseContext(), R.color.primaryDark19_white));
                    break;
                default:
                    tintManager.setStatusBarTintColor(ContextCompat.getColor(getBaseContext(), R.color.primary_dark));
                    break;
            }
        }
        */

        if (getSupportActionBar() != null) {
            //getSupportActionBar().setHomeButtonEnabled(true);
            //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle(R.string.intent_editor_title);
            getSupportActionBar().setElevation(0/*GlobalGUIRoutines.dpToPx(1)*/);
        }

        Intent intent = getIntent();
        application = intent.getParcelableExtra(ApplicationEditorDialogX.EXTRA_APPLICATION);
        ppIntent = intent.getParcelableExtra(ApplicationEditorDialogX.EXTRA_PP_INTENT);
        if (ppIntent == null)
            PPApplication.logE("ApplicationEditorIntentActivity.onCreate", "ppIntent=null");
        else
            PPApplication.logE("ApplicationEditorIntentActivity.onCreate", "ppIntent._id="+ppIntent._id);

        startApplicationDelay = getIntent().getIntExtra(EXTRA_DIALOG_PREFERENCE_START_APPLICATION_DELAY, 0);

        okButton = findViewById(R.id.application_editor_intent_ok);
        //intentScrollView = findViewById(R.id.application_editor_intent_scroll_view);

        intentNameEditText = findViewById(R.id.application_editor_intent_intent_name);
        intentNameEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                enableOKButton();
            }
        });

        intentIntentTypeSpinner = findViewById(R.id.application_editor_intent_intent_type_spinner);
        GlobalGUIRoutines.HighlightedSpinnerAdapter spinnerAdapter = new GlobalGUIRoutines.HighlightedSpinnerAdapter(
                this,
                R.layout.highlighted_spinner,
                getResources().getStringArray(R.array.applicationEditorIntentIntentTypeArray));
        spinnerAdapter.setDropDownViewResource(R.layout.highlighted_spinner_dropdown);
        intentIntentTypeSpinner.setAdapter(spinnerAdapter);
        intentIntentTypeSpinner.setPopupBackgroundResource(R.drawable.popupmenu_background);
        intentIntentTypeSpinner.setSupportBackgroundTintList(ContextCompat.getColorStateList(this/*getBaseContext()*/, R.color.highlighted_spinner));
        intentIntentTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ((GlobalGUIRoutines.HighlightedSpinnerAdapter)intentIntentTypeSpinner.getAdapter()).setSelection(position);
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        intentPackageName = findViewById(R.id.application_editor_intent_package_name);

        intentClassName = findViewById(R.id.application_editor_intent_class_name);
        intentData = findViewById(R.id.application_editor_intent_data);
        intentMimeType = findViewById(R.id.application_editor_intent_mime_type);

        intentActionSpinner = findViewById(R.id.application_editor_intent_action_spinner);
        spinnerAdapter = new GlobalGUIRoutines.HighlightedSpinnerAdapter(
                this,
                R.layout.highlighted_spinner,
                getResources().getStringArray(R.array.applicationEditorIntentActionArray));
        spinnerAdapter.setDropDownViewResource(R.layout.highlighted_spinner_dropdown);
        intentActionSpinner.setAdapter(spinnerAdapter);
        intentActionSpinner.setPopupBackgroundResource(R.drawable.popupmenu_background);
        intentActionSpinner.setSupportBackgroundTintList(ContextCompat.getColorStateList(this/*getBaseContext()*/, R.color.highlighted_spinner));
        intentActionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ((GlobalGUIRoutines.HighlightedSpinnerAdapter)intentActionSpinner.getAdapter()).setSelection(position);

                if (position == 0) {
                    intentActionEdit.setText(R.string.empty_string);
                    intentActionEdit.setEnabled(false);
                }
                else
                if (position == 1) {
                    intentActionEdit.setEnabled(true);
                }
                else {
                    intentActionEdit.setText(R.string.empty_string);
                    intentActionEdit.setEnabled(false);
                }
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        intentActionEdit = findViewById(R.id.application_editor_intent_action_edit);

        final Activity activity = this;

        categoryTextView = findViewById(R.id.application_editor_intent_category_value);
        categoryArray = getResources().getStringArray(R.array.applicationEditorIntentCategoryArray);
        categoryIndices = new boolean[categoryArray.length];
        AppCompatImageButton intentCategoryButton = findViewById(R.id.application_editor_intent_category_btn);
        intentCategoryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder =
                        new AlertDialog.Builder(activity)
                                .setTitle(R.string.application_editor_intent_categories_dlg_title)
                                //.setIcon(getDialogIcon())
                                .setMultiChoiceItems(R.array.applicationEditorIntentCategoryArray, categoryIndices,
                                        new DialogInterface.OnMultiChoiceClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                        categoryIndices[which] = isChecked;
                                    }
                                });
                builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        intentNameEditText.clearFocus();
                        intentPackageName.clearFocus();
                        intentClassName.clearFocus();
                        intentActionEdit.clearFocus();
                        intentData.clearFocus();
                        intentMimeType.clearFocus();

                        String categoryValue = "";
                        int i = 0;
                        for (boolean selected : categoryIndices) {
                            if (selected) {
                                if (!categoryValue.isEmpty())
                                    //noinspection StringConcatenationInLoop
                                    categoryValue = categoryValue + "\n";
                                //noinspection StringConcatenationInLoop
                                categoryValue = categoryValue + categoryArray[i];
                            }
                            ++i;
                        }
                        categoryTextView.setText(categoryValue);
                        /*intentScrollView.post(new Runnable() {
                            public void run() {
                                intentScrollView.scrollTo(0, categoryTextView.getBottom());
                            }
                        });*/
                    }
                });
                builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });

                //AlertDialog mDialog = builder.create();
                if (!isFinishing())
                    builder.show();
            }
        });

        flagsTextView = findViewById(R.id.application_editor_intent_flags_value);
        flagArray = getResources().getStringArray(R.array.applicationEditorIntentFlagArray);
        flagIndices = new boolean[flagArray.length];
        AppCompatImageButton intentFlagsButton = findViewById(R.id.application_editor_intent_flags_btn);
        intentFlagsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder =
                        new AlertDialog.Builder(activity)
                                .setTitle(R.string.application_editor_intent_flags_dlg_title)
                                //.setIcon(getDialogIcon())
                                .setMultiChoiceItems(R.array.applicationEditorIntentFlagArray, flagIndices, new DialogInterface.OnMultiChoiceClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                        flagIndices[which] = isChecked;
                                    }
                                });
                builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        intentNameEditText.clearFocus();
                        intentPackageName.clearFocus();
                        intentClassName.clearFocus();
                        intentActionEdit.clearFocus();
                        intentData.clearFocus();
                        intentMimeType.clearFocus();

                        String flagsValue = "";
                        int i = 0;
                        for (boolean selected : flagIndices) {
                            if (selected) {
                                if (!flagsValue.isEmpty())
                                    //noinspection StringConcatenationInLoop
                                    flagsValue = flagsValue + "\n";
                                //noinspection StringConcatenationInLoop
                                flagsValue = flagsValue + flagArray[i];
                            }
                            ++i;
                        }
                        flagsTextView.setText(flagsValue);
                        /*intentScrollView.post(new Runnable() {
                            public void run() {
                                intentScrollView.scrollTo(0, flagsTextView.getBottom());
                            }
                        });*/
                    }
                });
                builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });

                AlertDialog mDialog = builder.create();
                if (!isFinishing())
                    mDialog.show();
            }
        });

        intentExtraKeyName1 = findViewById(R.id.application_editor_intent_extra_key_1);
        intentExtraKeyValue1 = findViewById(R.id.application_editor_intent_extra_value_1);

        intentExtraSpinner1  = findViewById(R.id.application_editor_intent_extra_type_spinner_1);
        spinnerAdapter = new GlobalGUIRoutines.HighlightedSpinnerAdapter(
                this,
                R.layout.highlighted_spinner,
                getResources().getStringArray(R.array.applicationEditorIntentExtraTypeArray));
        spinnerAdapter.setDropDownViewResource(R.layout.highlighted_spinner_dropdown);
        intentExtraSpinner1.setAdapter(spinnerAdapter);
        intentExtraSpinner1.setPopupBackgroundResource(R.drawable.popupmenu_background);
        intentExtraSpinner1.setSupportBackgroundTintList(ContextCompat.getColorStateList(this/*getBaseContext()*/, R.color.highlighted_spinner));
        intentExtraSpinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ((GlobalGUIRoutines.HighlightedSpinnerAdapter)intentExtraSpinner1.getAdapter()).setSelection(position);
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        intentExtraKeyName2 = findViewById(R.id.application_editor_intent_extra_key_2);
        intentExtraKeyValue2 = findViewById(R.id.application_editor_intent_extra_value_2);

        intentExtraSpinner2  = findViewById(R.id.application_editor_intent_extra_type_spinner_2);
        spinnerAdapter = new GlobalGUIRoutines.HighlightedSpinnerAdapter(
                this,
                R.layout.highlighted_spinner,
                getResources().getStringArray(R.array.applicationEditorIntentExtraTypeArray));
        spinnerAdapter.setDropDownViewResource(R.layout.highlighted_spinner_dropdown);
        intentExtraSpinner2.setAdapter(spinnerAdapter);
        intentExtraSpinner2.setPopupBackgroundResource(R.drawable.popupmenu_background);
        intentExtraSpinner2.setSupportBackgroundTintList(ContextCompat.getColorStateList(this/*getBaseContext()*/, R.color.highlighted_spinner));
        intentExtraSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ((GlobalGUIRoutines.HighlightedSpinnerAdapter)intentExtraSpinner2.getAdapter()).setSelection(position);
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        intentExtraKeyName3 = findViewById(R.id.application_editor_intent_extra_key_3);
        intentExtraKeyValue3 = findViewById(R.id.application_editor_intent_extra_value_3);

        intentExtraSpinner3  = findViewById(R.id.application_editor_intent_extra_type_spinner_3);
        spinnerAdapter = new GlobalGUIRoutines.HighlightedSpinnerAdapter(
                this,
                R.layout.highlighted_spinner,
                getResources().getStringArray(R.array.applicationEditorIntentExtraTypeArray));
        spinnerAdapter.setDropDownViewResource(R.layout.highlighted_spinner_dropdown);
        intentExtraSpinner3.setAdapter(spinnerAdapter);
        intentExtraSpinner3.setPopupBackgroundResource(R.drawable.popupmenu_background);
        intentExtraSpinner3.setSupportBackgroundTintList(ContextCompat.getColorStateList(this/*getBaseContext()*/, R.color.highlighted_spinner));
        intentExtraSpinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ((GlobalGUIRoutines.HighlightedSpinnerAdapter)intentExtraSpinner3.getAdapter()).setSelection(position);
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        intentExtraKeyName4 = findViewById(R.id.application_editor_intent_extra_key_4);
        intentExtraKeyValue4 = findViewById(R.id.application_editor_intent_extra_value_4);

        intentExtraSpinner4  = findViewById(R.id.application_editor_intent_extra_type_spinner_4);
        spinnerAdapter = new GlobalGUIRoutines.HighlightedSpinnerAdapter(
                this,
                R.layout.highlighted_spinner,
                getResources().getStringArray(R.array.applicationEditorIntentExtraTypeArray));
        spinnerAdapter.setDropDownViewResource(R.layout.highlighted_spinner_dropdown);
        intentExtraSpinner4.setAdapter(spinnerAdapter);
        intentExtraSpinner4.setPopupBackgroundResource(R.drawable.popupmenu_background);
        intentExtraSpinner4.setSupportBackgroundTintList(ContextCompat.getColorStateList(this/*getBaseContext()*/, R.color.highlighted_spinner));
        intentExtraSpinner4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ((GlobalGUIRoutines.HighlightedSpinnerAdapter)intentExtraSpinner4.getAdapter()).setSelection(position);
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        intentExtraKeyName5 = findViewById(R.id.application_editor_intent_extra_key_5);
        intentExtraKeyValue5 = findViewById(R.id.application_editor_intent_extra_value_5);

        intentExtraSpinner5  = findViewById(R.id.application_editor_intent_extra_type_spinner_5);
        spinnerAdapter = new GlobalGUIRoutines.HighlightedSpinnerAdapter(
                this,
                R.layout.highlighted_spinner,
                getResources().getStringArray(R.array.applicationEditorIntentExtraTypeArray));
        spinnerAdapter.setDropDownViewResource(R.layout.highlighted_spinner_dropdown);
        intentExtraSpinner5.setAdapter(spinnerAdapter);
        intentExtraSpinner5.setPopupBackgroundResource(R.drawable.popupmenu_background);
        intentExtraSpinner5.setSupportBackgroundTintList(ContextCompat.getColorStateList(this/*getBaseContext()*/, R.color.highlighted_spinner));
        intentExtraSpinner5.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ((GlobalGUIRoutines.HighlightedSpinnerAdapter)intentExtraSpinner5.getAdapter()).setSelection(position);
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        actionsArray = getResources().getStringArray(R.array.applicationEditorIntentActionArray);
        if (ppIntent != null) {
            intentNameEditText.setText(ppIntent._name);
            intentIntentTypeSpinner.setSelection(ppIntent._intentType);
            intentPackageName.setText(ppIntent._packageName);
            intentClassName.setText(ppIntent._className);
            intentData.setText(ppIntent._data);
            intentMimeType.setText(ppIntent._mimeType);

            if ((ppIntent._action == null) || ppIntent._action.isEmpty()) {
                intentActionSpinner.setSelection(0);
                intentActionEdit.setText(R.string.empty_string);
                intentActionEdit.setEnabled(false);
            } else {
                boolean custom = true;
                for (String action : actionsArray) {
                    if (action.equals(ppIntent._action)) {
                        custom = false;
                        break;
                    }
                }
                if (custom) {
                    intentActionSpinner.setSelection(1);
                    intentActionEdit.setText(ppIntent._action);
                    intentActionEdit.setEnabled(false);
                } else {
                    intentActionSpinner.setSelection(Arrays.asList(actionsArray).indexOf(ppIntent._action));
                    intentActionEdit.setText(R.string.empty_string);
                    intentActionEdit.setEnabled(false);
                }
            }

            if (ppIntent._categories != null) {
                String categoryValue = ppIntent._categories.replaceAll("\\|", "\n");
                categoryTextView.setText(categoryValue);
                List<String> stringList = new ArrayList<>(Arrays.asList(categoryArray));
                String[] splits = ppIntent._categories.split("\\|");
                for (String category : splits) {
                    int i = stringList.indexOf(category);
                    if (i != -1) {
                        categoryIndices[i] = true;
                    }
                }
            }

            if (ppIntent._flags != null) {
                String flagsValue = ppIntent._flags.replaceAll("\\|", "\n");
                flagsTextView.setText(flagsValue);
                List<String> stringList = new ArrayList<>(Arrays.asList(flagArray));
                String[] splits = ppIntent._flags.split("\\|");
                for (String flag : splits) {
                    int i = stringList.indexOf(flag);
                    if (i != -1)
                        flagIndices[i] = true;
                }
            }

            if (ppIntent._extraKey1 != null) {
                intentExtraKeyName1.setText(ppIntent._extraKey1);
                intentExtraKeyValue1.setText(ppIntent._extraValue1);
                intentExtraSpinner1.setSelection(ppIntent._extraType1);
            }
            if (ppIntent._extraKey2 != null) {
                intentExtraKeyName2.setText(ppIntent._extraKey2);
                intentExtraKeyValue2.setText(ppIntent._extraValue2);
                intentExtraSpinner2.setSelection(ppIntent._extraType2);
            }
            if (ppIntent._extraKey3 != null) {
                intentExtraKeyName3.setText(ppIntent._extraKey3);
                intentExtraKeyValue3.setText(ppIntent._extraValue3);
                intentExtraSpinner3.setSelection(ppIntent._extraType3);
            }
            if (ppIntent._extraKey4 != null) {
                intentExtraKeyName4.setText(ppIntent._extraKey4);
                intentExtraKeyValue4.setText(ppIntent._extraValue4);
                intentExtraSpinner4.setSelection(ppIntent._extraType4);
            }
            if (ppIntent._extraKey5 != null) {
                intentExtraKeyName5.setText(ppIntent._extraKey5);
                intentExtraKeyValue5.setText(ppIntent._extraValue5);
                intentExtraSpinner5.setSelection(ppIntent._extraType5);
            }

        }
        else {
            intentActionEdit.setEnabled(false);
        }

        enableOKButton();
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveIntent();
                Intent returnIntent = new Intent();
                returnIntent.putExtra(ApplicationEditorDialogX.EXTRA_PP_INTENT, ppIntent);
                returnIntent.putExtra(ApplicationEditorDialogX.EXTRA_APPLICATION, application);
                returnIntent.putExtra(EXTRA_DIALOG_PREFERENCE_START_APPLICATION_DELAY, startApplicationDelay);
                setResult(Activity.RESULT_OK, returnIntent);
                finish();
            }
        });

        Button cancelButton = findViewById(R.id.application_editor_intent_cancel);
        //cancelButton.setAllCaps(false);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent returnIntent = new Intent();
                setResult(Activity.RESULT_CANCELED, returnIntent);
                finish();
            }
        });

        Button testButton = findViewById(R.id.application_editor_intent_test);
        testButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveIntent();
                Intent testIntent = createIntent(ppIntent);
                boolean ok = false;
                if (testIntent != null) {
                    if (ppIntent._intentType == 0) {
                        try {
                            testIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(testIntent);
                            ok = true;
                        } catch (Exception ignored) {
                        }
                    }
                    else {
                        try {
                            sendBroadcast(testIntent);
                            ok = true;
                        } catch (Exception ignored) {
                        }
                    }
                }
                if (!ok) {
                    AlertDialog.Builder builder;
                    if (ppIntent._intentType == 0) {
                        builder =
                                new AlertDialog.Builder(activity)
                                        .setTitle(R.string.application_editor_intent_test_title)
                                        //.setIcon(getDialogIcon())
                                        .setMessage(R.string.application_editor_intent_test_activity_bad_data);
                    }
                    else {
                        builder =
                                new AlertDialog.Builder(activity)
                                        .setTitle(R.string.application_editor_intent_test_title)
                                        //.setIcon(getDialogIcon())
                                        .setMessage(R.string.application_editor_intent_test_broadcast_bad_data);
                    }
                    builder.setPositiveButton(android.R.string.ok, null);

                    AlertDialog mDialog = builder.create();
                    if (!isFinishing())
                        mDialog.show();
                }
            }
        });
    }

    private void saveIntent() {
        if (intentNameEditText.getText() != null)
            ppIntent._name = intentNameEditText.getText().toString();
        else
            ppIntent._name = "";
        if (application != null)
            application.appLabel = ppIntent._name;
        ppIntent._intentType = intentIntentTypeSpinner.getSelectedItemPosition();
        if (intentPackageName.getText() != null)
            ppIntent._packageName = intentPackageName.getText().toString().replaceAll(" ","");
        else
            ppIntent._packageName = "";
        if (intentClassName.getText() != null)
            ppIntent._className = intentClassName.getText().toString().replaceAll(" ","");
        else
            ppIntent._className = "";
        if (intentData.getText() != null)
            ppIntent._data = intentData.getText().toString();
        else
            ppIntent._data = "";
        if (intentMimeType.getText() != null)
            ppIntent._mimeType = intentMimeType.getText().toString().replaceAll(" ","");
        else
            ppIntent._mimeType = "";

        int actionSpinnerId = intentActionSpinner.getSelectedItemPosition();
        if (actionSpinnerId == 0)
            ppIntent._action = "";
        else
        if (actionSpinnerId == 1)
            if (intentActionEdit.getText() != null)
                ppIntent._action = intentActionEdit.getText().toString();
            else
                ppIntent._action = "";
        else {
            ppIntent._action = actionsArray[actionSpinnerId];
        }

        ppIntent._categories = "";
        int i = 0;
        for (boolean selected : categoryIndices) {
            if (selected) {
                if (!ppIntent._categories.isEmpty())
                    //noinspection StringConcatenationInLoop
                    ppIntent._categories = ppIntent._categories + "|";
                //noinspection StringConcatenationInLoop
                ppIntent._categories = ppIntent._categories + categoryArray[i];
            }
            ++i;
        }

        ppIntent._flags = "";
        i = 0;
        for (boolean selected : flagIndices) {
            if (selected) {
                if (!ppIntent._flags.isEmpty())
                    //noinspection StringConcatenationInLoop
                    ppIntent._flags = ppIntent._flags + "|";
                //noinspection StringConcatenationInLoop
                ppIntent._flags = ppIntent._flags + flagArray[i];
            }
            ++i;
        }

        if (intentExtraKeyName1.getText() != null)
            ppIntent._extraKey1 = intentExtraKeyName1.getText().toString();
        else
            ppIntent._extraKey1 = "";
        if (intentExtraKeyValue1.getText() != null)
            ppIntent._extraValue1 = intentExtraKeyValue1.getText().toString();
        else
            ppIntent._extraValue1 = "";
        ppIntent._extraType1 = intentExtraSpinner1.getSelectedItemPosition();

        if (intentExtraKeyName2.getText() != null)
            ppIntent._extraKey2 = intentExtraKeyName2.getText().toString();
        else
            ppIntent._extraKey2 = "";
        if (intentExtraKeyValue2.getText() != null)
            ppIntent._extraValue2 = intentExtraKeyValue2.getText().toString();
        else
            ppIntent._extraValue2 = "";
        ppIntent._extraType2 = intentExtraSpinner2.getSelectedItemPosition();

        if (intentExtraKeyName3.getText() != null)
            ppIntent._extraKey3 = intentExtraKeyName3.getText().toString();
        else
            ppIntent._extraKey3 = "";
        if (intentExtraKeyValue3.getText() != null)
            ppIntent._extraValue3 = intentExtraKeyValue3.getText().toString();
        else
            ppIntent._extraValue3 = "";
        ppIntent._extraType3 = intentExtraSpinner3.getSelectedItemPosition();

        if (intentExtraKeyName4.getText() != null)
            ppIntent._extraKey4 = intentExtraKeyName4.getText().toString();
        else
            ppIntent._extraKey4 = "";
        if (intentExtraKeyValue4.getText() != null)
            ppIntent._extraValue4 = intentExtraKeyValue4.getText().toString();
        else
            ppIntent._extraValue4 = "";
        ppIntent._extraType4 = intentExtraSpinner4.getSelectedItemPosition();

        if (intentExtraKeyName5.getText() != null)
            ppIntent._extraKey5 = intentExtraKeyName5.getText().toString();
        else
            ppIntent._extraKey5 = "";
        if (intentExtraKeyValue5.getText() != null)
            ppIntent._extraValue5 = intentExtraKeyValue5.getText().toString();
        else
            ppIntent._extraValue5 = "";
        ppIntent._extraType5 = intentExtraSpinner5.getSelectedItemPosition();
    }

    private void enableOKButton() {
        boolean enableOK = (!intentNameEditText.getText().toString().isEmpty());
        okButton.setEnabled(enableOK);
    }

    static Intent createIntent(PPIntent ppIntent) {
        /*
        intent = new Intent("android.intent.action.MAIN");
        intent.setComponent(new ComponentName("com.kuma.smartnotify","com.kuma.smartnotify.SmartNotifyMain"));
        intent.putExtra("CARMODEON",true);
        intent.putExtra("CARMODEOFF",true);
        */

        Intent intent;
        try {
            intent = new Intent();
            if ((ppIntent._packageName != null) && (!ppIntent._packageName.isEmpty()) &&
                    (ppIntent._className != null) && (!ppIntent._className.isEmpty()))
                intent.setComponent(new ComponentName(ppIntent._packageName, ppIntent._className));

{'fraction_non_alphanumeric': 0.0826951409858066, 'fraction_numerical': 0.0049304917363208025, 'mean_word_length': 2.0973659241856053, 'pattern_counts': {'":': 4, '<': 41, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6022257', 'n_tokens_mistral': 16815, 'n_tokens_neox': 15995, 'n_words': 2375}