Advertisement
Guest User

Untitled

a guest
Aug 28th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.03 KB | None | 0 0
  1. package com.example.vgolubeva.browser;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.app.Activity;
  5. import android.app.Dialog;
  6. import android.content.DialogInterface;
  7. import android.content.Intent;
  8. import android.content.res.Configuration;
  9. import android.graphics.Color;
  10. import android.net.Uri;
  11. import android.net.http.SslError;
  12. import android.os.Build;
  13. import android.os.Bundle;
  14. import android.os.Environment;
  15. import android.os.Handler;
  16. import android.provider.MediaStore;
  17. import android.support.v4.widget.SwipeRefreshLayout;
  18. import android.util.Log;
  19. import android.view.View;
  20. import android.webkit.GeolocationPermissions;
  21. import android.webkit.HttpAuthHandler;
  22. import android.webkit.SslErrorHandler;
  23. import android.webkit.URLUtil;
  24. import android.webkit.ValueCallback;
  25. import android.webkit.WebChromeClient;
  26. import android.webkit.WebResourceError;
  27. import android.webkit.WebResourceRequest;
  28. import android.webkit.WebResourceResponse;
  29. import android.webkit.WebSettings;
  30. import android.webkit.WebView;
  31. import android.webkit.WebViewClient;
  32. import android.widget.Button;
  33. import android.widget.EditText;
  34. import android.widget.Toast;
  35.  
  36. import com.loopj.android.http.AsyncHttpClient;
  37. import com.loopj.android.http.TextHttpResponseHandler;
  38.  
  39. import java.io.File;
  40. import java.io.IOException;
  41. import java.security.KeyManagementException;
  42. import java.security.NoSuchAlgorithmException;
  43. import java.security.SecureRandom;
  44. import java.security.cert.X509Certificate;
  45. import java.text.SimpleDateFormat;
  46. import java.util.Date;
  47.  
  48. import javax.net.ssl.SSLContext;
  49. import javax.net.ssl.SSLSocketFactory;
  50. import javax.net.ssl.TrustManager;
  51. import javax.net.ssl.X509TrustManager;
  52.  
  53. import cz.msebera.android.httpclient.Header;
  54. import cz.msebera.android.httpclient.auth.AuthScope;
  55.  
  56. import static com.example.vgolubeva.browser.R.id.webView;
  57. import static com.example.vgolubeva.browser.R.layout.activity_main;
  58.  
  59. public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {
  60.  
  61. final String LOG_TAG = "myLogs";
  62.  
  63. private WebView mWebView;
  64. private SwipeRefreshLayout mSwipeRefreshLayout;
  65. public String user = null;
  66. public String pass = null;
  67. private static final String TAG = MainActivity.class.getSimpleName();
  68. private String mCM;
  69. private ValueCallback<Uri> mUM;
  70. private ValueCallback<Uri[]> mUMA;
  71. private final static int FCR = 1;
  72. private AsyncHttpClient asyncHttpClient;
  73. private int authCounter = 0;
  74. public Dialog login;
  75.  
  76. private class SSLTolerentWebViewClient extends WebViewClient {
  77.  
  78. @Override
  79. public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
  80.  
  81. return super.shouldInterceptRequest(view, request);
  82. }
  83.  
  84.  
  85. @Override
  86. public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
  87. Log.e("auth","recieved");
  88. authCounter++;
  89. if (user != null && pass != null) {
  90. handler.proceed(user, pass);
  91. user = null;
  92. pass = null;
  93. }
  94. if(authCounter%2 == 0){
  95. handler.cancel();
  96. authCounter = 0;
  97. if (Build.VERSION.SDK_INT < 18) {
  98. view.clearView();
  99. } else {
  100. view.loadUrl("about:blank");
  101. login.show();
  102. }
  103. }else super.onReceivedHttpAuthRequest(view, handler, host, realm);
  104. }
  105.  
  106. @Override
  107. public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
  108. Log.e("ProcessPayment", "onReceivedError = " + error.toString());
  109. super.onReceivedError(view, request, error);
  110. }
  111.  
  112.  
  113.  
  114. @Override
  115. public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
  116. Log.e("ProcessPayment", "onReceivedHttpError = ");
  117. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  118. errorResponse.getReasonPhrase();
  119. }
  120.  
  121. }
  122.  
  123. @Override
  124. public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
  125. Log.d(LOG_TAG, "SSL IGNORE");
  126. handler.proceed();
  127. }
  128.  
  129. @Override
  130. public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
  131. super.onReceivedError(view, errorCode, description, failingUrl);
  132. }
  133. }
  134.  
  135. @Override
  136. protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  137. super.onActivityResult(requestCode, resultCode, intent);
  138. if (Build.VERSION.SDK_INT >= 15) {
  139. Uri[] results = null;
  140. if (resultCode == Activity.RESULT_OK) {
  141. if (requestCode == FCR) {
  142. if (null == mUMA) {
  143. return;
  144. }
  145. if (intent == null) {
  146. if (mCM != null) {
  147. results = new Uri[]{Uri.parse(mCM)};
  148. }
  149. } else {
  150. String dataString = intent.getDataString();
  151. if (dataString != null) {
  152. results = new Uri[]{Uri.parse(dataString)};
  153. }
  154. }
  155. }
  156. }
  157. mUMA.onReceiveValue(results);
  158. mUMA = null;
  159. } else {
  160. if (requestCode == FCR) {
  161. if (null == mUM) return;
  162. Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
  163. mUM.onReceiveValue(result);
  164. mUM = null;
  165. }
  166. }
  167. }
  168.  
  169. @Override
  170. public void onCreate(Bundle savedInstanceState) {
  171. super.onCreate(savedInstanceState);
  172. setContentView(activity_main);
  173.  
  174. asyncHttpClient = new AsyncHttpClient();
  175.  
  176. mWebView = (WebView) findViewById(webView);
  177. mWebView.setWebViewClient(new SSLTolerentWebViewClient());
  178. mWebView.getSettings().setAppCacheEnabled(true);
  179. mWebView.getSettings().setDatabaseEnabled(true);
  180. mWebView.getSettings().setDomStorageEnabled(true);
  181. mWebView.getSettings().setGeolocationEnabled(true);
  182. mWebView.getSettings().setLoadWithOverviewMode(true);
  183. mWebView.getSettings().setAllowContentAccess(true);
  184. mWebView.getSettings().setAllowFileAccess(true);
  185. mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
  186. mWebView.getSettings().setUseWideViewPort(true);
  187. mWebView.getSettings().setJavaScriptEnabled(true);
  188. mWebView.getSettings().setSaveFormData(false);
  189. mWebView.getSettings().setSavePassword(true);
  190. mWebView.getSettings().setGeolocationDatabasePath(getFilesDir().getPath());
  191. mWebView.getSettings().setDatabaseEnabled(true);
  192. mWebView.getSettings().setDomStorageEnabled(true);
  193. mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
  194. mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
  195. mWebView.setWebChromeClient(new WebChromeClient() {
  196.  
  197. //For Android 3.0+
  198. public void openFileChooser(ValueCallback<Uri> uploadMsg) {
  199. Log.d(LOG_TAG, "OPEN IMAGE");
  200. mUM = uploadMsg;
  201. Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  202. i.addCategory(Intent.CATEGORY_OPENABLE);
  203. i.setType("*/*");
  204. MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FCR);
  205. }
  206.  
  207. // For Android 3.0+, above method not supported in some android 3+ versions, in such case we use this
  208. public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
  209. Log.d(LOG_TAG, "OPEN IMAGE");
  210. // mUM = uploadMsg;
  211. Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  212. i.addCategory(Intent.CATEGORY_OPENABLE);
  213. i.setType("image/*");
  214. MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Browser"), FCR);
  215. }
  216.  
  217. //For Android 4.1+
  218. public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
  219. Log.d(LOG_TAG, "OPEN IMAGE");
  220. mUM = uploadMsg;
  221. Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  222. i.addCategory(Intent.CATEGORY_OPENABLE);
  223. i.setType("image/*");
  224. MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FCR);
  225. }
  226.  
  227. //For Android 5.0+
  228. public boolean onShowFileChooser(
  229.  
  230. WebView webView, ValueCallback<Uri[]> filePathCallback,
  231. WebChromeClient.FileChooserParams fileChooserParams) {
  232. if (mUMA != null) {
  233. mUMA.onReceiveValue(null);
  234. }
  235. Log.d(LOG_TAG, "OPEN IMAGE");
  236. mUMA = filePathCallback;
  237. Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  238. if (takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
  239. File photoFile = null;
  240. try {
  241. photoFile = createImageFile();
  242. takePictureIntent.putExtra("PhotoPath", mCM);
  243. } catch (IOException ex) {
  244. Log.e(TAG, "Image file creation failed", ex);
  245. }
  246. if (photoFile != null) {
  247. mCM = "file:" + photoFile.getAbsolutePath();
  248. takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
  249. } else {
  250. takePictureIntent = null;
  251. }
  252. }
  253. Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
  254. contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
  255. contentSelectionIntent.setType("image/*");
  256. Intent[] intentArray;
  257. if (takePictureIntent != null) {
  258. intentArray = new Intent[]{takePictureIntent};
  259. } else {
  260. intentArray = new Intent[0];
  261. }
  262.  
  263. Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
  264. chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
  265. chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
  266. chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
  267. startActivityForResult(chooserIntent, FCR);
  268. return true;
  269. }
  270.  
  271. @Override
  272. public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
  273. callback.invoke(origin, true, false);
  274. }
  275. });
  276.  
  277. mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
  278. mSwipeRefreshLayout.setOnRefreshListener(this);
  279. mSwipeRefreshLayout.setColorSchemeColors(Color.LTGRAY, Color.GRAY, Color.DKGRAY, Color.BLACK);
  280.  
  281. login = new Dialog(MainActivity.this);
  282. login.setContentView(R.layout.login_dialog);
  283. login.setTitle("Login");
  284. login.setCancelable(false);
  285.  
  286. Button btnLogin = (Button) login.findViewById(R.id.btnLogin);
  287. Button btnCancel = (Button) login.findViewById(R.id.btnCancel);
  288. final EditText txtUsername = (EditText) login.findViewById(R.id.txtUsername);
  289. final EditText txtPassword = (EditText) login.findViewById(R.id.txtPassword);
  290. btnLogin.setOnClickListener(new View.OnClickListener() {
  291. @Override
  292. public void onClick(View v) {
  293. if (txtUsername.getText().toString().trim().length() > 0 && txtPassword.getText().toString().trim().length() > 0) {
  294. login.dismiss();
  295.  
  296. user = txtUsername.getText().toString();
  297. pass = txtPassword.getText().toString();
  298.  
  299. // mWebView.loadUrl("https://" + user + ":" + pass + "@support.antivor.ru");
  300. Log.d(LOG_TAG, "OPEN SUPPORT ANTIVOR");
  301.  
  302. // asyncHttpClient.setBasicAuth(user,pass);
  303. asyncHttpClient.setSSLSocketFactory(new cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory(getSslContext(), cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER));
  304.  
  305. asyncHttpClient.get("https://" + user + ":" + pass + "@support.antivor.ru", new TextHttpResponseHandler() {
  306. @Override
  307. public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
  308. if(statusCode == 401){
  309. Toast.makeText(MainActivity.this,"Произошла ошибка авторизации",Toast.LENGTH_SHORT).show();
  310. }else{
  311. Toast.makeText(MainActivity.this,"Произошла ошибка с номером: "+statusCode,Toast.LENGTH_SHORT).show();
  312. }
  313. login.show();
  314. }
  315. //Content-Length: 3982
  316.  
  317. @Override
  318. public void onSuccess(int statusCode, Header[] headers, String responseString) {
  319. mWebView.loadUrl("https://support.antivor.ru");
  320. }
  321. });
  322.  
  323. // mWebView.loadUrl("https://support.antivor.ru");
  324. // mWebView.setHttpAuthUsernamePassword("https://support.antivor.ru","User Visible Realm",user,pass);
  325.  
  326. // new Handler().postDelayed(new Runnable() {
  327. // @Override
  328. // public void run() {
  329. // Log.d(LOG_TAG, "REFRASH 1");
  330. // mWebView.loadUrl("https://support.antivor.ru");
  331. // }
  332. // }, 2000);
  333.  
  334. } else {
  335. Toast.makeText(MainActivity.this, "Please enter Username or Password", Toast.LENGTH_LONG).show();
  336. }
  337. }
  338. });
  339. btnCancel.setOnClickListener(new View.OnClickListener() {
  340. @Override
  341. public void onClick(View v) {
  342. login.dismiss();
  343. }
  344. });
  345. login.setOnCancelListener(new DialogInterface.OnCancelListener() {
  346. @Override
  347. public void onCancel(DialogInterface dialog) {
  348. dialog.toString();
  349. }
  350. });
  351. login.show();
  352. }
  353.  
  354. @Override
  355. public void onRefresh() {
  356. Log.d(LOG_TAG, "REFRASH");
  357. new Handler().postDelayed(new Runnable() {
  358. @Override
  359. public void run() {
  360. mSwipeRefreshLayout.setRefreshing(false);
  361. mWebView.loadUrl("https://support.antivor.ru");
  362. }
  363. }, 2000);
  364. }
  365.  
  366. @Override
  367. public void onBackPressed() {
  368. Log.d(LOG_TAG, "BUTTON BACK");
  369. if (mWebView.canGoBack()) {
  370. mWebView.goBack();
  371. } else {
  372. super.onBackPressed();
  373. }
  374. }
  375.  
  376. @Override
  377. public void onConfigurationChanged(Configuration newConfig) {
  378. super.onConfigurationChanged(newConfig);
  379. }
  380.  
  381. private File createImageFile() throws IOException {
  382. @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  383. String imageFileName = "img_" + timeStamp + "_";
  384. File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  385. return File.createTempFile(imageFileName, ".jpg", storageDir);
  386. }
  387.  
  388.  
  389. public SSLContext getSslContext() {
  390.  
  391. TrustManager[] byPassTrustManagers = new TrustManager[] { new X509TrustManager() {
  392. public X509Certificate[] getAcceptedIssuers() {
  393. return new X509Certificate[0];
  394. }
  395.  
  396. public void checkClientTrusted(X509Certificate[] chain, String authType) {
  397. }
  398.  
  399. public void checkServerTrusted(X509Certificate[] chain, String authType) {
  400. }
  401. } };
  402.  
  403. SSLContext sslContext=null;
  404.  
  405. try {
  406. sslContext = SSLContext.getInstance("TLS");
  407. } catch (NoSuchAlgorithmException e) {
  408. e.printStackTrace();
  409. }
  410. try {
  411. sslContext.init(null, byPassTrustManagers, new SecureRandom());
  412. } catch (KeyManagementException e) {
  413. e.printStackTrace();
  414. }
  415.  
  416. return sslContext;
  417. }
  418.  
  419.  
  420.  
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement