优化存储和获取DeviceID
This commit is contained in:
@ -115,17 +115,17 @@ public class TokenUtils {
|
||||
public static void saveDeviceID(String deviceID, Context context){
|
||||
saveSharedPreferences(deviceID, context);
|
||||
saveDataFile(deviceID, context);
|
||||
svaeSDCard(deviceID, "");//SDCard根目录
|
||||
svaeSDCard(deviceID, "/gh-uuid");//SDCard根目录
|
||||
svaeSDCard(deviceID, "/system");//SDCard system目录
|
||||
svaeSDCard(deviceID, "/data");//SDCard data目录
|
||||
}
|
||||
|
||||
//将uuid存到sp
|
||||
private static void saveSharedPreferences(String deviceID, Context context) {
|
||||
SharedPreferences sp = context.getSharedPreferences("UUID", Context.MODE_PRIVATE);
|
||||
SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
|
||||
SharedPreferences.Editor edit = sp.edit();
|
||||
edit.putString("uuid", deviceID);
|
||||
edit.commit();
|
||||
edit.apply();
|
||||
Utils.log("saveDeviceID", "保存成功SP");
|
||||
}
|
||||
|
||||
@ -146,11 +146,18 @@ public class TokenUtils {
|
||||
//将uuid存到SD卡
|
||||
private static void svaeSDCard(String deviceID,String saveDir) {
|
||||
File sdCardDir = Environment.getExternalStorageDirectory();
|
||||
String path = sdCardDir.getPath() + saveDir + "/gh-uuid";
|
||||
String path = sdCardDir.getPath() + saveDir;
|
||||
|
||||
File file = new File(path);
|
||||
if (!file.exists()){
|
||||
file.mkdir();
|
||||
file.mkdirs();
|
||||
}
|
||||
|
||||
// 判断文件是否存在,存在则删除
|
||||
File uuidFile = new File(path +"/uuid");
|
||||
if (uuidFile.isFile() && uuidFile.exists()){
|
||||
Utils.log(saveDir + "文件夹里的文件存在,执行删除操作");
|
||||
uuidFile.delete();
|
||||
}
|
||||
|
||||
File writeFile = new File(file, "uuid");
|
||||
@ -167,34 +174,56 @@ public class TokenUtils {
|
||||
|
||||
}
|
||||
|
||||
public static String getDeviceID(Context context){
|
||||
loadDataFile(context);
|
||||
loadSDCard("");
|
||||
loadSDCard("/system");
|
||||
loadSDCard("/data");
|
||||
return loadSharedPreferences(context);
|
||||
// 启动助手时,检查uuid
|
||||
public static void checkDeviceID(Context context) {
|
||||
String uuid = loadSharedPreferences(context, false);
|
||||
if (uuid == null) {
|
||||
// 重新获取uuid,保存
|
||||
return;
|
||||
}
|
||||
// 检查
|
||||
if (loadSharedPreferences(context, true) == null){
|
||||
saveSharedPreferences(uuid, context);
|
||||
}
|
||||
if (loadDataFile(context ,true) == null){
|
||||
saveDataFile(uuid, context);
|
||||
}
|
||||
String[] dirName = {"/gh-uuid", "/system", "/data"};
|
||||
for (int i = 0; i< 3; i++) {
|
||||
String s = loadSDCard(dirName[i]);
|
||||
if (s == null) {
|
||||
svaeSDCard(uuid, dirName[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getDeviceID(Context context) {
|
||||
return loadSharedPreferences(context, false);
|
||||
}
|
||||
|
||||
//读取SharedPreferences的uuid
|
||||
private static String loadSharedPreferences(Context context) {
|
||||
SharedPreferences sp = context.getSharedPreferences("UUID", Context.MODE_PRIVATE);
|
||||
String uuid = sp.getString("uuid", "-1");
|
||||
if ("-1".equals(uuid)){
|
||||
return loadDataFile(context);
|
||||
private static String loadSharedPreferences(Context context, boolean isCheck) {
|
||||
SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
|
||||
String uuid = sp.getString("uuid", null);
|
||||
if (isCheck){
|
||||
return uuid;
|
||||
}
|
||||
if (uuid == null){
|
||||
return loadDataFile(context, false);
|
||||
}
|
||||
Utils.log("getDeviceID", "获取成功SP" + uuid);
|
||||
return "";
|
||||
return uuid;
|
||||
}
|
||||
|
||||
//读取data/data/PackageName/files的uuid
|
||||
private static String loadDataFile(Context context) {
|
||||
private static String loadDataFile(Context context, boolean isCheck) {
|
||||
File file = new File(context.getFilesDir(), "uuid");
|
||||
if (file.exists()){
|
||||
try {
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
byte[] b = new byte[1024];
|
||||
int count = -1;
|
||||
String uuid = "";
|
||||
String uuid = null;
|
||||
while ((count = fis.read(b)) != -1) {
|
||||
uuid = new String(b, 0, count, "UTF-8");
|
||||
}
|
||||
@ -203,30 +232,29 @@ public class TokenUtils {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}else {
|
||||
|
||||
String[] dirName = {"", "/system", "/data"};
|
||||
for (int i = 0; i<3; i++){
|
||||
}else if (!isCheck){
|
||||
String[] dirName = {"/gh-uuid", "/system", "/data"};
|
||||
for (int i = 0; i< 3; i++) {
|
||||
String s = loadSDCard(dirName[i]);
|
||||
if (!s.isEmpty()){
|
||||
if (s != null) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
return null;
|
||||
}
|
||||
|
||||
//读取SD卡的uuid
|
||||
private static String loadSDCard(String saveDir){
|
||||
File sdCardDir = Environment.getExternalStorageDirectory();
|
||||
String path = sdCardDir.getPath() + saveDir + "/gh-uuid";
|
||||
String path = sdCardDir.getPath() + saveDir;
|
||||
File file = new File(path, "uuid");
|
||||
if (file.exists()){
|
||||
FileInputStream fis = null;
|
||||
ByteArrayOutputStream bos = null;
|
||||
try {
|
||||
fis = new FileInputStream(file);
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
bos = new ByteArrayOutputStream();
|
||||
byte[] array = new byte[1024];
|
||||
int len = -1;
|
||||
while( (len = fis.read(array)) != -1){
|
||||
@ -236,11 +264,11 @@ public class TokenUtils {
|
||||
fis.close();
|
||||
Utils.log("getDeviceID", "获取成功SDCard"+"目录为:"+saveDir+"::"+bos.toString());
|
||||
return bos.toString();
|
||||
} catch (Exception e) {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user