1
\$\begingroup\$

I have three types alert dialogue. Each alert dialogue behave differently but I don't like to write three types of alert dialogue in same class because it extends the class size drastically. Here are the three alert dialogue given below:

public boolean onJsAlert(WebView view, String url, String message,
        final android.webkit.JsResult result) {
    new AlertDialog.Builder(currentActivity)
            .setTitle("Alert !")
            .setMessage(message)
            .setPositiveButton(android.R.string.ok,
                    new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            result.confirm();
                        }
                    }).setCancelable(false).create().show();

    return true;
}

@Override
public boolean onJsConfirm(WebView view, String url, String message,
        final android.webkit.JsResult result) {
    new AlertDialog.Builder(currentActivity)
            .setTitle("Confirmation")
            .setMessage(message)
            .setPositiveButton(android.R.string.yes,
                    new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            result.confirm();
                        }
                    })
            .setNegativeButton(android.R.string.no,
                    new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            result.cancel();
                        }
                    }).setCancelable(false).create().show();

    return true;
}

And the last alert dialogue is:

public void onGeolocationPermissionsShowPrompt(final String origin,
        final GeolocationPermissions.Callback callback) {

    final boolean remember = true;

    AlertDialog.Builder builder = new AlertDialog.Builder(
            WebviewActivity.this);
    builder.setTitle("Locations");
    builder.setMessage(" Would like to use your Current Location")
            .setCancelable(true)
            .setPositiveButton("Allow",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int id) {
                            callback.invoke(origin, true, remember);

                            SharedPreferences pref = currentActivity
                                    .getPreferences(currentActivity.MODE_PRIVATE);
                            SharedPreferences.Editor editor = pref
                                    .edit();
                            editor.putBoolean("isLocationAvailable",
                                    true);
                            editor.commit();

                            webview.loadUrl(getUrl(gps.getLatitude(),
                                    gps.getLongitude(), "true"));
                        }
                    })
            .setNegativeButton("Don't Allow",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int id) {
                            callback.invoke(origin, false, remember);
                            webview.loadUrl(getUrl("", "", "false"));

                        }
                    });
    AlertDialog alert = builder.create();
    alert.show();
}

How can I reduce the code size. Any suggestion please?

\$\endgroup\$
1
\$\begingroup\$

I also like to keep classes short, but I don't think it should influence your design. You can put the alert building code in another class file if you really want to.

I would not try to coerce the geolocation alert with onJsAlert/onJsConfirm since it is quite different. But you could do something if you really wanted to reuse some code for those two (onJsAlert/onJsConfirm). The positive OnClickListener could be defined outside and shared for onJsAlert/onJsConfirm. You could instead write a common method for building those two alert dialogs, with an if-statement that checks if a negative button should be added or not.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.