Java tutorial
/* * Overchan Android (Meta Imageboard Client) * Copyright (C) 2014-2016 miku-nyan <https://github.com/miku-nyan> * * 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 <http://www.gnu.org/licenses/>. */ package nya.miku.wishmaster.chans.nullchan; import android.content.Context; import android.content.SharedPreferences; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.preference.EditTextPreference; import android.preference.PreferenceGroup; import android.support.v4.content.res.ResourcesCompat; import android.text.InputType; import android.text.TextUtils; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.SequenceInputStream; import java.util.regex.Matcher; import java.util.regex.Pattern; import nya.miku.wishmaster.R; import nya.miku.wishmaster.api.interfaces.CancellableTask; import nya.miku.wishmaster.api.interfaces.ProgressListener; import nya.miku.wishmaster.api.models.BoardModel; import nya.miku.wishmaster.api.models.UrlPageModel; import nya.miku.wishmaster.api.util.WakabaReader; public class NullchanclubModule extends AbstractInstant0chan { private static final String CHAN_NAME = "0chan.club"; private static final String DEFAULT_DOMAIN = "0chan.club"; private static final String DOMAINS_HINT = "0chan.club, 0chan.ml"; private static final String[] DOMAINS = new String[] { DEFAULT_DOMAIN, "0chan.ml" }; private static final String PREF_KEY_DOMAIN = "PREF_KEY_DOMAIN"; public NullchanclubModule(SharedPreferences preferences, Resources resources) { super(preferences, resources); } @Override public String getChanName() { return CHAN_NAME; } @Override public String getDisplayingName() { return " (0chan.club)"; } @Override public Drawable getChanFavicon() { return ResourcesCompat.getDrawable(resources, R.drawable.favicon_0chan, null); } @Override protected String getUsingDomain() { String domain = preferences.getString(getSharedKey(PREF_KEY_DOMAIN), DEFAULT_DOMAIN); return TextUtils.isEmpty(domain) ? DEFAULT_DOMAIN : domain; } @Override protected String[] getAllDomains() { String domain = getUsingDomain(); for (String d : DOMAINS) if (domain.equals(d)) return DOMAINS; String[] domains = new String[DOMAINS.length + 1]; for (int i = 0; i < DOMAINS.length; ++i) domains[i] = DOMAINS[i]; domains[DOMAINS.length] = domain; return domains; } @Override protected boolean canHttps() { return true; } @Override protected boolean canCloudflare() { return true; } private void addDomainPreference(PreferenceGroup group) { Context context = group.getContext(); EditTextPreference domainPref = new EditTextPreference(context); domainPref.setTitle(R.string.pref_domain); domainPref.setSummary(resources.getString(R.string.pref_domain_summary, DOMAINS_HINT)); domainPref.setDialogTitle(R.string.pref_domain); domainPref.setKey(getSharedKey(PREF_KEY_DOMAIN)); domainPref.getEditText().setHint(DEFAULT_DOMAIN); domainPref.getEditText().setSingleLine(); domainPref.getEditText().setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI); group.addPreference(domainPref); } @Override public void addPreferencesOnScreen(PreferenceGroup preferenceGroup) { addDomainPreference(preferenceGroup); super.addPreferencesOnScreen(preferenceGroup); } @Override public BoardModel getBoard(String shortName, ProgressListener listener, CancellableTask task) throws Exception { BoardModel model = super.getBoard(shortName, listener, task); model.defaultUserName = "?"; return model; } @Override protected WakabaReader getKusabaReader(InputStream stream, UrlPageModel urlModel) { if ((urlModel != null) && (urlModel.chanName != null) && urlModel.chanName.equals("expand")) { stream = new SequenceInputStream(new ByteArrayInputStream("<form id=\"delform\">".getBytes()), stream); } return new NullclubReader(stream, canCloudflare()); } private static class NullclubReader extends Instant0chanReader { private static final Pattern PATTERN_TIMESTAMP = Pattern .compile("<span[^>]+class=\"timestamp\"[^>]*\">(\\d+)</span>"); public NullclubReader(InputStream in, boolean canCloudflare) { super(in, canCloudflare); } @Override protected void parseDate(String date) { Matcher matcher = PATTERN_TIMESTAMP.matcher(date); if (matcher.find()) { currentPost.timestamp = Integer.parseInt(matcher.group(1)) * 1000L; } else { super.parseDate(date); } } } }