Skip to content

Commit c8d7184

Browse files
authored
Merge pull request #49 from TheAndroidMaster/develop
Version 0.1.1
2 parents 9ee632c + d7ce205 commit c8d7184

File tree

14 files changed

+107
-40
lines changed

14 files changed

+107
-40
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ For demonstration and experimentation, an apk of the sample project can be downl
1919
The Gradle dependency is available through jCenter, which is used by default in Android Studio. To add the dependency to your project, copy this line into the dependencies section of your app's build.gradle file.
2020

2121
```gradle
22-
implementation 'me.jfenn:attribouter:0.1.0'
22+
implementation 'me.jfenn:attribouter:0.1.1'
2323
```
2424

2525
#### Starting an Activity

app/src/main/res/xml/about.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
login="TheAndroidMaster"
3232
position="1"
3333
task="Owner">
34-
<me.jfenn.attribouter.wedges.LinkWedge
34+
<me.jfenn.attribouter.wedges.link.LinkWedge
3535
name="Twitter"
3636
icon="https://twitter.com/favicon.ico"
3737
url="https://twitter.com/idontlikephp" />

attribouter/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apply plugin: 'com.android.library'
33
ext {
44
PUBLISH_GROUP_ID = 'me.jfenn'
55
PUBLISH_ARTIFACT_ID = 'attribouter'
6-
PUBLISH_VERSION = '0.1.0'
6+
PUBLISH_VERSION = '0.1.1'
77
}
88

99
android {
@@ -12,8 +12,8 @@ android {
1212
defaultConfig {
1313
minSdkVersion 15
1414
targetSdkVersion 27
15-
versionCode 9
16-
versionName "0.1.0"
15+
versionCode 10
16+
versionName "0.1.1"
1717

1818
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1919
vectorDrawables.useSupportLibrary = true

attribouter/src/main/java/me/jfenn/attribouter/data/github/GitHubData.java

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.ArrayList;
2222
import java.util.List;
2323
import java.util.Scanner;
24+
import java.util.concurrent.Semaphore;
2425

2526
public abstract class GitHubData {
2627

@@ -151,25 +152,85 @@ public GitHubData createInstance(Type type) {
151152
}
152153

153154
private static class GitHubThread extends Thread {
154-
155155
private File cacheFile;
156156
private GitHubData data;
157157
private String url;
158158
private String token;
159+
private Context context;
160+
161+
private static final int NUM_OF_PERMITS = 1;
162+
private Semaphore cacheToHttpSemaphore;
163+
private boolean continueHttpThread = false;
164+
private Thread cacheThread;
159165

160166
private GitHubThread(Context context, String token, GitHubData data, String url) {
161167
this.data = data;
162168
this.url = url;
163169
this.token = token;
170+
this.context = context;
171+
cacheToHttpSemaphore = new Semaphore(NUM_OF_PERMITS, true);
172+
cacheThread = startCacheThread();
173+
}
174+
175+
@Override
176+
public void run() {
177+
try {
178+
cacheToHttpSemaphore.acquire();
179+
180+
//in the event cache thread did not run before http thread (current thread)
181+
if (cacheThread.isAlive()) {
182+
try {
183+
//release lock so cache thread can run
184+
cacheToHttpSemaphore.release();
185+
//wait for cache thread to finish
186+
cacheThread.join();
187+
//get lock for this thread
188+
cacheToHttpSemaphore.acquire();
189+
} catch (InterruptedException e) {
190+
e.printStackTrace();
191+
}
192+
}
193+
} catch (InterruptedException e) {
194+
e.printStackTrace();
195+
}
196+
197+
try {
198+
if (continueHttpThread)
199+
doHttpConnection();
200+
} finally {
201+
cacheToHttpSemaphore.release();
202+
}
203+
}
204+
205+
private Thread startCacheThread() {
206+
Thread thread = new Thread(new Runnable() {
207+
@Override
208+
public void run() {
209+
try {
210+
cacheToHttpSemaphore.acquire();
211+
} catch (InterruptedException e) {
212+
e.printStackTrace();
213+
}
214+
215+
try {
216+
doCacheInspection();
217+
} finally {
218+
cacheToHttpSemaphore.release();
219+
}
220+
}
221+
});
222+
thread.setPriority(Thread.MAX_PRIORITY);
223+
thread.start();
224+
return thread;
225+
}
226+
227+
private void doCacheInspection() {
164228
File dir = new File(context.getCacheDir() + "/.attribouter/github");
165229
if (!dir.exists())
166230
dir.mkdirs();
167231

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

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

192253
if (cache != null) {
193254
callInit(cache);
194-
return;
195-
}
196-
}
255+
} else continueHttpThread = true;
256+
} else continueHttpThread = true;
257+
}
197258

259+
private void doHttpConnection(){
198260
HttpURLConnection connection = null;
199261
BufferedReader jsonReader = null;
200262
StringBuilder jsonBuilder = new StringBuilder();
@@ -206,12 +268,19 @@ public void run() {
206268
jsonReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
207269
if (connection.getResponseCode() == 403) {
208270
jsonReader.close();
271+
connection.disconnect();
209272
return;
210273
}
211274

212275
String line;
213-
while ((line = jsonReader.readLine()) != null)
276+
while ((line = jsonReader.readLine()) != null) {
277+
if(isInterrupted()){
278+
connection.disconnect();
279+
jsonReader.close();
280+
return;
281+
}
214282
jsonBuilder.append(line);
283+
}
215284
} catch (IOException e) {
216285
e.printStackTrace();
217286
jsonBuilder = null;

attribouter/src/main/java/me/jfenn/attribouter/wedges/ContributorWedge.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
import me.jfenn.attribouter.R;
1515
import me.jfenn.attribouter.data.github.GitHubData;
1616
import me.jfenn.attribouter.data.github.UserData;
17+
import me.jfenn.attribouter.dialogs.UserDialog;
18+
import me.jfenn.attribouter.interfaces.Mergeable;
19+
import me.jfenn.attribouter.utils.ResourceUtils;
1720
import me.jfenn.attribouter.wedges.link.EmailLinkWedge;
1821
import me.jfenn.attribouter.wedges.link.GitHubLinkWedge;
1922
import me.jfenn.attribouter.wedges.link.LinkWedge;
2023
import me.jfenn.attribouter.wedges.link.WebsiteLinkWedge;
21-
import me.jfenn.attribouter.dialogs.UserDialog;
22-
import me.jfenn.attribouter.interfaces.Mergeable;
23-
import me.jfenn.attribouter.utils.ResourceUtils;
2424

2525
public class ContributorWedge extends Wedge<ContributorWedge.ViewHolder> implements Mergeable<ContributorWedge> {
2626

@@ -54,11 +54,7 @@ public ContributorWedge(XmlResourceParser parser) throws XmlPullParserException,
5454
parser.getAttributeValue(null, "email"));
5555

5656
isHidden = parser.getAttributeBooleanValue(null, "hidden", false);
57-
5857
addChildren(parser);
59-
60-
if (login != null && !hasAll())
61-
addRequest(new UserData(login));
6258
}
6359

6460
ContributorWedge(@Nullable String login, @Nullable String name, @Nullable String avatarUrl, @Nullable String task, @Nullable Integer position, @Nullable String bio, @Nullable String blog, @Nullable String email) {
@@ -78,6 +74,9 @@ public ContributorWedge(XmlResourceParser parser) throws XmlPullParserException,
7874
addChild(new WebsiteLinkWedge(blog, 2));
7975
if (email != null)
8076
addChild(new EmailLinkWedge(email, -1));
77+
78+
if (login != null && !hasAll())
79+
addRequest(new UserData(login));
8180
}
8281

8382
@Override

attribouter/src/main/java/me/jfenn/attribouter/wedges/ContributorsWedge.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void bind(Context context, ViewHolder viewHolder) {
116116
viewHolder.topThreeView.setVisibility(View.GONE);
117117

118118
viewHolder.overflow.setVisibility(View.VISIBLE);
119-
viewHolder.overflow.setText(String.format(context.getString(R.string.title_attribouter_view_overflow), ResourceUtils.getString(context, contributorsTitle)));
119+
viewHolder.overflow.setText(ResourceUtils.getString(context, contributorsTitle));
120120

121121
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
122122
@Override

attribouter/src/main/java/me/jfenn/attribouter/wedges/LicenseWedge.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
import me.jfenn.attribouter.data.github.GitHubData;
2626
import me.jfenn.attribouter.data.github.LicenseData;
2727
import me.jfenn.attribouter.data.github.RepositoryData;
28+
import me.jfenn.attribouter.interfaces.Mergeable;
29+
import me.jfenn.attribouter.utils.ResourceUtils;
2830
import me.jfenn.attribouter.wedges.link.GitHubLinkWedge;
2931
import me.jfenn.attribouter.wedges.link.LicenseLinkWedge;
3032
import me.jfenn.attribouter.wedges.link.LinkWedge;
3133
import me.jfenn.attribouter.wedges.link.WebsiteLinkWedge;
32-
import me.jfenn.attribouter.interfaces.Mergeable;
33-
import me.jfenn.attribouter.utils.ResourceUtils;
3434

3535
public class LicenseWedge extends Wedge<LicenseWedge.ViewHolder> implements Mergeable<LicenseWedge> {
3636

@@ -79,14 +79,6 @@ public LicenseWedge(XmlResourceParser parser) throws IOException, XmlPullParserE
7979
parser.getAttributeValue(null, "license"));
8080

8181
addChildren(parser);
82-
83-
if (repo != null && !hasAllGeneric())
84-
addRequest(new RepositoryData(repo));
85-
if (licenseKey != null && (repo != null || title != null) && !hasAllLicense()) {
86-
LicenseData request = new LicenseData(licenseKey);
87-
request.addTag(token);
88-
addRequest(request);
89-
}
9082
}
9183

9284
public LicenseWedge(@Nullable String repo, @Nullable String title, @Nullable String description, @Nullable String licenseName, @Nullable String websiteUrl, @Nullable String gitHubUrl, @Nullable String licenseUrl, @Nullable String[] licensePermissions, @Nullable String[] licenseConditions, @Nullable String[] licenseLimitations, @Nullable String licenseDescription, @Nullable String licenseBody, @Nullable String licenseKey) {
@@ -115,6 +107,14 @@ public LicenseWedge(@Nullable String repo, @Nullable String title, @Nullable Str
115107
addChild(new GitHubLinkWedge(repo, 1));
116108
if (licenseBody != null || licenseUrl != null)
117109
addChild(new LicenseLinkWedge(this, 0));
110+
111+
if (repo != null && !hasAllGeneric())
112+
addRequest(new RepositoryData(repo));
113+
if (licenseKey != null && (repo != null || title != null) && !hasAllLicense()) {
114+
LicenseData request = new LicenseData(licenseKey);
115+
request.addTag(token);
116+
addRequest(request);
117+
}
118118
}
119119

120120
@Override

attribouter/src/main/java/me/jfenn/attribouter/wedges/LicensesWedge.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public void bind(Context context, ViewHolder viewHolder) {
187187
viewHolder.expand.setVisibility(View.GONE);
188188

189189
viewHolder.overflow.setVisibility(View.VISIBLE);
190-
viewHolder.overflow.setText(String.format(context.getString(R.string.title_attribouter_view_overflow), ResourceUtils.getString(context, title)));
190+
viewHolder.overflow.setText(ResourceUtils.getString(context, title));
191191

192192
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
193193
@Override

attribouter/src/main/java/me/jfenn/attribouter/wedges/TranslatorWedge.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ public TranslatorWedge(XmlResourceParser parser) throws IOException, XmlPullPars
4040
parser.getAttributeValue(null, "locales"),
4141
parser.getAttributeValue(null, "blog"),
4242
parser.getAttributeValue(null, "email"));
43-
44-
if (login != null && !hasEverything())
45-
addRequest(new UserData(login));
4643
}
4744

4845
TranslatorWedge(@Nullable String login, @Nullable String name, @Nullable String avatarUrl, @Nullable String locales, @Nullable String blog, @Nullable String email) {
@@ -53,6 +50,9 @@ public TranslatorWedge(XmlResourceParser parser) throws IOException, XmlPullPars
5350
this.locales = locales;
5451
this.blog = blog;
5552
this.email = email;
53+
54+
if (login != null && !hasEverything())
55+
addRequest(new UserData(login));
5656
}
5757

5858
@Override

attribouter/src/main/java/me/jfenn/attribouter/wedges/TranslatorsWedge.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void bind(Context context, ViewHolder viewHolder) {
124124
viewHolder.expand.setVisibility(View.GONE);
125125

126126
viewHolder.overflow.setVisibility(View.VISIBLE);
127-
viewHolder.overflow.setText(String.format(context.getString(R.string.title_attribouter_view_overflow), ResourceUtils.getString(context, translatorsTitle)));
127+
viewHolder.overflow.setText(ResourceUtils.getString(context, translatorsTitle));
128128

129129
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
130130
@Override

attribouter/src/main/res/layout/item_attribouter_contributors.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,6 @@
203203
android:paddingTop="4dp"
204204
android:textColor="?android:textColorPrimary"
205205
android:textSize="18sp"
206-
tools:text="@string/title_attribouter_view_overflow" />
206+
tools:text="View Contributors" />
207207

208208
</LinearLayout>

attribouter/src/main/res/layout/item_attribouter_licenses.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@
4747
android:paddingBottom="4dp"
4848
android:textSize="18sp"
4949
android:textColor="?android:textColorPrimary"
50-
tools:text="@string/title_attribouter_view_overflow" />
50+
tools:text="View Licenses" />
5151

5252
</LinearLayout>

attribouter/src/main/res/layout/item_attribouter_translators.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@
4747
android:paddingTop="4dp"
4848
android:textColor="?android:textColorPrimary"
4949
android:textSize="18sp"
50-
tools:text="@string/title_attribouter_view_overflow" />
50+
tools:text="View Translators" />
5151

5252
</LinearLayout>

attribouter/src/main/res/values/strings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,5 @@
1313
<string name="title_attribouter_license_conditions">Conditions</string>
1414
<string name="title_attribouter_license_limitations">Limitations</string>
1515
<string name="title_attribouter_more_info">More Info</string>
16-
<string name="title_attribouter_view_overflow">View %s</string>
1716
<string name="desc_attribouter_licenses_legal">This is not legal advice. The above information is intended solely to inform the user of general information about open source licenses. Open source licenses allow software to be freely used, modified, and shared. However, they do not come without exceptions. Please be aware of the restrictions of the licenses that you are using before publishing your software.</string>
1817
</resources>

0 commit comments

Comments
 (0)