package org.commcare.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.commcare.dalvik.R;
import org.commcare.suite.model.DisplayData;
import org.commcare.suite.model.DisplayUnit;
import org.commcare.utils.MediaUtil;
import org.commcare.views.media.AudioButton;
import org.javarosa.core.reference.InvalidReferenceException;
import org.javarosa.core.reference.ReferenceManager;
import org.javarosa.core.services.locale.Localizer;

import java.io.File;

/**
 * This layout for the GenericMenuFormAdapter allows you to load an image, audio, and text
 * to menus.
 *
 * @author wspride
 */
public class HorizontalMediaView extends RelativeLayout {
    private static final String t = "AVTLayout";

    private AudioButton mAudioButton;
    private ImageView mImageView;
    private final int iconDimension;

    public HorizontalMediaView(Context c) {
        super(c);
        this.iconDimension = (int) getResources().getDimension(R.dimen.menu_icon_size);
    }

    public void setDisplay(DisplayUnit display) {
        DisplayData mData = display.evaluate(null);
        setAVT(Localizer.processArguments(mData.getName(), new String[]{""}).trim(), mData.getAudioURI(), mData.getImageURI());
    }

    public void setAVT(String displayText, String audioURI, String imageURI) {
        removeAllViews();

        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        TextView mTextView = (TextView) inflater.inflate(R.layout.menu_list_item, null);
        mTextView.setText(displayText);

        // Layout configurations for our elements in the relative layout
        RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams audioParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(iconDimension, iconDimension);

        String audioFilename = "";
        if (audioURI != null && !audioURI.equals("")) {
            try {
                audioFilename = ReferenceManager._().DeriveReference(audioURI).getLocalURI();
            } catch (InvalidReferenceException e) {
                Log.e(t, "Invalid reference exception");
                e.printStackTrace();
            }
        }

        File audioFile = new File(audioFilename);

        // First set up the audio button
        if (!"".equals(audioFilename) && audioFile.exists()) {
            // An audio file is specified
            mAudioButton = new AudioButton(getContext(), audioURI, true);
            mAudioButton.setId(3245345); // random ID to be used by the relative layout.
            // Set not focusable so that list onclick will work
            mAudioButton.setFocusable(false);
            mAudioButton.setFocusableInTouchMode(false);
            audioParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            audioParams.addRule(CENTER_VERTICAL);
            addView(mAudioButton, audioParams);
        }

        Bitmap b = MediaUtil.inflateDisplayImage(getContext(), imageURI, iconDimension, iconDimension);
        if (b != null) {
            mImageView = new ImageView(getContext());
            mImageView.setPadding(10, 10, 10, 10);
            mImageView.setAdjustViewBounds(true);
            mImageView.setImageBitmap(b);
            mImageView.setId(23422634);
            imageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            audioParams.addRule(CENTER_VERTICAL);
            addView(mImageView, imageParams);
        }

        textParams.addRule(RelativeLayout.CENTER_VERTICAL);
        textParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        if (imageURI != null && !imageURI.equals("") && mImageView != null) {
            textParams.addRule(RelativeLayout.RIGHT_OF, mImageView.getId());
        } else {
            textParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        }

        if (mAudioButton != null) {
            textParams.addRule(RelativeLayout.LEFT_OF, mAudioButton.getId());
        }
        addView(mTextView, textParams);
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        if (visibility != View.VISIBLE) {
            if (mAudioButton != null) {
                mAudioButton.endPlaying();
            }
        }
    }
}

@@ -897,131 +897,128 @@
 ing 
-t = %22AVTLayout%22;%0A%0A    private AudioButton mAudioButton;%0A    private ImageView mImageView;%0A    private final int iconDimensi
+TAG = HorizontalMediaView.class.getSimpleName();%0A%0A    private final int iconDimension;%0A    private AudioButton audioButt
 on;%0A
@@ -1270,20 +1270,16 @@
 valuate(
-null
 );%0A     
@@ -2496,17 +2496,19 @@
   Log.e(
-t
+TAG
 , %22Inval
@@ -2796,34 +2796,33 @@
 ied%0A            
-mA
+a
 udioButton = new
@@ -2869,34 +2869,33 @@
 e);%0A            
-mA
+a
 udioButton.setId
@@ -3021,34 +3021,33 @@
 ork%0A            
-mA
+a
 udioButton.setFo
@@ -3066,34 +3066,33 @@
 e);%0A            
-mA
+a
 udioButton.setFo
@@ -3256,18 +3256,17 @@
 addView(
-mA
+a
 udioButt
@@ -3286,32 +3286,68 @@
 ms);%0A        %7D%0A%0A
+        ImageView imageView = null;%0A
         Bitmap b
@@ -3463,34 +3463,33 @@
 ) %7B%0A            
-mI
+i
 mageView = new I
@@ -3516,34 +3516,33 @@
 ));%0A            
-mI
+i
 mageView.setPadd
@@ -3566,34 +3566,33 @@
 0);%0A            
-mI
+i
 mageView.setAdju
@@ -3615,34 +3615,33 @@
 e);%0A            
-mI
+i
 mageView.setImag
@@ -3664,18 +3664,17 @@
         
-mI
+i
 mageView
@@ -3828,18 +3828,17 @@
 addView(
-mI
+i
 mageView
@@ -4042,18 +4042,17 @@
 (%22%22) && 
-mI
+i
 mageView
@@ -4123,10 +4123,9 @@
 OF, 
-mI
+i
 mage
@@ -4237,34 +4237,33 @@
  %7D%0A%0A        if (
-mA
+a
 udioButton != nu
@@ -4323,18 +4323,17 @@
 EFT_OF, 
-mA
+a
 udioButt
@@ -4590,18 +4590,17 @@
     if (
-mA
+a
 udioButt
@@ -4629,18 +4629,17 @@
         
-mA
+a
 udioButt

Rename fields and make some local to methods