Commits

Ville Saalo committed e6b22da

Some slight refactoring.

Comments (0)

Files changed (1)

src/com/saibotd/bitbeaker/SourceBrowserActivity.java

 import org.json.JSONException;
 import org.json.JSONObject;
 
+import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 
 public class SourceBrowserActivity extends MyActivity {
 
+	private static final String FILE_PREFIX = "fil_";
+	private static final String DIR_PREFIX = "dir_";
 	private String slug;
 	private String owner;
 	private String subdir;
 			JSONObject jsonObject = new JSONObject(result);
 			JSONArray dirs = jsonObject.getJSONArray("directories");
 			for(int i = 0; i < dirs.length(); i++){
-				filesAndDirs.add("dir_" + dirs.get(i));
+				filesAndDirs.add(DIR_PREFIX + dirs.get(i));
 			}
 			JSONArray files = jsonObject.getJSONArray("files");
 			for(int i = 0; i < files.length(); i++){
 				String filePath = files.getJSONObject(i).getString("path");
 				int lastSlashIndex = filePath.lastIndexOf("/");
-				filesAndDirs.add("fil_" + filePath.substring(lastSlashIndex+1));
+				filesAndDirs.add(FILE_PREFIX + filePath.substring(lastSlashIndex+1));
 			}
 			Collections.sort(filesAndDirs);
 		} catch (JSONException e) {
 					b.putString("owner", owner);
 					b.putString("branch", branch);
 					b.putStringArray("branches", branches);
-    				if(rec.substring(0, 4).equals("dir_")){
-    					b.putString("subdir", subdir + rec.substring(4) + "/");
-    					Intent intent = new Intent(v.getContext(), SourceBrowserActivity.class);
-    					intent.putExtras(b);
-    					v.getContext().startActivity(intent);
+					Class<? extends Activity> activityToStart;
+    				if(rec.startsWith(DIR_PREFIX)){
+    					b.putString("subdir", subdir + rec.substring(DIR_PREFIX.length()) + "/");
+    					activityToStart = SourceBrowserActivity.class;
     				} else {
-    					b.putString("file", subdir + rec.substring(4));
-    					Intent intent = new Intent(v.getContext(), SourceActivity.class);
-    					intent.putExtras(b);
-    					v.getContext().startActivity(intent);
+    					b.putString("file", subdir + rec.substring(FILE_PREFIX.length()));
+    					activityToStart = SourceActivity.class;    					
     				}
+    				Intent intent = new Intent(v.getContext(), activityToStart);
+					intent.putExtras(b);
+					v.getContext().startActivity(intent);
     			} catch (JSONException e) {
     				// TODO Auto-generated catch block
     				e.printStackTrace();
     		TextView title = (TextView) view.findViewById(R.id.title);
     		ImageView icon = (ImageView) view.findViewById(R.id.icon);
     		try {
-    			if(getItemAndAddName(position).getString("name").startsWith("dir_")){
-    				icon.setImageResource(R.drawable.icon_folder);
-    			} else {
-    				icon.setImageResource(R.drawable.icon_file);
-    			}
-    			title.setText(getItemAndAddName(position).getString("name").substring(4));
+    			int skipChars;
+				if (getItemAndAddName(position).getString("name").startsWith(DIR_PREFIX)) {
+					skipChars = DIR_PREFIX.length();
+					icon.setImageResource(R.drawable.icon_folder);
+				} else {
+					skipChars = FILE_PREFIX.length();
+					icon.setImageResource(R.drawable.icon_file);
+				}
+				title.setText(getItemAndAddName(position).getString("name").substring(skipChars));
     		} catch (JSONException e) {
     			// TODO Auto-generated catch block
     			e.printStackTrace();