Commits

Juha Kuitunen committed 0bfc581

view images directly from DiffActivity

Comments (0)

Files changed (4)

res/layout/diff.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
-  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/diff_webview"></WebView>
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical"
+    android:background="#ffffff">
+
+    <TextView
+        android:id="@+id/diff_imageinfo"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:visibility="gone"
+    />
+
+    <ImageView
+        android:id="@+id/diff_image"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:visibility="gone"
+    />
+
+    <WebView
+        android:id="@+id/diff_webview"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent">
+    </WebView>
+
 </LinearLayout>

src/com/saibotd/bitbeaker/DiffActivity.java

 import android.annotation.SuppressLint;
 import android.os.Bundle;
 import android.text.TextUtils;
+import android.view.View;
 import android.webkit.WebChromeClient;
 import android.webkit.WebView;
+import android.widget.ImageView;
+import android.widget.TextView;
 
 import com.actionbarsherlock.view.Menu;
 
 	private String file;
 	private String code;
 	private String changeset_id;
+	private WebView webView;
+	private ImageView diff_image;
+	private boolean showImage = false;
 
 	public void onCreate(Bundle savedInstanceState) {
 		super.onCreate(savedInstanceState);
 
 		AsyncLoader asyncLoader = new AsyncLoader();
 		/*
-		 * TODO: authenticate to get diff for own private repositories
-		 * NOTE: Bitbucket doesn't provide an API call for fetching the diffs. This kinda sucks as we need to implement
-		 * oAuth at some point in the future ... */
+		 * Currently we get raw diff for whole changeset at same time. First file
+		 * will be displayed slowly but the rest are fast due to cache
+		 */
 		asyncLoader.execute("https://bitbucket.org/"+ owner +"/"+slug+"/changeset/"+changeset_id+"/raw/");
-        setTitle(slug + ": " + changeset_id);
-        getSupportActionBar().setSubtitle(file);
+		setTitle(slug + ": " + changeset_id);
+		getSupportActionBar().setSubtitle(file);
+
+		webView = (WebView) findViewById(R.id.diff_webview);
+		diff_image = (ImageView) findViewById(R.id.diff_image);
+
+		if (isImage(file)) {
+			TextView imageinfo = (TextView) findViewById(R.id.diff_imageinfo);
+			imageinfo.setVisibility(View.VISIBLE);
+			imageinfo.setText(file+" is an image. Click here to toggle between diff and image.");
+			imageinfo.setOnClickListener(new View.OnClickListener() {
+				public void onClick(View v) {
+					if (diff_image.getDrawable()==null)
+						new AsyncImageLoader(diff_image).execute("https://bitbucket.org/"+owner+"/"+slug+"/raw/"+changeset_id+"/"+encode(file));
+					showImage = !showImage;
+					if (showImage) {
+						webView.setVisibility(View.GONE);
+						diff_image.setVisibility(View.VISIBLE);
+					}
+					else {
+						webView.setVisibility(View.VISIBLE);
+						diff_image.setVisibility(View.GONE);
+					}
+				}
+			});
+		}
+
 	}
-	
+
 	 @Override
 	public boolean onCreateOptionsMenu(Menu menu) {
 		return false;
 		result = splitDiff(result);
 		if (result.equals("")) result = "Diff not found for requested file.";
 		code = TextUtils.htmlEncode(result.replace("\t", "  "));
-		WebView webView = (WebView) findViewById(R.id.diff_webview);
 		webView.getSettings().setBuiltInZoomControls(true);
 		webView.getSettings().setUseWideViewPort(true);
 		webView.getSettings().setJavaScriptEnabled(true);

src/com/saibotd/bitbeaker/MyActivity.java

 		}
 
 		@Override
+		protected void onPreExecute() {
+			try {
+				setSupportProgressBarIndeterminateVisibility(true);
+			} catch (Exception e){ }
+		}
+
+		@Override
 		protected Bitmap doInBackground(String... parameters) {
 			Bitmap b = null;
 			try {
 				authorize(connection);
 
 				b = BitmapFactory.decodeStream(connection.getInputStream());
-				//b = ImageThreadLoader.readBitmapFromNetwork(new URL(parameters[0]));
 			} catch (Exception e) {
 				e.printStackTrace();
 				this.cancel(true);
 
 		@Override
 		protected void onPostExecute(Bitmap b) {
-			if(mImageView.isShown()){
+			if (mImageView.isShown()) {
 				mImageView.setImageBitmap(b);
 			}
-			try{
+			try {
 				setSupportProgressBarIndeterminateVisibility(false);
-			} catch(Exception e){  }
+			} catch (Exception e){ }
 		}
 
 		public void onCancel(DialogInterface arg0) {
 	}
 
 	/**
+	 * Check if file is an image based on it's extension.
+	 * 
+	 * @see http://developer.android.com/guide/appendix/media-formats.html
+	 */
+	public boolean isImage(String filename) {
+		boolean result = false;
+		if (filename.toLowerCase().endsWith(".png") ||
+			filename.toLowerCase().endsWith(".jpg")	||
+			filename.toLowerCase().endsWith(".bmp")	||
+			filename.toLowerCase().endsWith(".gif") ) {
+			result = true;
+		}
+		return result;
+	}
+
+	/**
 	 * Add authorization header to connection
 	 */
 	private void authorize(HttpsURLConnection conn) {
-		
 		if (!bitbeaker.getUsername().equals("")) {
 			String loginString = bitbeaker.getUsername() + ":"
 					+ bitbeaker.getPassword();

src/com/saibotd/bitbeaker/SourceActivity.java

 		slug = b.getString("slug");
 		owner = b.getString("owner");
 		file = b.getString("file");
+		ActionBar actionBar = getSupportActionBar();
+		setTitle(slug + " ("+branch+")");
+		actionBar.setSubtitle(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
 		}
 	}
 
-	private boolean isImage(String input) {
-		boolean result = false;
-		if (file.toLowerCase().endsWith(".png") ||
-			file.toLowerCase().endsWith(".jpg")	||
-			file.toLowerCase().endsWith(".gif") ) {
-			result = true;
-		}
-		return result;
-	}
-
 }