| 21478 |
rajender |
1 |
package com.saholic.profittill.main;
|
|
|
2 |
|
|
|
3 |
import android.app.Fragment;
|
|
|
4 |
import android.app.ProgressDialog;
|
|
|
5 |
import android.content.Context;
|
|
|
6 |
import android.content.Intent;
|
|
|
7 |
import android.content.SharedPreferences;
|
|
|
8 |
import android.graphics.Bitmap;
|
|
|
9 |
import android.net.ConnectivityManager;
|
|
|
10 |
import android.net.Uri;
|
|
|
11 |
import android.os.AsyncTask;
|
|
|
12 |
import android.os.Bundle;
|
|
|
13 |
import android.util.Log;
|
|
|
14 |
import android.view.KeyEvent;
|
|
|
15 |
import android.view.LayoutInflater;
|
|
|
16 |
import android.view.View;
|
|
|
17 |
import android.view.ViewGroup;
|
|
|
18 |
import android.webkit.CookieManager;
|
|
|
19 |
import android.webkit.CookieSyncManager;
|
|
|
20 |
import android.webkit.HttpAuthHandler;
|
|
|
21 |
import android.webkit.WebView;
|
|
|
22 |
import android.webkit.WebViewClient;
|
|
|
23 |
|
|
|
24 |
import com.mixpanel.android.mpmetrics.MixpanelAPI;
|
|
|
25 |
import com.saholic.profittill.Constants.ProfitTillConstants;
|
|
|
26 |
import com.saholic.profittill.R;
|
|
|
27 |
import com.saholic.profittill.Utils.AnalyticsUtility;
|
|
|
28 |
import com.saholic.profittill.Volley.Analytics;
|
|
|
29 |
import com.saholic.profittill.Volley.AnalyticsErrorResponse;
|
|
|
30 |
import com.saholic.profittill.Volley.AnalyticsJsonResponse;
|
|
|
31 |
import com.testin.agent.TestinAgent;
|
|
|
32 |
|
|
|
33 |
import org.apache.http.HttpResponse;
|
|
|
34 |
import org.apache.http.NameValuePair;
|
|
|
35 |
import org.apache.http.client.ClientProtocolException;
|
|
|
36 |
import org.apache.http.client.HttpClient;
|
|
|
37 |
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
38 |
import org.apache.http.client.methods.HttpPost;
|
|
|
39 |
import org.apache.http.entity.StringEntity;
|
|
|
40 |
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
41 |
import org.apache.http.message.BasicNameValuePair;
|
|
|
42 |
import org.json.JSONArray;
|
|
|
43 |
import org.json.JSONException;
|
|
|
44 |
import org.json.JSONObject;
|
|
|
45 |
|
|
|
46 |
import java.io.IOException;
|
|
|
47 |
import java.text.SimpleDateFormat;
|
|
|
48 |
import java.util.ArrayList;
|
|
|
49 |
import java.util.Date;
|
|
|
50 |
|
|
|
51 |
public class PreferencesFragment extends Fragment {
|
|
|
52 |
WebView preferencesWebView;
|
|
|
53 |
SharedPreferences userData;
|
|
|
54 |
SharedPreferences apiData;
|
|
|
55 |
SharedPreferences.Editor userDataEditor;
|
|
|
56 |
SharedPreferences.Editor apiSettingsEditor;
|
|
|
57 |
@Override
|
|
|
58 |
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
59 |
Bundle savedInstanceState) {
|
|
|
60 |
|
|
|
61 |
View rootView = inflater.inflate(R.layout.fragment_preferences, container, false);
|
|
|
62 |
TestinAgent.init(getActivity());
|
|
|
63 |
MixpanelAPI mixpanel = MixpanelAPI.getInstance(getActivity(), ProfitTillConstants.MIX_PANEL_TOKEN);
|
|
|
64 |
userData = getActivity().getSharedPreferences("User_Data", Context.MODE_PRIVATE);
|
|
|
65 |
apiData = getActivity().getSharedPreferences("API_Data", Context.MODE_PRIVATE);
|
|
|
66 |
userDataEditor = userData.edit();
|
|
|
67 |
apiSettingsEditor = apiData.edit();
|
|
|
68 |
String url=apiData.getString("preferences.url", null);;
|
|
|
69 |
String id = userData.getString("id", null);
|
|
|
70 |
String landingUrl;
|
|
|
71 |
if(userData.getString("preferences","false").equalsIgnoreCase("true")) {
|
|
|
72 |
landingUrl =url+"?user_id="+id+"&firsttime=1";
|
|
|
73 |
}else {
|
|
|
74 |
landingUrl=url+"?user_id="+id;
|
|
|
75 |
}
|
|
|
76 |
TestinAgent.setUserInfo(userData.getString("id", ""));
|
|
|
77 |
JSONObject props = new JSONObject();
|
|
|
78 |
try {
|
|
|
79 |
mixpanel.identify(userData.getString("id",null));
|
|
|
80 |
props.put("Screen", "My Preferences");
|
|
|
81 |
mixpanel.track("My Preferences", props);
|
|
|
82 |
} catch (JSONException e) {
|
|
|
83 |
e.printStackTrace();
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
if(isInternetOn()) {
|
|
|
87 |
WebClientClass webViewClient = new WebClientClass();
|
|
|
88 |
preferencesWebView = (WebView)rootView.findViewById(R.id.preferencesWebView);
|
|
|
89 |
preferencesWebView.setClickable(true);
|
|
|
90 |
preferencesWebView.setFocusableInTouchMode(true);
|
|
|
91 |
preferencesWebView.getSettings().setJavaScriptEnabled(true);
|
|
|
92 |
preferencesWebView.getSettings().setBuiltInZoomControls(true);
|
|
|
93 |
new Analytics(ProfitTillConstants.ANALYTICS_URL,
|
|
|
94 |
AnalyticsUtility.getAnalyticsRequest(getActivity(), userData.getString("id", ""), "Screen", "My Preferences", "My Preferences Screen"),
|
|
|
95 |
AnalyticsJsonResponse.getAnalyticsRequestInstance(),
|
|
|
96 |
AnalyticsErrorResponse.getAnalyitcsResponseInstance()).anlyticsRequest(getActivity());
|
|
|
97 |
if(Uri.parse(landingUrl).getHost().equalsIgnoreCase(ProfitTillConstants.DOMAIN)) {
|
|
|
98 |
CookieSyncManager.createInstance(getActivity());
|
|
|
99 |
CookieManager cookieManager1 = CookieManager.getInstance();
|
|
|
100 |
cookieManager1.setCookie(url, "token=" + userData.getString("token", ""));
|
|
|
101 |
cookieManager1.setCookie(url, "walletAuthentication=true");
|
|
|
102 |
cookieManager1.setCookie(url, "shopcluesActive=true");
|
|
|
103 |
}
|
|
|
104 |
preferencesWebView.loadUrl(landingUrl);
|
|
|
105 |
preferencesWebView.setWebViewClient(webViewClient);
|
|
|
106 |
}
|
|
|
107 |
else{
|
|
|
108 |
SearchFragment nextFrag= new SearchFragment();
|
|
|
109 |
Bundle args = new Bundle();
|
|
|
110 |
args.putString("finishingurl", landingUrl);
|
|
|
111 |
args.putString("fragmentNumber", "12");
|
|
|
112 |
nextFrag.setArguments(args);
|
|
|
113 |
getActivity().getFragmentManager().beginTransaction()
|
|
|
114 |
.replace(R.id.frame_container, nextFrag)
|
|
|
115 |
.addToBackStack(null)
|
|
|
116 |
.commit();
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
return rootView;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
@Override
|
|
|
123 |
public void onViewCreated(View view, Bundle savedInstanceState) {
|
|
|
124 |
super.onViewCreated(view, savedInstanceState);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
JSONObject obj;
|
|
|
128 |
public class WebClientClass extends WebViewClient {
|
|
|
129 |
ProgressDialog pd = null;
|
|
|
130 |
|
|
|
131 |
@Override
|
|
|
132 |
public void onPageStarted(WebView view, String url, Bitmap favicon) {
|
|
|
133 |
if(url.equalsIgnoreCase("http://m.saholic.com")){
|
|
|
134 |
pd = new ProgressDialog(getActivity());
|
|
|
135 |
pd.setTitle("Please wait");
|
|
|
136 |
pd.setMessage("Loading..");
|
|
|
137 |
pd.show();
|
|
|
138 |
}
|
|
|
139 |
super.onPageStarted(view, url, favicon);
|
|
|
140 |
obj = new JSONObject();
|
|
|
141 |
|
|
|
142 |
try {
|
|
|
143 |
JSONObject j = new JSONObject();
|
|
|
144 |
JSONArray jsArray = new JSONArray();
|
|
|
145 |
j.put("url",url);
|
|
|
146 |
j.put("user_id",userData.getString("id",null));
|
|
|
147 |
j.put("time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
|
148 |
jsArray.put(j);
|
|
|
149 |
obj.put("pushdata",jsArray);
|
|
|
150 |
} catch (JSONException e) {
|
|
|
151 |
e.printStackTrace();
|
|
|
152 |
}
|
|
|
153 |
new pushData().execute();
|
|
|
154 |
}
|
|
|
155 |
@Override
|
|
|
156 |
public void onPageFinished(WebView view, String url) {
|
|
|
157 |
if(url.equalsIgnoreCase("http://m.saholic.com")) {
|
|
|
158 |
pd.dismiss();
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
view.setOnKeyListener(new View.OnKeyListener()
|
|
|
162 |
{
|
|
|
163 |
@Override
|
|
|
164 |
public boolean onKey(View v, int keyCode, KeyEvent event)
|
|
|
165 |
{
|
|
|
166 |
if(event.getAction() == KeyEvent.ACTION_DOWN)
|
|
|
167 |
{
|
|
|
168 |
WebView webView = (WebView) v;
|
|
|
169 |
|
|
|
170 |
switch(keyCode)
|
|
|
171 |
{
|
|
|
172 |
case KeyEvent.KEYCODE_BACK:
|
|
|
173 |
if(webView.canGoBack())
|
|
|
174 |
{
|
|
|
175 |
webView.goBack();
|
|
|
176 |
return true;
|
|
|
177 |
}
|
|
|
178 |
else{
|
|
|
179 |
Intent i = new Intent(getActivity(),MainActivity.class);
|
|
|
180 |
i.putExtra("displayView","0");
|
|
|
181 |
startActivity(i);
|
|
|
182 |
}
|
|
|
183 |
break;
|
|
|
184 |
}
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
return false;
|
|
|
188 |
}
|
|
|
189 |
});
|
|
|
190 |
super.onPageFinished(view, url);
|
|
|
191 |
}
|
|
|
192 |
@Override
|
|
|
193 |
public void onReceivedHttpAuthRequest(WebView view,
|
|
|
194 |
HttpAuthHandler handler, String host, String realm) {
|
|
|
195 |
|
|
|
196 |
handler.proceed("dtr", "dtr18Feb2015");
|
|
|
197 |
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
@Override
|
|
|
201 |
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
|
|
202 |
super.shouldOverrideUrlLoading(view, url);
|
|
|
203 |
|
|
|
204 |
if(url.contains(apiData.getString("mobile.website.url",""))){
|
|
|
205 |
new Analytics(ProfitTillConstants.ANALYTICS_URL,
|
|
|
206 |
AnalyticsUtility.getAnalyticsRequest(getActivity(), userData.getString("id", ""), "Clicks", "My Preferences", "Show my deals"),
|
|
|
207 |
AnalyticsJsonResponse.getAnalyticsRequestInstance(),
|
|
|
208 |
AnalyticsErrorResponse.getAnalyitcsResponseInstance()).anlyticsRequest(getActivity());
|
|
|
209 |
Intent i = new Intent(getActivity(),MainActivity.class);
|
|
|
210 |
i.putExtra("displayView","0");
|
|
|
211 |
startActivity(i);
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
return false;
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
@Override
|
|
|
218 |
public void onResume() {
|
|
|
219 |
super.onResume();
|
|
|
220 |
getView().setFocusableInTouchMode(true);
|
|
|
221 |
getView().requestFocus();
|
|
|
222 |
getView().setOnKeyListener(new View.OnKeyListener() {
|
|
|
223 |
@Override
|
|
|
224 |
public boolean onKey(View v, int keyCode, KeyEvent event) {
|
|
|
225 |
|
|
|
226 |
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){
|
|
|
227 |
Intent i = new Intent(getActivity(), MainActivity.class);
|
|
|
228 |
i.putExtra("displayView", "0");
|
|
|
229 |
startActivity(i);
|
|
|
230 |
return true;
|
|
|
231 |
|
|
|
232 |
}
|
|
|
233 |
return false;
|
|
|
234 |
}
|
|
|
235 |
});
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
class pushData extends AsyncTask<String, Integer, String> {
|
|
|
239 |
|
|
|
240 |
@Override
|
|
|
241 |
protected void onPreExecute() {
|
|
|
242 |
super.onPreExecute();
|
|
|
243 |
Log.e("Push Service", "Pre execute");
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
@Override
|
|
|
247 |
protected String doInBackground(String... arg0) {
|
|
|
248 |
HttpClient httpclient = new DefaultHttpClient();
|
|
|
249 |
try {
|
|
|
250 |
ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();
|
|
|
251 |
nameValuePairs.add(new BasicNameValuePair("pushdata",obj.toString()));
|
|
|
252 |
HttpPost httppost = new HttpPost(apiData.getString("url.push.api",null));
|
|
|
253 |
httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
|
|
|
254 |
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
|
|
|
255 |
System.out.println(obj.toString());
|
|
|
256 |
StringEntity se = new StringEntity(obj.toString());
|
|
|
257 |
HttpResponse response = httpclient.execute(httppost);
|
|
|
258 |
int status = response.getStatusLine().getStatusCode();
|
|
|
259 |
|
|
|
260 |
if(status == 200){
|
|
|
261 |
System.out.println("ResponseCode of record: "+ status + " is " + status);
|
|
|
262 |
return "success";
|
|
|
263 |
} else {
|
|
|
264 |
Log.d("ResponseCode",status+"");
|
|
|
265 |
System.out.println(" NOT Transfered");
|
|
|
266 |
return "failure";
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
} catch (ClientProtocolException e) {
|
|
|
270 |
return "failure";
|
|
|
271 |
} catch (IOException e) {
|
|
|
272 |
return "failure";
|
|
|
273 |
}
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
@Override
|
|
|
277 |
protected void onPostExecute(String result) {
|
|
|
278 |
super.onPostExecute(result);
|
|
|
279 |
}
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
public final boolean isInternetOn() {
|
|
|
283 |
|
|
|
284 |
ConnectivityManager connec =
|
|
|
285 |
(ConnectivityManager)getActivity().getSystemService(getActivity().getBaseContext().CONNECTIVITY_SERVICE);
|
|
|
286 |
|
|
|
287 |
if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
|
|
|
288 |
connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
|
|
|
289 |
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
|
|
|
290 |
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {
|
|
|
291 |
return true;
|
|
|
292 |
|
|
|
293 |
} else if (
|
|
|
294 |
connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
|
|
|
295 |
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED ) {
|
|
|
296 |
|
|
|
297 |
return false;
|
|
|
298 |
}
|
|
|
299 |
return false;
|
|
|
300 |
}
|
|
|
301 |
}
|