Commits

Juha Kuitunen  committed fd10b59

view images even for private repositories is working now via source browser

  • Participants
  • Parent commits 4fb3749

Comments (0)

Files changed (5)

File res/layout/new_issue_comment.xml

 <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
+
   <ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="wrap_content">
+
       <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content">
+
           <TextView android:text="@string/content" android:autoLink="all" android:layout_margin="5dip" android:layout_height="wrap_content" android:id="@+id/TextView02" android:layout_width="match_parent" android:linksClickable="true"></TextView>
 
           <EditText
               android:layout_height="wrap_content"
               android:inputType="textMultiLine"
               android:minLines="3" >
-</EditText>
+          </EditText>
+
       </LinearLayout>
+
   </ScrollView>
-  
+
 </LinearLayout>

File res/layout/source.xml

   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent" android:orientation="vertical">
-    <WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/source_webview"></WebView>
+    <ImageView android:id="@+id/source_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" />
+    <WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/source_webview" android:visibility="gone"></WebView>
 </LinearLayout>

File src/com/saibotd/bitbeaker/AsyncImageLoader.java

-package com.saibotd.bitbeaker;
-
-import java.net.URL;
-import java.net.URLConnection;
-
-import android.content.DialogInterface;
-import android.content.DialogInterface.OnCancelListener;
-import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
-import android.os.AsyncTask;
-import android.widget.ImageView;
-
-public class AsyncImageLoader extends AsyncTask<String, Integer, Bitmap> implements OnCancelListener {
-	private final ImageView mImageView;
-
-	public AsyncImageLoader(ImageView imageView) {
-		super();
-		mImageView = imageView;
-	}
-
-	@Override
-	protected Bitmap doInBackground(String... parameters) {
-		Bitmap b = null;
-		try {
-			BitmapFactory.Options options=new BitmapFactory.Options();
-			options.inSampleSize = 8;
-			URL url = new URL(parameters[0]);
-			URLConnection connection = url.openConnection();
-			connection.setUseCaches(true);
-			b = BitmapFactory.decodeStream(url.openStream());
-			//b = ImageThreadLoader.readBitmapFromNetwork(new URL(parameters[0]));
-		} catch (Exception e) {
-			e.printStackTrace();
-			this.cancel(true);
-		}
-		return b;
-	}
-
-	@Override
-	protected void onPostExecute(Bitmap b) {
-		if(mImageView.isShown()){
-			mImageView.setImageBitmap(b);
-		}
-	}
-
-	public void onCancel(DialogInterface arg0) {
-		this.cancel(true);
-	}
-}

File src/com/saibotd/bitbeaker/MyActivity.java

 
 import android.app.AlertDialog;
 import android.app.ProgressDialog;
+import android.content.DialogInterface;
+import android.content.DialogInterface.OnCancelListener;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.util.Base64;
 import android.util.Log;
+import android.widget.ImageView;
 
 import com.actionbarsherlock.app.SherlockActivity;
 import com.actionbarsherlock.view.MenuItem;
 		}
 		return "";
 	}
-    
-    public String encode(String in){
-    	try {
-    		in = URLEncoder.encode(in, "UTF-8");
-    		if(in.contains("%2F")) in = in.replaceAll("\\%2F", "/");
-    		if(in.contains("+")) in = in.replaceAll("\\+", "%20");
-    		return in;
+
+	public String encode(String in){
+		try {
+			in = URLEncoder.encode(in, "UTF-8");
+			if(in.contains("%2F")) in = in.replaceAll("\\%2F", "/");
+			if(in.contains("+")) in = in.replaceAll("\\+", "%20");
+			return in;
 		} catch (UnsupportedEncodingException e) { e.printStackTrace(); }
-    	return "";
-    }
+		return "";
+	}
+
+	protected class AsyncImageLoader extends AsyncTask<String, Integer, Bitmap> implements OnCancelListener {
+		private final ImageView mImageView;
+
+		public AsyncImageLoader(ImageView imageView) {
+			super();
+			mImageView = imageView;
+		}
+
+		@Override
+		protected Bitmap doInBackground(String... parameters) {
+			Bitmap b = null;
+			try {
+				BitmapFactory.Options options=new BitmapFactory.Options();
+				options.inSampleSize = 8;
+
+				URL url = new URL(parameters[0]);
+				HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
+				connection.setUseCaches(true);
+
+				if (!bitbeaker.getUsername().equals("")) {
+					String loginString = bitbeaker.getUsername() + ":"
+							+ bitbeaker.getPassword();
+					connection.setRequestProperty(
+							"Authorization",
+							"Basic "
+									+ Base64.encodeToString(loginString.getBytes(),
+											Base64.DEFAULT).trim());
+					Log.d("AUTH", "True");
+				}
+
+				b = BitmapFactory.decodeStream(connection.getInputStream());
+				//b = ImageThreadLoader.readBitmapFromNetwork(new URL(parameters[0]));
+			} catch (Exception e) {
+				e.printStackTrace();
+				this.cancel(true);
+			}
+			return b;
+		}
+
+		@Override
+		protected void onPostExecute(Bitmap b) {
+			if(mImageView.isShown()){
+				mImageView.setImageBitmap(b);
+			}
+			try{
+				setSupportProgressBarIndeterminateVisibility(false);
+			} catch(Exception e){  }
+		}
+
+		public void onCancel(DialogInterface arg0) {
+			this.cancel(true);
+		}
+	}
 
 }

File src/com/saibotd/bitbeaker/SourceActivity.java

 import android.annotation.SuppressLint;
 import android.os.Bundle;
 import android.text.TextUtils;
-import android.util.Log;
-import android.webkit.JsResult;
+import android.view.View;
 import android.webkit.WebChromeClient;
 import android.webkit.WebView;
+import android.widget.ImageView;
 
 import com.actionbarsherlock.app.ActionBar;
 import com.actionbarsherlock.view.Menu;
 	private String file;
 	private String code;
 
-    public void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        setContentView(R.layout.source);
-        Bundle b = getIntent().getExtras();
-        branch = b.getString("branch");
-        slug = b.getString("slug");
-        owner = b.getString("owner");
-        file = b.getString("file");
-        if (isImage(file)) showImage("https://bitbucket.org/" + owner + "/" + slug + "/raw/" + branch + encode(file));
-        else new AsyncLoader().execute("https://api.bitbucket.org/1.0/repositories/" + owner + "/" + slug + "/raw/" + branch + encode(file));
-        ActionBar actionBar = getSupportActionBar();
-        setTitle(slug + " ("+branch+")");
-        actionBar.setSubtitle(file);
-    }
+	public void onCreate(Bundle savedInstanceState) {
+		super.onCreate(savedInstanceState);
+		setContentView(R.layout.source);
+		Bundle b = getIntent().getExtras();
+		branch = b.getString("branch");
+		slug = b.getString("slug");
+		owner = b.getString("owner");
+		file = b.getString("file");
+		if (isImage(file)) {
+			ImageView imageView = (ImageView) findViewById(R.id.source_image);
+			imageView.setVisibility(View.VISIBLE);
+			new AsyncImageLoader(imageView).execute("https://bitbucket.org/" + owner + "/" + slug + "/raw/" + branch + encode(file));
+		}
+		else new AsyncLoader().execute("https://api.bitbucket.org/1.0/repositories/" + owner + "/" + slug + "/raw/" + branch + encode(file));
+		ActionBar actionBar = getSupportActionBar();
+		setTitle(slug + " ("+branch+")");
+		actionBar.setSubtitle(file);
+	}
 
 	@Override
 	public boolean onCreateOptionsMenu(Menu menu) {
 		super.AsyncLoaderDone(result);
 		code = TextUtils.htmlEncode(result.replace("\t", "  "));
 		WebView webView = (WebView) findViewById(R.id.source_webview);
+		webView.setVisibility(View.VISIBLE);
 		webView.getSettings().setBuiltInZoomControls(true);
 		webView.getSettings().setUseWideViewPort(true);
 		webView.getSettings().setJavaScriptEnabled(true);
 		webView.loadUrl("file:///android_asset/source.html");
 	}
 
-	@SuppressLint("SetJavaScriptEnabled")
-	public void showImage(String url) {
-		WebView webView = (WebView) findViewById(R.id.source_webview);
-		webView.getSettings().setBuiltInZoomControls(true);
-		webView.getSettings().setUseWideViewPort(true);
-		webView.getSettings().setJavaScriptEnabled(true);
-		webView.setWebChromeClient(new WebChromeClient());
-		webView.loadData("<html><body><p><a href=\"javascript:toggleBgColor()\">Toggle background color</a></p><img src=\""+url+"\" />" +
-				"<script language=\"javascript\">function toggleBgColor() { " +
-				"if (document.body.bgColor==\"#000000\") { document.body.bgColor = \"#ffffff\"; document.body.link = \"#0000ff\"; document.body.vLink = \"#0000ff\"; }" +
-				"else { document.body.bgColor = \"#000000\"; document.body.link = \"#ccccff\"; document.body.vLink = \"#ccccff\"; }" +
-				"}</script></body></html>", "text/html", null);
-		try { setSupportProgressBarIndeterminateVisibility(false); } catch (Exception e) { }
-	}
-
-	final class MyWebChromeClient extends WebChromeClient {
+	/*final class MyWebChromeClient extends WebChromeClient {
 		@Override
 		public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
 			Log.d("JAVASCRIPT", message);
 			result.confirm();
 			return true;
 		}
-	}
+	}*/
 
 	private class JavaScriptInterface{
 		@SuppressWarnings("unused")