com.instiwork.ProfileActivityOther.java Source code

Java tutorial

Introduction

Here is the source code for com.instiwork.ProfileActivityOther.java

Source

package com.instiwork;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.NoConnectionError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.instiwork.Logger.Logger;
import com.instiwork.adpaters.ProfileLicenseListAdapter;
import com.instiwork.application.InstiworkApplication;
import com.instiwork.constants.InstiworkConstants;
import com.instiwork.customview.RobotoLight;
import com.instiwork.fragments.ProfileEmployee;
import com.instiwork.fragments.ProfileEmployer;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;

public class ProfileActivityOther extends AppCompatActivity {

    ViewPager mViewPager;
    RecyclerView license_list;

    String TAG = "ProfileActivityOther";
    ProgressBar pbar_profile;
    LinkedList<JSONObject> licenseLinkedList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle(" ");

        license_list = (RecyclerView) findViewById(R.id.license_list);
        pbar_profile = (ProgressBar) findViewById(R.id.pbar_profile);
        pbar_profile.setVisibility(View.GONE);

        Picasso.with(getApplicationContext()).load(R.drawable.profile_header_bg).fit().centerCrop()
                .into(((ImageView) findViewById(R.id.backdropback)));

        Picasso.with(getApplicationContext()).load(R.drawable.child_task_bg).fit().centerCrop()
                .into(((ImageView) findViewById(R.id.rl_bg)));

        fetchAccount();

        mViewPager = (ViewPager) findViewById(R.id.fragment_holder);
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mSectionsPagerAdapter);
        tabLayout.setupWithViewPager(mViewPager);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_profile_other, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            onBackPressed();
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    //-----------Frament pager adapter

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            switch (position) {
            case 0:
                return (ProfileEmployer.getInstance(getIntent().getStringExtra("bidder_id")));
            case 1:
                return (ProfileEmployee.getInstance(getIntent().getStringExtra("bidder_id")));
            default:
                return null;
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
            case 0:
                return "Employer";
            case 1:
                return "Employee";
            default:
                return null;
            }
        }
    }

    protected void fetchAccount() {
        pbar_profile.setVisibility(View.VISIBLE);
        String url = "";
        try {
            url = InstiworkConstants.URL_DOMAIN + "Signup_ios/view_user?userid="
                    + getIntent().getStringExtra("bidder_id");
        } catch (Exception e) {
            url = InstiworkConstants.URL_DOMAIN + "Signup_ios/view_user?userid="
                    + InstiworkApplication.getInstance().getUSER_ID();
        }
        Logger.showMessage(TAG, "Fetch Contact Details " + url);

        JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Logger.showMessage(TAG, response.toString());
                        pbar_profile.setVisibility(View.GONE);
                        try {
                            if (response.getString("status").equalsIgnoreCase("Success")) {

                                try {
                                    ((RobotoLight) findViewById(R.id.txt_profile)).setText(
                                            "" + response.getJSONObject("response").getString("name") + "   ");
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                                ((RobotoLight) findViewById(R.id.prof_company))
                                        .setText("" + response.getJSONObject("response").getString("companyname"));
                                ((RobotoLight) findViewById(R.id.prof_name))
                                        .setText("" + response.getJSONObject("response").getString("name"));
                                ((RobotoLight) findViewById(R.id.prof_email))
                                        .setText("" + response.getJSONObject("response").getString("email"));
                                ((RobotoLight) findViewById(R.id.prof_mobile))
                                        .setText("" + response.getJSONObject("response").getString("Contact"));
                                ((RobotoLight) findViewById(R.id.prof_country))
                                        .setText("" + response.getJSONObject("response").getString("country_name"));

                                if (!response.getJSONObject("response").getString("profileimage")
                                        .equalsIgnoreCase("")) {
                                    try {
                                        Picasso.with(getApplicationContext())
                                                .load(response.getJSONObject("response").getString("profileimage"))
                                                .fit().centerInside().error(R.mipmap.ic_no_image)
                                                .into(((ImageView) findViewById(R.id.profile_request_details)));
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        Picasso.with(getApplicationContext()).load(R.mipmap.ic_no_image).fit()
                                                .centerInside().error(R.mipmap.ic_no_image)
                                                .into(((ImageView) findViewById(R.id.profile_request_details)));
                                    }
                                } else {
                                    Picasso.with(getApplicationContext()).load(R.mipmap.ic_no_image).fit()
                                            .centerInside().error(R.mipmap.ic_no_image)
                                            .into(((ImageView) findViewById(R.id.profile_request_details)));
                                }

                                try {
                                    licenseLinkedList = new LinkedList<JSONObject>();
                                    if (response.getJSONObject("response").getJSONArray("license").length() > 0) {
                                        for (int i = 0; i < response.getJSONObject("response")
                                                .getJSONArray("license").length(); i++) {
                                            licenseLinkedList.add(response.getJSONObject("response")
                                                    .getJSONArray("license").getJSONObject(i));
                                        }
                                    }
                                    if (licenseLinkedList == null) {
                                        ((RobotoLight) findViewById(R.id.no_license)).setVisibility(View.VISIBLE);
                                        license_list.setVisibility(View.GONE);
                                    } else {
                                        try {
                                            if (licenseLinkedList.size() > 0) {
                                                ((RobotoLight) findViewById(R.id.no_license))
                                                        .setVisibility(View.GONE);
                                                license_list.setVisibility(View.VISIBLE);
                                                ProfileLicenseListAdapter profileLicenseListAdapter = new ProfileLicenseListAdapter(
                                                        ProfileActivityOther.this, licenseLinkedList);
                                                license_list.setItemAnimator(new DefaultItemAnimator());
                                                license_list.setAdapter(profileLicenseListAdapter);
                                                license_list.setLayoutManager(
                                                        new LinearLayoutManager(getApplicationContext(),
                                                                LinearLayoutManager.HORIZONTAL, false));
                                            } else {
                                                ((RobotoLight) findViewById(R.id.no_license))
                                                        .setVisibility(View.VISIBLE);
                                                license_list.setVisibility(View.GONE);
                                            }
                                        } catch (Exception ee) {

                                            ee.printStackTrace();

                                            ((RobotoLight) findViewById(R.id.no_license))
                                                    .setVisibility(View.VISIBLE);
                                            license_list.setVisibility(View.GONE);
                                        }
                                    }
                                } catch (Exception e) {
                                    InstiworkApplication.getInstance().setLicenseArray(null);
                                    Logger.showMessage(TAG, "license Exception" + e.toString());
                                }
                            } else if (response.getString("status").equalsIgnoreCase("Error")) {
                                if (response.getJSONObject("response").getString("message")
                                        .equalsIgnoreCase("No user details found")) {
                                    Snackbar.make(findViewById(android.R.id.content),
                                            response.getJSONObject("response").getString("message"),
                                            Snackbar.LENGTH_LONG).setActionTextColor(Color.RED).show();
                                } else {
                                    Snackbar.make(findViewById(android.R.id.content),
                                            response.getJSONObject("response").getString("message"),
                                            Snackbar.LENGTH_LONG).setActionTextColor(Color.RED).show();
                                }
                            } else {
                                Snackbar.make(findViewById(android.R.id.content),
                                        response.getJSONObject("response").getString("message"),
                                        Snackbar.LENGTH_LONG).setActionTextColor(Color.RED).show();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            //                            pDialog.dismiss();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Logger.showMessage(TAG, "error is::" + volleyError.toString());
                        pbar_profile.setVisibility(View.GONE);
                        if (volleyError instanceof NoConnectionError) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(ProfileActivityOther.this);
                            builder.setTitle("Instiwork");
                            builder.setMessage("You do not have internet connection!");
                            builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.dismiss();
                                    fetchAccount();
                                }
                            });
                            builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.dismiss();
                                    onBackPressed();
                                }
                            });
                            builder.setCancelable(false);
                            builder.show();
                        } else {
                            try {
                                String json = null;
                                NetworkResponse response = volleyError.networkResponse;
                                if (response != null && response.data != null) {
                                    json = new String(response.data);
                                    json = trimMessage(json, "message");
                                    if (json != null)
                                        displayMessage(json);
                                    //                            displayMessage(json + " Error Code : " + response.statusCode);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
        jsonReq.setRetryPolicy(new DefaultRetryPolicy(10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        InstiworkApplication.getInstance().addToRequestQueue(jsonReq);
    }

    public String trimMessage(String json, String key) {
        String trimmedString = "";
        try {
            JSONObject obj = new JSONObject(json);
            trimmedString = obj.getJSONObject("error").getString(key);
        } catch (JSONException e) {
            e.printStackTrace();
            return json;
        }
        return trimmedString;
    }

    public void displayMessage(String toastString) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ProfileActivityOther.this);
        alertDialogBuilder.setTitle("Error");
        alertDialogBuilder.setMessage(Html.fromHtml(toastString)).setCancelable(false)
                .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                }).setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }

}