-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Utils.java
116 lines (97 loc) · 4.29 KB
/
Utils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.jacopomii.googledialermod;
import static com.jacopomii.googledialermod.Constants.DIALER_CALLRECORDINGPROMPT;
import static com.jacopomii.googledialermod.Constants.DIALER_DATA_DATA;
import static com.jacopomii.googledialermod.Constants.DIALER_PACKAGE_NAME;
import static com.jacopomii.googledialermod.Constants.PHENOTYPE_DB;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;
import androidx.appcompat.app.AlertDialog;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.topjohnwu.superuser.Shell;
import org.json.JSONException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Formatter;
public class Utils {
private static final String TAG = "Utils";
public static boolean checkIsDialerInstalled(Context context) {
try {
context.getPackageManager().getApplicationInfo(DIALER_PACKAGE_NAME, 0);
} catch (PackageManager.NameNotFoundException e) {
return false;
}
return Shell.cmd(String.format("test -d %s", DIALER_DATA_DATA)).exec().isSuccess();
}
public static boolean checkIsPhenotypeDBInstalled() {
return Shell.cmd(String.format("test -f %s", PHENOTYPE_DB)).exec().isSuccess();
}
public static void copyFile(InputStream inputStream, OutputStream outputStream) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, read);
}
inputStream.close();
outputStream.flush();
outputStream.close();
}
public static String byteArrayToHexString(byte[] byteArray) {
Formatter formatter = new Formatter();
for (byte b : byteArray)
formatter.format("%02x", b);
return formatter.toString();
}
public static void revertAllMods(Context context) {
DBFlagsSingleton.getInstance(context).deleteAllFlagOverrides();
deleteCallrecordingpromptFolder();
}
public static void deleteCallrecordingpromptFolder() {
Shell.cmd(
String.format(
"rm -rf %s",
DIALER_CALLRECORDINGPROMPT
)
).exec();
}
public static void checkIsLatestGithubVersion(Context context) {
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(
new JsonObjectRequest(
Request.Method.GET,
context.getString(R.string.github_api_link) + "/releases/latest",
null,
response -> {
try {
Version actualVersion = new Version(BuildConfig.VERSION_NAME);
Version fetchedVersion = new Version(response.getString("tag_name").substring(1));
if (actualVersion.compareTo(fetchedVersion) < 0) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.new_version_alert)
.setNeutralButton(R.string.github, null)
.setPositiveButton(android.R.string.ok, null);
AlertDialog alert = builder.create();
alert.setOnShowListener(dialogInterface -> alert.getButton(AlertDialog.BUTTON_NEUTRAL).setOnClickListener(
view ->
context.startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(context.getString(R.string.github_link)+"/releases"))
)
)
);
alert.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
},
error -> Log.e("Utils", "checkIsLatestGithubVersion: HTTP request failed")
)
);
}
}