Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
mobi.cangol.mobile.service.session.SessionServiceImpl Maven / Gradle / Ivy
package mobi.cangol.mobile.service.session;
import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Build;
import android.os.StrictMode;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import mobi.cangol.mobile.CoreApplication;
import mobi.cangol.mobile.logging.Log;
import mobi.cangol.mobile.service.AppService;
import mobi.cangol.mobile.service.Service;
import mobi.cangol.mobile.service.ServiceProperty;
import mobi.cangol.mobile.service.conf.ConfigService;
import mobi.cangol.mobile.utils.FileUtils;
import mobi.cangol.mobile.utils.Object2FileUtils;
/**
* Created by weixuewu on 15/10/24.
*/
@Service("SessionService")
class SessionServiceImpl implements SessionService {
private final static String TAG = "SessionService";
private final static String JSON = ".json";
private final static String JSONA = ".jsona";
private final static String SER = ".ser";
private Application mContext = null;
private ConfigService mConfigService = null;
private ServiceProperty mServiceProperty = null;
private boolean debug = false;
private Map mMap = null;
@Override
public void onCreate(Application context) {
mContext = context;
//这里使用application中的session也可实例化一个新的
mMap = new ConcurrentHashMap();
mConfigService = (ConfigService) ((CoreApplication) mContext).getAppService(AppService.CONFIG_SERVICE);
refresh();
}
private SharedPreferences getShared() {
return mContext.getSharedPreferences(mConfigService.getSharedName(), Context.MODE_MULTI_PROCESS);
}
@Override
public String getName() {
return TAG;
}
@Override
public void onDestroy() {
mMap.clear();
}
@Override
public void setDebug(boolean debug) {
this.debug = debug;
}
@Override
public void init(ServiceProperty serviceProperty) {
this.mServiceProperty = serviceProperty;
}
@Override
public ServiceProperty getServiceProperty() {
return mServiceProperty;
}
@Override
public ServiceProperty defaultServiceProperty() {
ServiceProperty sp = new ServiceProperty(TAG);
return sp;
}
@Override
public boolean containsKey(String key) {
return mMap.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return mMap.containsValue(value);
}
@Override
public int getInt(String key, int defValue) {
if (mMap.containsKey(key)) {
return (int) mMap.get(key);
}
return defValue;
}
@Override
public boolean getBoolean(String key, boolean defValue) {
if (mMap.containsKey(key)) {
return (boolean) mMap.get(key);
}
return defValue;
}
@Override
public long getLong(String key, long defValue) {
if (mMap.containsKey(key)) {
return (long) mMap.get(key);
}
return defValue;
}
@Override
public float getFloat(String key, float defValue) {
if (mMap.containsKey(key)) {
return (float) mMap.get(key);
}
return defValue;
}
@Override
public String getString(String key, String defValue) {
if (mMap.containsKey(key)) {
return (String) mMap.get(key);
}
return defValue;
}
@Override
public Set getStringSet(String key, Set defValue) {
if (mMap.containsKey(key)) {
return (Set) mMap.get(key);
}
return defValue;
}
@Override
public JSONObject getJSONObject(String key) {
if (mMap.containsKey(key)) {
return (JSONObject) mMap.get(key);
}
return null;
}
@Override
public JSONArray getJSONArray(String key) {
if (mMap.containsKey(key)) {
return (JSONArray) mMap.get(key);
}
return null;
}
@Override
public Serializable getSerializable(String key) {
if (mMap.containsKey(key)) {
return (Serializable) mMap.get(key);
}
return null;
}
@Override
public void saveInt(String key, int value) {
getShared().edit().putInt(key, value).commit();
mMap.put(key, value);
}
@Override
public void saveBoolean(String key, boolean value) {
getShared().edit().putBoolean(key, value).commit();
mMap.put(key, value);
}
@Override
public void saveFloat(String key, float value) {
getShared().edit().putFloat(key, value).commit();
mMap.put(key, value);
}
@Override
public void saveLong(String key, long value) {
getShared().edit().putLong(key, value).commit();
mMap.put(key, value);
}
@Override
public void saveString(String key, String value) {
getShared().edit().putString(key, value).commit();
mMap.put(key, value);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void saveStringSet(String key, Set value) {
getShared().edit().putStringSet(key, value).commit();
mMap.put(key, value);
}
@Override
public void saveJSONObject(String key, JSONObject value) {
Object2FileUtils.writeJSONObject2File(value, mConfigService.getCacheDir() + File.separator + key + JSON);
mMap.put(key, value);
}
@Override
public void saveJSONArray(String key, JSONArray value) {
Object2FileUtils.writeJSONArray2File(value, mConfigService.getCacheDir() + File.separator + key + JSONA);
mMap.put(key, value);
}
@Override
public void saveSerializable(String key, Serializable value) {
Object2FileUtils.writeObject(value, mConfigService.getCacheDir() + File.separator + key + SER);
mMap.put(key, value);
}
@Override
public void saveAll(Map map) {
for (String key : map.keySet()) {
if (map.get(key) instanceof Float) {
saveFloat(key, (Float) map.get(key));
} else if (map.get(key) instanceof Boolean) {
saveBoolean(key, (Boolean) map.get(key));
} else if (map.get(key) instanceof String) {
saveString(key, (String) map.get(key));
} else if (map.get(key) instanceof Integer) {
saveInt(key, (Integer) map.get(key));
} else if (map.get(key) instanceof Long) {
saveLong(key, (Long) map.get(key));
} else if (map.get(key) instanceof Set && Build.VERSION.SDK_INT >= 11) {
saveStringSet(key, (Set) map.get(key));
} else if (map.get(key) instanceof JSONObject) {
saveJSONObject(key, (JSONObject) map.get(key));
} else if (map.get(key) instanceof JSONArray) {
saveJSONArray(key, (JSONArray) map.get(key));
} else if (Serializable.class.isAssignableFrom(map.get(key).getClass())) {
saveSerializable(key, (Serializable) map.get(key));
} else {
//其他缓存方案
throw new IllegalArgumentException(map.get(key).getClass() + " is not cache type");
}
}
}
@Override
public Object get(String key) {
if (mMap.containsKey(key)) {
return mMap.get(key);
}
return null;
}
@Override
public void put(String key, Object value) {
mMap.put(key, value);
}
@Override
public void putAll(Map map) {
mMap.putAll(map);
}
@Override
public void remove(String key) {
mMap.remove(key);
getShared().edit().remove(key).commit();
FileUtils.delete(mConfigService.getCacheDir() + File.separator + key + JSON);
FileUtils.delete(mConfigService.getCacheDir() + File.separator + key + JSONA);
FileUtils.delete(mConfigService.getCacheDir() + File.separator + key + SER);
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void refresh() {
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
Map map = getShared().getAll();
StrictMode.setThreadPolicy(oldPolicy);
mMap.putAll(map);
if (debug) Log.d("scan cache file");
new AsyncTask>() {
@Override
protected List doInBackground(String... params) {
List files = FileUtils.searchBySuffix(new File(params[0]), null, params[1], params[2], params[3]);
//System.gc();
return files;
}
@Override
protected void onPostExecute(List files) {
super.onPostExecute(files);
for (File file : files) {
if (file.getName().endsWith(JSON)) {
JSONObject json = Object2FileUtils.readFile2JSONObject(file);
String key = file.getName().substring(0, file.getName().lastIndexOf(JSON));
mMap.put(key, json);
} else if (file.getName().endsWith(JSONA)) {
JSONObject jsona = Object2FileUtils.readFile2JSONObject(file);
String key = file.getName().substring(0, file.getName().lastIndexOf(JSONA));
mMap.put(key, jsona);
} else if (file.getName().endsWith(SER)) {
Object obj = Object2FileUtils.readObject(file);
String key = file.getName().substring(0, file.getName().lastIndexOf(SER));
mMap.put(key, obj);
} else {
//其他缓存方案
Log.e("found cache file");
}
}
}
}.execute(mConfigService.getCacheDir().getAbsolutePath(), JSON, JSONA, SER);
}
@Override
public void clearAll() {
mMap.clear();
getShared().edit().clear().commit();
FileUtils.delAllFile(mConfigService.getCacheDir().getAbsolutePath());
}
}