package com.google.blockly.android.ui.fieldview;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatSpinner;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;

/**
 * Common Code for Field DropDown Adapters
 * This makes a Spinner's width, the width of the currently selected item, not a one size fits all.
 */
class FieldAdapter<T> extends ArrayAdapter<T> {
    private AppCompatSpinner mSpinner;

    public FieldAdapter(@NonNull Context context, int resource, @NonNull List<T> objects, @Nullable AppCompatSpinner mSpinner) {
        super(context, resource, objects);
        this.mSpinner = mSpinner;
    }

    public FieldAdapter(@NonNull Context context, int resource, @Nullable AppCompatSpinner mSpinner) {
        super(context, resource);
        this.mSpinner = mSpinner;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        // Generate Stack Trace
        StringWriter stackTrace = new StringWriter();
        PrintWriter printer = new PrintWriter(stackTrace);
        new RuntimeException().printStackTrace(printer);
        // Use Stack Trace To Check if Method is being Called By android.widget.Spinner.measureContentWidth (fixes #735)
        if (mSpinner != null && stackTrace.toString().contains("at android.widget.Spinner.measureContentWidth")) {
            return super.getView(mSpinner.getSelectedItemPosition(), convertView, parent);
        } else {
            return super.getView(position, convertView, parent);
        }
    }
}

@@ -311,65 +311,8 @@
 r;%0A%0A
-import java.io.PrintWriter;%0Aimport java.io.StringWriter;%0A
 impo
@@ -969,278 +969,159 @@
     
-@NonNull%0A    @Override%0A    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) %7B%0A        // Generate Stack Trace%0A        StringWriter stackTrace = new StringWriter();%0A        PrintWriter printer = new PrintWriter(stackTrace);%0A       
+private boolean isMeasuringContent() %7B%0A        if (mSpinner != null) %7B%0A            // Generate Stack Trace%0A            StackTraceElement%5B%5D stackTrace =
  new
@@ -1140,20 +1140,18 @@
 ption().
-prin
+ge
 tStackTr
@@ -1158,18 +1158,15 @@
 ace(
-printer);%0A
+);%0A    
     
@@ -1294,32 +1294,78 @@
     
-if (mSpinner != null && 
+    for (int i = 0; i %3C stackTrace.length; i++) %7B%0A                if (
 stac
@@ -1370,16 +1370,19 @@
 ackTrace
+%5Bi%5D
 .toStrin
@@ -1399,11 +1399,8 @@
 ns(%22
-at 
 andr
@@ -1439,24 +1439,333 @@
 tWidth%22)) %7B%0A
+                    return true;%0A                %7D%0A            %7D%0A            return false;%0A        %7D else %7B%0A            return false;%0A        %7D%0A    %7D%0A%0A    @NonNull%0A    @Override%0A    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) %7B%0A        if (isMeasuringContent()) %7B%0A
             

Improve Performance