Android Open Source - Android-Apps Shared Preferences Util






From Project

Back to project page Android-Apps.

License

The source code is released under:

Apache License

If you think the Android project Android-Apps listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.kniezrec.xbmcgear.preferences;
// w  w  w .  j  ava2 s.  c  om
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.kniezrec.xbmcgear.R;
import com.kniezrec.xbmcgear.connection.AndroidApplication;
import com.kniezrec.xbmcgear.connection.Connection;

public class SharedPreferencesUtil {
    private static final String FIRST_TIME = "_FirstTimeKey";
    private static final String FIRST_GEAR_TIME = "_FirstGearTimeKey";
    private static final String IP = "IP_";
    private static final String PORT = "PORT_";
    private static final String USERNAME = "USERNAME_";
    private static final String PASSWORD = "PASSWORD_";
    private static final String ID = "ID_";
    private static final String MAC = "MAC_";
    private static final String THEME_ID = "THEME_ID_";
    private static final String PROFILE_NAME = "PROFILE_NAME";

    public static boolean isHostDefined() {
        Context ctx = AndroidApplication.getInstance().getApplicationContext();
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(ctx);
        return (prefs.contains(IP));
    }

    public static boolean isFirstRun() {
        Context ctx = AndroidApplication.getInstance().getApplicationContext();
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(ctx);
        if ((prefs.contains(FIRST_TIME))) {
            return false;
        } else {
            SharedPreferences.Editor edit = prefs.edit();
            edit.putBoolean(FIRST_TIME, true);
            edit.apply();
            return true;
        }
    }

    public static boolean isFirstGearRun() {
        Context ctx = AndroidApplication.getInstance().getApplicationContext();
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(ctx);
        if (prefs.contains(FIRST_TIME) || prefs.contains(FIRST_GEAR_TIME)) {
            return false;
        } else {
            SharedPreferences.Editor edit = prefs.edit();
            edit.putBoolean(FIRST_GEAR_TIME, true);
            edit.apply();
            return true;
        }
    }

    public static int getTheme() {
        Context ctx = AndroidApplication.getInstance().getApplicationContext();
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(ctx);
        return prefs.getInt(THEME_ID, R.drawable.watch_preview_1);
    }

    public static void setTheme(int id) {
        Context ctx = AndroidApplication.getInstance().getApplicationContext();
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(ctx);
        SharedPreferences.Editor edit = prefs.edit();
        edit.putInt(THEME_ID, id);
        edit.apply();
    }

    public static Host getPreferences() {
        Context ctx = AndroidApplication.getInstance().getApplicationContext();
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(ctx);
        Host h = new Host();
        h.setIp(prefs.getString(IP, ""));
        h.setPort(prefs.getInt(PORT, 80));
        h.setUsername(prefs.getString(USERNAME, "xbmc"));
        h.setPassword(prefs.getString(PASSWORD, ""));
        h.setID(prefs.getInt(ID, 0));
        h.setProfileName(prefs.getString(PROFILE_NAME, ""));
        h.setHardware(prefs.getString(MAC, ""));
        return h;
    }

    public static int getActiveID() {
        Context ctx = AndroidApplication.getInstance().getApplicationContext();
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(ctx);
        return prefs.getInt(ID, 0);
    }

    public static void updateIfNeeded(Host h) {
        Context ctx = AndroidApplication.getInstance().getApplicationContext();
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(ctx);
        String ip = prefs.getString(IP, "");
        if (ip != "" && ip.equals(h.getIp())) {
            SharedPreferences.Editor edit = prefs.edit();
            edit.putString(MAC, h.getHardware());
            edit.apply();
            Connection.getInstance().setHost(h);
        }
    }

    public static void changePreferences(Host h) {
        Context ctx = AndroidApplication.getInstance().getApplicationContext();
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(ctx);
        SharedPreferences.Editor edit = prefs.edit();
        edit.putString(IP, h.getIp());
        edit.putString(USERNAME, h.getUsername());
        edit.putString(PASSWORD, h.getPassword());
        edit.putInt(PORT, h.getPort());
        edit.putInt(ID, h.getID());
        edit.putString(PROFILE_NAME, h.getProfileName());
        edit.putString(MAC, h.getHardware());
        edit.apply();
        Connection.getInstance().setHost(h);
    }

    public static void clear() {
        Context ctx = AndroidApplication.getInstance().getApplicationContext();
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(ctx);
        SharedPreferences.Editor edit = prefs.edit();
        edit.remove(IP);
        edit.apply();
        Connection.getInstance().setHost(null);
    }
}




Java Source Code List

com.kniezrec.remoterecorder.Communication.java
com.kniezrec.remoterecorder.MainServiceConnection.java
com.kniezrec.remoterecorder.MainService.java
com.kniezrec.remoterecorder.RequestType.java
com.kniezrec.remoterecorder.Request.java
com.kniezrec.voiceremote2.BSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremote2.BSeriesSender.java
com.kniezrec.voiceremote2.CSeriesButtons.java
com.kniezrec.voiceremote2.CSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremote2.CSeriesSender.java
com.kniezrec.voiceremote2.CommandsFragment.java
com.kniezrec.voiceremote2.Commands.java
com.kniezrec.voiceremote2.Discovery.java
com.kniezrec.voiceremote2.FSeriesButtons.java
com.kniezrec.voiceremote2.Group.java
com.kniezrec.voiceremote2.HelpFragment.java
com.kniezrec.voiceremote2.HostnamePreference.java
com.kniezrec.voiceremote2.KeyCodeSender.java
com.kniezrec.voiceremote2.ListActionsFragment.java
com.kniezrec.voiceremote2.MainActivity.java
com.kniezrec.voiceremote2.MainFragment.java
com.kniezrec.voiceremote2.Mapper.java
com.kniezrec.voiceremote2.MyExpandableListAdapter.java
com.kniezrec.voiceremote2.NewActionEdit.java
com.kniezrec.voiceremote2.NewActionSingleEdit.java
com.kniezrec.voiceremote2.NewAction.java
com.kniezrec.voiceremote2.RemoteButton.java
com.kniezrec.voiceremote2.SenderFactory.java
com.kniezrec.voiceremote2.Sender.java
com.kniezrec.voiceremote2.SettingsActivity.java
com.kniezrec.voiceremote2.TextSender.java
com.kniezrec.voiceremotefree.BSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremotefree.BSeriesSender.java
com.kniezrec.voiceremotefree.CSeriesButtons.java
com.kniezrec.voiceremotefree.CSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremotefree.CSeriesSender.java
com.kniezrec.voiceremotefree.Commands.java
com.kniezrec.voiceremotefree.Discovery.java
com.kniezrec.voiceremotefree.FSeriesButtons.java
com.kniezrec.voiceremotefree.HelpActivity.java
com.kniezrec.voiceremotefree.HostnamePreference.java
com.kniezrec.voiceremotefree.KeyCodeSender.java
com.kniezrec.voiceremotefree.ListActionsActivity.java
com.kniezrec.voiceremotefree.MainActivity.java
com.kniezrec.voiceremotefree.Mapper.java
com.kniezrec.voiceremotefree.NewActionEdit.java
com.kniezrec.voiceremotefree.NewActionSingleEdit.java
com.kniezrec.voiceremotefree.NewAction.java
com.kniezrec.voiceremotefree.RemoteButton.java
com.kniezrec.voiceremotefree.SenderFactory.java
com.kniezrec.voiceremotefree.Sender.java
com.kniezrec.voiceremotefree.Setings.java
com.kniezrec.voiceremotefree.SettingsActivity.java
com.kniezrec.voiceremotefree.TextSender.java
com.kniezrec.xbmcgear.connection.AndroidApplication.java
com.kniezrec.xbmcgear.connection.Connection.java
com.kniezrec.xbmcgear.connection.GearJSON.java
com.kniezrec.xbmcgear.connection.JSONRPCRequest.java
com.kniezrec.xbmcgear.connection.JSONRequestFactory.java
com.kniezrec.xbmcgear.connection.NSDResolve.java
com.kniezrec.xbmcgear.connection.NSDSearch.java
com.kniezrec.xbmcgear.connection.ProviderConnection.java
com.kniezrec.xbmcgear.connection.ProviderService.java
com.kniezrec.xbmcgear.connection.ResponseParser.java
com.kniezrec.xbmcgear.connection.WakeOnLan.java
com.kniezrec.xbmcgear.player.Kodi.java
com.kniezrec.xbmcgear.player.Player.java
com.kniezrec.xbmcgear.player.Playlist.java
com.kniezrec.xbmcgear.player.Song.java
com.kniezrec.xbmcgear.player.Video.java
com.kniezrec.xbmcgear.preferences.HostTable.java
com.kniezrec.xbmcgear.preferences.Host.java
com.kniezrec.xbmcgear.preferences.HostsDataSource.java
com.kniezrec.xbmcgear.preferences.HostsDatabaseHelper.java
com.kniezrec.xbmcgear.preferences.SharedPreferencesUtil.java
com.kniezrec.xbmcgear.presentation.AnimationManager.java
com.kniezrec.xbmcgear.presentation.AutoConfigurationActivity.java
com.kniezrec.xbmcgear.presentation.HostSetActivity.java
com.kniezrec.xbmcgear.presentation.InstanceActivity.java
com.kniezrec.xbmcgear.presentation.MainActivity.java
com.kniezrec.xbmcgear.presentation.StyleDialogFragment.java
com.kniezrec.xbmcgear.presentation.ViewMode.java
com.uraroji.garage.android.lame.SimpleLame.java
com.uraroji.garage.android.mp3recvoice.RecMicToMp3.java
de.quist.samy.remocon.Base64.java
de.quist.samy.remocon.Base64.java
de.quist.samy.remocon.ConnectionDeniedException.java
de.quist.samy.remocon.ConnectionDeniedException.java
de.quist.samy.remocon.Key.java
de.quist.samy.remocon.Key.java
de.quist.samy.remocon.Loggable.java
de.quist.samy.remocon.Loggable.java
de.quist.samy.remocon.RemoteReader.java
de.quist.samy.remocon.RemoteReader.java
de.quist.samy.remocon.RemoteSession.java
de.quist.samy.remocon.RemoteSession.java