Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Get screen resolution

Alex Dorohin
1 decade ago Sep 15, 2012 7:18am

I do so

WebApplicationContext context = ((WebApplicationContext) this.getContext());
WebBrowser webBrowser = context.getBrowser();

result
webBrowser.getAddress() - ok
webBrowser.getBrowserApplication() - ok
webBrowser.getScreenHeight() - only 0
webBrowser.getScreenWidth() - only 0

Last updated on Sep, 15th 2012
Jens Jansson
1 decade ago Sep 15, 2012 8:04am

Do you do this in application init? I know that some of the information are not avaiable at that point.

Alex Dorohin
1 decade ago Sep 15, 2012 8:34am
public class MCS extends com.vaadin.Application {

    final Window main = new Window("");

    @Override
    public void init () {
        if (this.ipFilter()) {
            this.start();
        } else {
            this.ban();
        }
    }

 public void start () {
        setMainWindow(main);
}

private boolean ipFilter () {

        WebApplicationContext context = ((WebApplicationContext) this.getContext());
        WebBrowser webBrowser = context.getBrowser();

        String[] ip_access = {"127.0.0.1",
                             "192.168.1.100",
                             "192.168.1.101",
                             "192.168.1.102"};

        return filter(webBrowser.getAddress(), ip_access);
    }

    private boolean filter (String title, String[] patterns) {
        try {
            for (String s : patterns) {
                if (-1 != title.indexOf(s)) {
                    return true;
                }
            }
        } catch (NullPointerException e) {
        }
        return false;
    }
Last updated on Sep, 15th 2012
Alex Dorohin
1 decade ago Sep 15, 2012 12:46pm

Well, how do I get the screen resolution?

Last updated on Sep, 15th 2012
Jens Jansson
1 decade ago Sep 15, 2012 2:09pm

Delay it until after init()

Alex Dorohin
1 decade ago Sep 15, 2012 4:31pm

can example?
something is not working

Deleted User
1 decade ago Sep 15, 2012 5:03pm

This is how i do

1) let you Application implements HttpServletRequestListener
2)
// use this method to persist user details (log or db?)
public void onRequestStart(HttpServletRequest request, HttpServletResponse response)
{
if (getContext() == null)
return;
if (getMainWindow().getBrowserWindowWidth() <=0)
return;
WebBrowser browser = ((com.vaadin.terminal.gwt.server.WebApplicationContext)getContext()).getBrowser();
if (!isPrinted) {
printDetails(browser);
isPrinted = true;
}
if (browser.getScreenHeight() == screenHeight || browser.getScreenWidth() == screenWidth)
return;
screenHeight = browser.getScreenHeight();
screenWidth = browser.getScreenWidth();
logger.info("********* Client Screen is " + browser.getScreenWidth() + " x " + browser.getScreenHeight() + " *********");
}
3)
private void printDetails(WebBrowser browser) {
String browserName = browser.isIE() ? "Internet Explorer" : browser.isFirefox() ? "FireFox" : browser.isChrome() ? "Chrome" : browser.isSafari() ? "Safari" : browser.isOpera() ? "Opera" : "Unknown";
String version = browser.getBrowserMajorVersion() + "." + browser.getBrowserMinorVersion();
String os = browser.isWindows() ? "Windows" : browser.isLinux() ? "Linux" : browser.isMacOSX() ? "MacOS" : "Unknown";
String ipAddress = browser.getAddress();
logger.info( String.format("\nBrowser is %s - %s \nOS is %s\nIP is %s\nCurrent Date is %s\nScreen Resolution is %s\nIs Touch enabled? %s\nLocale - %s\nTimezone offset - %s DaylightSaving - %s. DST is %s", browserName, version, os, ipAddress, browser.getCurrentDate(), (browser.getScreenWidth() + " x " + browser.getScreenHeight()), browser.isTouchDevice(), browser.getLocale(),getHourMinute(browser.getRawTimezoneOffset()), getHourMinute(browser.getDSTSavings()), (!browser.isDSTInEffect() ? "NOT " : "") + "in effect"));
System.out.println("Browser is " + browserName + " - " + version);
System.out.println("OS is " + os);
System.out.println("IP is " + ipAddress);
System.out.println("Current Date is " + browser.getCurrentDate());
System.out.println("Screen Resolution is - " + browser.getScreenWidth() + " x " + browser.getScreenHeight() );
System.out.println("Client Area is - " + getMainWindow().getBrowserWindowWidth() + " x " + getMainWindow().getBrowserWindowHeight() );
System.out.println("is Touch enabled Device? " + browser.isTouchDevice());
System.out.println("Locale - " + browser.getLocale());
System.out.println("Timezone Offset " + getHourMinute(browser.getRawTimezoneOffset()) + " DayLightSaving " + getHourMinute(browser.getDSTSavings()) + ". DST is " + (!browser.isDSTInEffect() ? "NOT " : "") + "in effect");
}

Last updated on Sep, 15th 2012
Alex Dorohin
1 decade ago Sep 15, 2012 5:30pm