title: Kindle Fire and Youtube Links in WebView
body: <p>We have several apps in the Android Market that we also submitted to Amazon to their Appstore for Android.  Things worked well until the Kindle Fire came out.  In several of our apps, we launch Youtube videos from a link on a WebView.  Here is the relevant code from setOnClickListener that pushes the view and handles the Youtube links:</p>
  
  <pre><code>mWebView.setWebViewClient (new WebViewClient () {
       @ Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
          if (url.startsWith("vnd.youtube")){  
              startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
              return true;
           } else{
              return false;
           }
       }
  });
  </code></pre>
  
  <p>As I said, this works great on all Android devices that we've tested on, but we are getting notified from Amazon that they are getting crashes when clicking on the video links.  I assume this is happening because the Fire does not come loaded with an YouTube app.  Has anyone else experienced similar issues?  What would be the prefered method for playing a youtube video from a webview on the Fire?  </p>
  
  <p>Here is the logcat supplied by Amazon:</p>
  
  <pre><code>01-09 12:28:32.715 W/dalvikvm(5805): threadid=1: thread exiting with uncaught exception (group=0x40015560)
  01-09 12:28:32.715 D/ActivityManager(1372): Starting: Intent { act=android.intent.action.VIEW dat=vnd.youtube://XXXXXXXXXX } from pid 5805
  01-09 12:28:32.793 E/AndroidRuntime(5805): FATAL EXCEPTION: main
  01-09 12:28:32.793 E/AndroidRuntime(5805): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=vnd.youtube://XXXXXXXXXX }
  01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409)
  01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
  01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.app.Activity.startActivityFromChild(Activity.java:3103)
  01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.app.Activity.startActivityForResult(Activity.java:2883)
  01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.app.Activity.startActivity(Activity.java:2969)
  01-09 12:28:32.793 E/AndroidRuntime(5805):  at com.XXXXXXXXXX.XXXXXXXXXX.VideoActivity$1$1.shouldOverrideUrlLoading(VideoActivity.java)
  01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:216)
  01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:323)
  01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.os.Handler.dispatchMessage(Handler.java:99)
  01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.os.Looper.loop(Looper.java:130)
  01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.app.ActivityThread.main(ActivityThread.java:3683)
  01-09 12:28:32.793 E/AndroidRuntime(5805):  at java.lang.reflect.Method.invokeNative(Native Method)
  01-09 12:28:32.793 E/AndroidRuntime(5805):  at java.lang.reflect.Method.invoke(Method.java:507)
  01-09 12:28:32.793 E/AndroidRuntime(5805):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:850)
  01-09 12:28:32.793 E/AndroidRuntime(5805):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
  01-09 12:28:32.793 E/AndroidRuntime(5805):  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <hr>
  
  <p><strong>Edit</strong></p>
  
  <p>I was able to get this working properly now.  Basically, I copied my video folder (which contains all of the html for the video pages) in assets to a new video_fire directory.  I updated the actual code to point to the video_fire directory if the android.os.Build.MODEL is "Kindle Fire" and to the normal video directory for all else. I also modified the webview allocation to use getParent() rather than VideoActivity.this.  This fixed the issue I described in the comments about full screen displays. Below is the updated code that I used (when clicking on an item in a ListView it displays the appropriate html page):</p>
  
  <pre><code>public void onItemClick(AdapterView&lt;?&gt; a, View v, int position, long id) {
      String contentsOfHtml;
      try {
          if (android.os.Build.MODEL.equals("Kindle Fire")) {
              contentsOfHtml = readFileAsString("video_fire/"+htmlarray[position]);
          } else {
              contentsOfHtml = readFileAsString("video/"+htmlarray[position]);
          }
      } catch (IOException e) { 
          // Should never happen! 
          throw new RuntimeException(e); 
      } 
      WebView mWebView =  new WebView(getParent());
      WebSettings webSettings = mWebView.getSettings(); 
      webSettings.setPluginsEnabled(true);
      webSettings.setJavaScriptEnabled(true);
      mWebView.setWebViewClient (new WebViewClient () {
          @ Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
          if (url.startsWith("vnd.youtube")){  
              startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
              return true;
           } else{
              return false;
           }
          }
      });
  mWebView.loadDataWithBaseURL("file:///android_asset/", contentsOfHtml, "text/html", "utf-8", "");
  ReflectionGroup.group.replaceView(mWebView);
  return;
  });
  </code></pre>
  
  <p>Yes, I'm using ActivityGroup which is depreciated (this is old code).  I'm working on updating the base code that this uses to implement Fragments...</p>
  
  <p>I then have html for normal devices:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;link href="style.css" rel="stylesheet" type="text/css"&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;p class="title"&gt;Title&lt;/p&gt;
      &lt;hr/&gt;
      &lt;a href="vnd.youtube://XXXXX"&gt;&lt;div&gt;&lt;center&gt;&lt;img src="images/video1.jpg" width="220" height="150"&gt;&lt;/center&gt;&lt;/div&gt;&lt;/a&gt;
      &lt;/center&gt;
      &lt;p class="para"&gt;Text&lt;/p&gt;
      &lt;hr/&gt;       
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And specific HTML for Kindle Fire.  I suppose I should check to see if this code would work on other devices, too, but that will have to be put on my to-do list.</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;link href="style.css" rel="stylesheet" type="text/css"&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;p class="title"&gt;Title&lt;/p&gt;
      &lt;hr/&gt;
  &lt;iframe class="youtube-player" type="text/html" width="600" height="361" src="http://www.youtube.com/embed/XXXXXXXX" frameborder="0"&gt;
  &lt;/iframe&gt;
      &lt;/center&gt;
      &lt;p class="para"&gt;Text    &lt;/p&gt;
      &lt;hr/&gt;       
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>