com.nttec.everychan.chans.synch.SynchModule.java Source code

Java tutorial

Introduction

Here is the source code for com.nttec.everychan.chans.synch.SynchModule.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.synch;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.preference.ListPreference;
import android.preference.PreferenceGroup;
import android.support.v4.content.res.ResourcesCompat;
import com.nttec.everychan.R;
import com.nttec.everychan.api.AbstractVichanModule;
import com.nttec.everychan.api.interfaces.CancellableTask;
import com.nttec.everychan.api.interfaces.ProgressListener;
import com.nttec.everychan.api.models.AttachmentModel;
import com.nttec.everychan.api.models.BoardModel;
import com.nttec.everychan.api.models.DeletePostModel;
import com.nttec.everychan.api.models.SimpleBoardModel;
import com.nttec.everychan.api.util.ChanModels;
import com.nttec.everychan.api.util.LazyPreferences;
import com.nttec.everychan.lib.org_json.JSONObject;

public class SynchModule extends AbstractVichanModule {

    private static final String CHAN_NAME = "syn-ch";
    private static final String DOMAINS_HINT = "syn-ch.com, syn-ch.org, syn-ch.ru, syn-ch.com.ua";
    private static final String[] DOMAINS = new String[] { "syn-ch.com", "syn-ch.org", "syn-ch.ru",
            "syn-ch.com.ua" };
    private static final SimpleBoardModel[] BOARDS = new SimpleBoardModel[] {
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "b", "", "?", true),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "d", "", "?", true),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "r", "Random", "?", true),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "a", "?", "?", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "g", "", "?", true),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "mlp", "My Little Pony", "?",
                    false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "mu", "", "?", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "r34", "Rule 34", "?", true),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "s", "", "?",
                    false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "tv", "  ?",
                    "?", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "vg", "", "?",
                    false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "wh", "Warhammer", "?", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "int", "Le int", "?", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "old", "", "?", false),
            ChanModels.obtainSimpleBoardModel(CHAN_NAME, "test", "? ", "?",
                    false), };
    private static final String[] ATTACHMENT_FORMATS = new String[] { "jpg", "png", "bmp", "svg", "swf", "mp3",
            "m4a", "flac", "zip", "rar", "tar", "gz", "txt", "pdf", "torrent", "webm" };
    private static final String PREF_KEY_DOMAIN = "PREF_KEY_DOMAIN";

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

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

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

    @Override
    public Drawable getChanFavicon() {
        return ResourcesCompat.getDrawable(resources, R.drawable.favicon_synch, null);
    }

    @Override
    protected String getUsingDomain() {
        return preferences.getString(getSharedKey(PREF_KEY_DOMAIN), DOMAINS[0]);
    }

    @Override
    protected String[] getAllDomains() {
        return DOMAINS;
    }

    @Override
    protected boolean canHttps() {
        return true;
    }

    @Override
    protected boolean useHttpsDefaultValue() {
        return false;
    }

    @Override
    public void addPreferencesOnScreen(PreferenceGroup preferenceGroup) {
        Context context = preferenceGroup.getContext();
        ListPreference domainPref = new LazyPreferences.ListPreference(context);
        domainPref.setKey(getSharedKey(PREF_KEY_DOMAIN));
        domainPref.setTitle(R.string.pref_domain);
        domainPref.setSummary(resources.getString(R.string.pref_domain_summary, DOMAINS_HINT));
        domainPref.setDialogTitle(R.string.pref_domain);
        domainPref.setEntries(DOMAINS);
        domainPref.setEntryValues(DOMAINS);
        domainPref.setDefaultValue(DOMAINS[0]);
        preferenceGroup.addPreference(domainPref);
        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.timeZoneId = "GMT+3";
        model.defaultUserName = "?";
        model.allowCustomMark = true;
        model.customMarkDescription = "Spoiler";
        model.attachmentsFormatFilters = ATTACHMENT_FORMATS;
        return model;
    }

    @Override
    protected AttachmentModel mapAttachment(JSONObject object, String boardName, boolean isSpoiler) {
        AttachmentModel model = super.mapAttachment(object, boardName, isSpoiler);
        if (model != null) {
            if (model.thumbnail != null)
                model.thumbnail = model.thumbnail.replace("//", "/").replaceAll("^/\\w+", "");
            if (model.path != null)
                model.path = model.path.replace("//", "/").replaceAll("^/\\w+", "");
        }
        return model;
    }

    @Override
    protected String getDeleteFormValue(DeletePostModel model) {
        return "";
    }

    @Override
    protected String getReportFormValue(DeletePostModel model) {
        return "??";
    }

    @Override
    public String fixRelativeUrl(String url) {
        if (url.startsWith("/src/") | url.startsWith("/thumb/"))
            return "http://cdn.syn-ch.com" + url;
        return super.fixRelativeUrl(url);
    }
}