diff --git a/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/presenter/SitesSetupPresenter.java b/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/presenter/SitesSetupPresenter.java index 4b14a6d209..c3d000b16a 100644 --- a/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/presenter/SitesSetupPresenter.java +++ b/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/presenter/SitesSetupPresenter.java @@ -16,6 +16,7 @@ */ package com.github.adamantcheese.chan.core.presenter; +import android.content.Context; import android.widget.Toast; import com.github.adamantcheese.chan.R; @@ -107,8 +108,8 @@ public void onShowDialogClicked() { callback.showAddDialog(); } - public void onAddClicked(String url) { - siteService.addSite(url, new SiteService.SiteAddCallback() { + public void onAddClicked(String url, Context ctx) { + siteService.addSite(url, ctx, new SiteService.SiteAddCallback() { @Override public void onSiteAdded(Site site) { siteAdded(site); diff --git a/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/site/SiteService.java b/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/site/SiteService.java index 2f81549a4e..81ee399317 100644 --- a/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/site/SiteService.java +++ b/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/site/SiteService.java @@ -21,6 +21,20 @@ import com.github.adamantcheese.chan.core.settings.json.JsonSettings; import java.util.List; +import java.net.URI; +import java.net.URISyntaxException; + +import com.github.adamantcheese.chan.core.site.sites.lynxchan.LynxChanIndexRequest; +import com.github.adamantcheese.chan.core.site.sites.lynxchan.LynxChanIndexResult; + +import android.content.Context; +import com.android.volley.RequestQueue; +import com.android.volley.VolleyError; +import com.android.volley.Cache; +import com.android.volley.Network; +import com.android.volley.toolbox.DiskBasedCache; +import com.android.volley.toolbox.BasicNetwork; +import com.android.volley.toolbox.HurlStack; import javax.inject.Inject; @@ -30,6 +44,8 @@ public class SiteService { private boolean initialized = false; + RequestQueue requestQueue; + @Inject public SiteService(SiteRepository siteRepository, SiteResolver resolver) { this.siteRepository = siteRepository; @@ -40,7 +56,7 @@ public boolean areSitesSetup() { return !siteRepository.all().getAll().isEmpty(); } - public void addSite(String url, SiteAddCallback callback) { + public void addSite(String url, Context ctx, SiteAddCallback callback) { Site existing = resolver.findSiteForUrl(url); if (existing != null) { callback.onSiteAddFailed("site already added"); @@ -53,7 +69,25 @@ public void addSite(String url, SiteAddCallback callback) { if (resolve.match == SiteResolver.SiteResolverResult.Match.BUILTIN) { siteClass = resolve.builtinResult; } else if (resolve.match == SiteResolver.SiteResolverResult.Match.EXTERNAL) { - callback.onSiteAddFailed("external sites not hardcoded is not implemented yet"); + callback.onSiteAddFailed("not detected in predefined list, checking if LynxChan instance..."); + + Cache cache = new DiskBasedCache(ctx.getCacheDir(), 1024 * 1024); // 1MB cap + Network network = new BasicNetwork(new HurlStack()); + requestQueue = new RequestQueue(cache, network); + requestQueue.start(); + try { + URI uri = new URI(url); + + // hacky little trick that gets around the fact that new URI("16chan.xyz") + // sets the host to null and the path to "16chan.xyz" while + // new URI("https://16chan.xyz/") sets the host to "16chan.xyz" and the + // path to null + String host = uri.getHost() != null ? uri.getHost() : uri.getPath(); + requestQueue.add(new LynxChanIndexRequest(host, response -> { lynxChanIndexResponse(response, callback); }, error -> { lynxChanIndexFailure(error, callback); })); + } catch (URISyntaxException e) { + callback.onSiteAddFailed("not a url"); + } + return; } else { callback.onSiteAddFailed("not a url"); @@ -65,6 +99,18 @@ public void addSite(String url, SiteAddCallback callback) { callback.onSiteAdded(site); } + public void lynxChanIndexResponse(LynxChanIndexResult response, SiteAddCallback callback) { + if (response.IsLynxChanInstance()) { + callback.onSiteAddFailed("detected LynxChan instance, but not implemented yet"); + } else { + callback.onSiteAddFailed("not a valid LynxChan instance"); + } + } + + public void lynxChanIndexFailure(VolleyError error, SiteAddCallback callback) { + callback.onSiteAddFailed("not in premade list and can't detect chansite instance"); + } + public void updateUserSettings(Site site, JsonSettings jsonSettings) { SiteModel siteModel = siteRepository.byId(site.id()); if (siteModel == null) throw new NullPointerException("siteModel == null"); diff --git a/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/site/sites/lynxchan/LynxChanIndexRequest.java b/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/site/sites/lynxchan/LynxChanIndexRequest.java new file mode 100644 index 0000000000..14ec2b101f --- /dev/null +++ b/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/site/sites/lynxchan/LynxChanIndexRequest.java @@ -0,0 +1,59 @@ +/* + * Kuroba - *chan browser https://github.com/TheStranjer/Kuroba (fork of https://github.com/Adamantcheese/Kuroba) + * Copyright (C) 2019 TheStranjer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.github.adamantcheese.chan.core.site.sites.lynxchan; + +import com.android.volley.Response.ErrorListener; +import com.android.volley.Response.Listener; + +import com.github.adamantcheese.chan.core.net.JsonReaderRequest; +import android.util.JsonReader; +import android.util.JsonToken; + +import okhttp3.HttpUrl; + +public class LynxChanIndexRequest extends JsonReaderRequest { + private static final String TAG = "LynxChanInstanceCheck"; + public LynxChanIndexRequest(String hostname, Listener listener, ErrorListener errorListener) { + super(new HttpUrl.Builder().scheme("https").host(hostname).addPathSegment("index.json").build().toString(), listener, errorListener); + } + + @Override + public LynxChanIndexResult readJson(JsonReader reader) throws Exception { + reader.beginObject(); + while (reader.hasNext()) { + if (reader.peek() != JsonToken.NAME) { + reader.skipValue(); + continue; + } + + String key = reader.nextName(); + switch (key) { + case "version": + String nextString = reader.nextString(); + return new LynxChanIndexResult(nextString); + default: + break; + } + } + + return new LynxChanIndexResult(); + } + + +} diff --git a/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/site/sites/lynxchan/LynxChanIndexResult.java b/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/site/sites/lynxchan/LynxChanIndexResult.java new file mode 100644 index 0000000000..fe6da1a6f3 --- /dev/null +++ b/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/site/sites/lynxchan/LynxChanIndexResult.java @@ -0,0 +1,15 @@ +package com.github.adamantcheese.chan.core.site.sites.lynxchan; + +public class LynxChanIndexResult { + public String version; + + public LynxChanIndexResult(String version) { + this.version = version; + } + + public LynxChanIndexResult() {} + + public boolean IsLynxChanInstance() { + return version != null; + } +} \ No newline at end of file diff --git a/Kuroba/app/src/main/java/com/github/adamantcheese/chan/ui/layout/SiteAddLayout.java b/Kuroba/app/src/main/java/com/github/adamantcheese/chan/ui/layout/SiteAddLayout.java index 2ac91bc708..0a5cb53bfc 100644 --- a/Kuroba/app/src/main/java/com/github/adamantcheese/chan/ui/layout/SiteAddLayout.java +++ b/Kuroba/app/src/main/java/com/github/adamantcheese/chan/ui/layout/SiteAddLayout.java @@ -19,6 +19,8 @@ public class SiteAddLayout private Dialog dialog; private SitesSetupPresenter presenter; + private Context ctx; + public SiteAddLayout(Context context) { this(context, null); } @@ -29,6 +31,7 @@ public SiteAddLayout(Context context, AttributeSet attrs) { public SiteAddLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); + this.ctx = context; } @Override @@ -60,7 +63,7 @@ protected void onDetachedFromWindow() { } public void onPositiveClicked() { - presenter.onAddClicked(url.getText().toString()); + presenter.onAddClicked(url.getText().toString(), ctx); } @Override