From 22afe8ac5f3ced24bf6e36091aea53b826e322bd Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 21 Aug 2016 12:24:34 +0200 Subject: [PATCH] Don't crash on function save Internals of TextInputLayout have changed and EditText now is not on the first level --- .../org/solovyev/android/calculator/App.java | 33 +++++++++++++++++-- .../functions/FunctionParamsView.java | 28 +++++++++------- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/App.java b/app/src/main/java/org/solovyev/android/calculator/App.java index 8bf8eb77..e96770ce 100644 --- a/app/src/main/java/org/solovyev/android/calculator/App.java +++ b/app/src/main/java/org/solovyev/android/calculator/App.java @@ -22,6 +22,9 @@ package org.solovyev.android.calculator; +import org.solovyev.android.Check; +import org.solovyev.android.calculator.floating.FloatingCalculatorService; + import android.app.Activity; import android.app.Application; import android.app.Dialog; @@ -54,9 +57,6 @@ import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; import android.widget.ArrayAdapter; -import org.solovyev.android.Check; -import org.solovyev.android.calculator.floating.FloatingCalculatorService; - import java.util.List; import java.util.concurrent.atomic.AtomicInteger; @@ -283,6 +283,33 @@ public final class App { processViewsOfType0(view, viewClass, viewProcessor); } + @Nullable + public static T find(@Nonnull View view, @Nonnull Class viewClass) { + return find0(view, viewClass); + } + + private static T find0(View view, Class viewClass) { + if (view instanceof ViewGroup) { + final ViewGroup viewGroup = (ViewGroup) view; + + if (viewClass.isAssignableFrom(ViewGroup.class)) { + //noinspection unchecked + return (T) view; + } + + for (int index = 0; index < viewGroup.getChildCount(); index++) { + final T child = find0(viewGroup.getChildAt(index), viewClass); + if (child != null) { + return child; + } + } + } else if (viewClass.isAssignableFrom(view.getClass())) { + //noinspection unchecked + return (T) view; + } + return null; + } + @Nonnull public static ArrayAdapter makeSimpleSpinnerAdapter(@NonNull Context context) { return new ArrayAdapter<>(context, R.layout.support_simple_spinner_dropdown_item); diff --git a/app/src/main/java/org/solovyev/android/calculator/functions/FunctionParamsView.java b/app/src/main/java/org/solovyev/android/calculator/functions/FunctionParamsView.java index 3e584185..1ff24d46 100644 --- a/app/src/main/java/org/solovyev/android/calculator/functions/FunctionParamsView.java +++ b/app/src/main/java/org/solovyev/android/calculator/functions/FunctionParamsView.java @@ -22,6 +22,11 @@ package org.solovyev.android.calculator.functions; +import org.solovyev.android.Check; +import org.solovyev.android.calculator.App; +import org.solovyev.android.calculator.Preferences; +import org.solovyev.android.calculator.R; + import android.annotation.TargetApi; import android.content.Context; import android.content.res.Resources; @@ -36,18 +41,19 @@ import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.EditorInfo; -import android.widget.*; -import org.solovyev.android.Check; -import org.solovyev.android.calculator.App; -import org.solovyev.android.calculator.Preferences; -import org.solovyev.android.calculator.R; +import android.widget.EditText; +import android.widget.ImageButton; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.TextView; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; @@ -278,11 +284,9 @@ public class FunctionParamsView extends LinearLayout { @Nonnull private EditText getParamView(@Nonnull ViewGroup row) { final TextInputLayout paramLabel = getParamLabel(row); - for (int i = 0; i < paramLabel.getChildCount(); i++) { - final View child = paramLabel.getChildAt(i); - if (child instanceof EditText) { - return (EditText) child; - } + final EditText paramView = App.find(paramLabel, EditText.class); + if (paramView != null) { + return paramView; } Check.shouldNotHappen(); return null;