diff --git a/app/src/main/java/de/domjos/unitrackermobile/activities/AccountActivity.java b/app/src/main/java/de/domjos/unitrackermobile/activities/AccountActivity.java index b84bff58..aad63d8a 100644 --- a/app/src/main/java/de/domjos/unitrackermobile/activities/AccountActivity.java +++ b/app/src/main/java/de/domjos/unitrackermobile/activities/AccountActivity.java @@ -273,7 +273,7 @@ protected void initControls() { new Thread(() -> { try { if(!chkAccountGuest.isChecked()) { - if(currentAccount.getAuthentication() == Authentication.Auth.OAUTH) { + if(currentAccount.getAuthentication() == Authentication.Auth.OAUTH && this.txtAccountAPI.getText().toString().isEmpty()) { GithubTokenProvider githubTokenProvider = new GithubTokenProvider(currentAccount); currentAccount.setAPIKey(githubTokenProvider.refreshToken()); } diff --git a/app/src/main/java/de/domjos/unitrackermobile/activities/MainActivity.java b/app/src/main/java/de/domjos/unitrackermobile/activities/MainActivity.java index 0fc7c442..fb1ce8a2 100644 --- a/app/src/main/java/de/domjos/unitrackermobile/activities/MainActivity.java +++ b/app/src/main/java/de/domjos/unitrackermobile/activities/MainActivity.java @@ -21,6 +21,8 @@ import android.app.Activity; import android.content.Intent; import android.graphics.BitmapFactory; +import android.graphics.drawable.Drawable; +import android.os.Build; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -308,7 +310,7 @@ protected void initControls() { this.cmdIssuesAdd = this.findViewById(R.id.cmdIssueAdd); this.spMainAccounts = this.navigationView.getHeaderView(0).findViewById(R.id.spMainAccounts); - this.accountList = new ArrayAdapter<>(this.getApplicationContext(),R.layout.spinner_item); + this.accountList = new ArrayAdapter<>(this.getApplicationContext(), R.layout.spinner_item); this.spMainAccounts.setAdapter(this.accountList); this.accountList.notifyDataSetChanged(); @@ -768,17 +770,16 @@ private void fillFields() { Authentication authentication = MainActivity.GLOBALS.getSettings(getApplicationContext()).getCurrentAuthentication(); if (authentication != null) { if (authentication.getServer().equals("")) { - ivMainCover.setImageDrawable(getResources().getDrawable(R.drawable.ic_account_circle_black_24dp)); + ivMainCover.setImageDrawable(this.getDrawable()); lblAccountTitle.setText(R.string.accounts_noAccount); lblMainCommand.setText(R.string.accounts_add); } else { if (authentication.getCover() != null) { ivMainCover.setImageBitmap(BitmapFactory.decodeByteArray(authentication.getCover(), 0, authentication.getCover().length)); } else { - ivMainCover.setImageDrawable(getResources().getDrawable(R.drawable.ic_account_circle_black_24dp)); + ivMainCover.setImageDrawable(this.getDrawable()); } - final StringBuilder tracker = new StringBuilder(); tracker.append(authentication.getTitle()); tracker.append(" ("); @@ -797,12 +798,21 @@ private void fillFields() { lblMainCommand.setText(R.string.accounts_change); } } else { - ivMainCover.setImageDrawable(getResources().getDrawable(R.drawable.ic_account_circle_black_24dp)); + ivMainCover.setImageDrawable(this.getDrawable()); lblAccountTitle.setText(R.string.accounts_noAccount); lblMainCommand.setText(R.string.accounts_add); } } + @SuppressWarnings("deprecation") + private Drawable getDrawable() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + return getDrawable(R.drawable.ic_account_circle_black_24dp); + } else { + return getResources().getDrawable(R.drawable.ic_account_circle_black_24dp); + } + } + private void selectProject() { for (int i = 0; i <= this.projectList.getCount() - 1; i++) { Project current = this.projectList.getItem(i); diff --git a/app/src/main/res/layout/spinner_item.xml b/app/src/main/res/layout/spinner_item.xml index ca9934d1..b4381445 100644 --- a/app/src/main/res/layout/spinner_item.xml +++ b/app/src/main/res/layout/spinner_item.xml @@ -20,6 +20,9 @@ \ No newline at end of file diff --git a/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/permissions/GithubPermissions.java b/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/permissions/GithubPermissions.java index 70b4c560..caf04a20 100644 --- a/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/permissions/GithubPermissions.java +++ b/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/permissions/GithubPermissions.java @@ -40,12 +40,12 @@ public boolean addProjects() { @Override public boolean updateProjects() { - return false; + return true; } @Override public boolean deleteProjects() { - return false; + return true; } @Override diff --git a/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/services/authentication/AccessTokenProvider.java b/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/services/authentication/AccessTokenProvider.java index 77b91610..ad9970d9 100644 --- a/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/services/authentication/AccessTokenProvider.java +++ b/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/services/authentication/AccessTokenProvider.java @@ -2,7 +2,7 @@ public interface AccessTokenProvider { - String authType(); String token(); String refreshToken(); + String authorization(); } diff --git a/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/services/authentication/GithubTokenProvider.java b/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/services/authentication/GithubTokenProvider.java index 206c96f5..6e4eda36 100644 --- a/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/services/authentication/GithubTokenProvider.java +++ b/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/services/authentication/GithubTokenProvider.java @@ -15,11 +15,6 @@ public GithubTokenProvider(Authentication authentication) { this.token = this.authentication.getAPIKey(); } - @Override - public String authType() { - return "token"; - } - @Override public String token() { if(this.token.equals("")) { @@ -34,14 +29,24 @@ public String refreshToken() { JSONEngine jsonEngine = new JSONEngine(this.authentication); JSONObject jsonObject = new JSONObject(); jsonObject.put("client_secret", "b5d664ec43c104fe21b8dbf7699fbb6d82118ad8"); + jsonObject.put("client_id", "fb4ad3bae97a0a91e6a4"); + jsonObject.put("note", "UniTrackerMobile Authentication"); JSONArray jsonArray = new JSONArray(); jsonArray.put("user"); + jsonArray.put("public_repo"); + jsonArray.put("delete_repo"); jsonObject.put("scopes", jsonArray); - jsonEngine.executeRequest(this.authentication.getServer() + "/authorizations/clients/fb4ad3bae97a0a91e6a4", jsonObject.toString(), "PUT"); + + jsonEngine.executeRequest(this.authentication.getServer() + "/authorizations", jsonObject.toString(), "POST"); if(jsonEngine.getCurrentState() == 200 || jsonEngine.getCurrentState() == 201) { - return new JSONObject(jsonEngine.getCurrentMessage()).getString("hashed_token"); + return new JSONObject(jsonEngine.getCurrentMessage()).getString("token"); } } catch (Exception ignored) {} return null; } + + @Override + public String authorization() { + return this.authentication.getUserName() + ":" + this.token; + } } diff --git a/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/services/engine/JSONEngine.java b/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/services/engine/JSONEngine.java index d5382c1c..ae0680d6 100644 --- a/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/services/engine/JSONEngine.java +++ b/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/services/engine/JSONEngine.java @@ -28,6 +28,7 @@ import de.domjos.unitrackerlibrary.services.authentication.AccessTokenProvider; import de.domjos.unitrackerlibrary.utils.Converter; import okhttp3.Call; +import okhttp3.ConnectionPool; import okhttp3.Credentials; import okhttp3.MediaType; import okhttp3.MultipartBody; @@ -85,7 +86,7 @@ protected void removeHeader(String header) { this.headers.remove(header); } - protected int executeRequest(String path) throws Exception { + public int executeRequest(String path) throws Exception { try { Call call = this.initAuthentication(path); Response response = call.execute(); @@ -271,7 +272,8 @@ private OkHttpClient getClient(boolean basic) { OkHttpClient.Builder builder = new OkHttpClient.Builder() .followRedirects(true) .connectTimeout(30, TimeUnit.SECONDS) - .readTimeout(30, TimeUnit.SECONDS); + .readTimeout(30, TimeUnit.SECONDS) + .connectionPool(new ConnectionPool(100, 5, TimeUnit.MINUTES)); if(basic) { builder.authenticator((route, response) -> { @@ -291,10 +293,7 @@ private OkHttpClient getClient(boolean basic) { }); } else { if(this.accessTokenProvider!=null) { - builder.authenticator((route, response) -> { - String token = this.accessTokenProvider.token(); - return response.request().newBuilder().header("Authorization", this.accessTokenProvider.authType() + " " + token).build(); - }); + builder.authenticator((route, response) -> response.request().newBuilder().header("Authorization", this.accessTokenProvider.authorization()).build()); } else { return this.getClient(true); } diff --git a/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/services/tracker/Github.java b/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/services/tracker/Github.java index 3ba0bd46..1a41124a 100644 --- a/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/services/tracker/Github.java +++ b/unitrackerlibrary/src/main/java/de/domjos/unitrackerlibrary/services/tracker/Github.java @@ -47,10 +47,18 @@ public final class Github extends JSONEngine implements IBugService { private Authentication authentication; + private final static String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; + private String username; public Github(Authentication authentication) { super(authentication, new GithubTokenProvider(authentication)); this.authentication = authentication; + + if(this.authentication.getHints().containsKey("userName")) { + this.username = this.authentication.getHints().get("userName"); + } else { + this.username = this.authentication.getUserName(); + } } @Override @@ -58,6 +66,7 @@ public boolean testConnection() throws Exception { int status = this.executeRequest("/user"); if (status == 200 || status == 201) { JSONObject jsonObject = new JSONObject(this.getCurrentMessage()); + this.authentication.getHints().put("userName", jsonObject.getString("login")); return jsonObject.has("plan"); } return false; @@ -71,7 +80,7 @@ public String getTrackerVersion() { @Override public List> getProjects() throws Exception { List> projects = new LinkedList<>(); - int status = this.executeRequest("/users/" + this.authentication.getUserName() + "/repos"); + int status = this.executeRequest("/users/" + this.username + "/repos"); if (status == 200 || status == 201) { JSONArray versionArray = new JSONArray(this.getCurrentMessage()); @@ -87,13 +96,13 @@ public List> getProjects() throws Exception { project.setEnabled(!jsonObject.getBoolean("disabled")); if (jsonObject.has("created_at")) { - Date dt = Converter.convertStringToDate(jsonObject.getString("created_at"), "yyyy-MM-dd'T'HH:mm:ss'Z'"); + Date dt = Converter.convertStringToDate(jsonObject.getString("created_at"), Github.DATE_TIME_FORMAT); if (dt != null) { project.setCreatedAt(dt.getTime()); } } if (jsonObject.has("updated_at")) { - Date dt = Converter.convertStringToDate(jsonObject.getString("updated_at"), "yyyy-MM-dd'T'HH:mm:ss'Z'"); + Date dt = Converter.convertStringToDate(jsonObject.getString("updated_at"), Github.DATE_TIME_FORMAT); if (dt != null) { project.setUpdatedAt(dt.getTime()); } @@ -134,7 +143,7 @@ public Long insertOrUpdateProject(Project project) throws Exception { method = "POST"; } else { url = "/repos/" + project.getTitle(); - method = "POST"; + method = "PATCH"; } int status = this.executeRequest(url, jsonObject.toString(), method); @@ -159,7 +168,7 @@ public List> getVersions(String filter, Long project_id) throws Ex List> versions = new LinkedList<>(); Project project = this.getProject(project_id); if (project != null) { - int status = this.executeRequest("/repos/" + this.authentication.getUserName() + "/" + project.getAlias() + "/releases"); + int status = this.executeRequest("/repos/" + this.username + "/" + project.getAlias() + "/releases"); if (status == 200 || status == 201) { JSONArray jsonArray = new JSONArray(this.getCurrentMessage()); @@ -168,7 +177,7 @@ public List> getVersions(String filter, Long project_id) throws Ex Version version = new Version<>(); version.setTitle(jsonObject.getString("name")); version.setDescription(jsonObject.getString("body")); - version.setReleasedVersionAt(Converter.convertStringToDate(jsonObject.getString("published_at"), "yyyy-MM-dd'T'HH:mm:ss'Z'").getTime()); + version.setReleasedVersionAt(Converter.convertStringToDate(jsonObject.getString("published_at"), Github.DATE_TIME_FORMAT).getTime()); version.setId(jsonObject.getLong("id")); version.setReleasedVersion(jsonObject.getBoolean("prerelease")); versions.add(version); @@ -193,10 +202,10 @@ public void insertOrUpdateVersion(Version version, Long project_id) throws Project project = this.getProject(project_id); if (project != null) { if (version.getId() == null) { - url = "/repos/" + this.authentication.getUserName() + "/" + project.getAlias() + "/releases"; + url = "/repos/" + this.username + "/" + project.getAlias() + "/releases"; method = "POST"; } else { - url = "/repos/" + this.authentication.getUserName() + "/" + project.getAlias() + "/releases/" + version.getId(); + url = "/repos/" + this.username + "/" + project.getAlias() + "/releases/" + version.getId(); method = "PATCH"; } @@ -208,7 +217,7 @@ public void insertOrUpdateVersion(Version version, Long project_id) throws public void deleteVersion(Long id, Long project_id) throws Exception { Project project = this.getProject(project_id); if (project != null) { - this.deleteRequest("/repos/" + this.authentication.getUserName() + "/" + project.getTitle() + "/releases/" + id); + this.deleteRequest("/repos/" + this.username + "/" + project.getTitle() + "/releases/" + id); } } @@ -295,8 +304,8 @@ public Issue getIssue(Long id, Long project_id) throws Exception { issue.setId(issueObject.getLong("number")); issue.setTitle(issueObject.getString("title")); issue.setDescription(issueObject.getString("body")); - issue.setLastUpdated(Converter.convertStringToDate(issueObject.getString("updated_at"), "yyyy-MM-dd'T'HH:mm:ss'Z'")); - issue.setSubmitDate(Converter.convertStringToDate(issueObject.getString("created_at"), "yyyy-MM-dd'T'HH:mm:ss'Z'")); + issue.setLastUpdated(Converter.convertStringToDate(issueObject.getString("updated_at"), Github.DATE_TIME_FORMAT)); + issue.setSubmitDate(Converter.convertStringToDate(issueObject.getString("created_at"), Github.DATE_TIME_FORMAT)); issue.setHandler(this.getUser(issueObject.getJSONObject("user"))); } } @@ -311,7 +320,7 @@ public void insertOrUpdateIssue(Issue issue, Long project_id) throws Excep jsonObject.put("title", issue.getTitle()); jsonObject.put("body", issue.getDescription()); JSONArray jsonArray = new JSONArray(); - jsonArray.put(this.authentication.getUserName()); + jsonArray.put(this.username); jsonObject.put("assignees", jsonArray); this.executeRequest("/repos/" + project.getTitle() + "/issues", jsonObject.toString(), "POST"); }