com.money.manager.ex.settings.SyncPreferences.java Source code

Java tutorial

Introduction

Here is the source code for com.money.manager.ex.settings.SyncPreferences.java

Source

/*
 * Copyright (C) 2012-2016 The Android Money Manager Ex Project Team
 *
 * 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.money.manager.ex.settings;

import android.content.Context;
import android.content.SharedPreferences;

import com.money.manager.ex.R;

import org.apache.commons.lang3.math.NumberUtils;

/**
 * Handles sync-related preferences.
 */
public class SyncPreferences extends SettingsBase {

    public SyncPreferences(Context context) {
        super(context);

    }

    /**
     * Delete all preferences.
     */
    public void clear() {
        getPreferences().edit().clear().apply();
    }

    public boolean get(Integer key, boolean defaultValue) {
        return getPreferences().getBoolean(getKey(key), defaultValue);
    }

    public String get(Integer key, String defaultValue) {
        return getPreferences().getString(getKey(key), defaultValue);
    }

    @Override
    protected SharedPreferences getPreferences() {
        return getContext().getSharedPreferences(PreferenceConstants.SYNC_PREFERENCES, Context.MODE_PRIVATE);
    }

    public boolean isSyncEnabled() {
        return get(R.string.pref_sync_enabled, false);
    }

    public int getSyncInterval() {
        int defaultSchedule = 30; // time in minutes
        String setSchedule = get(R.string.pref_sync_interval, Integer.toString(defaultSchedule));
        if (!NumberUtils.isNumber(setSchedule))
            return defaultSchedule;

        return Integer.parseInt(setSchedule);
    }

    public boolean getUploadImmediately() {
        return get(R.string.pref_upload_immediately, true);
    }

    public String loadPreference(Integer key, String defaultValue) {
        String realKey = getContext().getString(key);

        return getPreferences().getString(realKey, defaultValue);
    }

    public void setSyncEnabled(boolean value) {
        set(R.string.pref_sync_enabled, value);
    }

    /**
     * Set synchronization period.
     * @param value Sync frequency in minutes.
     */
    public void setSyncInterval(int value) {
        set(R.string.pref_sync_interval, Integer.toString(value));
    }

    public boolean shouldSyncOnlyOnWifi() {
        return get(R.string.pref_sync_via_wifi, false);
    }

    // private

    private String getKey(Integer resourceId) {
        return getContext().getString(resourceId);
    }
}