nya.miku.wishmaster.chans.nullchan.NullchanoneModule.java Source code

Java tutorial

Introduction

Here is the source code for nya.miku.wishmaster.chans.nullchan.NullchanoneModule.java

Source

/*
 * 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 nya.miku.wishmaster.R;

public class NullchanoneModule extends AbstractInstant0chan {
    private static final String CHAN_NAME = "0chan.one";
    private static final String CHAN_DOMAIN = "0chan.one";

    private static final String PREF_KEY_DOMAIN = "PREF_KEY_DOMAIN";

    public NullchanoneModule(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_0chan, null);
    }

    @Override
    protected String getUsingDomain() {
        String domain = preferences.getString(getSharedKey(PREF_KEY_DOMAIN), CHAN_DOMAIN);
        return TextUtils.isEmpty(domain) ? CHAN_DOMAIN : domain;
    }

    @Override
    protected String[] getAllDomains() {
        if (!getChanName().equals(CHAN_NAME) || CHAN_DOMAIN.equals(getUsingDomain())) {
            return super.getAllDomains();
        }
        return new String[] { CHAN_DOMAIN, getUsingDomain() };
    }

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

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

    @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.setDialogTitle(R.string.pref_domain);
        domainPref.setKey(getSharedKey(PREF_KEY_DOMAIN));
        domainPref.getEditText().setHint(CHAN_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);
    }

}