Friday, October 28, 2011

gridview which scrolls horizontally with network call

/**
 * Class Name : CategoryMenu
 *
 * Parent Class :Activity
 *
 * Interfaces: None
 *
 * Description:This class displays the categories and
   subcategories menu to the user. Categories will bedisplayed
    as a one line horizontal gallery and Subcategories as a list.

 */

import java.util.ArrayList;
import java.util.List;

import com.ServiceMessenger.R;




import com.inception.dataparser.Category;
import com.inception.dataparser.ServiceProvider;
import com.inception.dataparser.UserProfiles;
import com.inception.dataprovider.DatabaseHelper;
import com.inception.network.MessageAlertService;
import com.inception.network.NetworkService;
import com.inception.network.NetworkUtilService;
import com.inception.sm.ApplicationManager;
import com.inception.sm.Globals;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView.ScaleType;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;


public class CategoryMenu extends Activity {
    /** Called when the activity is first created. */

    ArrayList<UserProfiles> mUserProfiles = new ArrayList<UserProfiles>();
    public List<String> mList = null;
    private ArrayList<ServiceProvider> mProfileList = new ArrayList<ServiceProvider>();
    public static String mUProfilrid;
    private static final int INDETERMINATE = 0;
   
    String mProfileid;


    ///    public static boolean mLogin_status=false;

    private int  mLoadingFirst;
    SharedPreferences mSettings;
    SharedPreferences mSettings1;
    public static SharedPreferences mLogin_status;
    static Boolean mStatus;


    private int mCatvs;
    private int mCurrentv;
    private int mCatv1;
    private LayoutInflater mInflater;
    private NotificationManager mNotificationManager;
    private ProgressDialog mProgDlg;
    private static ApplicationManager mAppMgr;
    private DatabaseHelper mDBHelper;
    final Messenger mMessenger = new Messenger(new IncomingHandler());
    Messenger mService = null;

    //wedgets
    Intent i;
    Bitmap viewBgrnd;
    Integer[] mThumbIds;
    ImageButton img ;
    Button button;
    ImageView img1;
    TextView tx;
    Button b1;
   
    //gallery wedgets
    private ScrollView nsv;
    private HorizontalScrollView sv;
    private LinearLayout llh;
    private LinearLayout.LayoutParams layoutParamsTV;
    private LinearLayout.LayoutParams layoutParamsLL;
    private LinearLayout.LayoutParams layoutParamsLLD;
    private LinearLayout llv;
   
    static int  total;
    private Category mCat = new Category();
    /**
     * Class Name :IncomingHandler
     *
     * Parent Class :Handler
     *
     * Interfaces: None
     *
     * Description:Handler class for handling
     *
     *  incoming messages from server

     */

    class IncomingHandler extends Handler {

        /**
         * Callback method to handle messages coming from server
         *
         */

        @Override
        public void handleMessage(Message msg) {
            Log.e("UIAct","handleMessage");
            switch (msg.what) {
            case NetworkService.MSG_CATALOG_AVAILABLE:
                mList= new ArrayList<String>();
               
                Log.e("MSG_CATALOG_AVAILABLE",""+mList.size());
               
                mList=mAppMgr.getCategories();
                horizontalScrollGalleryLayout(mList);
                mProgDlg.dismiss();
                //mGallery.setAdapter(new ImageAdapter(CategoryMenu.this));
                break;


            default:
                super.handleMessage(msg);
            }
        }


    }
    /**
     * Callback method to get connection from server
     *
     */
    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mService = new Messenger(service);

            try {
                Log.e("UIAct","onServiceConnected");

                Message msg = Message.obtain(null, NetworkService.MSG_REGISTER_CLIENT);
                msg.replyTo = mMessenger;
                mService.send(msg);
                //Log.e("mCatv in 2nd if","xyz"+mCatvs);
                //Log.e("mCurrentv in 2nd if",""+mCurrentv);
                //msg.replyTo = mMessenger;
                msg = Message.obtain(null, NetworkService.MSG_GET_CATALOG);
                msg.replyTo = mMessenger;
                mService.send(msg);
                Log.d("connecting to","categories service");
                //    }


            } catch (RemoteException e) {
                // In this case the service has crashed before we could even do anything with it
            }

        }

        /**
         * Callback method to get disconnect from server
         *
         */

        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been unexpectedly disconnected - process crashed.
            mService = null;
            Log.e("UIAct","onServiceDisconnected");
        }
    };

    /**
     * Callback method to launch the Acitivity
     *
     */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSettings = this.getSharedPreferences(Globals.PREFS_NAME, 0);
        mLogin_status = this.getSharedPreferences(Globals.PREFS_NAME, 0);
        mStatus=mLogin_status.getBoolean("status", false);
        mSettings1 = this.getSharedPreferences(Globals.PREFS_NAME, 0);
       
       
        Context context = getApplicationContext();
        nsv=new ScrollView(this);
        sv = new HorizontalScrollView(this);
        llh = new LinearLayout(this);
        llh.setOrientation(LinearLayout.HORIZONTAL);
        layoutParamsTV = new LinearLayout.LayoutParams(80,80);
        layoutParamsTV.setMargins(15, 30, 15, 15);

        layoutParamsLL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParamsLLD = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        mDBHelper = new DatabaseHelper(this);
        mAppMgr = ApplicationManager.getApplicationMgrInstance(this.getApplicationContext());
        mList =new ArrayList<String>();
        mList =mAppMgr.getCategories();
        total=0;
        //horizontalScrollGalleryLayout1(mList);
        Log.i("mList",""+mList.size());

        if(mList.size()==0){
            boolean check_con=NetworkUtilService.getInstance(this).isOnline(this);            Log.i("check_con",""+check_con);
            if (NetworkUtilService.getInstance(this).isOnline(this)) {
                Log.e("binding to","service");
                showDialog(INDETERMINATE);
                //mLoadingFirst=1;
                bindService(new Intent(CategoryMenu.this, NetworkService.class), mConnection, Context.BIND_AUTO_CREATE);
                Log.e("loading first time","for catalog");       
                //horizontalScrollGalleryLayout(mList);
                mProgDlg.dismiss();
            }
            else
            { 
               
                AlertDialog.Builder dialog1 = new AlertDialog.Builder(CategoryMenu.this);
                dialog1.setTitle("ALERT");
                dialog1.setMessage("No Connection! Try Later");
                dialog1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog1, int id) {

                        CategoryMenu.this.finish();
                        //dialog1.cancel();
                    }
                });
                /*dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                       
                        dialog.cancel();
                    }
                });*/
                AlertDialog alert =dialog1.create();
                // Title for AlertDialog
                alert.setTitle("OUT OF CONNECTION");
                // Icon for AlertDialog
                alert.setIcon(R.drawable.icon);
                alert.show();
            }



        }
        else
        {
            horizontalScrollGalleryLayout(mList);
            Log.e("mList in else",""+mList.size());
            //    Log.e(mList.get(0).);
        }
       

    }
   
    public void horizontalScrollGalleryLayout (List<String> mList3) {
        Integer[] mThumbIds = {
                R.drawable.gallery_photo_1,R.drawable.gallery_photo_2,R.drawable.gallery_photo_3,
                R.drawable.gallery_photo_4,R.drawable.gallery_photo_5,R.drawable.gallery_photo_6,
                R.drawable.gallery_photo_1,R.drawable.gallery_photo_2,R.drawable.gallery_photo_3,
                R.drawable.gallery_photo_4,R.drawable.gallery_photo_2,
                R.drawable.gallery_photo_1,R.drawable.gallery_photo_2,R.drawable.gallery_photo_3,
                R.drawable.gallery_photo_4,R.drawable.gallery_photo_5,R.drawable.gallery_photo_6,
                R.drawable.gallery_photo_1,R.drawable.gallery_photo_2,R.drawable.gallery_photo_3,
                R.drawable.gallery_photo_4,R.drawable.gallery_photo_5,R.drawable.gallery_photo_6,
                R.drawable.gallery_photo_1,R.drawable.gallery_photo_2,R.drawable.gallery_photo_3,
                R.drawable.gallery_photo_4,R.drawable.gallery_photo_5,R.drawable.gallery_photo_6,
                /*  R.drawable.gallery_photo_1,R.drawable.gallery_photo_2,R.drawable.gallery_photo_3,
                  R.drawable.gallery_photo_4,R.drawable.gallery_photo_5,R.drawable.gallery_photo_6,
                  R.drawable.gallery_photo_1,R.drawable.gallery_photo_2,R.drawable.gallery_photo_3*/


        };
        Log.i("I got call","for creation of horizontalgallery");
        sv = new HorizontalScrollView(this);
        llh = new LinearLayout(this);
        llh.setOrientation(LinearLayout.HORIZONTAL);
        layoutParamsTV = new LinearLayout.LayoutParams(75,75);
        layoutParamsTV.setMargins(15, 30, 15, 15);

        layoutParamsLL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParamsLLD = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        Log.e("in horizantal","scroll view 1");
        for(int k=0;k<mList3.size();k++)
        {  
            if(mList3.size()==total)
            {
                break;
            }
            llv = new LinearLayout(this);
           
            for (int i=0; i<3; i++) {
                llv.setOrientation(LinearLayout.VERTICAL);
                //button= new Button(this);
                //button=new Button(this);
                img1 = new ImageView(this);
                tx = new TextView(this);
                mCat = new Category();
                try {

                    img1.setBackgroundResource(mThumbIds[total]);
                    img1.setTag(mList3.get(total));
                    tx.setText(mList3.get(total));
                    tx.setTextColor(Color.WHITE);
                    //tx.setPadding(25, 0, 15, 0);
                    tx.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

                    img1.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View view) {
                            // Perform action on click
                            //CharSequence cat = "Education";
                            CharSequence cat = (CharSequence) view.getTag();
                            Log.e("imgTag after category clicked",""+ cat);
                            CharSequence category = cat;
                            mCat=mAppMgr.getCategoryObjectFromId(category);
                            String isleaf = mCat.getIs_leaf();
                            String parent_id=mCat.getParent_id();
                            String cat_id=mCat.getCategory_id();

                            Log.e("isleaf",isleaf);
                            //Log.e("parent_id",parent_id);
                            if(isleaf.equals("false"))
                            {
                                Intent i=new Intent(CategoryMenu.this,ProfilesList.class);
                                //i.putExtra("subcat", category);
                                Log.d("false executed","Thanks");
                                i.putExtra("cat", category);
                                i.putExtra("view", Globals.CONST_SUBCATEGORY);
                                startActivity(i);
                            }
                            else
                            {
                                Intent i=new Intent(CategoryMenu.this,ProfilesList.class);
                                Log.d("true executed","Thanks");

                                i.putExtra("cat",cat_id);
                                i.putExtra("view", Globals.CONST_PROFILE);
                                startActivity(i);
                            }


                            //CharSequence category = cat;
                            //Log.e("category",""+category);
                            //i.putExtra("subcat", category);
                            //i.putExtra("view", Globals.CONST_PROFILE);

                            Log.e("am in","sub categories");



                        }
                    });


                    total++;

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                llv.addView(img1,layoutParamsTV);
                llv.addView(tx);
                if(mList3.size()==total)
                    break;
                //b1[k].setBackgroundResource(mThumbIds[k]);
            }

            //llv.addView(button, layoutParamsTV);

            llh.addView(llv, layoutParamsLL);
            llh.setBackgroundColor(Color.TRANSPARENT);
            llh.setHorizontalScrollBarEnabled(false);
        }

        sv.addView(llh, layoutParamsLLD);
        nsv.addView(sv);
        sv.setHorizontalScrollBarEnabled(false);
        nsv.setVerticalScrollBarEnabled(false);
        //setContentView(sv);
        setContentView(nsv);
       
    }


    /**
     *Progress bar  to show progress of fetching data from server
     */

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case INDETERMINATE: {
            mProgDlg = new ProgressDialog(this);
            mProgDlg.setTitle("Indeterminate");
            mProgDlg.setMessage("Please wait while loading...");
            mProgDlg.setIndeterminate(true);
            mProgDlg.setCancelable(true);
            return mProgDlg;
        }
        }
        return null;
    }






   
    public void onDestroy() {
        Log.i("CategoryMenu","onDestroy Called");
        super.onDestroy();
        try {
            stopService(new Intent(CategoryMenu.this, MessageAlertService.class));
        } catch (Throwable t) {
            Log.e("CategoryMenu", "stopService", t);
        }
    }
}

No comments:

Post a Comment