/** * Optional upgrade, to extend the notifier, with a little variety to change the variables. * notifier in App Inventor, but based upon the original code. * Credits: @HalAbelson - MIT who wrote the original notifier code * Credits: @Shreyash for the Rush extension builder * Credits: @TIMAI2 available source was the basis of the upgrade * Credits: @Gordon_Lu available source * Credits: @DIY_channel available source * Additions: @Oto, 24-03-2023(Ukraine) */ package sxem.org.metodnotifier; import android.app.Activity; import android.app.AlertDialog; import com.google.appinventor.components.runtime.EventDispatcher; import com.google.appinventor.components.annotations.SimpleEvent; import com.google.appinventor.components.annotations.SimpleFunction; import com.google.appinventor.components.runtime.AndroidNonvisibleComponent; import com.google.appinventor.components.runtime.ComponentContainer; import android.widget.EditText; import android.widget.LinearLayout; import android.view.View; import android.content.DialogInterface; import android.view.inputmethod.InputMethodManager; import android.content.Context; import android.widget.NumberPicker; import android.graphics.Typeface; public class MetodNotifier extends AndroidNonvisibleComponent { private final Context context; private final Activity activity; public MetodNotifier(ComponentContainer container){ super(container.$form()); activity = container.$context(); this.context = container.$context(); } /** ###################################################################################################### * */ @SimpleFunction(description = "Показує діалогове вікно запитань, яке встановлює користувач, і дозволяє " + " позитивну відповідь або скасування від користувача. Викликається відповідна подія \n " + "від трьох кнопок \"Left\",\"Right\",\"Cancel\" \n ") public void InfoChoiceDialog(final int identify, String message, String title, String buttonTextLeft, String buttonTextRight, String buttonTextCancel) { InfoChoiceAlert(identify, message, title, buttonTextLeft, buttonTextRight, buttonTextCancel); } private void InfoChoiceAlert(final int identify, String message, String title, String buttonTextLeft, String buttonTextRight, String buttonTextCancel) { AlertDialog.Builder alert = new AlertDialog.Builder(activity); alert.setTitle(title); alert.setMessage(message); alert.setPositiveButton(buttonTextLeft, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { InfoChoiceInput(identify, buttonTextLeft.toString()); } }); alert.setNeutralButton( buttonTextRight, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { InfoChoiceInput(identify, buttonTextRight.toString()); } }); alert.setNegativeButton(buttonTextCancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { InfoChoiceCanceled(identify); //AfterQuestion("Cancel"); } }); alert.show(); } @SimpleEvent( description = "Event raised after the user has responded to ShowQuestionDialog.") public void InfoChoiceInput(final int identify, String response) { EventDispatcher.dispatchEvent(this, "InfoChoiceInput", identify, response); } @SimpleEvent( description = "Подія викликана, коли користувач скасував ПоказатиТекстДіалог.") public void InfoChoiceCanceled(final int identify) { EventDispatcher.dispatchEvent(this, "InfoChoiceCanceled", identify); } /** ############################################################################################################## * LOGIN/PASS ENTRY DIALOG , String hintUp, String hintDown */ @SimpleFunction(description = "Показує подвійне діалогове вікно, де користувач може ввести назву" + "і деталі назви. Користувач також може скасувати") public void DoubleWindowDialog(final int identify, String message, String title, String hintUp, String hintDown, String textUp, String textDown, boolean cancelable) { DoubleWindowTxt( identify, message, title, hintUp, hintDown, textUp, textDown, cancelable); } private void DoubleWindowTxt(final int identify, String message, String title, String hintUp, String hintDown, String textUp, String textDown, boolean cancelable) { AlertDialog.Builder alert = new AlertDialog.Builder(activity); alert.setTitle(title); alert.setMessage(message); alert.setCancelable(false); LinearLayout layout = new LinearLayout(activity); layout.setOrientation(LinearLayout.VERTICAL); final EditText inputLogin = new EditText(activity); inputLogin.setHint(hintUp); inputLogin.setText(textUp); layout.addView(inputLogin); final EditText inputPass = new EditText(activity); inputPass.setHint(hintDown); inputPass.setText(textDown); layout.addView(inputPass); alert.setView(layout); alert.setPositiveButton("OK!", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { HideKeyboard((View) layout); DoubleWindowInput(identify, inputLogin.getText().toString(), inputPass.getText().toString()); } }); if (cancelable) { alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { DoubleWindowCanceled(identify); HideKeyboard((View) layout); } }); } alert.show(); // Request focus after showing dialog Запитати фокус після показу діалогу inputLogin.requestFocus(); // Show soft keyboard Показати програмну клавіатуру InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); } @SimpleEvent(description = "Подія, що виникає після того, як користувач відповів на ДубльВікнаТекст.") public void DoubleWindowInput(final int identify, String textUp, String textDown) { EventDispatcher.dispatchEvent(this, "DoubleWindowInput", identify, textUp, textDown); } @SimpleEvent(description = "Event raised when the user canceled ДубльВікнаТекст.") public void DoubleWindowCanceled(final int identify) { EventDispatcher.dispatchEvent(this, "DoubleWindowCanceled", identify); } /** ############################################################################################################## */ @SimpleFunction(description = "Відображає діалогове вікно вибору номерів, яке дає змогу користувачеві вибрати число з попередньо визначеного діапазону.") public void NumberPickerDialog(final int identify, String message, String title, String buttonText, int defaultValue, int minValue, int maxValue, boolean cancelable) { TextNumberPickerDialog(identify, message, title, buttonText, defaultValue, minValue, maxValue, cancelable); } private void TextNumberPickerDialog(final int identify, String message, String title, String buttonText, int defaultValue, int minValue, int maxValue, boolean cancelable) { AlertDialog.Builder alert = new AlertDialog.Builder(this.context); final NumberPicker numberPicker = new NumberPicker(this.context); numberPicker.setMaxValue(maxValue); numberPicker.setMinValue(minValue); numberPicker.setValue(defaultValue); alert.setView(numberPicker); alert.setCancelable(false); alert.setTitle(title); alert.setMessage(message); alert.setPositiveButton(buttonText, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { NumberPickerInput(identify, numberPicker.getValue()); } }); if (cancelable) { alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { NumberPickerCanceled(identify); } }); } alert.show(); } @SimpleEvent(description = "This event is fired when the user has pressed the OK button in a number picker dialog.") public void NumberPickerInput(int identify, int number) { EventDispatcher.dispatchEvent(this, "NumberPickerInput", identify, number); } @SimpleEvent(description = "This event is fired when the user has pressed the cancel button in a number picker dialog.") public void NumberPickerCanceled(int identify) { EventDispatcher.dispatchEvent(this, "NumberPickerCanceled", identify); } /** ############################################################################################################## * */ @SimpleFunction(description = "Відображає діалогове вікно для введення тексту. \n " + "logic = 0 Немає типу вмісту. Текст не редагується \n " + "logic = 1 звичайн. текст. \n " + "logic = 2 тільки цифри. \n " + "logic = 3 номер телефону. \n " + "logic = 4 дата/час. \n " + "logic = 12 Числове поле пароля \n " + "logic = 21 адреса електронної пошти. \n " + "logic = 24 введення часу. \n " + "logic = 81 текстове поле пароля. ") public void TxtDigitShowDialog(final int identify, String message, String title, String textDefault, String hintText, int hintColor, int inputColor, String buttonText, String cancelButtonText, boolean cancelable, int logic) { TextTxtDigitShowDialog( identify, message, title, textDefault, hintText, hintColor, inputColor, buttonText, cancelButtonText, cancelable, logic); } private void TextTxtDigitShowDialog(final int identify, String message, String title, String textDefault, String hintText, int hintColor, int inputColor, String buttonText, String cancelButtonText, boolean cancelable, int logic) { AlertDialog.Builder alert = new AlertDialog.Builder(this.context); alert.setTitle(title); alert.setMessage(message); alert.setCancelable(false); final EditText edit = new EditText(this.context); edit.setText(textDefault); edit.setHintTextColor(hintColor); edit.setTextColor(inputColor); edit.setHint(hintText); edit.setTypeface(Typeface.DEFAULT, Typeface.BOLD); edit.setInputType(logic); alert.setView(edit); alert.setPositiveButton(buttonText, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { TxtDigitShowInput(identify, edit.getText().toString()); InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(edit.getWindowToken(), 0); } }); if (cancelable) { alert.setNegativeButton(cancelButtonText, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { TxtDigitShowCanceled(identify); InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(edit.getWindowToken(), 0); } }); } alert.show(); } @SimpleEvent(description = "This event is fired when the user has pressed the OK button in a text response dialog.") public void TxtDigitShowInput(final int identify, String response) { EventDispatcher.dispatchEvent(this, "TxtDigitShowInput", identify, response); } @SimpleEvent(description = "This event is fired when the user has pressed the cancel button in a text response dialog.") public void TxtDigitShowCanceled(final int identify) { EventDispatcher.dispatchEvent(this, "TxtDigitShowCanceled", identify); } /** ######################################################################################################### * Приховати програмну клавіатуру після того, як користувач введе текст або скасує введення. */ public void HideKeyboard(View view) { if (view != null) { InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } } }