Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (C) Stichting Akvo (Akvo Foundation)
 *
 * This file is part of Akvo Caddisfly.
 *
 * Akvo Caddisfly 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.
 *
 * Akvo Caddisfly 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 Akvo Caddisfly. If not, see <http://www.gnu.org/licenses/>.
 */

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

import android.preference.PreferenceManager;
import android.support.annotation.StringRes;

public class Main {
    private static final String KEY_FORMAT = "%s_%s";

    /**
     * Checks if the key is already saved in the preferences
     *
     * @param context the context
     * @param keyId   the key id
     * @return true if found
     */
    public static boolean containsKey(Context context, @StringRes int keyId) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPreferences.contains(getKey(context, keyId));
    }

    /**
     * Gets a preference key from strings
     *
     * @param context the context
     * @param keyId   the key id
     * @return the string key
     */
    private static String getKey(Context context, @StringRes int keyId) {
        return context.getString(keyId);
    }

    /**
     * Gets a string value from preferences
     *
     * @param context      the context
     * @param keyId        the key id
     * @param defaultValue default value
     * @return the stored string value
     */
    public static String getString(Context context, @StringRes int keyId, String defaultValue) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPreferences.getString(getKey(context, keyId), defaultValue);
    }

    /**
     * Gets a string value from preferences
     *
     * @param context      the context
     * @param keyId        the key id
     * @param defaultValue default value
     * @return the stored string value
     */
    public static String getString(Context context, String keyId, String defaultValue) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPreferences.getString(keyId, defaultValue);
    }

    public static String getString(Context context, String code, @StringRes int keyId, String defaultValue) {
        String key = String.format(KEY_FORMAT, code, getKey(context, keyId));
        return getString(context, key, defaultValue);
    }
}