Back to project page AndroidWifiServer.
The source code is released under:
Apache License
If you think the Android project AndroidWifiServer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package jp.maju.wifiserver.util; /* www. j a v a2 s . c o m*/ import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; import jp.maju.wifiserver.R; import jp.maju.wifiserver.SocketInfo; import android.app.Application; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.text.TextUtils; /* * Copyright {2014} {Matsuda Jumpei} * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ public class PreferenceUtil { private static final String TAG = PreferenceUtil.class.getSimpleName(); public enum KEYS { KEY_HOST_OF_A_SSID_ON_CLIENT, KEY_PORT_OF_A_SSID_ON_CLIENT, KEY_PORT_OF_A_SSID_ON_SERVER, KEY_DEFAULT_SIDE, KEY_HTTP_REFERER, KEY_ANCHOR, KEY_SERVER_MESSAGE, KEY_CLIENT_MESSAGE } private PreferenceUtil() { } public static SharedPreferences getPreference(Application app) { return app.getSharedPreferences(TAG, Context.MODE_PRIVATE); } public static List<String> getRegisteredSSID(Application app) { List<String> ssidList = new ArrayList<>(); SharedPreferences pref = getPreference(app); int count = pref.getInt(app.getString(R.string.key_server_ssid_count), 0); for (int i = 0; i < count; i++) { String ssid = pref.getString( app.getString(R.string.key_server_ssid) + TAG + i, null); if (!TextUtils.isEmpty(ssid)) { ssidList.add(ssid); } } count = ssidList.size(); pref.edit() .putInt(app.getString(R.string.key_server_ssid_count), count) .commit(); return ssidList; } public static String getServerHostOnClient(Application app, String ssid) { return getPreference(app).getString(KEYS.KEY_HOST_OF_A_SSID_ON_CLIENT.toString()+ssid, null); } public static int getServerPortOnClient(Application app, String ssid) { return getPreference(app).getInt(KEYS.KEY_PORT_OF_A_SSID_ON_CLIENT.toString()+ssid, 8080); } public static void setServerPortOnClient(Application app, String ssid, int port) { Editor e = getPreference(app).edit(); e.putInt(KEYS.KEY_PORT_OF_A_SSID_ON_CLIENT.toString()+ssid, port); e.commit(); } public static void setServerHostOnClient(Application app, String ssid, String host) { Editor e = getPreference(app).edit(); e.putString(KEYS.KEY_HOST_OF_A_SSID_ON_CLIENT.toString()+ssid, host); e.commit(); } public static void save(Application app, SocketInfo si) { PreferenceUtil.setServerHostOnClient(app, si.getSsid(), si.getHost()); PreferenceUtil.setServerPortOnClient(app, si.getSsid(), si.getPort()); } public static SocketInfo load(Application app) { String ssid = CommonUtil.getCurrentSSID(app); if (ssid == null) { return null; } String host = PreferenceUtil.getServerHostOnClient(app, ssid); int port = PreferenceUtil.getServerPortOnClient(app, ssid); if (host == null||port < 0) { return null; } SocketInfo si = new SocketInfo(ssid); si.setHost(host); si.setPort(port); return si; } public static int getServerPortOnServer(Application app, String ssid) { return getPreference(app).getInt(KEYS.KEY_PORT_OF_A_SSID_ON_SERVER.toString()+ssid, 8080); } public static void setServerPortOnServer(Application app, String ssid, int port) { Editor e = getPreference(app).edit(); e.putInt(KEYS.KEY_PORT_OF_A_SSID_ON_SERVER.toString()+ssid, port); e.commit(); } public static String getUUID(Application app) { SharedPreferences pref = getPreference(app); String uuid = pref.getString(app.getString(R.string.key_uuid), null); if (TextUtils.isEmpty(uuid)) { uuid = UUID.randomUUID().toString(); pref.edit().putString(app.getString(R.string.key_uuid), uuid).commit(); } return uuid; } public static boolean existUser(Application app, String uuid) { SharedPreferences pref = getPreference(app); return pref.getString(uuid, null) != null; } public static void registerUser(Application app, String uuid, String username, String password) { SharedPreferences pref = getPreference(app); Editor e = pref.edit(); e.putString(uuid, username); e.putString(username, password); e.commit(); } public static void addUser(Application app, String uuid, String username) { SharedPreferences pref = getPreference(app); Editor e = pref.edit(); e.putString(uuid, username); e.commit(); } public static String getUsername(Application app, String uuid) { SharedPreferences pref = getPreference(app); return pref.getString(uuid, uuid); } public static boolean verify(Application app, String username, String password) { SharedPreferences pref = getPreference(app); String storedPass = pref.getString(username, null); if (!TextUtils.isEmpty(storedPass) && storedPass.equals(password)) { return true; } else { return false; } } /** * * @param app * @return 0 == not set, 1 == server, 2 == client */ public static int getDefaultSide(Application app) { return getPreference(app).getInt(KEYS.KEY_DEFAULT_SIDE.toString()+CommonUtil.getCurrentSSID(app), 0); } /** * * @param app * @param side 0 == not set, 1 == server, 2 == client */ public static void setDefaultSide(Application app, int side) { Editor e = getPreference(app).edit(); e.putInt(KEYS.KEY_DEFAULT_SIDE.toString()+CommonUtil.getCurrentSSID(app), side); e.commit(); } public static String getHttpRefererLogin(Application app) { return getPreference(app).getString(KEYS.KEY_HTTP_REFERER.toString()+"login", null); } public static void setHttpRefererLogin(Application app, String href) { Editor e = getPreference(app).edit(); e.putString(KEYS.KEY_HTTP_REFERER.toString()+"login", href); e.commit(); } public static String getHttpRefererLogout(Application app) { return getPreference(app).getString(KEYS.KEY_HTTP_REFERER.toString()+"logout", null); } public static void setHttpRefererLogout(Application app, String href) { Editor e = getPreference(app).edit(); e.putString(KEYS.KEY_HTTP_REFERER.toString()+"logout", href); e.commit(); } public static String getAnchor(Application app) { return getPreference(app).getString(KEYS.KEY_ANCHOR.toString(), null); } public static void setAnchor(Application app, String anchor) { Editor e = getPreference(app).edit(); e.putString(KEYS.KEY_ANCHOR.toString(), anchor); e.commit(); } public static String getServerMessage(Application app) { return getPreference(app).getString(KEYS.KEY_SERVER_MESSAGE.toString(), null); } public static void setServerMessage(Application app, String message) { Editor e = getPreference(app).edit(); e.putString(KEYS.KEY_SERVER_MESSAGE.toString(), message); e.commit(); } public static String getClientMessage(Application app, String prefix) { return getPreference(app).getString(prefix+KEYS.KEY_CLIENT_MESSAGE.toString(), null); } public static void setClientMessage(Application app, String message, String prefix) { Editor e = getPreference(app).edit(); e.putString(prefix+KEYS.KEY_CLIENT_MESSAGE.toString(), message); e.commit(); } @SuppressWarnings("deprecation") private static boolean isDifferentDate(long beforeTime, long currentTime) { if (-1L == beforeTime) { return true; } if (currentTime < beforeTime) { return true; } Date bd = new Date(beforeTime); Date cd = new Date(currentTime); return (bd.getYear() != cd.getYear()) || (bd.getMonth() != cd.getMonth()) || (bd.getDate() != cd.getDate()); } public static boolean isDifferentDate(Application app, String key) { SharedPreferences pref = PreferenceUtil.getPreference(app); Editor e = pref.edit(); long beforeTime = pref.getLong(key, -1L); long curTime = System.currentTimeMillis(); if (isDifferentDate(beforeTime, curTime)) { e.putLong(key, curTime).commit(); return true; } else { return false; } } }