package net.progval.android.andquote;

import java.net.MalformedURLException;
import java.util.Iterator;
import java.io.InputStream;

import net.progval.android.andquote.utils.OpenQuoteApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ScrollView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class MainActivity extends Activity implements OnClickListener {
    private static SharedPreferences settings; 
    private OpenQuoteApi api;
    private LinearLayout layout;
    
    private class ProgressListener implements OpenQuoteApi.ProgressListener {

        public void onProgressUpdate(int progress) {
            MainActivity.this.setProgress(progress);
        }

        public void onFail(int status_message) {
            Toast.makeText(MainActivity.this, status_message, 
                Toast.LENGTH_SHORT).show();
        }

        public void onSuccess(InputStream stream) {
            OpenQuoteApi.Site[] sites = OpenQuoteApi.Site.parse_sites(stream);
            for (int i=0; i<sites.length && sites[i]!=null; i++)
                MainActivity.this.registerSite(sites[i]);
        }
        
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);
        MainActivity.settings = PreferenceManager.getDefaultSharedPreferences(this);
        
        this.setTitle(R.string.app_name);

        LinearLayout layout = new LinearLayout(this);
        ScrollView scrollview = new ScrollView(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                    LinearLayout.LayoutParams.FILL_PARENT,
                                    LinearLayout.LayoutParams.FILL_PARENT);
        scrollview.setLayoutParams(params);
        layout.addView(scrollview);
        this.setContentView(layout);

        this.layout = new LinearLayout(this);
        this.layout.setOrientation(this.layout.VERTICAL);
        scrollview.addView(this.layout);
        this.layout.setOrientation(this.layout.VERTICAL);
        
        this.api = new OpenQuoteApi(this.settings.getString("api.url", ""));
        try {
            this.api.get(new MainActivity.ProgressListener(), "/");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.mainactivity_menu_settings:
                Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
                this.startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void registerSite(OpenQuoteApi.Site site) {
        Button button = new Button(this);
        button.setText(site.name);
        button.setTag(site.id);
        button.setOnClickListener(this);
        this.layout.addView(button);
    }
    
    public void onClick(View view) {
        Button button = (Button) view;
        String id = (String) button.getTag();
        Intent intent = new Intent(this, SiteActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("site_id", id);
        bundle.putString("site_name", button.getText().toString());
        intent.putExtras(bundle);
        this.startActivity(intent);
    }
    
    
}

@@ -98,16 +98,44 @@
 erator;%0A
+import java.util.ArrayList;%0A
 import j
@@ -639,38 +639,139 @@
 et.T
-oast;%0Aimport android.view.Menu
+ableLayout;%0Aimport android.widget.Toast;%0Aimport android.widget.BaseAdapter;%0Aimport android.view.Menu;%0Aimport android.view.ViewGroup
 ;%0Aim
@@ -1082,35 +1082,86 @@
 private 
-LinearLayout layout
+GridView gridview;%0A    ArrayList%3CView%3E buttons = new ArrayList%3CView%3E()
 ;%0A    %0A 
@@ -1797,24 +1797,550 @@
      %0A    %7D%0A
+    public class CustomAdapter extends BaseAdapter %7B%0A%0A        public View getView(int position, View convertView, ViewGroup parent) %7B%0A            return MainActivity.this.buttons.get(position);%0A        %7D%0A        public final int getCount() %7B%0A            return MainActivity.this.buttons.size();%0A        %7D%0A%0A        public final Object getItem(int position) %7B%0A            return MainActivity.this.buttons.get(position);%0A        %7D%0A%0A        public final long getItemId(int position) %7B%0A            return position;%0A        %7D%0A    %7D%0A
     %0A    @Ov
@@ -2691,287 +2691,46 @@
     
-ScrollView scrollview = new ScrollView(this);%0A        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(%0A                                    LinearLayout.LayoutParams.FILL_PARENT,%0A                                    LinearLayout.LayoutParams.FILL_PARENT
+this.setContentView(layout
 );%0A
+%0A
         
 scro
@@ -2729,40 +2729,40 @@
     
-scrollview.setLayoutParams(param
+this.gridview = new GridView(thi
 s);%0A
@@ -2784,22 +2784,25 @@
 addView(
-scroll
+this.grid
 view);%0A 
@@ -2817,233 +2817,131 @@
 his.
-setContentView(layout);%0A%0A        this.layout = new LinearLayout(this);%0A        this.layout.setOrientation(this.layout.VERTICAL);%0A        scrollview.addView(this.layout);%0A        this.layout.setOrientation(this.layout.VERTICAL
+gridview.setAdapter(new CustomAdapter());%0A        this.gridview.setColumnWidth(200);%0A        this.gridview.setNumColumns(-1
 );%0A 
@@ -4055,30 +4055,27 @@
    this.
-layout.addView
+buttons.add
 (button)

Use a GridView to display list of sites.