Fixed http request crash
This commit is contained in:
@@ -128,8 +128,8 @@ import eu.chainfire.libsuperuser.Shell;
|
|||||||
public class Miscellaneous extends Service
|
public class Miscellaneous extends Service
|
||||||
{
|
{
|
||||||
protected static String writeableFolderStringCache = null;
|
protected static String writeableFolderStringCache = null;
|
||||||
protected final static String http_error_string = "HTTP_ERROR";
|
protected final static String http_error_string = "HTTP_ERROR";
|
||||||
protected final static String last_trigger_url_result_string = "last_trigger_url_result";
|
protected final static String last_trigger_url_result_string = "last_trigger_url_result";
|
||||||
protected final static String httpMainData = "httpMainData";
|
protected final static String httpMainData = "httpMainData";
|
||||||
protected final static String doNoEncodingString = "NoEncoding";
|
protected final static String doNoEncodingString = "NoEncoding";
|
||||||
protected final static String httpEncoding = "UTF-8";
|
protected final static String httpEncoding = "UTF-8";
|
||||||
@@ -163,15 +163,15 @@ public class Miscellaneous extends Service
|
|||||||
HttpClient httpclient = new DefaultHttpClient();
|
HttpClient httpclient = new DefaultHttpClient();
|
||||||
StringBuilder responseBody = new StringBuilder();
|
StringBuilder responseBody = new StringBuilder();
|
||||||
boolean errorFound = false;
|
boolean errorFound = false;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
URL urlObject = new URL(url);
|
URL urlObject = new URL(url);
|
||||||
HttpURLConnection connection;
|
HttpURLConnection connection;
|
||||||
|
|
||||||
if(url.toLowerCase().contains("https"))
|
if(url.toLowerCase().contains("https"))
|
||||||
{
|
{
|
||||||
connection = (HttpsURLConnection) urlObject.openConnection();
|
connection = (HttpsURLConnection) urlObject.openConnection();
|
||||||
if(Settings.httpAcceptAllCertificates)
|
if(Settings.httpAcceptAllCertificates)
|
||||||
@@ -187,21 +187,21 @@ public class Miscellaneous extends Service
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
connection = (HttpURLConnection) urlObject.openConnection();
|
connection = (HttpURLConnection) urlObject.openConnection();
|
||||||
|
|
||||||
// Add http simple authentication if specified
|
// Add http simple authentication if specified
|
||||||
if(username != null && password != null)
|
if(username != null && password != null)
|
||||||
{
|
{
|
||||||
String encodedCredentials = Base64.encodeToString(new String(username + ":" + password).getBytes(), Base64.DEFAULT);
|
String encodedCredentials = Base64.encodeToString(new String(username + ":" + password).getBytes(), Base64.DEFAULT);
|
||||||
connection.setRequestMethod("POST");
|
connection.setRequestMethod("POST");
|
||||||
connection.setDoOutput(true);
|
connection.setDoOutput(true);
|
||||||
connection.setRequestProperty ("Authorization", "Basic " + encodedCredentials);
|
connection.setRequestProperty ("Authorization", "Basic " + encodedCredentials);
|
||||||
}
|
}
|
||||||
else if(method.equals(ActivityManageActionTriggerUrl.methodPost))
|
else if(method.equals(ActivityManageActionTriggerUrl.methodPost))
|
||||||
connection.setRequestMethod("POST");
|
connection.setRequestMethod("POST");
|
||||||
|
|
||||||
if(httpParams.size() > 0)
|
if(httpParams != null && httpParams.size() > 0)
|
||||||
{
|
{
|
||||||
connection.setRequestMethod("POST");
|
connection.setRequestMethod("POST");
|
||||||
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||||
@@ -220,29 +220,29 @@ public class Miscellaneous extends Service
|
|||||||
writer.close();
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
InputStream content = (InputStream)connection.getInputStream();
|
InputStream content = (InputStream)connection.getInputStream();
|
||||||
BufferedReader in = new BufferedReader (new InputStreamReader (content));
|
BufferedReader in = new BufferedReader (new InputStreamReader (content));
|
||||||
String line;
|
String line;
|
||||||
while ((line = in.readLine()) != null)
|
while ((line = in.readLine()) != null)
|
||||||
responseBody.append(line + Miscellaneous.lineSeparator);
|
responseBody.append(line + Miscellaneous.lineSeparator);
|
||||||
}
|
}
|
||||||
catch(Exception e)
|
catch(Exception e)
|
||||||
{
|
{
|
||||||
Miscellaneous.logEvent("e", "HTTP error", Log.getStackTraceString(e), 3);
|
Miscellaneous.logEvent("e", "HTTP error", Log.getStackTraceString(e), 3);
|
||||||
errorFound = true;
|
errorFound = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
if(errorFound)
|
if(errorFound)
|
||||||
return http_error_string;
|
return http_error_string;
|
||||||
else
|
else
|
||||||
return responseBody.toString();
|
return responseBody.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getQuery(List<NameValuePair> params, String method) throws UnsupportedEncodingException
|
private static String getQuery(List<NameValuePair> params, String method) throws UnsupportedEncodingException
|
||||||
@@ -295,35 +295,35 @@ public class Miscellaneous extends Service
|
|||||||
|
|
||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String downloadUrlWithoutCertificateChecking(String url, String username, String password, String method, Map<String, String> httpParams)
|
public static String downloadUrlWithoutCertificateChecking(String url, String username, String password, String method, Map<String, String> httpParams)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
HttpParams params = new BasicHttpParams();
|
HttpParams params = new BasicHttpParams();
|
||||||
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
|
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
|
||||||
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
|
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
|
||||||
HttpClient httpclient = new DefaultHttpClient(params);
|
HttpClient httpclient = new DefaultHttpClient(params);
|
||||||
httpclient = Actions.getInsecureSslClient(httpclient);
|
httpclient = Actions.getInsecureSslClient(httpclient);
|
||||||
|
|
||||||
HttpRequestBase httpRequest;
|
HttpRequestBase httpRequest;
|
||||||
if(
|
if(
|
||||||
method.equals(ActivityManageActionTriggerUrl.methodPost)
|
method.equals(ActivityManageActionTriggerUrl.methodPost)
|
||||||
||
|
||
|
||||||
(username != null && password != null)
|
(username != null && password != null)
|
||||||
||
|
||
|
||||||
httpParams.size() > 0
|
httpParams.size() > 0
|
||||||
)
|
)
|
||||||
httpRequest = new HttpPost(url);
|
httpRequest = new HttpPost(url);
|
||||||
else
|
else
|
||||||
httpRequest = new HttpGet(url);
|
httpRequest = new HttpGet(url);
|
||||||
|
|
||||||
// Add http simple authentication if specified
|
// Add http simple authentication if specified
|
||||||
if(username != null && password != null)
|
if(username != null && password != null)
|
||||||
{
|
{
|
||||||
String encodedCredentials = Base64.encodeToString(new String(username + ":" + password).getBytes(), Base64.DEFAULT);
|
String encodedCredentials = Base64.encodeToString(new String(username + ":" + password).getBytes(), Base64.DEFAULT);
|
||||||
httpRequest.addHeader("Authorization", "Basic " + encodedCredentials);
|
httpRequest.addHeader("Authorization", "Basic " + encodedCredentials);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(httpParams.size() > 0)
|
if(httpParams.size() > 0)
|
||||||
{
|
{
|
||||||
@@ -334,20 +334,20 @@ public class Miscellaneous extends Service
|
|||||||
|
|
||||||
((HttpPost)httpRequest).setEntity(new UrlEncodedFormEntity(paramPairs, "UTF-8"));
|
((HttpPost)httpRequest).setEntity(new UrlEncodedFormEntity(paramPairs, "UTF-8"));
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpResponse response = httpclient.execute(httpRequest);
|
HttpResponse response = httpclient.execute(httpRequest);
|
||||||
HttpEntity entity = response.getEntity();
|
HttpEntity entity = response.getEntity();
|
||||||
if (entity != null)
|
if (entity != null)
|
||||||
{
|
{
|
||||||
// System.out.println(EntityUtils.toString(entity));
|
// System.out.println(EntityUtils.toString(entity));
|
||||||
return EntityUtils.toString(entity);
|
return EntityUtils.toString(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch(Exception e)
|
catch(Exception e)
|
||||||
{
|
{
|
||||||
Miscellaneous.logEvent("e", "HTTP error", Log.getStackTraceString(e), 3);
|
Miscellaneous.logEvent("e", "HTTP error", Log.getStackTraceString(e), 3);
|
||||||
return http_error_string;
|
return http_error_string;
|
||||||
}
|
}
|
||||||
// finally
|
// finally
|
||||||
// {
|
// {
|
||||||
// // When HttpClient instance is no longer needed,
|
// // When HttpClient instance is no longer needed,
|
||||||
@@ -356,34 +356,34 @@ public class Miscellaneous extends Service
|
|||||||
// httpclient.getConnectionManager().shutdown();
|
// httpclient.getConnectionManager().shutdown();
|
||||||
// return responseBody.toString();
|
// return responseBody.toString();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int boolToInt(boolean input)
|
public static int boolToInt(boolean input)
|
||||||
{
|
{
|
||||||
if(input)
|
if(input)
|
||||||
return 1;
|
return 1;
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind(Intent arg0)
|
public IBinder onBind(Intent arg0)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void logEvent(String type, String header, String description, int logLevel)
|
public static void logEvent(String type, String header, String description, int logLevel)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
header = getAnyContext().getResources().getString(R.string.app_name);
|
header = getAnyContext().getResources().getString(R.string.app_name);
|
||||||
}
|
}
|
||||||
catch(NullPointerException e)
|
catch(NullPointerException e)
|
||||||
{
|
{
|
||||||
header = "Automation";
|
header = "Automation";
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Settings.logToConsole)
|
if(Settings.logToConsole)
|
||||||
{
|
{
|
||||||
@@ -411,19 +411,19 @@ public class Miscellaneous extends Service
|
|||||||
protected static boolean logCleanerRunning = false;
|
protected static boolean logCleanerRunning = false;
|
||||||
protected static void rotateLogFile(File logFile)
|
protected static void rotateLogFile(File logFile)
|
||||||
{
|
{
|
||||||
logCleanerRunning = true;
|
logCleanerRunning = true;
|
||||||
|
|
||||||
long maxSizeInBytes = (long)Settings.logFileMaxSize * 1024 * 1024;
|
long maxSizeInBytes = (long)Settings.logFileMaxSize * 1024 * 1024;
|
||||||
|
|
||||||
if(logFile.exists() && logFile.length() > (maxSizeInBytes))
|
if(logFile.exists() && logFile.length() > (maxSizeInBytes))
|
||||||
{
|
{
|
||||||
Miscellaneous.logEvent("i", "Logfile", "Cleaning up log file.", 3);
|
Miscellaneous.logEvent("i", "Logfile", "Cleaning up log file.", 3);
|
||||||
File archivedLogFile = new File(getWriteableFolder() + "/" + logFileName + "-old");
|
File archivedLogFile = new File(getWriteableFolder() + "/" + logFileName + "-old");
|
||||||
logFile.renameTo(archivedLogFile);
|
logFile.renameTo(archivedLogFile);
|
||||||
Miscellaneous.logEvent("i", "Logfile", "Cleaning up log file finished. Old log renamed to " + archivedLogFile.getAbsolutePath(), 3);
|
Miscellaneous.logEvent("i", "Logfile", "Cleaning up log file finished. Old log renamed to " + archivedLogFile.getAbsolutePath(), 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
logCleanerRunning = false;
|
logCleanerRunning = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static boolean testFolder(String folderPath)
|
protected static boolean testFolder(String folderPath)
|
||||||
@@ -492,34 +492,34 @@ public class Miscellaneous extends Service
|
|||||||
{
|
{
|
||||||
// if (testFolder(f))
|
// if (testFolder(f))
|
||||||
// {
|
// {
|
||||||
String pathToUse = f + "/" + Settings.folderName;
|
String pathToUse = f + "/" + Settings.folderName;
|
||||||
|
|
||||||
// Toast.makeText(getAnyContext(), "Using " + pathToUse + " to store settings and log.", Toast.LENGTH_LONG).show();
|
// Toast.makeText(getAnyContext(), "Using " + pathToUse + " to store settings and log.", Toast.LENGTH_LONG).show();
|
||||||
// Migrate existing files
|
// Migrate existing files
|
||||||
File oldDirectory = new File(pathToUse);
|
File oldDirectory = new File(pathToUse);
|
||||||
File newDirectory = new File(writeableFolderStringCache);
|
File newDirectory = new File(writeableFolderStringCache);
|
||||||
File oldConfigFilePath = new File(pathToUse + "/" + XmlFileInterface.settingsFileName);
|
File oldConfigFilePath = new File(pathToUse + "/" + XmlFileInterface.settingsFileName);
|
||||||
if (oldConfigFilePath.exists() && oldConfigFilePath.canWrite())
|
if (oldConfigFilePath.exists() && oldConfigFilePath.canWrite())
|
||||||
{
|
{
|
||||||
Miscellaneous.logEvent("i", "Path", "Found old path " + pathToUse + " for settings and logs. Migrating old files to new directory.", 2);
|
Miscellaneous.logEvent("i", "Path", "Found old path " + pathToUse + " for settings and logs. Migrating old files to new directory.", 2);
|
||||||
|
|
||||||
for (File fileToBeMoved : oldDirectory.listFiles())
|
for (File fileToBeMoved : oldDirectory.listFiles())
|
||||||
{
|
{
|
||||||
File dstFile = new File(writeableFolderStringCache + "/" + fileToBeMoved.getName());
|
File dstFile = new File(writeableFolderStringCache + "/" + fileToBeMoved.getName());
|
||||||
|
|
||||||
/*
|
/*
|
||||||
For some stupid reason Android's file.moveTo can't move files between
|
For some stupid reason Android's file.moveTo can't move files between
|
||||||
mount points. That's why we have to copy it and delete the src if successful.
|
mount points. That's why we have to copy it and delete the src if successful.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if(copyFileUsingStream(fileToBeMoved, dstFile))
|
if(copyFileUsingStream(fileToBeMoved, dstFile))
|
||||||
fileToBeMoved.delete();
|
fileToBeMoved.delete();
|
||||||
}
|
|
||||||
|
|
||||||
String message = String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.filesHaveBeenMovedTo), newDirectory.getAbsolutePath());
|
|
||||||
Miscellaneous.writeStringToFile(oldDirectory.getAbsolutePath() + "/readme.txt", message);
|
|
||||||
break migration;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String message = String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.filesHaveBeenMovedTo), newDirectory.getAbsolutePath());
|
||||||
|
Miscellaneous.writeStringToFile(oldDirectory.getAbsolutePath() + "/readme.txt", message);
|
||||||
|
break migration;
|
||||||
|
}
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
} catch (Exception e)
|
} catch (Exception e)
|
||||||
@@ -560,10 +560,10 @@ public class Miscellaneous extends Service
|
|||||||
FileWriter fileWriter = new FileWriter(getLogFile(), true);
|
FileWriter fileWriter = new FileWriter(getLogFile(), true);
|
||||||
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
|
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
|
||||||
Date date = new Date();
|
Date date = new Date();
|
||||||
|
|
||||||
bufferedWriter.write("\n" + date + ": " + type + " / " + header + " / " + description);
|
bufferedWriter.write("\n" + date + ": " + type + " / " + header + " / " + description);
|
||||||
bufferedWriter.close();
|
bufferedWriter.close();
|
||||||
|
|
||||||
// Log.i("LogFile", "Log entry written.");
|
// Log.i("LogFile", "Log entry written.");
|
||||||
}
|
}
|
||||||
catch(Exception e)
|
catch(Exception e)
|
||||||
@@ -571,21 +571,21 @@ public class Miscellaneous extends Service
|
|||||||
Log.e("LogFile", "Error writing logs to file: " + e.getMessage());
|
Log.e("LogFile", "Error writing logs to file: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isAndroidEmulator()
|
public static boolean isAndroidEmulator()
|
||||||
{
|
{
|
||||||
String TAG = "EmulatorTest";
|
String TAG = "EmulatorTest";
|
||||||
String model = Build.MODEL;
|
String model = Build.MODEL;
|
||||||
// Miscellaneous.logEvent("i", TAG, "model=" + model);
|
// Miscellaneous.logEvent("i", TAG, "model=" + model);
|
||||||
String product = Build.PRODUCT;
|
String product = Build.PRODUCT;
|
||||||
// Miscellaneous.logEvent("i", TAG, "product=" + product);
|
// Miscellaneous.logEvent("i", TAG, "product=" + product);
|
||||||
boolean isEmulator = false;
|
boolean isEmulator = false;
|
||||||
if (product != null)
|
if (product != null)
|
||||||
{
|
{
|
||||||
isEmulator = product.equals("sdk") || product.contains("_sdk") || product.contains("sdk_");
|
isEmulator = product.equals("sdk") || product.contains("_sdk") || product.contains("sdk_");
|
||||||
}
|
}
|
||||||
// Miscellaneous.logEvent("i", TAG, "isEmulator=" + isEmulator);
|
// Miscellaneous.logEvent("i", TAG, "isEmulator=" + isEmulator);
|
||||||
return isEmulator;
|
return isEmulator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean compare(String direction, String needle, String haystack)
|
public static boolean compare(String direction, String needle, String haystack)
|
||||||
@@ -593,8 +593,8 @@ public class Miscellaneous extends Service
|
|||||||
// If only one of needle or haystack is null
|
// If only one of needle or haystack is null
|
||||||
if(
|
if(
|
||||||
(needle == null && haystack != null)
|
(needle == null && haystack != null)
|
||||||
||
|
||
|
||||||
(needle != null && haystack == null)
|
(needle != null && haystack == null)
|
||||||
)
|
)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@@ -619,29 +619,29 @@ public class Miscellaneous extends Service
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int compareTimes(TimeObject time1, TimeObject time2)
|
public static int compareTimes(TimeObject time1, TimeObject time2)
|
||||||
{
|
{
|
||||||
// Miscellaneous.logEvent("i", "TimeCompare", "To compare: " + time1.toString() + " / " + time2.toString());
|
// Miscellaneous.logEvent("i", "TimeCompare", "To compare: " + time1.toString() + " / " + time2.toString());
|
||||||
|
|
||||||
if(time1.getHours() == time2.getHours() && time1.getMinutes() == time2.getMinutes())
|
if(time1.getHours() == time2.getHours() && time1.getMinutes() == time2.getMinutes())
|
||||||
{
|
{
|
||||||
// Miscellaneous.logEvent("i", "TimeCompare", "Times are equal.");
|
// Miscellaneous.logEvent("i", "TimeCompare", "Times are equal.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(time1.getHours() > time2.getHours())
|
if(time1.getHours() > time2.getHours())
|
||||||
{
|
{
|
||||||
// Miscellaneous.logEvent("i", "TimeCompare", "Time1 is bigger/later by hours.");
|
// Miscellaneous.logEvent("i", "TimeCompare", "Time1 is bigger/later by hours.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(time1.getHours() < time2.getHours())
|
if(time1.getHours() < time2.getHours())
|
||||||
{
|
{
|
||||||
// Miscellaneous.logEvent("i", "TimeCompare", "Time2 is bigger/later by hours.");
|
// Miscellaneous.logEvent("i", "TimeCompare", "Time2 is bigger/later by hours.");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(time1.getHours() == time2.getHours())
|
if(time1.getHours() == time2.getHours())
|
||||||
{
|
{
|
||||||
if(time1.getMinutes() < time2.getMinutes())
|
if(time1.getMinutes() < time2.getMinutes())
|
||||||
@@ -649,14 +649,14 @@ public class Miscellaneous extends Service
|
|||||||
// Miscellaneous.logEvent("i", "TimeCompare", "Hours are equal. Time2 is bigger/later by minutes.");
|
// Miscellaneous.logEvent("i", "TimeCompare", "Hours are equal. Time2 is bigger/later by minutes.");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(time1.getMinutes() > time2.getMinutes())
|
if(time1.getMinutes() > time2.getMinutes())
|
||||||
{
|
{
|
||||||
// Miscellaneous.logEvent("i", "TimeCompare", "Hours are equal. Time1 is bigger/later by minutes.");
|
// Miscellaneous.logEvent("i", "TimeCompare", "Hours are equal. Time1 is bigger/later by minutes.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Miscellaneous.logEvent("i", "TimeCompare", "Default return code. Shouldn't be here.", 5);
|
Miscellaneous.logEvent("i", "TimeCompare", "Default return code. Shouldn't be here.", 5);
|
||||||
return 0;
|
return 0;
|
||||||
@@ -700,13 +700,13 @@ public class Miscellaneous extends Service
|
|||||||
Miscellaneous.logEvent("i", "TimeCompare", "Default return code. Shouldn't be here.", 5);
|
Miscellaneous.logEvent("i", "TimeCompare", "Default return code. Shouldn't be here.", 5);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String convertStreamToString(InputStream is)
|
public static String convertStreamToString(InputStream is)
|
||||||
{
|
{
|
||||||
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
|
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
|
||||||
return s.hasNext() ? s.next() : "";
|
return s.hasNext() ? s.next() : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Context getAnyContext()
|
public static Context getAnyContext()
|
||||||
{
|
{
|
||||||
Context returnContext;
|
Context returnContext;
|
||||||
@@ -744,7 +744,7 @@ public class Miscellaneous extends Service
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
public static String replaceVariablesInText(String source, Context context) throws Exception
|
public static String replaceVariablesInText(String source, Context context) throws Exception
|
||||||
{
|
{
|
||||||
@@ -752,7 +752,7 @@ public class Miscellaneous extends Service
|
|||||||
// Miscellaneous.logEvent("i", "Raw source", source);
|
// Miscellaneous.logEvent("i", "Raw source", source);
|
||||||
if(source.contains("[uniqueid]"))
|
if(source.contains("[uniqueid]"))
|
||||||
source = source.replace("[uniqueid]", Secure.getString(context.getContentResolver(), Secure.ANDROID_ID));
|
source = source.replace("[uniqueid]", Secure.getString(context.getContentResolver(), Secure.ANDROID_ID));
|
||||||
|
|
||||||
if(source.contains("[latitude]") | source.contains("[longitude]"))
|
if(source.contains("[latitude]") | source.contains("[longitude]"))
|
||||||
{
|
{
|
||||||
if(LocationProvider.getLastKnownLocation() != null)
|
if(LocationProvider.getLastKnownLocation() != null)
|
||||||
@@ -765,17 +765,17 @@ public class Miscellaneous extends Service
|
|||||||
Miscellaneous.logEvent("w", "TriggerURL", context.getResources().getString(R.string.triggerUrlReplacementPositionError), 3);
|
Miscellaneous.logEvent("w", "TriggerURL", context.getResources().getString(R.string.triggerUrlReplacementPositionError), 3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(source.contains("[phonenr]"))
|
if(source.contains("[phonenr]"))
|
||||||
{
|
{
|
||||||
String lastPhoneNr = PhoneStatusListener.getLastPhoneNumber();
|
String lastPhoneNr = PhoneStatusListener.getLastPhoneNumber();
|
||||||
|
|
||||||
if(lastPhoneNr != null && lastPhoneNr.length() > 0)
|
if(lastPhoneNr != null && lastPhoneNr.length() > 0)
|
||||||
source = source.replace("[phonenr]", PhoneStatusListener.getLastPhoneNumber());
|
source = source.replace("[phonenr]", PhoneStatusListener.getLastPhoneNumber());
|
||||||
else
|
else
|
||||||
Miscellaneous.logEvent("w", "TriggerURL", context.getResources().getString(R.string.triggerUrlReplacementPositionError), 3);
|
Miscellaneous.logEvent("w", "TriggerURL", context.getResources().getString(R.string.triggerUrlReplacementPositionError), 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(source.contains("[serialnr]"))
|
if(source.contains("[serialnr]"))
|
||||||
{
|
{
|
||||||
if (Build.VERSION.SDK_INT > 8)
|
if (Build.VERSION.SDK_INT > 8)
|
||||||
@@ -786,16 +786,16 @@ public class Miscellaneous extends Service
|
|||||||
|
|
||||||
if(
|
if(
|
||||||
source.contains("[d]") ||
|
source.contains("[d]") ||
|
||||||
source.contains("[m]") ||
|
source.contains("[m]") ||
|
||||||
source.contains("[Y]") ||
|
source.contains("[Y]") ||
|
||||||
source.contains("[h]") ||
|
source.contains("[h]") ||
|
||||||
source.contains("[H]") ||
|
source.contains("[H]") ||
|
||||||
source.contains("[i]") ||
|
source.contains("[i]") ||
|
||||||
source.contains("[s]") ||
|
source.contains("[s]") ||
|
||||||
source.contains("[ms]") ||
|
source.contains("[ms]") ||
|
||||||
source.contains("[w]") ||
|
source.contains("[w]") ||
|
||||||
source.contains("[F]")
|
source.contains("[F]")
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
Calendar cal = Calendar.getInstance();
|
Calendar cal = Calendar.getInstance();
|
||||||
|
|
||||||
@@ -911,7 +911,7 @@ public class Miscellaneous extends Service
|
|||||||
|
|
||||||
if (notificationText != null && notificationText.length() > 0)
|
if (notificationText != null && notificationText.length() > 0)
|
||||||
//source = source.replace("[notificationText]", escapeStringForUrl(notificationText));
|
//source = source.replace("[notificationText]", escapeStringForUrl(notificationText));
|
||||||
source = source.replace("[notificationText]", notificationText);
|
source = source.replace("[notificationText]", notificationText);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
source = source.replace("[notificationText]", "notificationText unknown");
|
source = source.replace("[notificationText]", "notificationText unknown");
|
||||||
@@ -1016,18 +1016,18 @@ public class Miscellaneous extends Service
|
|||||||
|
|
||||||
source = source.substring(0, pos1) + escapeStringForUrl(replacement) + source.substring(pos2 +1);
|
source = source.substring(0, pos1) + escapeStringForUrl(replacement) + source.substring(pos2 +1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Miscellaneous.logEvent("i", "URL after replace", source);
|
// Miscellaneous.logEvent("i", "URL after replace", source);
|
||||||
|
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a log entry and exit the application, so the crash is actually visible.
|
* Write a log entry and exit the application, so the crash is actually visible.
|
||||||
* Might even cause the activity to be automatically restarted by the OS.
|
* Might even cause the activity to be automatically restarted by the OS.
|
||||||
*/
|
*/
|
||||||
public static UncaughtExceptionHandler uncaughtExceptionHandler = new UncaughtExceptionHandler()
|
public static UncaughtExceptionHandler uncaughtExceptionHandler = new UncaughtExceptionHandler()
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public void uncaughtException(Thread thread, Throwable ex)
|
public void uncaughtException(Thread thread, Throwable ex)
|
||||||
{
|
{
|
||||||
@@ -1035,13 +1035,13 @@ public class Miscellaneous extends Service
|
|||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
public static AlertDialog messageBox(String title, String message, Context context)
|
public static AlertDialog messageBox(String title, String message, Context context)
|
||||||
{
|
{
|
||||||
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
|
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
|
||||||
|
|
||||||
alertDialog.setTitle(title);
|
alertDialog.setTitle(title);
|
||||||
alertDialog.setMessage(message);
|
alertDialog.setMessage(message);
|
||||||
|
|
||||||
alertDialog.setPositiveButton(context.getResources().getString(R.string.ok), new DialogInterface.OnClickListener()
|
alertDialog.setPositiveButton(context.getResources().getString(R.string.ok), new DialogInterface.OnClickListener()
|
||||||
{
|
{
|
||||||
@@ -1071,12 +1071,12 @@ public class Miscellaneous extends Service
|
|||||||
}
|
}
|
||||||
return haveConnectedWifi || haveConnectedMobile;
|
return haveConnectedWifi || haveConnectedMobile;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the device is rooted.
|
* Checks if the device is rooted.
|
||||||
*
|
*
|
||||||
* @return <code>true</code> if the device is rooted, <code>false</code> otherwise.
|
* @return <code>true</code> if the device is rooted, <code>false</code> otherwise.
|
||||||
*/
|
*/
|
||||||
public static boolean isPhoneRooted()
|
public static boolean isPhoneRooted()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -1103,14 +1103,14 @@ public class Miscellaneous extends Service
|
|||||||
}
|
}
|
||||||
catch (Exception e1)
|
catch (Exception e1)
|
||||||
{
|
{
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
|
|
||||||
// try executing commands
|
// try executing commands
|
||||||
return canExecuteCommand("/system/xbin/which su")
|
return canExecuteCommand("/system/xbin/which su")
|
||||||
||
|
||
|
||||||
canExecuteCommand("/system/bin/which su")
|
canExecuteCommand("/system/bin/which su")
|
||||||
||
|
||
|
||||||
canExecuteCommand("which su");
|
canExecuteCommand("which su");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1130,7 +1130,7 @@ public class Miscellaneous extends Service
|
|||||||
}
|
}
|
||||||
|
|
||||||
return executedSuccesfully;
|
return executedSuccesfully;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static char getDecimalSeparator()
|
public static char getDecimalSeparator()
|
||||||
{
|
{
|
||||||
@@ -1173,16 +1173,16 @@ public class Miscellaneous extends Service
|
|||||||
else
|
else
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isNumeric(String str)
|
public static boolean isNumeric(String str)
|
||||||
{
|
{
|
||||||
return str.matches("-?\\d+(\\.\\d+)?"); //match a number with optional '-' and decimal.
|
return str.matches("-?\\d+(\\.\\d+)?"); //match a number with optional '-' and decimal.
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disables the SSL certificate checking for new instances of {@link HttpsURLConnection} This has been created to
|
* Disables the SSL certificate checking for new instances of {@link HttpsURLConnection} This has been created to
|
||||||
* aid testing on a local box, not for use on production.
|
* aid testing on a local box, not for use on production.
|
||||||
*/
|
*/
|
||||||
private static void disableSSLCertificateChecking()
|
private static void disableSSLCertificateChecking()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -1226,48 +1226,48 @@ public class Miscellaneous extends Service
|
|||||||
Miscellaneous.logEvent("e", "SSL", Log.getStackTraceString(e), 4);
|
Miscellaneous.logEvent("e", "SSL", Log.getStackTraceString(e), 4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TrustManager[] getInsecureTrustManager()
|
public static TrustManager[] getInsecureTrustManager()
|
||||||
{
|
{
|
||||||
TrustManager[] trustAllCerts =
|
TrustManager[] trustAllCerts =
|
||||||
new TrustManager[]
|
new TrustManager[]
|
||||||
{
|
{
|
||||||
new X509TrustManager()
|
new X509TrustManager()
|
||||||
{
|
{
|
||||||
public X509Certificate[] getAcceptedIssuers()
|
public X509Certificate[] getAcceptedIssuers()
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException
|
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException
|
||||||
{
|
{
|
||||||
// Not implemented
|
// Not implemented
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException
|
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException
|
||||||
{
|
{
|
||||||
// Not implemented
|
// Not implemented
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return trustAllCerts;
|
return trustAllCerts;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HostnameVerifier getInsecureHostnameVerifier()
|
public static HostnameVerifier getInsecureHostnameVerifier()
|
||||||
{
|
{
|
||||||
HostnameVerifier allHostsValid = new HostnameVerifier()
|
HostnameVerifier allHostsValid = new HostnameVerifier()
|
||||||
{
|
{
|
||||||
public boolean verify(String hostname, SSLSession session)
|
public boolean verify(String hostname, SSLSession session)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return allHostsValid;
|
return allHostsValid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
@@ -2083,7 +2083,7 @@ public class Miscellaneous extends Service
|
|||||||
//create dir if required while unzipping
|
//create dir if required while unzipping
|
||||||
if (ze.isDirectory())
|
if (ze.isDirectory())
|
||||||
{
|
{
|
||||||
// dirChecker(ze.getName());
|
// dirChecker(ze.getName());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -2175,49 +2175,49 @@ public class Miscellaneous extends Service
|
|||||||
return formattedDate;
|
return formattedDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int arraySearchIndexOf(String[] haystack, String needle, boolean caseSensitive, boolean matchFullLine)
|
public static int arraySearchIndexOf(String[] haystack, String needle, boolean caseSensitive, boolean matchFullLine)
|
||||||
{
|
{
|
||||||
if(matchFullLine)
|
if(matchFullLine)
|
||||||
{
|
{
|
||||||
if(caseSensitive)
|
if(caseSensitive)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < haystack.length; i++)
|
for (int i = 0; i < haystack.length; i++)
|
||||||
{
|
{
|
||||||
if (haystack[i].equals(needle))
|
if (haystack[i].equals(needle))
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (int i = 0; i < haystack.length; i++)
|
for (int i = 0; i < haystack.length; i++)
|
||||||
{
|
{
|
||||||
if (haystack[i].toLowerCase().equals(needle.toLowerCase()))
|
if (haystack[i].toLowerCase().equals(needle.toLowerCase()))
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if(caseSensitive)
|
if(caseSensitive)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < haystack.length; i++)
|
for (int i = 0; i < haystack.length; i++)
|
||||||
{
|
{
|
||||||
if (haystack[i].contains(needle))
|
if (haystack[i].contains(needle))
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (int i = 0; i < haystack.length; i++)
|
for (int i = 0; i < haystack.length; i++)
|
||||||
{
|
{
|
||||||
if (haystack[i].toLowerCase().contains(needle.toLowerCase()))
|
if (haystack[i].toLowerCase().contains(needle.toLowerCase()))
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean arraySearch(String[] haystack, String needle, boolean caseSensitive, boolean matchFullLine)
|
public static boolean arraySearch(String[] haystack, String needle, boolean caseSensitive, boolean matchFullLine)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user