FileDocCategorySizeDatePackage
PowerTestActivity.javaAPI DocAndroid 5.1 API8397Thu Mar 12 22:22:42 GMT 2015com.android.browserpowertest

PowerTestActivity

public class PowerTestActivity extends android.app.Activity

Fields Summary
public static final String
LOGTAG
public static final String
PARAM_URL
public static final String
PARAM_TIMEOUT
public static final int
RESULT_TIMEOUT
public static final int
MSG_TIMEOUT
public static final int
MSG_NAVIGATE
public static final String
MSG_NAV_URL
public static final String
MSG_NAV_LOGTIME
private android.webkit.WebView
webView
private SimpleWebViewClient
webViewClient
private SimpleChromeClient
chromeClient
private android.os.Handler
handler
private boolean
timeoutFlag
private boolean
logTime
private boolean
pageDone
private Object
pageDoneLock
private int
pageStartCount
private int
manualDelay
private long
startTime
private long
pageLoadTime
private PageDoneRunner
pageDoneRunner
Constructors Summary
public PowerTestActivity()


      
    
Methods Summary
public android.os.HandlergetHandler()

        return handler;
    
public booleangetPageError()

        return webViewClient.getPageErrorFlag();
    
public longgetPageLoadTime()

        return pageLoadTime;
    
private voidhandleTimeout()

        int progress = webView.getProgress();
        webView.stopLoading();
        Log.v(LOGTAG, "Page timeout triggered, progress = " + progress);
        timeoutFlag = true;
        handler.postDelayed(pageDoneRunner, manualDelay);
    
private booleanisPageDone()

        synchronized (pageDoneLock) {
            return pageDone;
        }
    
private voidnavigate(java.lang.String url, int timeout)

        if(url == null) {
            Log.v(LOGTAG, "URL is null, cancelling...");
            finish();
        }
        webView.stopLoading();
        if(logTime) {
            webView.clearCache(true);
        }
        startTime = System.currentTimeMillis();
        Log.v(LOGTAG, "Navigating to URL: " + url);
        webView.loadUrl(url);

        if(timeout != 0) {
            //set a timer with specified timeout (in ms)
            handler.sendMessageDelayed(handler.obtainMessage(MSG_TIMEOUT),
                    timeout);
        }
    
protected voidonCreate(android.os.Bundle savedInstanceState)

        super.onCreate(savedInstanceState);

        Log.v(LOGTAG, "onCreate, inst=" + Integer.toHexString(hashCode()));

        LinearLayout contentView = new LinearLayout(this);
        contentView.setOrientation(LinearLayout.VERTICAL);
        setContentView(contentView);
        setTitle("Idle");

        webView = new WebView(this);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
        webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);

        webViewClient = new SimpleWebViewClient();
        chromeClient = new SimpleChromeClient();
        webView.setWebViewClient(webViewClient);
        webView.setWebChromeClient(chromeClient);

        contentView.addView(webView, new LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, 0.0f));

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_TIMEOUT:
                        handleTimeout();
                        return;
                    case MSG_NAVIGATE:
                        manualDelay = msg.arg2;
                        navigate(msg.getData().getString(MSG_NAV_URL), msg.arg1);
                        logTime = msg.getData().getBoolean(MSG_NAV_LOGTIME);
                        return;
                }
            }
        };

        pageDoneLock = new Object();
    
protected voidonDestroy()

        super.onDestroy();
        Log.v(LOGTAG, "onDestroy, inst=" + Integer.toHexString(hashCode()));
        webView.clearCache(true);
        webView.destroy();
    
public voidreset()

        synchronized (pageDoneLock) {
            pageDone = false;
        }
        timeoutFlag = false;
        pageStartCount = 0;
        chromeClient.resetJsTimeout();
    
private voidsetPageDone(boolean pageDone)

        synchronized (pageDoneLock) {
            this.pageDone = pageDone;
            pageDoneLock.notifyAll();
        }
    
private final voidvalidateNotAppThread()

        if (Looper.myLooper() == Looper.getMainLooper()) {
            throw new RuntimeException(
                "This method can not be called from the main application thread");
        }
    
public booleanwaitUntilDone()

        validateNotAppThread();
        synchronized (pageDoneLock) {
            while(!isPageDone()) {
                try {
                    pageDoneLock.wait();
                } catch (InterruptedException ie) {
                    //no-op
                }
            }
        }
        return timeoutFlag;