钱能钱包 v3.10.2版本的 MD5 值为:339a4b585ef2aa3bfaf7e4baeb254647
以下内容为反编译后的 Util.java 源代码,内容仅作参考
package com.netease.cloud.nos.yidun.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.util.Base64;
import cn.jpush.android.local.JPushConstants;
import com.netease.cloud.nos.yidun.constants.Code;
import com.netease.cloud.nos.yidun.constants.Constants;
import com.netease.cloud.nos.yidun.core.Callback;
import com.netease.cloud.nos.yidun.core.WanAccelerator;
import com.netease.cloud.nos.yidun.core.WanNOSObject;
import com.netease.cloud.nos.yidun.exception.InvalidParameterException;
import com.netease.cloud.nos.yidun.http.HttpResult;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.SSLException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Util {
private static final String LOGTAG = LogUtil.makeLogTag(Util.class);
public static CountDownLatch acquireLock() {
return new CountDownLatch(1);
}
public static void addHeaders(Request.Builder builder, WanNOSObject wanNOSObject) {
if (wanNOSObject.getContentType() != null && !wanNOSObject.getContentType().equals("")) {
builder.addHeader("Content-Type", wanNOSObject.getContentType());
}
if (wanNOSObject.getUserMetadata() == null || wanNOSObject.getUserMetadata().size() <= 0) {
return;
}
Map<String, String> userMetadata = wanNOSObject.getUserMetadata();
for (String str : userMetadata.keySet()) {
builder.addHeader("x-nos-meta-" + str, userMetadata.get(str));
}
}
public static String buildLBSUrl(String str, String str2) {
String str3 = LOGTAG;
LogUtil.d(str3, "query lbs url: " + str);
return str + "?version=1.0&bucketname=" + str2;
}
public static String buildPostDataUrl(String str, String str2, String str3, String str4, long j2, boolean z) {
String str5;
if (str4 != null) {
str5 = encode(str2) + "/" + encode(str3) + "?version=1.0&context=" + str4 + "&offset=" + j2 + "&complete=" + z;
} else {
str5 = encode(str2) + "/" + encode(str3) + "?version=1.0&offset=" + j2 + "&complete=" + z;
}
LogUtil.d(LOGTAG, "post data url server: " + str + ", query string: " + str5);
return str + "/" + str5;
}
public static String buildQueryUrl(String str, String str2, String str3, String str4) {
String str5;
if (str4 != null) {
str5 = encode(str2) + "/" + encode(str3) + "?uploadContext&version=1.0&context=" + str4;
} else {
str5 = encode(str2) + "/" + encode(str3) + "?uploadContext&version=1.0";
}
return str + "/" + str5;
}
public static void checkParameters(Context context, Object obj, Object obj2, WanNOSObject wanNOSObject, Callback callback) {
String uploadToken = wanNOSObject.getUploadToken();
String nosBucketName = wanNOSObject.getNosBucketName();
String nosObjectName = wanNOSObject.getNosObjectName();
if (context == null || obj == null || obj2 == null || callback == null || uploadToken == null || nosBucketName == null || nosObjectName == null) {
throw new InvalidParameterException("parameters could not be null");
}
}
public static void closeInputStream(InputStream inputStream) {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e2) {
e2.printStackTrace();
String str = LOGTAG;
LogUtil.e(str, "Failed to close InputStream: " + e2.getMessage());
}
}
}
public static void deleteTempFiles(Context context) {
File[] listFiles;
File sDPath = getSDPath(context);
File file = new File(sDPath.getPath() + Constants.TEMP_FILE);
if (!file.exists() || (listFiles = file.listFiles()) == null) {
return;
}
for (File file2 : listFiles) {
file2.delete();
}
}
private static String encode(String str) {
return URLEncoder.encode(str, WanAccelerator.getConf().getCharset());
}
public static boolean getBooleanData(Context context, String str) {
return getDefaultPreferences(context).getBoolean(str, false);
}
public static String getData(Context context, String str) {
return getDefaultPreferences(context).getString(str, null);
}
private static SharedPreferences getDefaultPreferences(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context);
}
public static ExecutorService getExecutorService() {
return Executors.newSingleThreadExecutor();
}
public static OkHttpClient getHttpClient(Context context) {
return Http.getHttpClient(context);
}
public static int getHttpCode(HttpResult httpResult) {
Exception exception;
int statusCode = httpResult.getStatusCode();
if (statusCode == 200 || (exception = httpResult.getException()) == null) {
return statusCode;
}
if (exception instanceof ConnectTimeoutException) {
String str = LOGTAG;
LogUtil.d(str, "connection timeout Exception:" + exception.getMessage());
return Code.CONNECTION_TIMEOUT;
} else if (exception instanceof SocketTimeoutException) {
String str2 = LOGTAG;
LogUtil.d(str2, "Read Socket Timeout Exception:" + exception.getMessage());
return Code.SOCKET_TIMEOUT;
} else if (exception instanceof SSLException) {
String str3 = LOGTAG;
LogUtil.d(str3, "SSL Exception:" + exception.getMessage());
return Code.SSL_FAILED;
} else {
if (exception instanceof SocketException) {
String str4 = LOGTAG;
LogUtil.d(str4, "Socket Exception" + exception.getMessage());
String lowerCase = exception.getMessage().toLowerCase();
if (lowerCase.contains("refused")) {
return Code.CONNECTION_REFUSED;
}
if (lowerCase.contains("reset")) {
return Code.CONNECTION_RESET;
}
} else if (exception instanceof JSONException) {
String str5 = LOGTAG;
LogUtil.d(str5, "JSON Exception" + exception.getMessage());
return Code.INVALID_RESPONSE_DATA;
}
return statusCode;
}
}
public static String getIPAddress() {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
Enumeration<InetAddress> inetAddresses = networkInterfaces.nextElement().getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress nextElement = inetAddresses.nextElement();
if (!nextElement.isLoopbackAddress() && (nextElement instanceof Inet4Address)) {
return nextElement.getHostAddress();
}
}
}
return "";
} catch (SocketException unused) {
LogUtil.e(LOGTAG, "get ip address socket exception");
return "";
}
}
public static String getIPString(String str) {
if (str == null || str.equals("")) {
return "";
}
if (str.startsWith("https")) {
str = str.replaceAll(JPushConstants.HTTPS_PRE, "");
}
if (str.startsWith("http")) {
str = str.replaceAll(JPushConstants.HTTP_PRE, "");
}
return str.replaceAll("^(\\d{1,3}(\\.\\d{1,3}){3}).*", "$1");
}
public static int getIntData(Context context, String str) {
return getDefaultPreferences(context).getInt(str, 0);
}
public static OkHttpClient getLbsHttpClient(Context context) {
return Http.getLbsHttpClient(context);
}
public static long getLongData(Context context, String str) {
return getDefaultPreferences(context).getLong(str, 0L);
}
public static String getMonitorUrl(String str) {
return str + "/stat/sdk?version=1.0";
}
public static String getResultString(HttpResult httpResult, String str) {
if (httpResult != null && httpResult.getMsg() != null && httpResult.getMsg().has(str)) {
try {
return httpResult.getMsg().getString(str);
} catch (JSONException e2) {
LogUtil.e(LOGTAG, "get result string parse json failed", e2);
}
}
return "";
}
public static File getSDPath(Context context) {
return Environment.getExternalStorageState().equals("mounted") ? Environment.getExternalStorageDirectory() : context.getCacheDir();
}
public static String getToken(String str, String str2, long j2, String str3, String str4, String str5, String str6) {
JSONObject jSONObject = new JSONObject();
if (str != null) {
jSONObject.put("Bucket", str);
}
if (str2 != null) {
jSONObject.put("Object", str2);
}
if (j2 != 0) {
jSONObject.put("Expires", j2);
}
if (str5 != null) {
jSONObject.put("CallbackUrl", str5);
}
if (str6 != null) {
jSONObject.put("CallbackBody", str6);
}
String str7 = new String(Base64.encode(jSONObject.toString().getBytes(), 2));
SecretKeySpec secretKeySpec = new SecretKeySpec(str4.getBytes(), "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(secretKeySpec);
String str8 = new String(Base64.encode(mac.doFinal(str7.getBytes()), 2));
return "UPLOAD " + str3 + ":" + str8 + ":" + str7;
}
public static String[] getUploadServer(Context context, String str, boolean z) {
String data;
if (!z) {
data = getData(context, str + Constants.UPLOAD_SERVER_KEY);
} else {
data = getData(context, str + Constants.HTTPS_UPLOAD_SERVER_KEY);
}
if (data == null) {
return null;
}
return data.split(";");
}
public static long ipToLong(String str) {
if (str == null || str.equals("")) {
return 0L;
}
int indexOf = str.indexOf(".");
int i2 = indexOf + 1;
int indexOf2 = str.indexOf(".", i2);
int i3 = indexOf2 + 1;
int indexOf3 = str.indexOf(".", i3);
long[] jArr = {Long.parseLong(str.substring(0, indexOf)), Long.parseLong(str.substring(i2, indexOf2)), Long.parseLong(str.substring(i3, indexOf3)), Long.parseLong(str.substring(indexOf3 + 1))};
return (jArr[0] << 24) + (jArr[1] << 16) + (jArr[2] << 8) + jArr[3];
}
public static boolean isValidLbsIP(String str) {
String replaceFirst;
if (str != null && !str.equals("")) {
if (str.startsWith(JPushConstants.HTTPS_PRE)) {
replaceFirst = str.replaceFirst(JPushConstants.HTTPS_PRE, "");
} else if (str.startsWith(JPushConstants.HTTP_PRE)) {
replaceFirst = str.replaceFirst(JPushConstants.HTTP_PRE, "");
}
if (!replaceFirst.endsWith("/lbs")) {
return false;
}
String replaceFirst2 = replaceFirst.replaceFirst("/lbs", "");
if (!replaceFirst2.equals("0.0.0.0") && !replaceFirst2.equals("255.255.255.255")) {
return ValidIP.validate(replaceFirst2);
}
}
return false;
}
public static void netStateChange(Context context) {
String str = LOGTAG;
LogUtil.d(str, "network connection change");
NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo();
if (activeNetworkInfo != null && activeNetworkInfo.isAvailable() && activeNetworkInfo.isConnected()) {
NetworkType netWorkType = NetworkType.getNetWorkType(context);
int intData = getIntData(context, Constants.BUCKET_NUMBER);
LogUtil.d(str, "bucketNum =" + intData + ", netType = " + netWorkType.getNetworkType());
for (int i2 = 0; i2 < intData; i2++) {
String data = getData(context, Constants.BUCKET_NAME + i2);
if (data != null) {
setBooleanData(context, data + Constants.LBS_STATUS, false);
setData(context, data + Constants.NET_TYPE, netWorkType.getNetworkType());
}
}
}
}
public static String pipeBuildPostDataUrl(String str, String str2, String str3, long j2, boolean z) {
String str4;
if (str3 != null) {
str4 = encode(str) + "/" + encode(str2) + "?version=1.0&context=" + str3 + "&offset=" + j2 + "&complete=" + z;
} else {
str4 = encode(str) + "/" + encode(str2) + "?version=1.0&offset=" + j2 + "&complete=" + z;
}
return "/" + str4;
}
public static String pipeBuildQueryUrl(String str, String str2, String str3) {
String str4;
if (str3 != null) {
str4 = encode(str) + "/" + encode(str2) + "?uploadContext&version=1.0&context=" + str3;
} else {
str4 = encode(str) + "/" + encode(str2) + "?uploadContext&version=1.0";
}
return "/" + str4;
}
public static void releaseLock(CountDownLatch countDownLatch) {
countDownLatch.countDown();
}
private static String replaceWithHttps(String str) {
return str.replaceAll(JPushConstants.HTTP_PRE, JPushConstants.HTTPS_PRE);
}
public static void setBooleanData(Context context, String str, boolean z) {
SharedPreferences.Editor edit = getDefaultPreferences(context).edit();
edit.putBoolean(str, z);
edit.commit();
}
public static void setBucketName(Context context, String str) {
SharedPreferences defaultPreferences = getDefaultPreferences(context);
int i2 = defaultPreferences.getInt(Constants.BUCKET_NUMBER, 0);
for (int i3 = 0; i3 < i2; i3++) {
if (defaultPreferences.getString(Constants.BUCKET_NAME + i3, null).equals(str)) {
return;
}
}
SharedPreferences.Editor edit = defaultPreferences.edit();
edit.putString(Constants.BUCKET_NAME + i2, str);
edit.putInt(Constants.BUCKET_NUMBER, i2 + 1);
edit.commit();
}
public static void setData(Context context, String str, String str2) {
SharedPreferences.Editor edit = getDefaultPreferences(context).edit();
edit.putString(str, str2);
edit.commit();
}
public static Request.Builder setHeader(Request.Builder builder, Map<String, String> map) {
if (map == null) {
return builder;
}
for (String str : map.keySet()) {
builder.addHeader(str, map.get(str));
}
return builder;
}
public static HttpResult setLBSData(Context context, String str, JSONObject jSONObject) {
try {
String string = jSONObject.getString("lbs");
String transformString = transformString(jSONObject.getJSONArray("upload"));
String str2 = LOGTAG;
LogUtil.d(str2, "lbsString: " + string);
LogUtil.d(str2, "upload server string: " + transformString);
if (string != null) {
setData(context, str + Constants.LBS_KEY, string);
}
if (transformString != null) {
String replaceWithHttps = replaceWithHttps(transformString);
LogUtil.d(str2, "https servers: " + replaceWithHttps);
setData(context, str + Constants.UPLOAD_SERVER_KEY, transformString);
setData(context, str + Constants.HTTPS_UPLOAD_SERVER_KEY, replaceWithHttps);
setLongData(context, str + Constants.LBS_TIME, Long.valueOf(System.currentTimeMillis()));
setBooleanData(context, str + Constants.LBS_STATUS, true);
}
setBucketName(context, str);
return new HttpResult(Code.HTTP_SUCCESS, jSONObject, null);
} catch (JSONException e2) {
LogUtil.e(LOGTAG, "get json array exception", e2);
return new HttpResult(Code.INVALID_LBS_DATA, jSONObject, null);
}
}
public static void setLock(CountDownLatch countDownLatch) {
try {
countDownLatch.await();
} catch (InterruptedException e2) {
LogUtil.e(LOGTAG, "set lock with interrupted exception", e2);
}
}
public static void setLongData(Context context, String str, Long l2) {
SharedPreferences.Editor edit = getDefaultPreferences(context).edit();
edit.putLong(str, l2.longValue());
edit.commit();
}
public static int transformCode(int i2) {
if (i2 != -5) {
if (i2 != -4) {
if (i2 != -3) {
return i2 != -2 ? Code.UNKNOWN_REASON : Code.INVALID_TOKEN;
}
return 500;
}
return Code.HTTP_EXCEPTION;
}
return Code.HTTP_NO_RESPONSE;
}
private static String transformString(JSONArray jSONArray) {
if (jSONArray == null || jSONArray.length() == 0) {
return null;
}
String str = "";
for (int i2 = 0; i2 < jSONArray.length(); i2++) {
try {
str = str + jSONArray.getString(i2);
if (i2 != jSONArray.length() - 1) {
str = str + ";";
}
} catch (JSONException e2) {
LogUtil.e(LOGTAG, "get json string exception", e2);
}
}
return str;
}
}