reformat code & optimize import & rearrange code

This commit is contained in:
CsHeng
2017-05-19 10:54:54 +08:00
parent 0c1b48cee2
commit 9369218821
622 changed files with 20567 additions and 18549 deletions

View File

@ -8,23 +8,34 @@ import android.os.Build;
import android.os.Environment;
import android.provider.Settings;
import android.text.TextUtils;
import com.gh.common.constant.Config;
import com.gh.gamecenter.BuildConfig;
import com.gh.gamecenter.retrofit.*;
import com.gh.gamecenter.retrofit.JSONObjectResponse;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import com.gh.gamecenter.retrofit.StringResponse;
import com.tencent.stat.StatConfig;
import okhttp3.*;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class TokenUtils {
// 注册设备
@ -77,124 +88,6 @@ public class TokenUtils {
});
}
// 获取用户token
public static synchronized Observable<String> getToken(final Context context, boolean isCheck) {
String token = null;
if (isCheck) {
SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
token = sp.getString("token", null);
if (token != null) {
long expire = sp.getLong("token_expire", 0) * 1000 - 10 * 1000;
long time = Utils.getTime(context);
// 判断token是否过期
if (time >= expire) {
// token已过期
token = null;
}
}
}
if (token == null) {
Map<String, String> params = new HashMap<>();
params.put("device_id", getDeviceId(context));
return RetrofitManager.getUser()
.postLogin(RequestBody.create(MediaType.parse("application/json"), new JSONObject(params).toString()))
.flatMap(new Func1<ResponseBody, Observable<String>>() {
@Override
public Observable<String> call(ResponseBody responseBody) {
String value = null;
try {
SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
Editor editor = sp.edit();
JSONObject response = new JSONObject(responseBody.string());
editor.putString("user_name", response.getString("name"));
editor.putString("user_icon", response.getString("icon"));
response = response.getJSONObject("token");
editor.putString("token", response.getString("value"));
editor.putLong("token_expire", response.getLong("expire"));
editor.apply();
// 服务器返回的token和本地已存的token相同更新本地时间
getTime(context);
value = response.getString("value");
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return Observable.just(value);
}
}).onErrorResumeNext(new Func1<Throwable, Observable<? extends String>>() {
@Override
public Observable<? extends String> call(Throwable throwable) {
return Observable.error(throwable);
}
});
}
return Observable.just(token);
}
// 检查设备信息是否已经上传完整
public static synchronized void checkDeviceInfo(Context context, String token) {
final SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
final HashMap<String, String> params = new HashMap<>();
if (!sp.getBoolean("isUploadExtra", false)) {
params.put("MANUFACTURER", Build.MANUFACTURER);
params.put("MODEL", Build.MODEL);
params.put("ANDROID_SDK", String.valueOf(Build.VERSION.SDK_INT));
params.put("ANDROID_VERSION", Build.VERSION.RELEASE);
}
if (!sp.getBoolean("isUploadMac", true)) {
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
String mac = wm.getConnectionInfo().getMacAddress();
if (!TextUtils.isEmpty(mac) || !":::::".equals(mac)) {
params.put("MAC", mac);
}
}
if (!sp.getBoolean("isUploadMid", true)) {
String mid = StatConfig.getMid(context);
if (!TextUtils.isEmpty(mid) || !"0".equals(mid)) {
params.put("MTA_ID", mid);
}
}
if (params.size() != 0) {
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
new JSONObject(params).toString());
RetrofitManager.getUser().postDevice(token, body, TokenUtils.getDeviceId(context))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<ResponseBody>() {
@Override
public void onResponse(ResponseBody response) {
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("isUploadExtra", true);
editor.putBoolean("isUploadMac", true);
editor.putBoolean("isUploadMid", true);
editor.apply();
}
});
}
}
// 获取服务器时间
public static synchronized void getTime(Context context) {
final SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
RetrofitManager.getApi().getTime()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new StringResponse() {
@Override
public void onResponse(String response) {
if (response.matches("^[0-9]{10}$")) {
try {
Editor editor = sp.edit();
editor.putLong("server_time", Long.parseLong(response));
editor.putLong("client_time", System.currentTimeMillis() / 1000);
editor.apply();
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
});
}
public static synchronized void saveDeviceId(Context context, String device_id) {
saveSharedPreferences(context, device_id);
saveDataFile(context, device_id);
@ -257,10 +150,86 @@ public class TokenUtils {
}
// 获取用户token
public static synchronized Observable<String> getToken(final Context context, boolean isCheck) {
String token = null;
if (isCheck) {
SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
token = sp.getString("token", null);
if (token != null) {
long expire = sp.getLong("token_expire", 0) * 1000 - 10 * 1000;
long time = Utils.getTime(context);
// 判断token是否过期
if (time >= expire) {
// token已过期
token = null;
}
}
}
if (token == null) {
Map<String, String> params = new HashMap<>();
params.put("device_id", getDeviceId(context));
return RetrofitManager.getUser()
.postLogin(RequestBody.create(MediaType.parse("application/json"), new JSONObject(params).toString()))
.flatMap(new Func1<ResponseBody, Observable<String>>() {
@Override
public Observable<String> call(ResponseBody responseBody) {
String value = null;
try {
SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
Editor editor = sp.edit();
JSONObject response = new JSONObject(responseBody.string());
editor.putString("user_name", response.getString("name"));
editor.putString("user_icon", response.getString("icon"));
response = response.getJSONObject("token");
editor.putString("token", response.getString("value"));
editor.putLong("token_expire", response.getLong("expire"));
editor.apply();
// 服务器返回的token和本地已存的token相同更新本地时间
getTime(context);
value = response.getString("value");
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return Observable.just(value);
}
}).onErrorResumeNext(new Func1<Throwable, Observable<? extends String>>() {
@Override
public Observable<? extends String> call(Throwable throwable) {
return Observable.error(throwable);
}
});
}
return Observable.just(token);
}
public static synchronized String getDeviceId(Context context) {
return loadSharedPreferences(context, false);
}
// 获取服务器时间
public static synchronized void getTime(Context context) {
final SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
RetrofitManager.getApi().getTime()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new StringResponse() {
@Override
public void onResponse(String response) {
if (response.matches("^[0-9]{10}$")) {
try {
Editor editor = sp.edit();
editor.putLong("server_time", Long.parseLong(response));
editor.putLong("client_time", System.currentTimeMillis() / 1000);
editor.apply();
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
});
}
//读取SharedPreferences的uuid
private static String loadSharedPreferences(Context context, boolean isCheck) {
SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
@ -333,4 +302,46 @@ public class TokenUtils {
return null;
}
// 检查设备信息是否已经上传完整
public static synchronized void checkDeviceInfo(Context context, String token) {
final SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
final HashMap<String, String> params = new HashMap<>();
if (!sp.getBoolean("isUploadExtra", false)) {
params.put("MANUFACTURER", Build.MANUFACTURER);
params.put("MODEL", Build.MODEL);
params.put("ANDROID_SDK", String.valueOf(Build.VERSION.SDK_INT));
params.put("ANDROID_VERSION", Build.VERSION.RELEASE);
}
if (!sp.getBoolean("isUploadMac", true)) {
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
String mac = wm.getConnectionInfo().getMacAddress();
if (!TextUtils.isEmpty(mac) || !":::::".equals(mac)) {
params.put("MAC", mac);
}
}
if (!sp.getBoolean("isUploadMid", true)) {
String mid = StatConfig.getMid(context);
if (!TextUtils.isEmpty(mid) || !"0".equals(mid)) {
params.put("MTA_ID", mid);
}
}
if (params.size() != 0) {
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
new JSONObject(params).toString());
RetrofitManager.getUser().postDevice(token, body, TokenUtils.getDeviceId(context))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<ResponseBody>() {
@Override
public void onResponse(ResponseBody response) {
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("isUploadExtra", true);
editor.putBoolean("isUploadMac", true);
editor.putBoolean("isUploadMid", true);
editor.apply();
}
});
}
}
}