Skip to content
This repository was archived by the owner on Oct 5, 2019. It is now read-only.

Commit 03d915b

Browse files
committed
Cache license
1 parent 103da8b commit 03d915b

File tree

6 files changed

+45
-81
lines changed

6 files changed

+45
-81
lines changed

app/src/main/java/com/grarak/kerneladiutor/activities/MainActivity.java

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@
6161
import com.grarak.kerneladiutor.utils.kernel.wake.Wake;
6262
import com.grarak.kerneladiutor.utils.root.RootUtils;
6363

64-
import java.io.File;
65-
6664
import io.fabric.sdk.android.Fabric;
6765

6866
/**
@@ -128,7 +126,17 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
128126
* 2: License is invalid
129127
* 3: Donate apk is patched/cracked
130128
*/
131-
launch(data == null ? -1 : data.getIntExtra("result", -1));
129+
int result = data == null ? -1 : data.getIntExtra("result", -1);
130+
if (result == 0) {
131+
try {
132+
ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(
133+
"com.grarak.kerneladiutordonate", 0);
134+
Utils.writeFile(applicationInfo.dataDir + "/license",
135+
Utils.encodeString(Utils.getAndroidId(this)), false, true);
136+
} catch (PackageManager.NameNotFoundException ignored) {
137+
}
138+
}
139+
launch(result);
132140

133141
} else if (requestCode == 1) {
134142

@@ -291,6 +299,7 @@ protected void onPostExecute(Void aVoid) {
291299
private PackageInfo mPackageInfo;
292300
private boolean mPatched;
293301
private boolean mInternetAvailable;
302+
private boolean mLicensedCached;
294303

295304
@Override
296305
protected void onPreExecute() {
@@ -309,33 +318,45 @@ protected void onPreExecute() {
309318

310319
@Override
311320
protected Boolean doInBackground(Void... params) {
312-
if (mApplicationInfo != null && mPackageInfo != null && mPackageInfo.versionCode == 130) {
313-
mPatched = !Utils.checkMD5("5c7a92a5b2dcec409035e1114e815b00",
314-
new File(mApplicationInfo.publicSourceDir));
321+
if (mApplicationInfo != null && mPackageInfo != null
322+
&& mPackageInfo.versionCode == 130) {
323+
mPatched = !Utils.getMD5sum(mApplicationInfo.publicSourceDir)
324+
.equals("5c7a92a5b2dcec409035e1114e815b00");
315325
try {
316-
Process process = Runtime.getRuntime().exec("ping -W 5 -c 1 8.8.8.8");
317-
mInternetAvailable = process.waitFor() == 0;
318-
process.destroy();
326+
if (Utils.existFile(mApplicationInfo.dataDir + "/license")) {
327+
String content = Utils.readFile(mApplicationInfo.dataDir + "/license");
328+
if (!content.isEmpty() && (content = Utils.decodeString(content)) != null) {
329+
if (content.equals(Utils.getAndroidId(MainActivity.this))) {
330+
mLicensedCached = true;
331+
}
332+
}
333+
}
334+
335+
if (!mLicensedCached) {
336+
Process process = Runtime.getRuntime().exec("ping -W 5 -c 1 8.8.8.8");
337+
mInternetAvailable = process.waitFor() == 0;
338+
process.destroy();
339+
}
319340
} catch (Exception e) {
320341
e.printStackTrace();
321342
}
343+
return !mPatched;
322344
}
323-
return mApplicationInfo != null && mPackageInfo != null && mPackageInfo.versionCode == 130
324-
&& !mPatched;
345+
return false;
325346
}
326347

327348
@Override
328-
protected void onPostExecute(Boolean aBoolean) {
329-
super.onPostExecute(aBoolean);
330-
if (aBoolean && mInternetAvailable) {
349+
protected void onPostExecute(Boolean donationValid) {
350+
super.onPostExecute(donationValid);
351+
if (donationValid && mLicensedCached) {
352+
launch(0);
353+
} else if (donationValid && mInternetAvailable) {
331354
Intent intent = new Intent(Intent.ACTION_MAIN);
332355
intent.setComponent(new ComponentName("com.grarak.kerneladiutordonate",
333356
"com.grarak.kerneladiutordonate.MainActivity"));
334357
startActivityForResult(intent, 0);
335-
} else if (mApplicationInfo != null && mPackageInfo != null
336-
&& mPackageInfo.versionCode == 130
337-
&& !mInternetAvailable && !mPatched) {
338-
launch(0);
358+
} else if (donationValid) {
359+
launch(1);
339360
} else {
340361
launch(mPatched ? 3 : -1);
341362
}

app/src/main/java/com/grarak/kerneladiutor/activities/NavigationActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,11 @@ public void onFocusChange(View v, boolean hasFocus) {
279279
int result = Prefs.getInt("license", -1, this);
280280
int intentResult = getIntent().getIntExtra("result", -1);
281281

282-
if ((result == intentResult && result == 2) && mLicenseDialog) {
282+
if ((result == intentResult && (result == 1 || result == 2)) && mLicenseDialog) {
283283
ViewUtils.dialogBuilder(getString(R.string.license_invalid), null,
284284
new DialogInterface.OnClickListener() {
285285
@Override
286286
public void onClick(DialogInterface dialog, int which) {
287-
288287
}
289288
}, new DialogInterface.OnDismissListener() {
290289
@Override

app/src/main/java/com/grarak/kerneladiutor/utils/Device.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public static String getVersion() {
154154
public static void load() {
155155
for (String prop : sProps) {
156156
ROM_VERSION = RootUtils.getProp(prop);
157-
if (!ROM_VERSION.isEmpty()) {
157+
if (ROM_VERSION != null && !ROM_VERSION.isEmpty()) {
158158
break;
159159
}
160160
}

app/src/main/java/com/grarak/kerneladiutor/utils/Utils.java

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949

5050
import java.io.BufferedReader;
5151
import java.io.File;
52-
import java.io.FileInputStream;
5352
import java.io.FileNotFoundException;
5453
import java.io.FileReader;
5554
import java.io.FileWriter;
@@ -58,10 +57,8 @@
5857
import java.io.InputStreamReader;
5958
import java.io.UnsupportedEncodingException;
6059
import java.math.BigDecimal;
61-
import java.math.BigInteger;
6260
import java.math.RoundingMode;
6361
import java.security.MessageDigest;
64-
import java.security.NoSuchAlgorithmException;
6562
import java.util.Locale;
6663
import java.util.Random;
6764

@@ -242,61 +239,8 @@ public static boolean isPatched(ApplicationInfo applicationInfo) {
242239
return false;
243240
}
244241

245-
// MD5 code from
246-
// https://github.com/CyanogenMod/android_packages_apps_CMUpdater/blob/cm-12.1/src/com/cyanogenmod/updater/utils/MD5.java
247-
public static boolean checkMD5(String md5, File updateFile) {
248-
if (md5 == null || updateFile == null || md5.isEmpty()) {
249-
Log.e(TAG, "MD5 string empty or updateFile null");
250-
return false;
251-
}
252-
253-
String calculatedDigest = calculateMD5(updateFile);
254-
if (calculatedDigest == null) {
255-
Log.e(TAG, "calculatedDigest null");
256-
return false;
257-
}
258-
259-
return calculatedDigest.equalsIgnoreCase(md5);
260-
}
261-
262-
private static String calculateMD5(File updateFile) {
263-
MessageDigest digest;
264-
try {
265-
digest = MessageDigest.getInstance("MD5");
266-
} catch (NoSuchAlgorithmException e) {
267-
Log.e(TAG, "Exception while getting digest", e);
268-
return null;
269-
}
270-
271-
InputStream is;
272-
try {
273-
is = new FileInputStream(updateFile);
274-
} catch (FileNotFoundException e) {
275-
Log.e(TAG, "Exception while getting FileInputStream", e);
276-
return null;
277-
}
278-
279-
byte[] buffer = new byte[8192];
280-
int read;
281-
try {
282-
while ((read = is.read(buffer)) > 0) {
283-
digest.update(buffer, 0, read);
284-
}
285-
byte[] md5sum = digest.digest();
286-
BigInteger bigInt = new BigInteger(1, md5sum);
287-
String output = bigInt.toString(16);
288-
// Fill to 32 chars
289-
output = String.format("%32s", output).replace(' ', '0');
290-
return output;
291-
} catch (IOException e) {
292-
throw new RuntimeException("Unable to process file for MD5", e);
293-
} finally {
294-
try {
295-
is.close();
296-
} catch (IOException e) {
297-
Log.e(TAG, "Exception on closing MD5 input stream", e);
298-
}
299-
}
242+
public static String getMD5sum(String file) {
243+
return file.isEmpty() ? "" : RootUtils.runCommand("md5sum -b " + file);
300244
}
301245

302246
public static boolean isTablet(Context context) {

app/src/main/java/com/grarak/kerneladiutor/views/recyclerview/downloads/DownloadKernelView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public void onPreExecute() {
219219

220220
@Override
221221
public Boolean doInBackground(Void arg) {
222-
return Utils.checkMD5(md5, new File(path));
222+
return Utils.getMD5sum(path).equals(md5);
223223
}
224224

225225
@Override

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<string name="ok">OK</string>
2222
<string name="unknown">Unknown</string>
2323

24-
<string name="license_invalid">Thanks for purchasing the donation app! Unfortunately license check failed. Please try again later. If it\'s still not working, please contact me.</string>
24+
<string name="license_invalid">Thanks for purchasing the donation app! Unfortunately license check failed. Please try again later. If it\'s still not working, please contact me. You need internet access!</string>
2525
<string name="pirated">Seems like the donation app wasn\'t purchased through Play Store! If you pirated it, then shame on you!</string>
2626
<string name="donate">Donate</string>
2727
<string name="donate_summary">This feature is only available when the donation app is installed! Don\'t worry though. Because this app is open source you can easily build it yourself and remove this check. But I hope you support developers anyway. Thanks for using Kernel Adiutor! Once you bought the donation app you have to restart Kernel Adiutor in order to enable all features.</string>

0 commit comments

Comments
 (0)