Juha Kuitunen avatar Juha Kuitunen committed f1009d4

get branches from API and display their names when button is pressed

Comments (0)

Files changed (4)

Add a comment to this file

res/drawable-hdpi/fork_28x32.png

Added
New image

res/menu/menu_source.xml

+<?xml version="1.0" encoding="utf-8"?>
+<menu xmlns:android="http://schemas.android.com/apk/res/android" >
+    <item android:id="@+id/menu_branch" android:title="@string/branch" android:showAsAction="always|withText" android:icon="@drawable/fork_28x32"></item>
+</menu>

res/values/strings.xml

     <string name="browse_source">Browse Source</string>
     <string name="source_browser">Source Browser</string>
     <string name="source">Source</string>
+    <string name="branch">Branch</string>
     <string name="issues">Issues</string>
     <string name="log_out">Log out</string>
     <string name="search_repo">Search for repositories</string>
     </string-array>
     <string name="submit_new_issue">Submit new issue</string>
     <string name="hint">"Note: You don't need to login to use the search function!"</string>
-
     <string name="issue_filter_prompt">Filter issues</string>
     <string-array name="status_of_issue">
         <item>Show all issues</item>
         <item>duplicate</item>
         <item>wontfix</item>
     </string-array>
-        <string name="menu_source">Source</string>
-        <string name="submit_comment">Submit</string>
-        <string name="new_comment_hint">Leave a comment</string>
-        <string name="add">Add</string>
-        <string name="proceed">Proceed</string>
-        <string name="submit">Submit</string>
-
+    <string name="menu_source">Source</string>
+    <string name="submit_comment">Submit</string>
+    <string name="new_comment_hint">Leave a comment</string>
+    <string name="add">Add</string>
+    <string name="proceed">Proceed</string>
+    <string name="submit">Submit</string>
 </resources>

src/com/saibotd/bitbeaker/SourceBrowserActivity.java

 import android.widget.ImageView;
 import android.widget.ListView;
 import android.widget.TextView;
+import android.widget.Toast;
 
 import com.actionbarsherlock.app.ActionBar;
 import com.actionbarsherlock.view.Menu;
+import com.actionbarsherlock.view.MenuInflater;
+import com.actionbarsherlock.view.MenuItem;
 
 public class SourceBrowserActivity extends MyActivity {
-	
+
 	private String slug;
 	private String scm;
 	private String owner;
 	private String subdir;
-	
+	private String branches;
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         AsyncLoader asyncLoader = new AsyncLoader();
         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));
+        listBranches();
     }
-    
-    @Override
-   	public boolean onCreateOptionsMenu(Menu menu) {
-   		return false;
-   	}
-    
+
+	private void listBranches() {
+		bitbeaker.delKV("https://api.bitbucket.org/1.0/repositories/" + owner + "/" + slug + "/branches");
+		new AsyncBranchesLoader().execute("https://api.bitbucket.org/1.0/repositories/" + owner + "/" + slug + "/branches");
+	}
+
+	protected class AsyncBranchesLoader extends AsyncLoader {
+		@Override
+		protected void onPostExecute(String result) {
+			AsyncBranchesLoaderDone(result);
+		}
+	}
+
+	protected void AsyncBranchesLoaderDone(String result) {
+		super.AsyncLoaderDone(result);
+		JSONObject json;
+		try {
+			json = new JSONObject(result);
+			branches = json.names().toString();
+		} catch (JSONException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+	}
+
+	public boolean onCreateOptionsMenu(Menu menu) {
+		MenuInflater inflater = getSupportMenuInflater();
+		inflater.inflate(R.menu.menu_source, menu);
+		return true;
+	}
+
+	@Override
+	public boolean onOptionsItemSelected(MenuItem item) {
+		if (item.getItemId() == R.id.menu_branch) {
+			Toast.makeText(this, branches, Toast.LENGTH_LONG).show();
+			return true;
+		}
+		else return super.onOptionsItemSelected(item);
+	}
+
     protected void AsyncLoaderDone(String result) {
     	super.AsyncLoaderDone(result);
     	JSONArray jsonArray = new JSONArray();
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.