com.nttec.everychan.chans.nullchan.NullchanccModule.java Source code

Java tutorial

Introduction

Here is the source code for com.nttec.everychan.chans.nullchan.NullchanccModule.java

Source

/*
 * Everychan 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 com.nttec.everychan.chans.nullchan;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.SequenceInputStream;
import android.annotation.SuppressLint;
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 com.nttec.everychan.R;
import com.nttec.everychan.api.interfaces.CancellableTask;
import com.nttec.everychan.api.interfaces.ProgressListener;
import com.nttec.everychan.api.models.BoardModel;
//import com.nttec.everychan.api.models.CaptchaModel;
import com.nttec.everychan.api.models.SimpleBoardModel;
import com.nttec.everychan.api.models.UrlPageModel;
import com.nttec.everychan.api.util.ChanModels;
import com.nttec.everychan.api.util.ReplacingReader;
import com.nttec.everychan.api.util.WakabaReader;

public class NullchanccModule extends AbstractInstant0chan {
    private static final String CHAN_NAME = "0chan.cc";
    private static final String DEFAULT_DOMAIN = "0chan.cc";
    private static final String DOMAINS_HINT = "0chan.cc, 31.220.3.61";
    private static final SimpleBoardModel[] BOARDS = new SimpleBoardModel[] {
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "b", "", "all", true),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "d", "?", "all", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "r", "?", "all", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "0", " ?", "all", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "e", "?", "geek", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "t", "", "geek", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "hw", "", "geek", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "s", "", "geek", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "c", "", "geek", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "vg", "", "geek", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "8", "8-bit  pixel art", "geek", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "bg", "?? ", "geek", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "wh", "Warhammer", "geek", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "a", "?", "other", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "au", "?", "other", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "bo", "", "other", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "co", "?", "other", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "cook", " ?", "other", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "f", "Flash", "other", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "fa", "  ?", "other", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "fl", "? ?", "other", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "m", "", "other", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "med", "", "other", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "ne", "", "other", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "ph", "", "other", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "tv", "  ?", "other", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "wp", "", "other", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "war", "", "other", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "h", "", "adult", true),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "g", "", "adult", true),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "fur", "", "adult", true) };
    private static final String PREF_KEY_DOMAIN = "PREF_KEY_DOMAIN";

    public NullchanccModule(SharedPreferences preferences, Resources resources) {
        super(preferences, resources);
    }

    @Override
    public String getChanName() {
        return CHAN_NAME;
    }

    @Override
    public String getDisplayingName() {
        return " (0chan.cc)";
    }

    @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() {
        if (!getChanName().equals(CHAN_NAME) || getUsingDomain().equals(DEFAULT_DOMAIN))
            return super.getAllDomains();
        return new String[] { DEFAULT_DOMAIN, getUsingDomain() };
    }

    @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
    protected SimpleBoardModel[] getBoardsList() {
        return BOARDS;
    }

    @Override
    public BoardModel getBoard(String shortName, ProgressListener listener, CancellableTask task) throws Exception {
        BoardModel model = super.getBoard(shortName, listener, task);
        model.defaultUserName = "?";
        return model;
    }

    @SuppressLint("SimpleDateFormat")
    @Override
    protected WakabaReader getKusabaReader(InputStream stream, UrlPageModel urlModel) {
        Reader reader;
        if (urlModel != null && urlModel.chanName != null && urlModel.chanName.equals("expand")) {
            stream = new SequenceInputStream(new ByteArrayInputStream("<form id=\"delform\">".getBytes()), stream);
            reader = new BufferedReader(new InputStreamReader(stream));
        } else {
            reader = new ReplacingReader(new BufferedReader(new InputStreamReader(stream)),
                    "<form id=\"delform20\"", "<form id=\"delform\"");
        }
        return new Instant0chanReader(reader, canCloudflare());
    }

    /*
    @Override
    public CaptchaModel getNewCaptcha(String boardName, String threadNumber, ProgressListener listener, CancellableTask task) throws Exception {
    CaptchaModel captcha = super.getNewCaptcha(boardName, threadNumber, listener, task);
    captcha.type = CaptchaModel.TYPE_NORMAL_DIGITS;
    return captcha;
    }
    */

    @Override
    public String fixRelativeUrl(String url) {
        if (useHttps())
            url = url.replace("http://0chan.cc", "https://0chan.cc");
        return super.fixRelativeUrl(url);
    }

}