Commits

Tobias Duehr committed d50da06

- Worked around all of the warnings
- Introduced a general encode function based on Kuitsis replace_whitespace and
URLEncoder - Please use this function whenever something has to be URL encoded
- The "Oops" error message is now only shown if there is an actual error and
not if a response is empty
- The "Up" or back function (click on logo) works again

Comments (0)

Files changed (12)

 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
+	<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
+	<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
 	<classpathentry kind="src" path="src"/>
 	<classpathentry kind="src" path="gen"/>
-	<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
-	<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
 	<classpathentry kind="output" path="bin/classes"/>
 </classpath>

src/com/saibotd/bitbeaker/DiffActivity.java

 package com.saibotd.bitbeaker;
 
+import android.annotation.SuppressLint;
 import android.os.Bundle;
 import android.text.TextUtils;
 import android.webkit.WebChromeClient;
 		return false;
 	}
 
+	@SuppressLint("SetJavaScriptEnabled")
 	public void AsyncLoaderDone(String result) {
 		super.AsyncLoaderDone(result);
 		result = splitDiff(result);

src/com/saibotd/bitbeaker/IssuesActivity.java

 package com.saibotd.bitbeaker;
 
-import java.net.URLEncoder;
-
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 			else {
 				if (pos == 0) filter = "";
 				else if (pos == 1) filter = "?status=new&status=open";
-				else filter = "?status="+URLEncoder.encode(mSpinnerAdapter.getItem((int) itemId).toString());
+				else filter = "?status=" + encode(mSpinnerAdapter.getItem((int) itemId).toString());
 				loadIssuesByFilter();
 			}
 			return false;

src/com/saibotd/bitbeaker/MyActivity.java

 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
 import java.net.URL;
+import java.net.URLEncoder;
 
 import javax.net.ssl.HttpsURLConnection;
 
 import android.app.AlertDialog;
 import android.app.ProgressDialog;
-import android.content.DialogInterface;
-import android.content.DialogInterface.OnCancelListener;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.util.Base64;
 import android.util.Log;
 
 import com.actionbarsherlock.app.SherlockActivity;
+import com.actionbarsherlock.view.MenuItem;
 import com.actionbarsherlock.view.Window;
 
 public class MyActivity extends SherlockActivity {
 	protected Bitbeaker bitbeaker;
 	protected ProgressDialog progressDialog;
 	protected AlertDialog alertDialog;
-	protected String postParams;
-	protected Boolean ignoreError;
 
 	public void onCreate(Bundle savedInstanceState) {
 		super.onCreate(savedInstanceState);
 		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
-		postParams = null;
-		ignoreError = false;
 		bitbeaker = (Bitbeaker) getApplication();
 		System.setProperty("http.keepAlive", "false");
 		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 	}
+	
+	@Override
+	public boolean onOptionsItemSelected(MenuItem item) {
+		switch (item.getItemId()) {
+		case android.R.id.home:
+			finish();
+			return true;
+		default:
+			return super.onOptionsItemSelected(item);
+		}
+	}
 
 	protected void AsyncLoaderDone(String result) {
 		Log.d("RESULT", result);
-		if (ignoreError == false && (result == null || result.length() == 0)) {
-			try {
-				alertDialog = new AlertDialog.Builder(this).create();
-				alertDialog.setTitle("Oops.");
-				alertDialog
-						.setMessage("There was a problem retrieving and/or processing data from Bitbucket.org. Checking your data connectivity and trying again might help.");
-				alertDialog.show();
-			} catch (Exception e) {}
-		}
-		ignoreError = false;
 	}
 
-	protected class AsyncLoader extends AsyncTask<String, Integer, String>
-			implements OnCancelListener {
+	protected class AsyncLoader extends AsyncTask<String, Integer, String> {
 
 		@Override
 		protected String doInBackground(String... params) {
-			if (params.length == 2)
-				postParams = params[1];
-			return getAuthHTTPS(params[0]);
+			String postParams = null;
+			if(params.length == 2) postParams = params[1];
+			return getAuthHTTPS(params[0], postParams);
 		}
 		
 		@Override
 
 		@Override
 		protected void onPostExecute(String result) {
-			AsyncLoaderDone(result);
 			try{
 				setSupportProgressBarIndeterminateVisibility(false);
 			} catch(Exception e){  }
-			if (progressDialog != null)
-				progressDialog.dismiss();
-		}
-
-		public void onCancel(DialogInterface arg0) {
-			this.cancel(true);
+			if (progressDialog != null) progressDialog.dismiss();
+			if(result.equals("Fail")){
+				try {
+					alertDialog = new AlertDialog.Builder(MyActivity.this).create();
+					alertDialog.setTitle("Oops.");
+					alertDialog.setMessage("There was a problem retrieving and/or processing data from Bitbucket.org. Checking your data connectivity and trying again might help.");
+					alertDialog.show();
+				} catch (Exception e) {}
+				result = "";
+			}
+			AsyncLoaderDone(result);
 		}
 
 	}
 
-	protected String getAuthHTTPS(String webadress) {
-		Log.d("FETCH", webadress);
-		String cache = bitbeaker.getKV(webadress);
+	protected String getAuthHTTPS(String webAdress, String postParams) {
+		Log.d("FETCH", webAdress);
+		String cache = bitbeaker.getKV(webAdress);
 		if (cache != null)
 			return cache;
 		URL url;
 		InputStream in = null;
 		try {
-			url = new URL(webadress);
+			url = new URL(webAdress);
 			HttpsURLConnection urlConnection = (HttpsURLConnection) url
 					.openConnection();
 			urlConnection.setUseCaches(true);
 			in = new BufferedInputStream(urlConnection.getInputStream());
 		} catch (Exception e) {
 			e.printStackTrace();
+			return "Fail";
 		}
 		String result = convertStreamToString(in);
 		if (!result.trim().equals("") && result.length() < 100000)
-			bitbeaker.setKV(webadress, result);
+			bitbeaker.setKV(webAdress, result);
 		return result;
 	}
 
 		}
 		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;
+		} catch (UnsupportedEncodingException e) { e.printStackTrace(); }
+    	return "";
+    }
 
 }

src/com/saibotd/bitbeaker/NewIssueActivity.java

 package com.saibotd.bitbeaker;
 
-import java.net.URLEncoder;
-
 import android.app.Activity;
 import android.os.Bundle;
 import android.widget.EditText;
   				String content = ((EditText) findViewById(R.id.new_issue_content)).getText().toString();
   				String type = ((Spinner) findViewById(R.id.new_issue_type)).getSelectedItem().toString(); 
   		        AsyncLoader asyncLoader = new AsyncLoader();
-  		        asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/"+ owner +"/"+ slug +"/issues/", "title=" + URLEncoder.encode(title) + "&content=" + URLEncoder.encode(content) + "&kind=" + URLEncoder.encode(type));
+  		        asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/"+ owner +"/"+ slug +"/issues/", "title=" + encode(title) + "&content=" +encode(content) + "&kind=" + encode(type));
   		    default:
   		        return super.onOptionsItemSelected(item);
   	    }

src/com/saibotd/bitbeaker/NewIssueCommentActivity.java

 package com.saibotd.bitbeaker;
 
-import java.net.URLEncoder;
-
 import android.app.Activity;
 import android.os.Bundle;
 import android.widget.EditText;
   				String content = ((EditText) findViewById(R.id.new_issue_comment_content)).getText().toString();
   		        AsyncLoader asyncLoader = new AsyncLoader();
   		        bitbeaker.delKV("https://api.bitbucket.org/1.0/repositories/"+ owner +"/"+ slug +"/issues/" + id + "/comments/");
-  		        asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/"+ owner +"/"+ slug +"/issues/" + id + "/comments/", "content=" + URLEncoder.encode(content));
+  		        asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/"+ owner +"/"+ slug +"/issues/" + id + "/comments/", "content=" + encode(content));
   		    default:
   		        return super.onOptionsItemSelected(item);
   	    }

src/com/saibotd/bitbeaker/RepositoriesActivity.java

 		case R.id.menu_search:
 			onSearchRequested();
 			return true;
-		case android.R.id.home:
-			finish();
-			return true;
 		default:
 			return super.onOptionsItemSelected(item);
 		}

src/com/saibotd/bitbeaker/RepositoryActivity.java

 		    	wikiIntent.putExtras(b);
 				startActivity(wikiIntent);
 				return true;
-		    case android.R.id.home:
-		    	finish();
-		    	return true;
 		    default:
 		        return super.onOptionsItemSelected(item);
 	    }
 				// TODO Auto-generated catch block
 				e.printStackTrace();
 			}
-			ignoreError = true;
 			AsyncLoader asyncLoader2 = new AsyncLoader();
 	        asyncLoader2.execute("https://api.bitbucket.org/1.0/repositories/"+ owner +"/"+ slug +"/changesets/");
 		} else {

src/com/saibotd/bitbeaker/SearchableActivity.java

 		case R.id.menu_search:
 			onSearchRequested();
 			return true;
-		case android.R.id.home:
-			finish();
-			return true;
 		default:
 			return super.onOptionsItemSelected(item);
 		}

src/com/saibotd/bitbeaker/SourceActivity.java

 package com.saibotd.bitbeaker;
 
+import android.annotation.SuppressLint;
 import android.os.Bundle;
 import android.text.TextUtils;
 import android.util.Log;
         owner = b.getString("owner");
         file = b.getString("file");
         AsyncLoader asyncLoader = new AsyncLoader();
-        if(scm.equals("git")) asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/"+ owner +"/"+slug+"/raw/master" + replace_whitespace(file));
-        else asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/"+ owner +"/"+slug+"/raw/tip" + replace_whitespace(file));
+        if(scm.equals("git")) asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/"+ owner +"/"+slug+"/raw/master" + encode(file));
+        else asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/"+ owner +"/"+slug+"/raw/tip" + encode(file));
         ActionBar actionBar = getSupportActionBar();
         setTitle(slug);
         actionBar.setSubtitle(file);
    	public boolean onCreateOptionsMenu(Menu menu) {
    		return false;
    	}
-    
-    private String replace_whitespace(String in) {
-    	//java.net.URLEncoder.encode(String, "UTF-8") did not gave right results
-    	return in.replaceAll(" ", "%20");
-    }
 	
+	@SuppressLint("SetJavaScriptEnabled")
 	public void AsyncLoaderDone(String result) {
 		super.AsyncLoaderDone(result);
 		code = TextUtils.htmlEncode(result.replace("\t", "  "));

src/com/saibotd/bitbeaker/SourceBrowserActivity.java

         scm = b.getString("scm");
         owner = b.getString("owner");
         subdir = b.getString("subdir");
-        //setListAdapter(new SourceBrowserAdapter(this, slug, owner, subdir));
         setContentView(R.layout.repositories);
         ActionBar actionBar = getSupportActionBar();
         setTitle(slug);
         actionBar.setSubtitle(subdir);
         AsyncLoader asyncLoader = new AsyncLoader();
-        if(scm.equals("git")) asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/" + owner + "/"+slug+"/src/master" + replace_whitespace(subdir));
-        else asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/" + owner + "/"+slug+"/src/tip" + replace_whitespace(subdir));
+        if(scm.equals("git")) asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/" + owner + "/"+slug+"/src/master" + encode(subdir));
+        else asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/" + owner + "/"+slug+"/src/tip" + encode(subdir));
     }
     
     @Override
    		return false;
    	}
     
-    private String replace_whitespace(String in) {
-    	//java.net.URLEncoder.encode(String, "UTF-8") did not gave right results
-    	return in.replaceAll(" ", "%20");
-    }
-    
     protected void AsyncLoaderDone(String result) {
     	super.AsyncLoaderDone(result);
     	JSONArray jsonArray = new JSONArray();

src/com/saibotd/bitbeaker/WikiActivity.java

 import org.json.JSONException;
 import org.json.JSONObject;
 
+import android.annotation.SuppressLint;
 import android.os.Bundle;
 import android.webkit.WebChromeClient;
 import android.webkit.WebView;
 	private String data; 
 	private WebView webView;
 
-    public void onCreate(Bundle savedInstanceState) {
+    @SuppressLint("SetJavaScriptEnabled")
+	public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.wiki);
         Bundle b = getIntent().getExtras();