Utils.java 6.92 KB
package com.gimi.common.cinema.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.gimi.common.cinema.model.CinemaConfig;
import com.gimi.common.cinema.model.Constant;
import com.gimi.common.cinema.model.SambaMsg;

/**
 * Created by 李攀 on 2015/12/21
 */
public class Utils {
    public static SambaMsg getSambaMsg(SharedPreferences sharedPreferences) {
        String sss = sharedPreferences.getString("ip", Constant.DEFAULT_IP);
        String usr = sharedPreferences.getString("usr", Constant.DEFAULT_USER);
        String pwd = sharedPreferences.getString("pwd", Constant.DEFAULT_PWD);
        String folder = sharedPreferences.getString("folder", Constant.DEFAULT_FOLDER);
        String path = sharedPreferences.getString("path", Constant.DEFAULT_PATH);

        return new SambaMsg(sss, usr, pwd, folder, path);
    }

    public static SharedPreferences getSp(Context context) {
        return context.getSharedPreferences(
                Constant.XML_NAME, Context.MODE_PRIVATE);
    }

    public static void saveSambaMsg(Context context, SambaMsg sambaMsg) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                Constant.XML_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("ip", sambaMsg.getIp());
        editor.putString("usr", sambaMsg.getUsr());
        editor.putString("pwd", sambaMsg.getPwd());
        editor.putString("folder", sambaMsg.getFolder());
        editor.apply();
    }

    public static SambaMsg getSambaMsg(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                Constant.XML_NAME, Context.MODE_PRIVATE);
        return getSambaMsg(sharedPreferences);
    }

    public static String getString(Context context, String name) {
        return getString(context, name, "");
    }

    public static String getString(Context context, String name, String defaultValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                Constant.XML_NAME, Context.MODE_PRIVATE);

        return sharedPreferences.getString(name, defaultValue);
    }

    public static String getString(SharedPreferences sharedPreferences, String name, String defaultValue) {
        return sharedPreferences.getString(name, defaultValue);
    }

    public static int getInt(SharedPreferences sharedPreferences, String name, int defaultValue) {
        return sharedPreferences.getInt(name, defaultValue);
    }

    public static int getInt(Context context, String name, int defaultValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                Constant.XML_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getInt(name, defaultValue);
    }

    public static long getLong(SharedPreferences sharedPreferences, String name, long defaultValue) {
        return sharedPreferences.getLong(name, defaultValue);
    }

    public static long getLong(SharedPreferences sharedPreferences, String name) {
        return sharedPreferences.getLong(name, 0);
    }

    public static long getLong(Context  context, String name) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                Constant.XML_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getLong(name, 0);
    }

    public static void saveString(Context context, String name, String value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                Constant.XML_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor1 = sharedPreferences.edit();
        editor1.putString(name, value);
        editor1.apply();
    }

    public static void saveString(SharedPreferences sharedPreferences, String name, String value) {
        SharedPreferences.Editor editor1 = sharedPreferences.edit();
        editor1.putString(name, value);
        editor1.apply();
    }

    public static void saveInt(Context context, String name, int value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                Constant.XML_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor1 = sharedPreferences.edit();
        editor1.putInt(name, value);
        editor1.apply();
    }


    public static void saveLong(Context context, String name, long value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                Constant.XML_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor1 = sharedPreferences.edit();
        editor1.putLong(name, value);
        editor1.apply();
    }

    public static void saveLong(SharedPreferences sharedPreferences, String name, long value) {
        SharedPreferences.Editor editor1 = sharedPreferences.edit();
        editor1.putLong(name, value);
        editor1.apply();
    }


    public static int getString(Context context, String name, int defaultValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                Constant.XML_NAME, Context.MODE_PRIVATE);

        return sharedPreferences.getInt(name, defaultValue);
    }

    public static boolean getBool(Context context, String name) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                Constant.XML_NAME, Context.MODE_PRIVATE);

        return sharedPreferences.getBoolean(name, false);
    }


    public static void saveBool(Context context, String name, boolean value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                Constant.XML_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor1 = sharedPreferences.edit();
        editor1.putBoolean(name, value);
        editor1.apply();
    }

    public static CinemaConfig readConfigBySP(SharedPreferences sharedPreferences) {
        CinemaConfig cinemaConfig = new CinemaConfig();
        int classVersion = sharedPreferences.getInt("classifications-version", 0);
        int dbVersion = sharedPreferences.getInt("db-version", 0);
        cinemaConfig.setClassVersion(classVersion);
        cinemaConfig.setDbVersion(dbVersion);
        String classifications = sharedPreferences.getString("classifications", "");
        String[] classArray = null;
        if (!TextUtils.isEmpty(classifications)) {
            classArray = classifications.split("@");
            cinemaConfig.setClassArray(classArray);
        }
        return cinemaConfig;
    }

    public static CinemaConfig readLocalConfig(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                Constant.XML_NAME, Context.MODE_PRIVATE);
        return readConfigBySP(sharedPreferences);
    }

    public static void saveInt(SharedPreferences sharedPreferences, String name, int value) {
        SharedPreferences.Editor editor1 = sharedPreferences.edit();
        editor1.putInt(name, value);
        editor1.apply();
    }
}