Files
assistant-android/app/src/main/java/com/gh/common/util/TimestampUtils.java
2018-01-05 18:17:39 +08:00

191 lines
9.4 KiB
Java

package com.gh.common.util;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import com.gh.common.constant.Config;
import com.gh.common.constant.Constants;
import java.util.regex.Pattern;
public class TimestampUtils {
// 基准时间
private static final long BASE = 1426608000000L;
private static ArrayMap<String, Integer> intervalMap; // 间隔
private static ArrayMap<String, Integer> cdMap; // cd
public static void initMap() {
initIntervalMap();
initCDMap();
}
private static void initIntervalMap() {
intervalMap = new ArrayMap<>();
intervalMap.put("^" + Config.API_HOST + "index/slides" + "$", 5);
intervalMap.put("^" + Config.API_HOST + "game/chajian" + "$", 10);
intervalMap.put("^" + Config.API_HOST + "game/tuijian" + "$", 15);
intervalMap.put("^" + Config.API_HOST + "game/.+/detail" + "$", 20);
intervalMap.put("^" + Config.API_HOST + "game/.+/digest" + "$", 25);
intervalMap.put("^" + Config.API_HOST + "game/remenkapai" + "$", 30);
intervalMap.put("^" + Config.API_HOST + "game/.+/news_digest" + "$", 35);
intervalMap.put("^" + Config.API_HOST + "game/column/.+\\?page=.+" + "$", 40);
intervalMap.put("^" + Config.API_HOST + "support/package/.+/game/digest" + "$", 45);
intervalMap.put("^" + Config.API_HOST + "game/danjiyouxi\\?limit=20\\&offset=.+" + "$", 50);
intervalMap.put("^" + Config.API_HOST + "news/.+/digest" + "$", 55);
intervalMap.put("^" + Config.API_HOST + "news/.+/detail" + "$", 60);
intervalMap.put("^" + Config.API_HOST + "news/.+/suggestion" + "$", 65);
intervalMap.put("^" + Config.API_HOST + "game/.+/news\\?limit=3" + "$", 70);
intervalMap.put("^" + Config.API_HOST + "zixun/zixun\\?limit=20\\&offset=.+" + "$", 75);
intervalMap.put("^" + Config.API_HOST + "game/.+/news\\?limit=20\\&offset=.+" + "$", 80);
intervalMap.put("^" + Config.API_HOST + "zixun/yuanchuang\\?limit=20\\&offset=.+" + "$", 85);
intervalMap.put("^" + Config.API_HOST + "news\\?type_group=.+\\&offset=.+\\&limit=20" + "$", 90);
intervalMap.put("^" + Config.API_HOST + "zixun/guanzhu\\?key=.+\\&limit=20\\&offset=.+" + "$", 95);
intervalMap.put("^" + Config.API_HOST + "game/.+/news\\?limit=20\\&offset=.+\\&type=.+" + "$", 100);
intervalMap.put("^" + Config.API_HOST + "zixun/guanzhu\\?game_id=.+\\&limit=20\\&offset=.+" + "$", 105);
intervalMap.put("^" + Config.API_HOST + "search/news\\?game_id=.+\\&keyword=.+\\&page=.+\\&limit=20" + "$", 110);
intervalMap.put("^" + Config.API_HOST + "search/game\\?keyword=.+" + "$", 115);
intervalMap.put("^" + Config.API_HOST + "support/setting/platform" + "$", 120);
intervalMap.put("^" + Config.API_HOST + "update/package/.+" + "$", 125);
intervalMap.put("^" + Config.API_HOST + "update/game/.+/package/.+" + "$", 130);
intervalMap.put("^" + Config.COMMENT_HOST + "article/.+/comment\\?limit=10\\&offset=.+" + "$", 135);
intervalMap.put("^" + Config.COMMENT_HOST + "article/.+/comment\\?order=hot\\&limit=10\\&offset=.+" + "$", 140);
}
private static void initCDMap() {
cdMap = new ArrayMap<>();
cdMap.put("^" + Config.API_HOST + "index/slides" + "$", Constants.GAME_CD);
cdMap.put("^" + Config.API_HOST + "game/chajian" + "$", Constants.GAME_CD);
cdMap.put("^" + Config.API_HOST + "game/tuijian" + "$", Constants.GAME_CD);
cdMap.put("^" + Config.API_HOST + "game/.+/detail" + "$", Constants.GAME_CD);
cdMap.put("^" + Config.API_HOST + "game/.+/digest" + "$", Constants.GAME_CD);
cdMap.put("^" + Config.API_HOST + "game/remenkapai" + "$", Constants.GAME_CD);
cdMap.put("^" + Config.API_HOST + "game/.+/news_digest" + "$", Constants.GAME_CD);
cdMap.put("^" + Config.API_HOST + "game/column/.+\\?page=.+" + "$", Constants.GAME_CD);
cdMap.put("^" + Config.API_HOST + "support/package/.+/game/digest" + "$", Constants.GAME_CD);
cdMap.put("^" + Config.API_HOST + "game/danjiyouxi\\?limit=20\\&offset=.+" + "$", Constants.GAME_CD);
cdMap.put("^" + Config.API_HOST + "news/.+/digest" + "$", Constants.NEWS_CD);
cdMap.put("^" + Config.API_HOST + "news/.+/detail" + "$", Constants.NEWS_CD);
cdMap.put("^" + Config.API_HOST + "news/.+/suggestion" + "$", Constants.NEWS_CD);
cdMap.put("^" + Config.API_HOST + "game/.+/news\\?limit=3" + "$", Constants.NEWS_CD);
cdMap.put("^" + Config.API_HOST + "zixun/zixun\\?limit=20\\&offset=.+" + "$", Constants.NEWS_CD);
cdMap.put("^" + Config.API_HOST + "game/.+/news\\?limit=20\\&offset=.+" + "$", Constants.NEWS_CD);
cdMap.put("^" + Config.API_HOST + "zixun/yuanchuang\\?limit=10\\&offset=.+" + "$", Constants.NEWS_CD);
cdMap.put("^" + Config.API_HOST + "news\\?type_group=.+\\&offset=.+\\&limit=20" + "$", Constants.NEWS_CD);
cdMap.put("^" + Config.API_HOST + "zixun/guanzhu\\?key=.+\\&limit=20\\&offset=.+" + "$", Constants.NEWS_CD);
cdMap.put("^" + Config.API_HOST + "game/.+/news\\?limit=20\\&offset=.+\\&type=.+" + "$", Constants.NEWS_CD);
cdMap.put("^" + Config.API_HOST + "zixun/guanzhu\\?game_id=.+\\&limit=20\\&offset=.+" + "$", Constants.NEWS_CD);
cdMap.put("^" + Config.API_HOST + "search/news\\?game_id=.+\\&keyword=.+\\&page=.+\\&limit=20" + "$", Constants.NEWS_CD);
cdMap.put("^" + Config.API_HOST + "search/game\\?keyword=.+" + "$", Constants.SEARCH_CD);
cdMap.put("^" + Config.API_HOST + "support/setting/platform" + "$", Constants.PLATFORM_CD);
cdMap.put("^" + Config.API_HOST + "update/package/.+" + "$", Constants.UPDATE_CD);
cdMap.put("^" + Config.API_HOST + "update/game/.+/package/.+" + "$", Constants.UPDATE_CD);
cdMap.put("^" + Config.COMMENT_HOST + "article/.+/comment\\?limit=10\\&offset=.+" + "$", Constants.COMMENT_CD);
cdMap.put("^" + Config.COMMENT_HOST + "article/.+/comment\\?order=hot\\&limit=10\\&offset=.+" + "$", Constants.COMMENT_CD);
cdMap.put("^" + Config.API_HOST + "device/.+/concern" + "$", 0);
cdMap.put("^" + Config.API_HOST + "device/.+/concern/.+" + "$", 0);
cdMap.put("^" + Config.API_HOST + "stat/download" + "$", 0);
cdMap.put("^" + Config.API_HOST + "disclaimer" + "$", 0);
cdMap.put("^" + Config.API_HOST + "search/game/default" + "$", 0);
cdMap.put("^" + Config.API_HOST + "support/upgrade\\?version=.+\\&channel=.+" + "$", 0);
cdMap.put("^" + Config.API_HOST + "support/time/current" + "$", 0);
cdMap.put("^" + Config.API_HOST + "support/setting/ui" + "$", 0);
cdMap.put("^" + Config.API_HOST + "support/download_status\\?version=.+\\&channel=.+" + "$", 0);
cdMap.put("^" + Config.API_HOST + "support/suggestion" + "$", 0);
cdMap.put("^" + Config.API_HOST + "game/.+/serverInfo" + "$", 0);
cdMap.put("^" + Config.API_HOST + "support/package/unused\\?skip=.+" + "$", 0);
}
/*
* 为url添加timestamp
*/
public static String addTimestamp(String url) {
// // TODO: 22/12/17 刷新版
// if (TextUtils.isEmpty(url)) {
// return url;
// }
// if (url.contains("?")) {
// String u = url + "&timestamp=" + System.currentTimeMillis();
// return u;
// } else {
// String u = url + "?timestamp=" + System.currentTimeMillis();
// return u;
// }
if (TextUtils.isEmpty(url)) {
return url;
}
int cd = 0;
for (String key : getCdMap().keySet()) {
if (Pattern.matches(key, url)) {
cd = getCdMap().get(key);
break;
}
}
if (cd == 0) {
return url;
}
if (url.contains("?")) {
String u = url + "&timestamp=" + getTimestamp(url, cd);
// Utils.log("url = " + u);
return u;
} else {
String u = url + "?timestamp=" + getTimestamp(url, cd);
// Utils.log("url = " + u);
return u;
}
}
private static ArrayMap<String, Integer> getCdMap() {
if (cdMap == null) {
cdMap = new ArrayMap<>();
}
return cdMap;
}
/*
* 根据CD获取时间戳
*/
public static long getTimestamp(String url, int cd) {
long base = BASE;
for (String key : getIntervalMap().keySet()) {
if (Pattern.matches(key, url)) {
// Utils.log("url add base = " + url);
base += getIntervalMap().get(key);
break;
}
}
long now = System.currentTimeMillis();
// Utils.log("url now timestamp = " + now);
return (long) (Math.ceil((now - base) / cd) * cd + base);
}
private static ArrayMap<String, Integer> getIntervalMap() {
if (intervalMap == null) {
intervalMap = new ArrayMap<>();
}
return intervalMap;
}
/*
* 去除url中的timestamp
*/
public static String removeTimestamp(String url) {
if (!url.contains("timestamp")) return url;
int index = url.lastIndexOf("timestamp");
String params = url.substring(index);
//连接符
String connector = url.substring(index - 1, index);
String key = url.substring(0, index - 1);
index = params.indexOf("&");
if (index != -1) {
key = key + connector + params.substring(index + 1);
}
return key;
}
}