| 21478 |
rajender |
1 |
package com.saholic.profittill.Utils;
|
|
|
2 |
|
|
|
3 |
import android.content.Intent;
|
|
|
4 |
import android.content.SharedPreferences;
|
|
|
5 |
import android.net.ConnectivityManager;
|
|
|
6 |
import android.os.AsyncTask;
|
|
|
7 |
import android.os.Bundle;
|
|
|
8 |
import android.support.v7.app.ActionBarActivity;
|
|
|
9 |
import android.util.Base64;
|
|
|
10 |
import android.util.Log;
|
|
|
11 |
import android.view.Menu;
|
|
|
12 |
import android.view.MenuItem;
|
|
|
13 |
import android.view.View;
|
|
|
14 |
import android.widget.Button;
|
|
|
15 |
import android.widget.Toast;
|
|
|
16 |
|
|
|
17 |
import com.saholic.profittill.Constants.ProfitTillConstants;
|
|
|
18 |
import com.saholic.profittill.R;
|
|
|
19 |
import com.saholic.profittill.main.LoginActivity;
|
|
|
20 |
import com.saholic.profittill.main.MainActivity;
|
|
|
21 |
|
|
|
22 |
import org.apache.http.HttpEntity;
|
|
|
23 |
import org.apache.http.HttpResponse;
|
|
|
24 |
import org.apache.http.client.HttpClient;
|
|
|
25 |
import org.apache.http.client.methods.HttpPost;
|
|
|
26 |
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
27 |
import org.apache.http.util.EntityUtils;
|
|
|
28 |
import org.json.JSONArray;
|
|
|
29 |
import org.json.JSONObject;
|
|
|
30 |
|
|
|
31 |
public class SplashInternetNotWorking extends ActionBarActivity {
|
|
|
32 |
Button checkInternet;
|
|
|
33 |
SharedPreferences apiData, userData;
|
|
|
34 |
SharedPreferences.Editor apiEditor,userEditor;
|
|
|
35 |
|
|
|
36 |
@Override
|
|
|
37 |
protected void onCreate(Bundle savedInstanceState) {
|
|
|
38 |
super.onCreate(savedInstanceState);
|
|
|
39 |
setContentView(R.layout.activity_splash_internet_not_working);
|
|
|
40 |
getSupportActionBar().hide();
|
|
|
41 |
userData = getApplicationContext().getSharedPreferences("User_Data", MODE_PRIVATE);
|
|
|
42 |
apiData = getApplicationContext().getSharedPreferences("API_Data", MODE_PRIVATE);
|
|
|
43 |
userEditor = userData.edit();
|
|
|
44 |
apiEditor = apiData.edit();
|
|
|
45 |
checkInternet = (Button)findViewById(R.id.checkSplashInternet);
|
|
|
46 |
checkInternet.setOnClickListener(new View.OnClickListener() {
|
|
|
47 |
@Override
|
|
|
48 |
public void onClick(View v) {
|
|
|
49 |
final String apiCheck = apiData.getString("user.registration.api",null);
|
|
|
50 |
if(apiCheck==null){
|
|
|
51 |
if(isInternetOn()){
|
|
|
52 |
new fetchAPISettings().execute();
|
|
|
53 |
}
|
|
|
54 |
else{
|
|
|
55 |
Toast.makeText(getApplicationContext()," Not Connected ", Toast.LENGTH_LONG).show();
|
|
|
56 |
}
|
|
|
57 |
}
|
|
|
58 |
else{
|
|
|
59 |
apiEditor.clear().commit();
|
|
|
60 |
String apiCheck1 = apiData.getString("user.registration.api"," ");
|
|
|
61 |
Log.e("Delete","Deleting file" + apiCheck1 + "Done");
|
|
|
62 |
if(isInternetOn()){
|
|
|
63 |
new fetchAPISettings().execute();
|
|
|
64 |
}
|
|
|
65 |
else{
|
|
|
66 |
Toast.makeText(getApplicationContext(), " Not Connected ", Toast.LENGTH_LONG).show();
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
});
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
@Override
|
|
|
76 |
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
77 |
// Inflate the menu; this adds items to the action bar if it is present.
|
|
|
78 |
getMenuInflater().inflate(R.menu.menu_splash_internet_not_working, menu);
|
|
|
79 |
return true;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
@Override
|
|
|
83 |
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
84 |
// Handle action bar item clicks here. The action bar will
|
|
|
85 |
// automatically handle clicks on the Home/Up button, so long
|
|
|
86 |
// as you specify a parent activity in AndroidManifest.xml.
|
|
|
87 |
int id = item.getItemId();
|
|
|
88 |
|
|
|
89 |
//noinspection SimplifiableIfStatement
|
|
|
90 |
if (id == R.id.action_settings) {
|
|
|
91 |
return true;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
return super.onOptionsItemSelected(item);
|
|
|
95 |
}
|
|
|
96 |
public final boolean isInternetOn() {
|
|
|
97 |
|
|
|
98 |
ConnectivityManager connec =
|
|
|
99 |
(ConnectivityManager)getSystemService(this.getBaseContext().CONNECTIVITY_SERVICE);
|
|
|
100 |
|
|
|
101 |
if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
|
|
|
102 |
connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
|
|
|
103 |
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
|
|
|
104 |
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {
|
|
|
105 |
|
|
|
106 |
Toast.makeText(this, " Connected ", Toast.LENGTH_LONG).show();
|
|
|
107 |
return true;
|
|
|
108 |
|
|
|
109 |
} else if (
|
|
|
110 |
connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
|
|
|
111 |
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED ) {
|
|
|
112 |
|
|
|
113 |
Toast.makeText(this, " Not Connected ", Toast.LENGTH_LONG).show();
|
|
|
114 |
return false;
|
|
|
115 |
}
|
|
|
116 |
return false;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
class fetchAPISettings extends AsyncTask<String, Integer, String> {
|
|
|
120 |
|
|
|
121 |
@Override
|
|
|
122 |
protected void onPreExecute() {
|
|
|
123 |
super.onPreExecute();
|
|
|
124 |
Log.e("Pre execute", "Pre execute for Getting App settings");
|
|
|
125 |
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
@Override
|
|
|
129 |
protected String doInBackground(String... arg0) {
|
|
|
130 |
try {
|
|
|
131 |
HttpClient httpclient = new DefaultHttpClient();
|
|
|
132 |
HttpPost httppost = new HttpPost(ProfitTillConstants.MOBILE_API);
|
|
|
133 |
final String basicAuth = "Basic " + Base64.encodeToString("dtr:dtr18Feb2015".getBytes(), Base64.NO_WRAP);
|
|
|
134 |
httppost.setHeader("Authorization", basicAuth);
|
|
|
135 |
HttpResponse response = httpclient.execute(httppost);
|
|
|
136 |
HttpEntity entity = response.getEntity();
|
|
|
137 |
|
|
|
138 |
int status = response.getStatusLine().getStatusCode();
|
|
|
139 |
|
|
|
140 |
if(status == 200){
|
|
|
141 |
System.out.println("ResponseCode of record: "+ status + " is " + status);
|
|
|
142 |
|
|
|
143 |
} else {
|
|
|
144 |
Log.d("ResponseCode",status+"");
|
|
|
145 |
System.out.println(" NOT Transfered");
|
|
|
146 |
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
JSONObject jObjGmail = new JSONObject(EntityUtils.toString(entity));
|
|
|
150 |
System.out.println(jObjGmail.toString());
|
|
|
151 |
JSONArray objects =jObjGmail.getJSONArray("settings");
|
|
|
152 |
System.out.println(objects.length());
|
|
|
153 |
for (int i = 0; i < objects.length(); i++) {
|
|
|
154 |
JSONObject jsonObject = objects.getJSONObject(i);
|
|
|
155 |
JSONObject j = jsonObject.getJSONObject("Mobileappsetting");
|
|
|
156 |
apiEditor.putString(j.getString("setting"),j.getString("value"));
|
|
|
157 |
System.out.println(j.getString("setting") + j.getString("value"));
|
|
|
158 |
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
} catch (Exception e) {
|
|
|
162 |
Log.e("Fail 1", e.toString());
|
|
|
163 |
|
|
|
164 |
}
|
|
|
165 |
return ProfitTillConstants.SUCCESS;
|
|
|
166 |
}
|
|
|
167 |
@Override
|
|
|
168 |
protected void onPostExecute(String result) {
|
|
|
169 |
super.onPostExecute(result);
|
|
|
170 |
apiEditor.commit();
|
|
|
171 |
final String idCheck = userData.getString("id",null);
|
|
|
172 |
if(idCheck!=null){
|
|
|
173 |
startActivity(new Intent(SplashInternetNotWorking.this,MainActivity.class));
|
|
|
174 |
}
|
|
|
175 |
else {
|
|
|
176 |
startActivity(new Intent(SplashInternetNotWorking.this, LoginActivity.class));
|
|
|
177 |
}
|
|
|
178 |
//Log.e("Post execute", "Post execute for Getting App settings");
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
}
|