Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
21185 rajender 1
package com.saholic.profittill.main;
2
 
3
import android.app.AlertDialog;
4
import android.app.ProgressDialog;
5
import android.content.Context;
6
import android.content.DialogInterface;
7
import android.content.Intent;
8
import android.content.SharedPreferences;
9
import android.graphics.drawable.ColorDrawable;
10
import android.net.ConnectivityManager;
11
import android.os.AsyncTask;
12
import android.support.v7.app.ActionBar;
13
import android.support.v7.app.AppCompatActivity;
14
import android.os.Bundle;
15
import android.util.Log;
16
import android.view.KeyEvent;
17
import android.view.View;
18
import android.widget.Button;
19
import android.widget.EditText;
20
import android.widget.Toast;
21
 
22
import com.facebook.Session;
23
import com.google.android.gms.common.ConnectionResult;
24
import com.google.android.gms.common.api.GoogleApiClient;
25
import com.google.android.gms.plus.Plus;
26
import com.mixpanel.android.mpmetrics.MixpanelAPI;
27
import com.saholic.profittill.Constants.ProfitTillConstants;
28
import com.saholic.profittill.R;
29
import com.testin.agent.TestinAgent;
30
 
31
import org.apache.http.HttpEntity;
32
import org.apache.http.HttpResponse;
33
import org.apache.http.NameValuePair;
34
import org.apache.http.client.HttpClient;
35
import org.apache.http.client.entity.UrlEncodedFormEntity;
36
import org.apache.http.client.methods.HttpGet;
37
import org.apache.http.client.methods.HttpPost;
38
import org.apache.http.impl.client.DefaultHttpClient;
39
import org.apache.http.message.BasicNameValuePair;
40
import org.apache.http.util.EntityUtils;
41
import org.json.JSONException;
42
import org.json.JSONObject;
43
 
44
import java.util.ArrayList;
45
 
46
public class BasicInformation extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
47
     EditText pincode, city, mobilenumber;
48
    ArrayList<NameValuePair> basicInfoNameValuePair;
49
    SharedPreferences userData;
50
    SharedPreferences apiData;
51
    SharedPreferences.Editor userDataEditor;
52
    SharedPreferences.Editor apiSettingsEditor;
53
    Button infosend;
54
    String type1;
55
    MixpanelAPI mixpanel;
56
    boolean glogout;
57
    public GoogleApiClient mGoogleApiClient;
58
 
59
 
60
    @Override
61
    protected void onCreate(Bundle savedInstanceState) {
62
        super.onCreate(savedInstanceState);
63
        setContentView(R.layout.activity_basic_information);
64
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.White)));
65
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
66
        getSupportActionBar().setCustomView(R.layout.profitmandiicon);
67
        userData = getApplicationContext().getSharedPreferences("User_Data", Context.MODE_PRIVATE);
68
        apiData = getApplicationContext().getSharedPreferences("API_Data", Context.MODE_PRIVATE);
69
        userDataEditor = userData.edit();
70
        apiSettingsEditor = apiData.edit();
71
        mixpanel= MixpanelAPI.getInstance(getApplicationContext(), ProfitTillConstants.MIX_PANEL_TOKEN);
72
        type1 = userData.getString("type", null);
73
 
74
        if (type1 != null && type1.equalsIgnoreCase("google")) {
75
            mGoogleApiClient = new GoogleApiClient.Builder(this)
76
                    .addConnectionCallbacks(this)
77
                    .addOnConnectionFailedListener(this).addApi(Plus.API)
78
                    .addScope(Plus.SCOPE_PLUS_LOGIN).build();
79
            mGoogleApiClient.connect();
80
        }
81
        pincode = (EditText) findViewById(R.id.Pincode);
82
        city = (EditText) findViewById(R.id.city);
83
        mobilenumber = (EditText) findViewById(R.id.PhoneNumber);
84
         new GetInfo().execute();
85
        infosend=(Button) findViewById(R.id.infosend);
86
        infosend.setOnClickListener(new View.OnClickListener() {
87
            @Override
88
            public void onClick(View v) {
89
                if (city.getText().toString().equals("")) {
90
                    city.setError("Enter City");
91
                } else if (pincode.getText().toString().equals("") || pincode.getText().toString().length() != 6) {
92
                    pincode.setError("Enter Pincode");
93
                } else if (mobilenumber.getText().toString().equals("") || mobilenumber.getText().toString().length() != 10) {
94
                    mobilenumber.setError("Enter MobileNumber");
95
                }
96
                else {
97
                    basicInfoNameValuePair = new ArrayList<>();
98
                    basicInfoNameValuePair.add(new BasicNameValuePair("id", userData.getString("id", null)));
99
                    basicInfoNameValuePair.add(new BasicNameValuePair("city", city.getText().toString()));
100
                    basicInfoNameValuePair.add(new BasicNameValuePair("pincode", pincode.getText().toString()));
101
                    basicInfoNameValuePair.add(new BasicNameValuePair("mobile_number", mobilenumber.getText().toString()));
102
                    new loadinfo().execute();
103
                }}});}
104
 
105
    @Override
106
    public void onConnected(Bundle bundle) {
107
 
108
    }
109
 
110
    @Override
111
    public void onConnectionSuspended(int i) {
112
 
113
    }
114
 
115
    @Override
116
    public void onConnectionFailed(ConnectionResult connectionResult) {
117
 
118
    }
119
 
120
    class loadinfo extends AsyncTask<String, Integer, JSONObject> {
121
 
122
        @Override
123
        protected void onPreExecute() {
124
            super.onPreExecute();
125
           infosend.setEnabled(false);
126
            pd2 = new ProgressDialog(BasicInformation.this);
127
            pd2.setMessage("Please wait...");
128
            pd2.setCancelable(false);
129
            pd2.show();
130
 
131
        }
132
 
133
        @Override
134
        protected JSONObject doInBackground(String... arg0) {
135
            String id = null;
136
            if (isInternetOn()) {
137
 
138
                try {
139
                    HttpClient httpclient = new DefaultHttpClient();
140
                    String updateURL = apiData.getString("useredit.url", "");
141
                    String url = updateURL + userData.getString("id", "");
142
                    HttpPost httppost = new HttpPost(url);
143
                    httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
144
                    httppost.setEntity(new UrlEncodedFormEntity(basicInfoNameValuePair));
145
                    HttpResponse response = httpclient.execute(httppost);
146
                    HttpEntity entity = response.getEntity();
147
                    int status = response.getStatusLine().getStatusCode();
148
                    JSONObject jObjGmail = new JSONObject(EntityUtils.toString(entity));
149
                    return jObjGmail;
150
 
151
                } catch (Exception e) {
152
                    e.printStackTrace();
153
 
154
                }
155
            }
156
            Toast.makeText(getApplicationContext(),"Internet problem",Toast.LENGTH_LONG).show();
157
            return null;
158
        }
159
 
160
 
161
        @Override
162
        protected void onPostExecute(JSONObject result) {
163
            super.onPostExecute(result);
164
            pd2.dismiss();
165
            if(result==null){
166
                finish();
167
            }
168
            try {
169
                if (result.getBoolean("success")){
170
                    Toast.makeText(getApplicationContext(), "Information save successfully", Toast.LENGTH_LONG).show();
171
                    Intent i = new Intent(BasicInformation.this, ReferrerActivity.class);
172
                    startActivity(i);
173
                } else {
174
                    Toast.makeText(getApplicationContext(), "Error While Uploading", Toast.LENGTH_LONG).show();
175
                }
176
            } catch (JSONException e) {
177
                e.printStackTrace();
178
            }
179
        }
180
    }
181
 
182
 
183
    ProgressDialog pd2;
184
 
185
 
186
    public final boolean isInternetOn() {
187
 
188
        ConnectivityManager connection =
189
                (ConnectivityManager) getSystemService(this.getBaseContext().CONNECTIVITY_SERVICE);
190
 
191
        if (connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
192
                connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
193
                connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
194
                connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED) {
195
            return true;
196
 
197
        } else if (
198
                connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
199
                        connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED) {
200
            return false;
201
        }
202
        return false;
203
    }
204
    @Override
205
    public void onBackPressed() {
206
        new AlertDialog.Builder(this)
207
                .setIcon(android.R.drawable.ic_dialog_alert)
208
                .setTitle("Exit!")
209
                .setMessage("Are you sure you want to sign out?")
210
                .setPositiveButton("Yes", new DialogInterface.OnClickListener(){
211
                    @Override
212
                    public void onClick(DialogInterface dialog, int which) {
213
 
214
                        if(type1.equalsIgnoreCase("facebook")) {
215
                            Log.d("fblogout","logout");
216
                            if (Session.getActiveSession() != null) {
217
                                Session.getActiveSession().closeAndClearTokenInformation();
218
                            }
219
                            Session.setActiveSession(null);
220
                            userDataEditor.clear().commit();
221
                            startActivity(new Intent(BasicInformation.this, LoginActivity.class));
222
                        }
223
                        else{
224
                            Log.d("gmailLogout","logout");
225
                            signOutFromGplus();
226
                            if(glogout==true){
227
                                startActivity(new Intent(BasicInformation.this, LoginActivity.class));
228
                            }
229
                        }
230
 
231
                    }
232
 
233
                })
234
                .setNegativeButton("No", null)
235
                .show();
236
    }
237
    public void signOutFromGplus() {
238
        if (mGoogleApiClient.isConnected()) {
239
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
240
            mGoogleApiClient.disconnect();
241
            mGoogleApiClient.connect();
242
            glogout=true;
243
            userDataEditor.clear();
244
            userDataEditor.commit();
245
        } else {
246
            glogout=false;
247
        }
248
    }
249
 
250
    class GetInfo extends AsyncTask<String, Integer, JSONObject> {
251
 
252
        @Override
253
        protected void onPreExecute() {
254
            super.onPreExecute();
255
        }
256
 
257
        @Override
258
        protected JSONObject doInBackground(String... arg0) {
259
            try {
260
                HttpClient httpclient = new DefaultHttpClient();
261
                String url = "http://api.profittill.com/users/mine?user_id="+userData.getString("id", null);
262
                HttpGet httppost=new HttpGet(url);
263
                httppost.setHeader("Authorization",ProfitTillConstants.BASIC_AUTH);
264
                HttpResponse response = httpclient.execute(httppost);
265
                HttpEntity entity = response.getEntity();
266
                JSONObject jObjGmail = new JSONObject(EntityUtils.toString(entity));
267
                return jObjGmail;
268
 
269
            }
270
            catch (Exception e) {
271
                e.printStackTrace();
272
                return null;
273
 
274
            }
275
 
276
        }
277
        @Override
278
        protected void onPostExecute(JSONObject result) {
279
            super.onPostExecute(result);
280
            try {
281
                String City=result.getString("city");
282
                String Pincode=result.getString("pincode");
283
                String Mobile_number=result.getString("mobile_number");
22377 rajender 284
                if(City.equalsIgnoreCase("null")){
285
                    city.setText("");
286
                }
287
                else {
288
                    city.setText(City);
289
                }
290
                if(Pincode.equalsIgnoreCase("null")){
291
                    pincode.setText("");
292
                }
293
                else {
294
                    pincode.setText(Pincode);
295
                }
296
                if(Mobile_number.equalsIgnoreCase("null")){
297
                    mobilenumber.setText("");
298
                }
299
                else {
300
                    mobilenumber.setText(Mobile_number);
301
                }
21185 rajender 302
            } catch (JSONException e) {
303
                e.printStackTrace();
304
            }
305
        }
306
 
307
 
308
    }
309
 
310
 
311
}