Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.vgolubeva.browser;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.app.Dialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.res.Configuration;
- import android.graphics.Color;
- import android.net.Uri;
- import android.net.http.SslError;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Environment;
- import android.os.Handler;
- import android.provider.MediaStore;
- import android.support.v4.widget.SwipeRefreshLayout;
- import android.util.Log;
- import android.view.View;
- import android.webkit.GeolocationPermissions;
- import android.webkit.HttpAuthHandler;
- import android.webkit.SslErrorHandler;
- import android.webkit.URLUtil;
- import android.webkit.ValueCallback;
- import android.webkit.WebChromeClient;
- import android.webkit.WebResourceError;
- import android.webkit.WebResourceRequest;
- import android.webkit.WebResourceResponse;
- import android.webkit.WebSettings;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.loopj.android.http.AsyncHttpClient;
- import com.loopj.android.http.TextHttpResponseHandler;
- import java.io.File;
- import java.io.IOException;
- import java.security.KeyManagementException;
- import java.security.NoSuchAlgorithmException;
- import java.security.SecureRandom;
- import java.security.cert.X509Certificate;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.SSLSocketFactory;
- import javax.net.ssl.TrustManager;
- import javax.net.ssl.X509TrustManager;
- import cz.msebera.android.httpclient.Header;
- import cz.msebera.android.httpclient.auth.AuthScope;
- import static com.example.vgolubeva.browser.R.id.webView;
- import static com.example.vgolubeva.browser.R.layout.activity_main;
- public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {
- final String LOG_TAG = "myLogs";
- private WebView mWebView;
- private SwipeRefreshLayout mSwipeRefreshLayout;
- public String user = null;
- public String pass = null;
- private static final String TAG = MainActivity.class.getSimpleName();
- private String mCM;
- private ValueCallback<Uri> mUM;
- private ValueCallback<Uri[]> mUMA;
- private final static int FCR = 1;
- private AsyncHttpClient asyncHttpClient;
- private int authCounter = 0;
- public Dialog login;
- private class SSLTolerentWebViewClient extends WebViewClient {
- @Override
- public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
- return super.shouldInterceptRequest(view, request);
- }
- @Override
- public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
- Log.e("auth","recieved");
- authCounter++;
- if (user != null && pass != null) {
- handler.proceed(user, pass);
- user = null;
- pass = null;
- }
- if(authCounter%2 == 0){
- handler.cancel();
- authCounter = 0;
- if (Build.VERSION.SDK_INT < 18) {
- view.clearView();
- } else {
- view.loadUrl("about:blank");
- login.show();
- }
- }else super.onReceivedHttpAuthRequest(view, handler, host, realm);
- }
- @Override
- public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
- Log.e("ProcessPayment", "onReceivedError = " + error.toString());
- super.onReceivedError(view, request, error);
- }
- @Override
- public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
- Log.e("ProcessPayment", "onReceivedHttpError = ");
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- errorResponse.getReasonPhrase();
- }
- }
- @Override
- public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
- Log.d(LOG_TAG, "SSL IGNORE");
- handler.proceed();
- }
- @Override
- public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
- super.onReceivedError(view, errorCode, description, failingUrl);
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
- super.onActivityResult(requestCode, resultCode, intent);
- if (Build.VERSION.SDK_INT >= 15) {
- Uri[] results = null;
- if (resultCode == Activity.RESULT_OK) {
- if (requestCode == FCR) {
- if (null == mUMA) {
- return;
- }
- if (intent == null) {
- if (mCM != null) {
- results = new Uri[]{Uri.parse(mCM)};
- }
- } else {
- String dataString = intent.getDataString();
- if (dataString != null) {
- results = new Uri[]{Uri.parse(dataString)};
- }
- }
- }
- }
- mUMA.onReceiveValue(results);
- mUMA = null;
- } else {
- if (requestCode == FCR) {
- if (null == mUM) return;
- Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
- mUM.onReceiveValue(result);
- mUM = null;
- }
- }
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(activity_main);
- asyncHttpClient = new AsyncHttpClient();
- mWebView = (WebView) findViewById(webView);
- mWebView.setWebViewClient(new SSLTolerentWebViewClient());
- mWebView.getSettings().setAppCacheEnabled(true);
- mWebView.getSettings().setDatabaseEnabled(true);
- mWebView.getSettings().setDomStorageEnabled(true);
- mWebView.getSettings().setGeolocationEnabled(true);
- mWebView.getSettings().setLoadWithOverviewMode(true);
- mWebView.getSettings().setAllowContentAccess(true);
- mWebView.getSettings().setAllowFileAccess(true);
- mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
- mWebView.getSettings().setUseWideViewPort(true);
- mWebView.getSettings().setJavaScriptEnabled(true);
- mWebView.getSettings().setSaveFormData(false);
- mWebView.getSettings().setSavePassword(true);
- mWebView.getSettings().setGeolocationDatabasePath(getFilesDir().getPath());
- mWebView.getSettings().setDatabaseEnabled(true);
- mWebView.getSettings().setDomStorageEnabled(true);
- mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
- mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
- mWebView.setWebChromeClient(new WebChromeClient() {
- //For Android 3.0+
- public void openFileChooser(ValueCallback<Uri> uploadMsg) {
- Log.d(LOG_TAG, "OPEN IMAGE");
- mUM = uploadMsg;
- Intent i = new Intent(Intent.ACTION_GET_CONTENT);
- i.addCategory(Intent.CATEGORY_OPENABLE);
- i.setType("*/*");
- MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FCR);
- }
- // For Android 3.0+, above method not supported in some android 3+ versions, in such case we use this
- public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
- Log.d(LOG_TAG, "OPEN IMAGE");
- // mUM = uploadMsg;
- Intent i = new Intent(Intent.ACTION_GET_CONTENT);
- i.addCategory(Intent.CATEGORY_OPENABLE);
- i.setType("image/*");
- MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Browser"), FCR);
- }
- //For Android 4.1+
- public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
- Log.d(LOG_TAG, "OPEN IMAGE");
- mUM = uploadMsg;
- Intent i = new Intent(Intent.ACTION_GET_CONTENT);
- i.addCategory(Intent.CATEGORY_OPENABLE);
- i.setType("image/*");
- MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FCR);
- }
- //For Android 5.0+
- public boolean onShowFileChooser(
- WebView webView, ValueCallback<Uri[]> filePathCallback,
- WebChromeClient.FileChooserParams fileChooserParams) {
- if (mUMA != null) {
- mUMA.onReceiveValue(null);
- }
- Log.d(LOG_TAG, "OPEN IMAGE");
- mUMA = filePathCallback;
- Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- if (takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
- File photoFile = null;
- try {
- photoFile = createImageFile();
- takePictureIntent.putExtra("PhotoPath", mCM);
- } catch (IOException ex) {
- Log.e(TAG, "Image file creation failed", ex);
- }
- if (photoFile != null) {
- mCM = "file:" + photoFile.getAbsolutePath();
- takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
- } else {
- takePictureIntent = null;
- }
- }
- Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
- contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
- contentSelectionIntent.setType("image/*");
- Intent[] intentArray;
- if (takePictureIntent != null) {
- intentArray = new Intent[]{takePictureIntent};
- } else {
- intentArray = new Intent[0];
- }
- Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
- chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
- chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
- chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
- startActivityForResult(chooserIntent, FCR);
- return true;
- }
- @Override
- public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
- callback.invoke(origin, true, false);
- }
- });
- mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
- mSwipeRefreshLayout.setOnRefreshListener(this);
- mSwipeRefreshLayout.setColorSchemeColors(Color.LTGRAY, Color.GRAY, Color.DKGRAY, Color.BLACK);
- login = new Dialog(MainActivity.this);
- login.setContentView(R.layout.login_dialog);
- login.setTitle("Login");
- login.setCancelable(false);
- Button btnLogin = (Button) login.findViewById(R.id.btnLogin);
- Button btnCancel = (Button) login.findViewById(R.id.btnCancel);
- final EditText txtUsername = (EditText) login.findViewById(R.id.txtUsername);
- final EditText txtPassword = (EditText) login.findViewById(R.id.txtPassword);
- btnLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (txtUsername.getText().toString().trim().length() > 0 && txtPassword.getText().toString().trim().length() > 0) {
- login.dismiss();
- user = txtUsername.getText().toString();
- pass = txtPassword.getText().toString();
- // mWebView.loadUrl("https://" + user + ":" + pass + "@support.antivor.ru");
- Log.d(LOG_TAG, "OPEN SUPPORT ANTIVOR");
- // asyncHttpClient.setBasicAuth(user,pass);
- asyncHttpClient.setSSLSocketFactory(new cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory(getSslContext(), cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER));
- asyncHttpClient.get("https://" + user + ":" + pass + "@support.antivor.ru", new TextHttpResponseHandler() {
- @Override
- public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
- if(statusCode == 401){
- Toast.makeText(MainActivity.this,"Произошла ошибка авторизации",Toast.LENGTH_SHORT).show();
- }else{
- Toast.makeText(MainActivity.this,"Произошла ошибка с номером: "+statusCode,Toast.LENGTH_SHORT).show();
- }
- login.show();
- }
- //Content-Length: 3982
- @Override
- public void onSuccess(int statusCode, Header[] headers, String responseString) {
- mWebView.loadUrl("https://support.antivor.ru");
- }
- });
- // mWebView.loadUrl("https://support.antivor.ru");
- // mWebView.setHttpAuthUsernamePassword("https://support.antivor.ru","User Visible Realm",user,pass);
- // new Handler().postDelayed(new Runnable() {
- // @Override
- // public void run() {
- // Log.d(LOG_TAG, "REFRASH 1");
- // mWebView.loadUrl("https://support.antivor.ru");
- // }
- // }, 2000);
- } else {
- Toast.makeText(MainActivity.this, "Please enter Username or Password", Toast.LENGTH_LONG).show();
- }
- }
- });
- btnCancel.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- login.dismiss();
- }
- });
- login.setOnCancelListener(new DialogInterface.OnCancelListener() {
- @Override
- public void onCancel(DialogInterface dialog) {
- dialog.toString();
- }
- });
- login.show();
- }
- @Override
- public void onRefresh() {
- Log.d(LOG_TAG, "REFRASH");
- new Handler().postDelayed(new Runnable() {
- @Override
- public void run() {
- mSwipeRefreshLayout.setRefreshing(false);
- mWebView.loadUrl("https://support.antivor.ru");
- }
- }, 2000);
- }
- @Override
- public void onBackPressed() {
- Log.d(LOG_TAG, "BUTTON BACK");
- if (mWebView.canGoBack()) {
- mWebView.goBack();
- } else {
- super.onBackPressed();
- }
- }
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- super.onConfigurationChanged(newConfig);
- }
- private File createImageFile() throws IOException {
- @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
- String imageFileName = "img_" + timeStamp + "_";
- File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
- return File.createTempFile(imageFileName, ".jpg", storageDir);
- }
- public SSLContext getSslContext() {
- TrustManager[] byPassTrustManagers = new TrustManager[] { new X509TrustManager() {
- public X509Certificate[] getAcceptedIssuers() {
- return new X509Certificate[0];
- }
- public void checkClientTrusted(X509Certificate[] chain, String authType) {
- }
- public void checkServerTrusted(X509Certificate[] chain, String authType) {
- }
- } };
- SSLContext sslContext=null;
- try {
- sslContext = SSLContext.getInstance("TLS");
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- try {
- sslContext.init(null, byPassTrustManagers, new SecureRandom());
- } catch (KeyManagementException e) {
- e.printStackTrace();
- }
- return sslContext;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement