Subversion Repositories SmartDukaan

Rev

Rev 21185 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.saholic.profittill.main;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.facebook.Session;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.plus.Plus;
import com.mixpanel.android.mpmetrics.MixpanelAPI;
import com.saholic.profittill.Constants.ProfitTillConstants;
import com.saholic.profittill.R;
import com.testin.agent.TestinAgent;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class BasicInformation extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
     EditText pincode, city, mobilenumber;
    ArrayList<NameValuePair> basicInfoNameValuePair;
    SharedPreferences userData;
    SharedPreferences apiData;
    SharedPreferences.Editor userDataEditor;
    SharedPreferences.Editor apiSettingsEditor;
    Button infosend;
    String type1;
    MixpanelAPI mixpanel;
    boolean glogout;
    public GoogleApiClient mGoogleApiClient;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic_information);
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.White)));
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.profitmandiicon);
        userData = getApplicationContext().getSharedPreferences("User_Data", Context.MODE_PRIVATE);
        apiData = getApplicationContext().getSharedPreferences("API_Data", Context.MODE_PRIVATE);
        userDataEditor = userData.edit();
        apiSettingsEditor = apiData.edit();
        mixpanel= MixpanelAPI.getInstance(getApplicationContext(), ProfitTillConstants.MIX_PANEL_TOKEN);
        type1 = userData.getString("type", null);

        if (type1 != null && type1.equalsIgnoreCase("google")) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this).addApi(Plus.API)
                    .addScope(Plus.SCOPE_PLUS_LOGIN).build();
            mGoogleApiClient.connect();
        }
        pincode = (EditText) findViewById(R.id.Pincode);
        city = (EditText) findViewById(R.id.city);
        mobilenumber = (EditText) findViewById(R.id.PhoneNumber);
         new GetInfo().execute();
        infosend=(Button) findViewById(R.id.infosend);
        infosend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (city.getText().toString().equals("")) {
                    city.setError("Enter City");
                } else if (pincode.getText().toString().equals("") || pincode.getText().toString().length() != 6) {
                    pincode.setError("Enter Pincode");
                } else if (mobilenumber.getText().toString().equals("") || mobilenumber.getText().toString().length() != 10) {
                    mobilenumber.setError("Enter MobileNumber");
                }
                else {
                    basicInfoNameValuePair = new ArrayList<>();
                    basicInfoNameValuePair.add(new BasicNameValuePair("id", userData.getString("id", null)));
                    basicInfoNameValuePair.add(new BasicNameValuePair("city", city.getText().toString()));
                    basicInfoNameValuePair.add(new BasicNameValuePair("pincode", pincode.getText().toString()));
                    basicInfoNameValuePair.add(new BasicNameValuePair("mobile_number", mobilenumber.getText().toString()));
                    new loadinfo().execute();
                }}});}

    @Override
    public void onConnected(Bundle bundle) {

    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    class loadinfo extends AsyncTask<String, Integer, JSONObject> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
           infosend.setEnabled(false);
            pd2 = new ProgressDialog(BasicInformation.this);
            pd2.setMessage("Please wait...");
            pd2.setCancelable(false);
            pd2.show();

        }

        @Override
        protected JSONObject doInBackground(String... arg0) {
            String id = null;
            if (isInternetOn()) {

                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    String updateURL = apiData.getString("useredit.url", "");
                    String url = updateURL + userData.getString("id", "");
                    HttpPost httppost = new HttpPost(url);
                    httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
                    httppost.setEntity(new UrlEncodedFormEntity(basicInfoNameValuePair));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    int status = response.getStatusLine().getStatusCode();
                    JSONObject jObjGmail = new JSONObject(EntityUtils.toString(entity));
                    return jObjGmail;

                } catch (Exception e) {
                    e.printStackTrace();

                }
            }
            Toast.makeText(getApplicationContext(),"Internet problem",Toast.LENGTH_LONG).show();
            return null;
        }


        @Override
        protected void onPostExecute(JSONObject result) {
            super.onPostExecute(result);
            pd2.dismiss();
            if(result==null){
                finish();
            }
            try {
                if (result.getBoolean("success")){
                    Toast.makeText(getApplicationContext(), "Information save successfully", Toast.LENGTH_LONG).show();
                    Intent i = new Intent(BasicInformation.this, ReferrerActivity.class);
                    startActivity(i);
                } else {
                    Toast.makeText(getApplicationContext(), "Error While Uploading", Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }


    ProgressDialog pd2;


    public final boolean isInternetOn() {

        ConnectivityManager connection =
                (ConnectivityManager) getSystemService(this.getBaseContext().CONNECTIVITY_SERVICE);

        if (connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
                connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
                connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
                connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED) {
            return true;

        } else if (
                connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
                        connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED) {
            return false;
        }
        return false;
    }
    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("Exit!")
                .setMessage("Are you sure you want to sign out?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        if(type1.equalsIgnoreCase("facebook")) {
                            Log.d("fblogout","logout");
                            if (Session.getActiveSession() != null) {
                                Session.getActiveSession().closeAndClearTokenInformation();
                            }
                            Session.setActiveSession(null);
                            userDataEditor.clear().commit();
                            startActivity(new Intent(BasicInformation.this, LoginActivity.class));
                        }
                        else{
                            Log.d("gmailLogout","logout");
                            signOutFromGplus();
                            if(glogout==true){
                                startActivity(new Intent(BasicInformation.this, LoginActivity.class));
                            }
                        }

                    }

                })
                .setNegativeButton("No", null)
                .show();
    }
    public void signOutFromGplus() {
        if (mGoogleApiClient.isConnected()) {
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            mGoogleApiClient.disconnect();
            mGoogleApiClient.connect();
            glogout=true;
            userDataEditor.clear();
            userDataEditor.commit();
        } else {
            glogout=false;
        }
    }

    class GetInfo extends AsyncTask<String, Integer, JSONObject> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected JSONObject doInBackground(String... arg0) {
            try {
                HttpClient httpclient = new DefaultHttpClient();
                String url = "http://api.profittill.com/users/mine?user_id="+userData.getString("id", null);
                HttpGet httppost=new HttpGet(url);
                httppost.setHeader("Authorization",ProfitTillConstants.BASIC_AUTH);
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                JSONObject jObjGmail = new JSONObject(EntityUtils.toString(entity));
                return jObjGmail;

            }
            catch (Exception e) {
                e.printStackTrace();
                return null;

            }

        }
        @Override
        protected void onPostExecute(JSONObject result) {
            super.onPostExecute(result);
            try {
                String City=result.getString("city");
                String Pincode=result.getString("pincode");
                String Mobile_number=result.getString("mobile_number");
                if(City.equalsIgnoreCase("null")){
                    city.setText("");
                }
                else {
                    city.setText(City);
                }
                if(Pincode.equalsIgnoreCase("null")){
                    pincode.setText("");
                }
                else {
                    pincode.setText(Pincode);
                }
                if(Mobile_number.equalsIgnoreCase("null")){
                    mobilenumber.setText("");
                }
                else {
                    mobilenumber.setText(Mobile_number);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }


    }


}