Merge branch '2.0' of https://git.oschina.net/dreamhua/GH-ASSISTv1.45 into 2.0
Conflicts: app/src/main/java/com/gh/common/util/TokenUtils.java
This commit is contained in:
@ -7,6 +7,7 @@ import android.net.wifi.WifiManager;
|
||||
import android.provider.Settings;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.text.TextUtils;
|
||||
import android.os.Environment;
|
||||
|
||||
import com.android.volley.Request;
|
||||
import com.android.volley.Response;
|
||||
@ -20,6 +21,10 @@ import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
@ -146,6 +151,166 @@ public class TokenUtils {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void saveDeviceID(String deviceID, Context context){
|
||||
saveSharedPreferences(deviceID, context);
|
||||
saveDataFile(deviceID, context);
|
||||
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(Config.PREFERENCE, Context.MODE_PRIVATE);
|
||||
SharedPreferences.Editor edit = sp.edit();
|
||||
edit.putString("uuid", deviceID);
|
||||
edit.apply();
|
||||
Utils.log("saveDeviceID", "保存成功SP");
|
||||
}
|
||||
|
||||
//将uuid存到data/data/PackageName/files文件夹下
|
||||
private static void saveDataFile(String deviceID, Context context) {
|
||||
FileOutputStream fops;
|
||||
try {
|
||||
fops = context.openFileOutput("uuid", Context.MODE_PRIVATE);
|
||||
fops.write(deviceID.getBytes());
|
||||
fops.close();
|
||||
Utils.log("saveDeviceID", "保存成功DataFile");
|
||||
} catch (Exception e) {
|
||||
Utils.log("保存uuid到data/data/PackageName/files文件异常" + e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//将uuid存到SD卡
|
||||
private static void svaeSDCard(String deviceID,String saveDir) {
|
||||
File sdCardDir = Environment.getExternalStorageDirectory();
|
||||
String path = sdCardDir.getPath() + saveDir;
|
||||
|
||||
File file = new File(path);
|
||||
if (!file.exists()){
|
||||
file.mkdirs();
|
||||
}
|
||||
|
||||
// 判断文件是否存在,存在则删除
|
||||
File uuidFile = new File(path +"/uuid");
|
||||
if (uuidFile.isFile() && uuidFile.exists()){
|
||||
Utils.log(saveDir + "文件夹里的文件存在,执行删除操作");
|
||||
uuidFile.delete();
|
||||
}
|
||||
|
||||
File writeFile = new File(file, "uuid");
|
||||
FileOutputStream fos;
|
||||
try {
|
||||
fos = new FileOutputStream(writeFile);
|
||||
fos.write(deviceID.getBytes());
|
||||
fos.close();
|
||||
Utils.log("saveDeviceID", "保存成功SDCard"+"目录为:"+saveDir);
|
||||
} catch (Exception e) {
|
||||
Utils.log("保存uuid到SDCard异常" + saveDir + e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 启动助手时,检查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, 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 uuid;
|
||||
}
|
||||
|
||||
//读取data/data/PackageName/files的uuid
|
||||
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 = null;
|
||||
while ((count = fis.read(b)) != -1) {
|
||||
uuid = new String(b, 0, count, "UTF-8");
|
||||
}
|
||||
Utils.log("getDeviceID", "获取成功DataFile"+ uuid);
|
||||
return uuid;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}else if (!isCheck){
|
||||
String[] dirName = {"/gh-uuid", "/system", "/data"};
|
||||
for (int i = 0; i< 3; i++) {
|
||||
String s = loadSDCard(dirName[i]);
|
||||
if (s != null) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
//读取SD卡的uuid
|
||||
private static String loadSDCard(String saveDir){
|
||||
File sdCardDir = Environment.getExternalStorageDirectory();
|
||||
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);
|
||||
bos = new ByteArrayOutputStream();
|
||||
byte[] array = new byte[1024];
|
||||
int len = -1;
|
||||
while( (len = fis.read(array)) != -1){
|
||||
bos.write(array,0,len);
|
||||
}
|
||||
bos.close();
|
||||
fis.close();
|
||||
Utils.log("getDeviceID", "获取成功SDCard"+"目录为:"+saveDir+"::"+bos.toString());
|
||||
return bos.toString();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user