際際滷

際際滷Share a Scribd company logo
PORTING WEB MMO GAMES
TO NATIVE PLATFORMS.
WORKING WITH WEBVIEW



                     Vitaliy Zasadnyy
             Android TechTalk, Feb 9
PREHISTORY




             7 games
OUR GAMES
TASK

¢ 2 platforms


¢ provide custom layout
¢ show loading screen
¢ replace some features with native analogs
WebView API
Loading Content

void loadUrl(String url)

void loadData(String data, String mimeType,
   String mimeType)

void postUrl(String url, byte[] postData)

void reload()
WebView API
Setting the Delegate

void setWebViewClient(WebViewClient client)

void setWebChromeClient(WebChromeClient client)
WebViewClient API
Loading Content Callbacks

void onPageStarted(WebView view, String url, Bitmap
   favicon)

void onPageFinished(WebView view, String url)

void onReceivedError(WebView view, int errorCode,
   String description, String failingUrl)

boolean shouldOverrideUrlLoading(WebView view,
   String url)
WebChromeClient API
Event Callbacks

boolean onJs...()

void onProgressChanged(WebView view, int
   newProgress)
T1: CUSTOM LAYOUT




       web          android
T1: CUSTOM LAYOUT




        first idea: custom user-agent
T1: CUSTOM LAYOUT
     better idea: custom headers

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

     Map<String, String> extraHeaders =
            new HashMap<String, String>();
     extraHeaders.put(
            EXTRA_HEADER_CLIENT_TYPE, EXTRA_HEADER_VALUE));

     view.loadUrl(url, extraHeaders);
                                               load with extra header
                            cancel request
     return true;
}
T1: CUSTOM LAYOUT
problem 1:    yourWebWiew.loadUrl("some url");



solution 1:
              public class YourWebView extends WebView {
                      ...
                  @Override
                  public void loadUrl(String url) {
                      // load url with extra headers
                  }
                      ...
              }
T1: CUSTOM LAYOUT
problem 2:    yourWebWiew.reload();



solution 2:
              public class YourWebView extends WebView {
                      ...
                  @Override                 get current url
                  public void reload() {
                      String url = getUrl();
                      // load url with extra headers
                  }
                      ...


                                                he b est
              }


                                       not    t
T1: CUSTOM LAYOUT
problem 3:    post methods (e.g. forms)


solution 3:   there is no solution



                     AOSP: Issue 9122
T2: LOADING SCREEN

                     ¢   starts at game launch

                     ¢   disappears only when
                         first page is loaded
T2: LOADING SCREEN

public class YourWebViewClient extends WebViewClient {
    ...
    @Override
    public void onPageStarted(WebView view, String url,
Bitmap
       favicon) {
         // show loading dialog
    }

     @Override
     public void onPageFinished(WebView view, String url) {
         // hide loading dialog
     }


                                                  lem
     ...


                                               rob
}

                                             p
T2: LOADING SCREEN

public class YourWebViewClient extends WebViewClient {
    private String mStartUrl;
    ...
    @Override
    public void onPageStarted(WebView view, String url, Bitmap
      favicon) {
        mStartUrl = url;
        // show fullscreen loading dialog
    }

     @Override
     public void onPageFinished(WebView view, String url) {
         if (mStartUrl.equals(url))
              // hide loading dialog
         else
              // redirect!
     }


                                                          ixed
     ...

                                                        f
}
T3: REPLACE SOME FEATURES
public class NravoWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view,
       String url) {
        // override behavior here
    }

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

       @Override
       public void onPageFinished(WebView view, String url) {
           if (!mIsRedirect) {
               view.loadUrl("javascript: { alert("Horray!");
}");
          }
     }
}
IF YOU'VE GOT INTERESTED

             NRavo games on:
THANKS FOR YOUR ATTENTION!
        QUESTIONS?




            presentation will be available on:
                       blog.zasadnyy.org.ua

More Related Content

Porting web mmo games to native platforms

  • 1. PORTING WEB MMO GAMES TO NATIVE PLATFORMS. WORKING WITH WEBVIEW Vitaliy Zasadnyy Android TechTalk, Feb 9
  • 2. PREHISTORY 7 games
  • 4. TASK ¢ 2 platforms ¢ provide custom layout ¢ show loading screen ¢ replace some features with native analogs
  • 5. WebView API Loading Content void loadUrl(String url) void loadData(String data, String mimeType, String mimeType) void postUrl(String url, byte[] postData) void reload()
  • 6. WebView API Setting the Delegate void setWebViewClient(WebViewClient client) void setWebChromeClient(WebChromeClient client)
  • 7. WebViewClient API Loading Content Callbacks void onPageStarted(WebView view, String url, Bitmap favicon) void onPageFinished(WebView view, String url) void onReceivedError(WebView view, int errorCode, String description, String failingUrl) boolean shouldOverrideUrlLoading(WebView view, String url)
  • 8. WebChromeClient API Event Callbacks boolean onJs...() void onProgressChanged(WebView view, int newProgress)
  • 9. T1: CUSTOM LAYOUT web android
  • 10. T1: CUSTOM LAYOUT first idea: custom user-agent
  • 11. T1: CUSTOM LAYOUT better idea: custom headers @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Map<String, String> extraHeaders = new HashMap<String, String>(); extraHeaders.put( EXTRA_HEADER_CLIENT_TYPE, EXTRA_HEADER_VALUE)); view.loadUrl(url, extraHeaders); load with extra header cancel request return true; }
  • 12. T1: CUSTOM LAYOUT problem 1: yourWebWiew.loadUrl("some url"); solution 1: public class YourWebView extends WebView { ... @Override public void loadUrl(String url) { // load url with extra headers } ... }
  • 13. T1: CUSTOM LAYOUT problem 2: yourWebWiew.reload(); solution 2: public class YourWebView extends WebView { ... @Override get current url public void reload() { String url = getUrl(); // load url with extra headers } ... he b est } not t
  • 14. T1: CUSTOM LAYOUT problem 3: post methods (e.g. forms) solution 3: there is no solution AOSP: Issue 9122
  • 15. T2: LOADING SCREEN ¢ starts at game launch ¢ disappears only when first page is loaded
  • 16. T2: LOADING SCREEN public class YourWebViewClient extends WebViewClient { ... @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { // show loading dialog } @Override public void onPageFinished(WebView view, String url) { // hide loading dialog } lem ... rob } p
  • 17. T2: LOADING SCREEN public class YourWebViewClient extends WebViewClient { private String mStartUrl; ... @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { mStartUrl = url; // show fullscreen loading dialog } @Override public void onPageFinished(WebView view, String url) { if (mStartUrl.equals(url)) // hide loading dialog else // redirect! } ixed ... f }
  • 18. T3: REPLACE SOME FEATURES public class NravoWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // override behavior here } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) {} @Override public void onPageFinished(WebView view, String url) { if (!mIsRedirect) { view.loadUrl("javascript: { alert("Horray!"); }"); } } }
  • 19. IF YOU'VE GOT INTERESTED NRavo games on:
  • 20. THANKS FOR YOUR ATTENTION! QUESTIONS? presentation will be available on: blog.zasadnyy.org.ua