0

i'm using android webview to login to dropbox using oauth.login process works fine in all devices greater than android 4.3.but the sign in button on dropbox login page gets disabled in 4.3 and below,

here's my code

public class OauthFragment extends DialogFragment implements View.OnClickListener { private OauthLayoutBinding mBinding;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogFragmentTheme);
}

/**
 * function to take to next screen to show list of folder to select as root folder for tombox
 */
private void startNextActivity() {
    startActivity(new Intent(getActivity(), DashBoardActivity.class));
    new DbxUserInfo(CommonUtils.getClient(getActivity()), getActivity()).execute();
    getActivity().finish();
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mBinding = DataBindingUtil.inflate(inflater, R.layout.oauth_layout, container, false);
    mBinding.toolbarParent.toolbar.setTitle(getString(R.string.login_with_dropbox));
    mBinding.toolbarParent.toolbar.setNavigationIcon(R.drawable.close_btn);
    mBinding.toolbarParent.toolbar.setNavigationOnClickListener(this);
    loadOauthView();
    return mBinding.getRoot();
}

private void loadOauthView() {
    //Making and loading url for oauth
    final String url = "https://www.dropbox.com/oauth2/authorize?response_type=token&client_id=" + Constants.APP_KEY + "&redirect_uri=http://localhost";
    mBinding.oauthView.setWebViewClient(new WebViewClient());
    mBinding.oauthView.setWebChromeClient(new WebChromeClient());
    setAllSettings(mBinding.oauthView.getSettings());
    mBinding.oauthView.loadUrl(url);
}

private void setAllSettings(WebSettings settings) {
    settings.setJavaScriptEnabled(true);
    settings.setSupportZoom(false);
}

@Override
public void onClick(View view) {
    //called from close button click
    dismiss();
}

private class OauthClient extends WebViewClient {
    private boolean checkForUrl(Uri uri) {
        return uri.getHost().equals("localhost");
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    @TargetApi(Build.VERSION_CODES.N)
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        return handleUrlAction(view, request.getUrl());
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
    }

    /**
     * checks if url matches with redirect url of oauth
     *
     * @param view Webview to load
     * @param url  Url to check or load
     * @return true if url matches with redirect url. false otherwise
     */
    private boolean handleUrlAction(WebView view, Uri url) {
        if (checkForUrl(url)) {
            try {
                String token = Uri.parse(url.toString().replace("/#", "?")).getQueryParameter("access_token");
                if (token != null && !token.isEmpty()) {
                    StorageUtils.storeString(Constants.Bundle.ACCESS_TOKEN, token, getActivity());
                    StorageUtils.storeBoolean(Constants.PREF_SYNC, true, getActivity());
                    StorageUtils.storeString(Constants.PREF_INTERVAL, Constants.DEFAULT_SYNC_INTERVAL, getActivity());
                    view.clearCache(true);
                    startNextActivity();
                } else {
                    dismiss();
                }

            } catch (Exception e) {

            }
        } else {
            view.loadUrl(url.toString());
        }
        return true;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return handleUrlAction(view, Uri.parse(url));
    }
}

}

please help

3

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.