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