/*
 * Copyright 2008 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.android.apps.mytracks;

import com.google.android.apps.mytracks.fragments.EulaDialogFragment;
import com.google.android.apps.mytracks.fragments.EulaDialogFragment.EulaCaller;
import com.google.android.apps.mytracks.util.ApiAdapterFactory;
import com.google.android.apps.mytracks.util.GoogleFeedbackUtils;
import com.google.android.apps.mytracks.util.SystemUtils;
import com.google.android.maps.mytracks.R;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.util.Locale;

/**
 * An activity that displays the help page.
 * 
 * @author Sandor Dornbush
 */
public class HelpActivity extends AbstractMyTracksActivity implements EulaCaller {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String subtitle = getString(R.string.my_tracks_app_name) + " v"
        + SystemUtils.getMyTracksVersion(this);
    ApiAdapterFactory.getApiAdapter()
        .setTitleAndSubtitle(this, getString(R.string.menu_help), subtitle);

    WebView webView = (WebView) findViewById(R.id.help_webview);
    String language = Locale.getDefault().getLanguage();
    if (language == null || language.equals("")) {
      language = "en";
    }
    webView.loadUrl(getString(R.string.my_tracks_help_url, language));
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient());

    findViewById(R.id.help_feedback).setOnClickListener(new View.OnClickListener() {

        @Override
      public void onClick(View v) {
          GoogleFeedbackUtils.bindFeedback(HelpActivity.this);
      }
    });
  }

  @Override
  protected int getLayoutResId() {
    return R.layout.help;
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.help, menu);
    return super.onCreateOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent;
    switch (item.getItemId()) {
      case R.id.help_play_store:
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id=com.google.android.maps.mytracks"));
        startActivity(intent);
        return true;
      case R.id.help_eula:
        EulaDialogFragment.newInstance(true)
            .show(getSupportFragmentManager(), EulaDialogFragment.EULA_DIALOG_TAG);
        return true;
      case R.id.help_forum:
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(
            "http://groups.google.com/a/googleproductforums.com/forum/#!categories/maps/mytracks/"));
        startActivity(intent);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override
  public void onEulaDone() {
    // Do nothing
  }
}

@@ -1014,16 +1014,56 @@
 cks.R;%0A%0A
+import android.annotation.SuppressLint;%0A
 import a
@@ -1479,16 +1479,76 @@
 ller %7B%0A%0A
+  WebView webView;%0A%0A  @SuppressLint(%22SetJavaScriptEnabled%22)%0A
   @Overr
@@ -1883,24 +1883,16 @@
 );%0A%0A    
-WebView 
 webView 
@@ -1936,16 +1936,162 @@
 bview);%0A
+    webView.getSettings().setJavaScriptEnabled(true);%0A    webView.setWebViewClient(new WebViewClient());%0A%0A    if (savedInstanceState == null) %7B%0A  
     Stri
@@ -2139,24 +2139,26 @@
 uage();%0A    
+  
 if (language
@@ -2198,16 +2198,18 @@
 %7B%0A      
+  
 language
@@ -2221,22 +2221,26 @@
 n%22;%0A    
+  
 %7D%0A    
+ 
+ 
 webView.
@@ -2306,108 +2306,70 @@
     
-webView.getSettings().setJavaScriptEnabled(true);%0A    webView.setWebViewClient(new WebViewClient());
+%7D else %7B%0A      webView.restoreState(savedInstanceState);%0A    %7D
 %0A%0A  
@@ -2514,18 +2514,16 @@
         
-  
 GoogleFe
@@ -2580,32 +2580,214 @@
  %7D%0A    %7D);%0A  %7D%0A%0A
+  @Override%0A  protected void onSaveInstanceState(Bundle outState) %7B%0A    super.onSaveInstanceState(outState);%0A    if (webView != null) %7B%0A      webView.saveState(outState);%0A    %7D%0A  %7D%0A%0A
   @Override%0A  pr

Handle screen rotation in help activity