Skip to content

Commit

Permalink
make app faster
Browse files Browse the repository at this point in the history
  • Loading branch information
domjos1994 committed Oct 30, 2019
1 parent aab093a commit e08fc83
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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(" (");
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/layout/spinner_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:text="@string/versions"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"/>
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public boolean addProjects() {

@Override
public boolean updateProjects() {
return false;
return true;
}

@Override
public boolean deleteProjects() {
return false;
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public interface AccessTokenProvider {

String authType();
String token();
String refreshToken();
String authorization();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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("")) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) -> {
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,26 @@

public final class Github extends JSONEngine implements IBugService<Long> {
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
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;
Expand All @@ -71,7 +80,7 @@ public String getTrackerVersion() {
@Override
public List<Project<Long>> getProjects() throws Exception {
List<Project<Long>> 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());
Expand All @@ -87,13 +96,13 @@ public List<Project<Long>> 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());
}
Expand Down Expand Up @@ -134,7 +143,7 @@ public Long insertOrUpdateProject(Project<Long> project) throws Exception {
method = "POST";
} else {
url = "/repos/" + project.getTitle();
method = "POST";
method = "PATCH";
}

int status = this.executeRequest(url, jsonObject.toString(), method);
Expand All @@ -159,7 +168,7 @@ public List<Version<Long>> getVersions(String filter, Long project_id) throws Ex
List<Version<Long>> versions = new LinkedList<>();
Project<Long> 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());
Expand All @@ -168,7 +177,7 @@ public List<Version<Long>> getVersions(String filter, Long project_id) throws Ex
Version<Long> 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);
Expand All @@ -193,10 +202,10 @@ public void insertOrUpdateVersion(Version<Long> version, Long project_id) throws
Project<Long> 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";
}

Expand All @@ -208,7 +217,7 @@ public void insertOrUpdateVersion(Version<Long> version, Long project_id) throws
public void deleteVersion(Long id, Long project_id) throws Exception {
Project<Long> 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);
}
}

Expand Down Expand Up @@ -295,8 +304,8 @@ public Issue<Long> 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")));
}
}
Expand All @@ -311,7 +320,7 @@ public void insertOrUpdateIssue(Issue<Long> 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");
}
Expand Down

0 comments on commit e08fc83

Please sign in to comment.