package com.sometrik.framework;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

public class NavigationBar extends FrameLayout implements NativeCommandHandler {
  
  FrameWork frame;
  LinearLayout baseLayout;
  LinearLayout.LayoutParams childParams;
  ArrayList<ImageView> childList;
  
  public NavigationBar(FrameWork frame) {
    super(frame);

    this.frame = frame;
    this.setBackground(frame.getResources().getDrawable(android.R.drawable.dialog_holo_light_frame));
    childList = new ArrayList<ImageView>();
    baseLayout = new LinearLayout(frame);
    baseLayout.setOrientation(LinearLayout.HORIZONTAL);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    baseLayout.setLayoutParams(params);
    setLayoutParams(params);

    childParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 50);
    childParams.weight = 1;
    
    addView(baseLayout);
  }

  @Override
  public void onScreenOrientationChange(boolean isLandscape) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void addChild(View view) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void addOption(int optionId, String text) {

    for (ImageView view : childList) {
      if (optionId == view.getId()) {
	try {
	  InputStream stream = frame.getAssets().open(text);
	  view.setImageBitmap(BitmapFactory.decodeStream(stream));
	} catch (IOException e) {
	  e.printStackTrace();
	}
      }
    }
  }

  @Override
  public void addColumn(String text, int columnType) {
    final int buttonId = columnType;
    ImageView view = new ImageView(frame);
    view.setLayoutParams(childParams);
    view.setId(columnType);
    InputStream stream;
    view.setScaleType(ScaleType.CENTER_INSIDE);
    try {
      stream = frame.getAssets().open(text);
      view.setImageBitmap(BitmapFactory.decodeStream(stream));
    } catch (IOException e) {
      e.printStackTrace();
    }
    
    view.setOnClickListener(new OnClickListener(){
      @Override
      public void onClick(View v) {
	System.out.println("Navigation OnClick: " + buttonId);
	frame.intChangedEvent(System.currentTimeMillis() / 1000.0, getId(), 1, buttonId);
      }
    });

    childList.add(view);
    baseLayout.addView(view);
  }

  @Override
  public void addData(String text, int row, int column, int sheet) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void setValue(String v) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void setValue(int v) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void reshape(int value, int size) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void setViewEnabled(Boolean enabled) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void setViewVisibility(boolean visible) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void setStyle(String key, String value) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void setError(boolean hasError, String errorText) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void clear() {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void flush() {
    // TODO Auto-generated method stub
    
  }

  @Override
  public int getElementId() {
    return getId();
  }

}

@@ -150,16 +150,52 @@
 actory;%0A
+import android.util.DisplayMetrics;%0A
 import a
@@ -208,24 +208,24 @@
 .view.View;%0A
-
 import andro
@@ -565,16 +565,40 @@
 ldList;%0A
+  int displayScale = 1;%0A
   %0A  pub
@@ -1140,16 +1140,121 @@
 arams);%0A
+    DisplayMetrics metrics = frame.setupDisplayMetrics();%0A    displayScale = (int) metrics.scaledDensity;
 %0A    chi
@@ -1338,10 +1338,25 @@
 NT, 
-50
+60 * displayScale
 );%0A 

Use displayScale in NavigationBar sizing