Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions app/src/main/java/net/osmtracker/GitHubUser.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
package net.osmtracker;

import net.osmtracker.github.*;

import android.content.Context;
import android.content.SharedPreferences;

public class GitHubUser {

private static final String PREF_NAME = "GitHubPrefs";
private static final String KEY_USERNAME = "username";
private static final String KEY_TOKEN = "token";

private SharedPreferences sharedPreferences;

public GitHubUser(Context context) {
sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
sharedPreferences = context.getSharedPreferences(GitHubConstants.SHARED_PREFS_NAME, Context.MODE_PRIVATE);
}

public void saveCredentials(String username, String token) {
sharedPreferences.edit()
.putString(KEY_USERNAME, username)
.putString(KEY_TOKEN, token)
.putString(GitHubConstants.KEY_USERNAME, username)
.putString(GitHubConstants.KEY_TOKEN, token)
.apply();
}

public String getUsername() {
return sharedPreferences.getString(KEY_USERNAME, "");
return sharedPreferences.getString(GitHubConstants.KEY_USERNAME, "");
}

public String getToken() {
return sharedPreferences.getString(KEY_TOKEN, "");
return sharedPreferences.getString(GitHubConstants.KEY_TOKEN, "");
}

public boolean hasCredentials() {
Expand Down
13 changes: 7 additions & 6 deletions app/src/main/java/net/osmtracker/activity/GitHubConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@

import net.osmtracker.GitHubUser;
import net.osmtracker.R;
import static net.osmtracker.github.GitHubConstants.GITHUB_TOKENS_URL;

public class GitHubConfig extends Activity {

private final static String GitHubToken_URL = "https://github.com/settings/tokens";

EditText editTextUserName, editTextUserToken;
private GitHubUser gitHubUser;

Expand All @@ -37,7 +35,7 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(GitHubToken_URL));
intent.setData(Uri.parse(GITHUB_TOKENS_URL));
startActivity(intent);
}
});
Expand All @@ -49,12 +47,15 @@ public void onClick(View v) {
String username = editTextUserName.getText().toString().trim();
String ghToken = editTextUserToken.getText().toString().trim();

// Empty is enabled to allow saving without credentials to erase the existing ones
/*
if (username.isEmpty()) {
editTextUserName.setError("Username required");
return;
}
if (ghToken.length() != 40) {
editTextUserToken.setError("Token must be 40 characters");
*/
if (ghToken.length() != 40 && !ghToken.isEmpty()) {
editTextUserToken.setError(getString(R.string.error_gh_token_lenght));
return;
}

Expand Down
12 changes: 2 additions & 10 deletions app/src/main/java/net/osmtracker/activity/GitHubNewFork.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import net.osmtracker.GitHubUser;
import net.osmtracker.R;
import static net.osmtracker.github.GitHubConstants.getRepoForksUrl;

import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -29,9 +30,7 @@
import java.util.Map;

public class GitHubNewFork extends Activity {

EditText editTextRootUsername, editTextRootRepo;
private String BaseURL = "https://api.github.com";
private GitHubUser gitHubUser;
private String newForkFullName;

Expand Down Expand Up @@ -69,7 +68,6 @@ public void onClick(View v) {
createNewFork(username, repo);
//Toast.makeText(GitHubNewFork.this, R.string.successfully_created, Toast.LENGTH_SHORT).show();
//finish();

}
});

Expand All @@ -78,7 +76,6 @@ public void onClick(View v) {
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

finish();
}
});
Expand All @@ -88,7 +85,7 @@ public void onClick(View v) {
}

private void createNewFork(String username, String repo) {
String fullURL = getBaseURL() + "/repos/"+ username +"/"+ repo +"/forks?name=fork";
String fullURL = getRepoForksUrl(username, repo);

JsonObjectRequest postResquest= new JsonObjectRequest(
Request.Method.POST,
Expand Down Expand Up @@ -119,15 +116,10 @@ public Map getHeaders() throws AuthFailureError
headers.put("Authorization", "Bearer " + gitHubUser.getToken());
return headers;
}

};
Volley.newRequestQueue(this).add(postResquest);
}

public String getBaseURL() {
return BaseURL;
}

public void setNewForkFullName(String newForkFullName) {
this.newForkFullName = newForkFullName;
}
Expand Down
8 changes: 2 additions & 6 deletions app/src/main/java/net/osmtracker/activity/GitHubNewRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import net.osmtracker.GitHubUser;
import net.osmtracker.R;
import static net.osmtracker.github.GitHubConstants.GITHUB_API_USER_REPOS_URL;

import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -33,7 +34,6 @@
public class GitHubNewRepo extends Activity {

EditText editTextNewRepo;
private String BaseURL = "https://api.github.com";
private GitHubUser gitHubUser;
private String newRepoFullName;

Expand Down Expand Up @@ -77,7 +77,7 @@ public void onClick(View v) {
}

private void createNewRepo(String repoName, boolean isPrivate) {
String fullURL = getBaseURL()+"/user/repos";
String fullURL = GITHUB_API_USER_REPOS_URL;

ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage(this.getResources().getString(R.string.github_creating_repository));
Expand Down Expand Up @@ -140,10 +140,6 @@ public byte[] getBody() {
Volley.newRequestQueue(this).add(postResquest);
}

public String getBaseURL() {
return BaseURL;
}

public void setNewRepoFullName(String newRepoFullName) {
this.newRepoFullName = newRepoFullName;
}
Expand Down
38 changes: 21 additions & 17 deletions app/src/main/java/net/osmtracker/activity/GitHubPullRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@

import net.osmtracker.GitHubUser;
import net.osmtracker.R;
import static net.osmtracker.github.GitHubConstants.getRepoPullsUrl;
import static net.osmtracker.github.GitHubConstants.getRepoUrl;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

Expand All @@ -32,9 +35,8 @@
public class GitHubPullRequest extends Activity {

EditText editTextTitle, editTextBody;
private String BaseURL = "https://api.github.com";
private String RepoOrigen;
private String DefaultBranch;
private String repoOrigen;
private String defaultBranch;
private GitHubUser gitHubUser;

@Override
Expand Down Expand Up @@ -84,7 +86,7 @@ public void onClick(View v) {
}

private void createPullRequest() {
String fullURL = getBaseURL()+"/repos/"+getRepoOrigen()+"/pulls";
String fullURL = getRepoPullsUrl(this.repoOrigen);

JsonObjectRequest postResquest= new JsonObjectRequest(
Request.Method.POST,
Expand All @@ -103,14 +105,23 @@ public void onResponse(JSONObject response) {
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
String errorMsg = "Error while creating the PR";
String errorMsg = "";
if (error.networkResponse != null && error.networkResponse.data != null) {
try {
String responseBody = new String(error.networkResponse.data, "utf-8");
Log.e("PR_ERROR", responseBody);
errorMsg += "\n" + responseBody;
JSONObject github_response = new JSONObject(responseBody);
JSONArray github_errors = github_response.getJSONArray("errors");
for (int i = 0; i < github_errors.length(); i++) {
JSONObject github_error = github_errors.getJSONObject(i);
errorMsg += github_error.getString("message") + "\n";
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
errorMsg += getString(R.string.error_gh_pr_creation);
}
}
Toast.makeText(GitHubPullRequest.this, errorMsg, Toast.LENGTH_LONG).show();
Expand Down Expand Up @@ -154,7 +165,7 @@ public byte[] getBody() {
}

private void getInfoRepo(String repoFullName) {
String fullURL = getBaseURL()+"/repos/"+repoFullName;
String fullURL = getRepoUrl(repoFullName);

JsonObjectRequest postResquest= new JsonObjectRequest(
Request.Method.GET,
Expand Down Expand Up @@ -187,23 +198,16 @@ public Map getHeaders() throws AuthFailureError {
Volley.newRequestQueue(this).add(postResquest);
}

public String getBaseURL() {
return BaseURL;
}

public String getRepoOrigen() {
return RepoOrigen;
}

public void setRepoOrigen(String repoOrigen) {
RepoOrigen = repoOrigen;
this.repoOrigen = repoOrigen;
}

public String getDefaultBranch() {
return DefaultBranch;
return defaultBranch;
}

public void setDefaultBranch(String defaultBranch) {
DefaultBranch = defaultBranch;
this.defaultBranch = defaultBranch;
}
}
62 changes: 40 additions & 22 deletions app/src/main/java/net/osmtracker/activity/GitHubUpload.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import net.osmtracker.util.Callback;
import net.osmtracker.util.DialogUtils;
import net.osmtracker.util.GitHubUtils;
import static net.osmtracker.github.GitHubConstants.getRepoFileContentUrl;
import static net.osmtracker.github.GitHubConstants.getUserReposUrl;

import org.json.JSONArray;
import org.json.JSONException;
Expand All @@ -50,9 +52,7 @@


public class GitHubUpload extends Activity {

private ArrayList<String> ArrayListRepos;
private String BaseURL = "https://api.github.com";
private GitHubUser gitHubUser;
private String RepoName = "";
EditText editTextCommitMsj;
Expand Down Expand Up @@ -166,7 +166,7 @@ private void openActivityOnClick(int btnId, Class<? extends Activity> destinatio
* Either starts uploading directly if we are authenticated against GitHub
*/
private void startUploadGitHub(final String fileInBase64, String filename, String commitMsj){
String fullURL = getBaseURL()+"/repos/"+getRepoName()+"/contents/"+filename;
String fullURL = getRepoFileContentUrl(getRepoName(), filename);

ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage(this.getResources().getString(R.string.uploading_file) + filename);
Expand Down Expand Up @@ -264,15 +264,19 @@ public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {

Toast.makeText(GitHubUpload.this, "R.string.upload_to_github_select_repo", Toast.LENGTH_SHORT).show();
}
});
}

private void listRepos() {
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage(getString(R.string.retrieving_repositories));
progressDialog.setCancelable(true);
progressDialog.show();

RequestQueue queue = Volley.newRequestQueue(this);
String sortBy = "created";
String fullURL = getBaseURL() + "/user/repos?" + "sort=" + sortBy ;
String fullURL = getUserReposUrl();

JsonArrayRequest getResquest = new JsonArrayRequest(
Request.Method.GET,
Expand All @@ -281,17 +285,7 @@ private void listRepos() {
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
// creating a new json object and
// getting each object from our json array.
try {
JSONObject responseObj = response.getJSONObject(i);
ArrayListRepos.add(responseObj.getString("full_name"));

} catch (JSONException e) {
e.printStackTrace();
}
}
listRepoResponseAction(response, progressDialog);
}
}, new Response.ErrorListener() {
@Override
Expand All @@ -318,16 +312,40 @@ public Map getHeaders() throws AuthFailureError
queue.add(getResquest);
}

/**
* This method is called when the list of repositories is retrieved
* from the server.
*
* @param response JSONArray with the repositories
* @param progressDialog ProgressDialog
*/
private void listRepoResponseAction(JSONArray response, ProgressDialog progressDialog) {
boolean errorOcurred = false;
for (int i = 0; i < response.length(); i++) {
// creating a new json object and
// getting each object from our json array.
try {
JSONObject responseObj = response.getJSONObject(i);
ArrayListRepos.add(responseObj.getString("full_name"));
} catch (JSONException e) {
progressDialog.dismiss();
Toast.makeText(GitHubUpload.this, R.string.error_retrieving_repositories, Toast.LENGTH_SHORT).show();
e.printStackTrace();
errorOcurred = true;
break;
}
}
if (!errorOcurred) {
Toast.makeText(GitHubUpload.this, R.string.successfully_retrieved_repositories, Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}

public String getRepoName() {
return RepoName;
}

public void setRepoName(String repoName) {
RepoName = repoName;
}

public String getBaseURL() {
return BaseURL;
}

}
Loading