/*
 * Copyright 2011 Azwan Adli Abdullah
 *
 * 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.gh4a;

import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.WebSettings;
import android.webkit.WebView;

/**
 * The DiffViewer activity.
 */
public class DiffViewerActivity extends BaseActivity {

    /** The user login. */
    protected String mUserLogin;

    /** The repo name. */
    protected String mRepoName;

    /** The sha. */
    protected String mSha;

    /** The diff. */
    protected String mDiff;

    /** The filename. */
    protected String mFilePath;
    
    /** The tree sha. */
    protected String mTreeSha;

    /**
     * Called when the activity is first created.
     * 
     * @param savedInstanceState the saved instance state
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.web_viewer);
        setUpActionBar();

        Bundle data = getIntent().getExtras();
        mUserLogin = data.getString(Constants.Repository.REPO_OWNER);
        mRepoName = data.getString(Constants.Repository.REPO_NAME);
        mSha = data.getString(Constants.Object.OBJECT_SHA);
        mTreeSha = data.getString(Constants.Object.TREE_SHA);
        mDiff = data.getString(Constants.Commit.DIFF);
        mFilePath = data.getString(Constants.Object.PATH);

        WebView diffView = (WebView) findViewById(R.id.web_view);
        String formatted = highlightSyntax();
        WebSettings s = diffView.getSettings();
        s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        s.setBuiltInZoomControls(true);
        s.setLightTouchEnabled(true);
        s.setLoadsImagesAutomatically(true);
        s.setPluginsEnabled(false);
        s.setSupportZoom(true);
        s.setUseWideViewPort(true);
        diffView.loadDataWithBaseURL("file:///android_asset/", formatted, "text/html", "utf-8", "");
    }

    /**
     * Highlight syntax.
     * 
     * @return the string
     */
    private String highlightSyntax() {
        StringBuilder content = new StringBuilder();
        content.append("<html><head><title></title>");
        content.append("</head><body><pre>");

        String encoded = TextUtils.htmlEncode(mDiff);
        String[] lines = encoded.split("\n");
        for (String line : lines) {
            if (line.startsWith("@@")) {
                line = "<div style=\"background-color: #EAF2F5;\">" + line + "</div>";
            }
            else if (line.startsWith("+")) {
                line = "<div style=\"background-color: #DDFFDD; border-color: #00AA00;\">" + line
                        + "</div>";
            }
            else if (line.startsWith("-")) {
                line = "<div style=\"background-color: #FFDDDD; border-color: #CC0000;\">" + line
                        + "</div>";
            }
            else {
                line = "<div>" + line + "</div>";
            }
            content.append(line);
        }
        content.append("</pre></body></html>");
        return content.toString();

    }
}

@@ -617,16 +617,61 @@
 .gh4a;%0A%0A
+import com.actionbarsherlock.app.ActionBar;%0A%0A
 import a
@@ -790,44 +790,8 @@
 w;%0A%0A
-/**%0A * The DiffViewer activity.%0A */%0A
 publ
@@ -850,157 +850,76 @@
     
-/** The user login. */%0A    protected String mUserLogin;%0A%0A    /** The repo name. */%0A    protected String mRepoName;%0A%0A    /** The sha. */
+private String mRepoOwner;%0A    private String mRepoName;
 %0A    pr
-otec
+iva
 te
-d
  Str
@@ -931,44 +931,20 @@
 Sha;
-%0A%0A    /** The diff. */
 %0A    pr
-otec
+iva
 te
-d
  Str
@@ -958,47 +958,19 @@
 ff;%0A
-%0A
     
-/** The filename. */%0A    protec
+priva
 te
-d
  Str
@@ -992,197 +992,33 @@
     
-%0A    /** The tree sha. */%0A    protected String mTreeSha;%0A%0A    /**%0A     * Called when the activity is first created.%0A     * %0A     * @param savedInstanceState the saved instance state%0A     */
+private String mTreeSha;%0A
 %0A   
@@ -1175,34 +1175,8 @@
 er);
-%0A        setUpActionBar();
 %0A%0A  
@@ -1233,17 +1233,17 @@
    m
-UserLogin
+RepoOwner
  = d
@@ -1591,24 +1591,237 @@
 ect.PATH);%0A%0A
+        ActionBar actionBar = getSupportActionBar();%0A        actionBar.setTitle(mFilePath);%0A        actionBar.setSubtitle(mRepoOwner + %22/%22 + mRepoName);%0A        actionBar.setDisplayHomeAsUpEnabled(true);%0A        %0A
         WebV
@@ -2373,83 +2373,8 @@
  %7D%0A%0A
-    /**%0A     * Highlight syntax.%0A     * %0A     * @return the string%0A     */%0A
     

Update diff viewer layout