Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved data retrieval in GitHubData class #48

Merged
merged 9 commits into from
Aug 16, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.Semaphore;

public abstract class GitHubData {

Expand Down Expand Up @@ -151,25 +152,85 @@ public GitHubData createInstance(Type type) {
}

private static class GitHubThread extends Thread {

private File cacheFile;
private GitHubData data;
private String url;
private String token;
private Context context;

private static final int NUM_OF_PERMITS = 1;
private Semaphore cacheToHttpSemaphore;
private boolean continueHttpThread = false;
private Thread cacheThread;

private GitHubThread(Context context, String token, GitHubData data, String url) {
this.data = data;
this.url = url;
this.token = token;
this.context = context;
cacheToHttpSemaphore = new Semaphore(NUM_OF_PERMITS, true);
cacheThread = startCacheThread();
}

@Override
public void run() {
try {
cacheToHttpSemaphore.acquire();

//in the event cache thread did not run before http thread (current thread)
if (cacheThread.isAlive()) {
try {
//release lock so cache thread can run
cacheToHttpSemaphore.release();
//wait for cache thread to finish
cacheThread.join();
//get lock for this thread
cacheToHttpSemaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}

try {
if (continueHttpThread)
doHttpConnection();
} finally {
cacheToHttpSemaphore.release();
}
}

private Thread startCacheThread() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
cacheToHttpSemaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}

try {
doCacheInspection();
} finally {
cacheToHttpSemaphore.release();
}
}
});
thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
return thread;
}

private void doCacheInspection() {
File dir = new File(context.getCacheDir() + "/.attribouter/github");
if (!dir.exists())
dir.mkdirs();

cacheFile = new File(dir, url.replace("/", ".") + ".json");
}

@Override
public void run() {
String cache = null;
if (Math.abs(System.currentTimeMillis() - cacheFile.lastModified()) < 864000000) {
StringBuilder cacheBuilder = new StringBuilder();
Expand All @@ -191,10 +252,11 @@ public void run() {

if (cache != null) {
callInit(cache);
return;
}
}
} else continueHttpThread = true;
} else continueHttpThread = true;
}

private void doHttpConnection(){
HttpURLConnection connection = null;
BufferedReader jsonReader = null;
StringBuilder jsonBuilder = new StringBuilder();
Expand All @@ -206,12 +268,19 @@ public void run() {
jsonReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
if (connection.getResponseCode() == 403) {
jsonReader.close();
connection.disconnect();
return;
}

String line;
while ((line = jsonReader.readLine()) != null)
while ((line = jsonReader.readLine()) != null) {
if(isInterrupted()){
connection.disconnect();
jsonReader.close();
return;
}
jsonBuilder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
jsonBuilder = null;
Expand Down
4 changes: 4 additions & 0 deletions docs/CONFIGURATION.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
title: The Configuration File
---

# The Configuration File

The great File of Configuration is truly a mighty hurdle to overcome when implementing this library. It contains many confusing and difficult concepts such as "tags" and "attributes". However, when it comes down to it, it is really just a weird form of layout file that specifies the information to display instead of how to display it.
Expand Down
1 change: 0 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Welcome to Attribouter's in-depth documentation page! It provides several exampl
- [LicenseWedge](./wedges/LICENSE.md)
- [TranslatorsWedge](./wedges/TRANSLATORS.md)
- [TranslatorWedge](./wedges/TRANSLATOR.md)
- [HeaderWedge](./wedges/HEADER.md)
- [TextWedge](./wedges/TEXT.md)
- [LinkWedge](./wedges/LINK.md)
- [EmailLinkWedge](./wedges/LINK.md#email)
Expand Down
4 changes: 4 additions & 0 deletions docs/RESOURCES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
title: Overriding Resources and Providing Translations
---

# Overriding Resources and Providing Translations

## Problem
Expand Down
6 changes: 5 additions & 1 deletion docs/SUBWEDGING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Subwedging
---
title: Extending a Wedge
---

# Extending a Wedge

Since the wedges used in the layout file are referenced by their corresponding class name, you may have guessed that it is possible to create your own wedge similarly to how one would create a custom view. You would be correct in that assumption. That said, because there are so many different use cases for this, I'm not going to provide a full "tutorial", but rather explain the basics of how wedges can be created.

Expand Down
4 changes: 3 additions & 1 deletion docs/THEMING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Theming
---
title: Custom Themes
---

## Activity / Dialog Themes

Expand Down
2 changes: 2 additions & 0 deletions docs/_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
url: https://jfenn.me
baseurl: /Attribouter
47 changes: 47 additions & 0 deletions docs/_layouts/default.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% if page.title %}{{ page.title }} – {% endif %}Docutabouttributes</title>

<link href="https://fonts.googleapis.com/css?family=Miriam+Libre|Roboto+Mono" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="https://jfenn.me/css/styles.css">
<link rel="stylesheet" type="text/css" href="https://jfenn.me/css/md.css">

<style>
img {
max-width: 100%;
max-height: 100vh;
margin: 1em 0;
}

div#main {
margin-left: 10vw;
margin-right: 10vw;
}
</style>
</head>

<body>
<nav>
<button class="selectable" onclick="location.href='/';"><i class="material-icons">home</i><span class="desktop">Home</span></button>
<button class="selectable" onclick="location.href='{{ site.baseurl }}/';" selected="true"><i class="material-icons">notes</i><span class="desktop">Docs</span></button>
<button class="selectable" onclick="location.href='/redirects/?t=github&d={{ site.baseurl }}';"><i class="material-icons">code</i><span class="desktop">GitHub</span></button>
<button class="selectable" onclick="location.href='/about/?{{ site.baseurl }}';"><i class="material-icons">info</i><span class="desktop">About</span></button>
</nav>

<div id="main" role="main" class="container">
{{ content }}
<br>
</div>

<footer>
<hr>
<a href="/redirects/?t=github&d={{ site.baseurl }}/tree/master/docs" target="_blank">Docs Source Code</a>
<br>
<a href="/policies/?web" target="_blank">Privacy Policy</a>
</footer>
</body>
</html>
4 changes: 4 additions & 0 deletions docs/utils/CROWDIN.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
title: Automating CrowdIn Translations
---

As stated in [issue #23](https://jfenn.me/redirects/?t=github&d=Attribouter/issues/23), no kind of "proper" support for CrowdIn will be added to Attribouter for a while because [their API does not look very fun](https://support.crowdin.com/api/api-integration-setup/). However, [@deletescape](https://github.com/deletescape) has written [a python script](https://github.com/deletescape/dscripts/blob/8b261226deda604df7405708e6e7ae67b6d2e480/gettranslators.py) (below, slightly modified to update the syntax) that uses their API to fetch translators and output them to a file in the correct syntax, which you may find useful for handling large amounts of translators.

```python
Expand Down
4 changes: 4 additions & 0 deletions docs/wedges/APP.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
title: AppWedge
---

The `AppWedge` displays the app icon, name, version, and links to the project sites in a centered layout. By default, the icon and app name are fetched from your app's `AndroidManifest.xml`, and all other information is fetched from the GitHub API if the `repo` attribute has been specified.

## Example
Expand Down
4 changes: 4 additions & 0 deletions docs/wedges/CONTRIBUTOR.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
title: ContributorWedge
---

The `ContributorWedge` displays a set of information about a person in a small layout. If the `bio` attribute is defined, a dialog will appear upon interaction displaying more information, such as... um... their biography... and links. If it is not defined, it will open the highest priority [`LinkWedge`](./LINK.md) child instead.

## Example
Expand Down
4 changes: 4 additions & 0 deletions docs/wedges/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
title: ContributorsWedge
---

A `ContributorsWedge` displays a list of child [`ContributorWedge`](./CONTRIBUTOR.md)s under a title, sorted by their `position` attributes. If the `repo` attribute is specified, this list will be merged with contributors from GitHub. If the `showDefaults` attribute is unspecified ot `true`, it will also merge it with contributors to this repository.

If a user with the login "TheAndroidMaster" is both in GitHub and the configuration file, its attributes will be merged so that any attributes beginning with a "^" character will override the information from GitHub, and any attributes not beginning with a "^" character will be used while the GitHub information is loading, or if the information from GitHub is not present or unavailable. See ["Overriding Resources and Providing Translations"](../RESOURCES.md) for more information on how merges work.
Expand Down
Empty file removed docs/wedges/HEADER.md
Empty file.
6 changes: 5 additions & 1 deletion docs/wedges/LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
title: LicenseWedge
---

The `LicenseWedge` is intended to display information about libraries used by your project. Simply displaying this wedge does not guarantee that you are following all of the conditions of the project's license, but it is a good start.

If only the `repo` attribute is specified, this wedge can fetch all of its necessary information from the GitHub API, however it is a good idea to include all of the information in the configuration file as well, so that it is still displayed if the user is offline or there is an issue with the GitHub API.
Expand Down Expand Up @@ -30,7 +34,7 @@ If only the `repo` attribute is specified, this wedge can fetch all of its neces

## Auto-generated Links

The links that are automatically created by this wedge are as follows. See [`LinkWedge`](./LINKS.md) for more information.
The links that are automatically created by this wedge are as follows. See [`LinkWedge`](./LINK.md) for more information.

|ID|Description|Required Attributes|
|-----|-----|-----|
Expand Down
4 changes: 4 additions & 0 deletions docs/wedges/LICENSES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
title: LicensesWedge
---

The `LicensesWedge` displays a list of child [`LicenseWedge`](./LICENSE.md)s with a title at the top. If the `showDefaults` attribute is undefined or `true`, this list will be merged with the licenses used by Attribouter. See ["Overriding Resources and Providing Translations"](../RESOURCES.md) for more information on how merges work.

## Example
Expand Down
4 changes: 4 additions & 0 deletions docs/wedges/LINK.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
title: LinkWedge
---

Link tags can exist as children of the [App](./APP.md), [Contributor](./CONTRIBUTOR.md), and [License](./LICENSE.md) wedges. They can also be added as children of the root tag, though that is not their intended use. A few links are generated automatically by these wedges, such as links to the github profile of contributors for which the `login` attribute has been specified, and so on. These auto-generated links are specified in the wedges' individual documentation pages.

If you wish to remove an auto-generated link, you can simply define a `LinkWedge` with the same `id` and the `hidden` attribute. However, if you want to modify another value of an auto-generated link, such as the title or icon, you may need to specify that you want to override consecutive merges as well so that it is not overwritten if the wedge receives more information from the GitHub API. See ["Overriding Resources and Providing Translations"](../RESOURCES.md) for more information on overriding merges.
Expand Down
4 changes: 4 additions & 0 deletions docs/wedges/TEXT.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
title: TextWedge
---

A `TextWedge` is a block of text. Fairly simple.

## Example
Expand Down
4 changes: 4 additions & 0 deletions docs/wedges/TRANSLATOR.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
title: TranslatorWedge
---

The `TranslatorWedge` displays information about a translator in an only slightly more concise layout than the [`ContributorWedge`](./CONTRIBUTOR.md).

## Example
Expand Down
4 changes: 4 additions & 0 deletions docs/wedges/TRANSLATORS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
title: TranslatorsWedge
---

The `TranslatorsWedge` displays a list of [`TranslatorWedge`](./TRANSLATOR.md)s in sections, sorted by locale. If a translator has translated multiple locales, they will appear under every locale that they have translated.

## Example
Expand Down