Search code examples
javaandroidandroid-recyclerviewwebviewone-time-password

How to Save Username and Password in Webview


Currently, I am still in the proccess of learning Android development so please excuse me if this question of mine is not easily understandable for you.

I create an Android app that is showing a set of list using RecyclerView and when the user click each of the names on the list, it will redirect them to a set of different websites and it will be displayed in WebView. Each of those websites have a different login page. I don't control those websites. Those websites turn off their cookies. Now, is it possible to save user's Username and Password in this situation?

When I included Facebook, Instagram and Twitter, my app able to save the Username and Password (maybe because they are saving cookies?). But if I try the particular websites (with the special situation) that I wanted to add, the Username and Password is not saved.

Since the setSavePassword method is deprecated, I can not found other way to implement saving Username and Password. I have read about SharedPreferences, JavaScript and Autofill several times, but I don't know how to properly implement them into my app. Because from most of what I have read, it is suggested that it will need a login page first to save the Username and Password.

This is my WebView code.

public class WebViewActivity extends AppCompatActivity {

    private WebView webView;
    String url = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        
        webView = findViewById(R.id.webView);
        
        WebSettings webSettings = webView.getSettings();
        
        websettings.setDomStorageEnabled(true);
        websettings.setJavaScriptEnabled(true);
        webView.getSettings().setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
        webView.getSettings().setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");
        
        if (Build.VERSION.SDK_INT >= 21) {
            CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
        } else {
            CookieManager.getInstance().setAcceptCookie(true);
        }
        
        webView.loadUrl(url);
        
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        }
        ...
    }
    
    ...
}

Thank you.


Solution

  • public class WebViewActivity extends AppCompatActivity {
    
        private WebView webView;
        String url = "";
    
        class MyJavaScriptInterface {
            MyJavaScriptInterface() {
            }
            @SuppressWarnings("unused")
            @JavascriptInterface
            public void processContent(String user,String pass) {
                Log.e("Remember", "user:"+user+" pass:"+pass);
                if (!(user.length() >= 1 && pass.length() >= 1))
                    return;
    
                   
                SharedPreferences sharedPreferences = getContext().getSharedPreferences("UserInformation",MODE_PRIVATE);
                if (sharedPreferences.getBoolean("isClickYes", false)) {
                    sharedPreferences.edit().putString("us",user).apply();
                    sharedPreferences.edit().putString("pass",pass).apply();
                }
    
                if (!sharedPreferences.getBoolean("isClickYes", false)) {
                    AlertDialog alertDialog = new AlertDialog.Builder(getContext())
                            .setTitle("save information")
                            .setMessage("Are You Want Save Password?")
    
                            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    sharedPreferences.edit().putString("us",user).apply();
                                    sharedPreferences.edit().putString("pass",pass).apply();
                                    sharedPreferences.edit().putBoolean("isClickYes",true).apply();
                                }
                            })
    
                            .setNegativeButton(android.R.string.no, null)
                            .setIcon(android.R.drawable.ic_popup_reminder)
                            .show();
                }
    
            }
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.webview);
    
            webView = findViewById(R.id.webView);
    
            WebSettings webSettings = webView.getSettings();
    
            websettings.setDomStorageEnabled(true);
            websettings.setJavaScriptEnabled(true);
            webView.addJavascriptInterface(new MyJavaScriptInterface(), "INTERFACE");
            webView.getSettings().setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
            webView.getSettings().setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");
    
            if (Build.VERSION.SDK_INT >= 21) {
                CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
            } else {
                CookieManager.getInstance().setAcceptCookie(true);
            }
    
            webView.setWebViewClient(new WebViewClient(){
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                }
    
    
    
    private void runOnUi(int page, WebView view ,SharedPreferences sharedPreferences) {
        int LOGIN      = 0;
        int LOGIN_PASS = 1;
        WebViewActivity.this.runOnUiThread(() -> {
            if (page == LOGIN_PASS) {
            view.loadUrl("javascript:(function(){l=document.getElementById(\"formLogin\"); l.setAttribute(\"onclick\",\"" +
                    "INTERFACE.processContent(  document.getElementById('txtUserId').value , document.getElementById('txtPassword').value  );\"   );       " + "})()");
    
            if (sharedPreferences.getBoolean("isClickYes", false)) {
                String pass = sharedPreferences.getString("pass", "");
                view.loadUrl("javascript:(function(){l=document.getElementById('txtPassword');l.value='" + pass + "'; })()");
            }
            } else if (page == LOGIN) {
                String user = sharedPreferences.getString("us", "");
                view.loadUrl("javascript:(function(){l=document.getElementById('txtUserId2');l.value='" + user + "'; })()");
            }
        });
    }
    
    
    
                @Nullable
                @Override
                public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    
    
    
    
        if (request.getUrl().toString().equals("https/.../....png")) {
            SharedPreferences sharedPreferences = getContext().getSharedPreferences("UserInformation", MODE_PRIVATE);
            if (request.getRequestHeaders().toString().contains("Referer=https/...../loginpass"))
                runOnUi(1, view, sharedPreferences);
            else if (request.getRequestHeaders().toString().contains("Referer=https/...../login"))
                runOnUi(0, view, sharedPreferences);
        }
    
    
    
                    return super.shouldInterceptRequest(view, request);
                }
            }
        
        }
        
        webView.loadUrl(url);
        ...
    }