Skip to content

Commit ae821b9

Browse files
committed
RuntimePermissions: Prepare WritingTree to the new permission checks
1 parent 7c083ca commit ae821b9

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

project/app/src/main/java/com/achep/base/permissions/RuntimePermissions.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,13 @@ public static boolean has(@NonNull Context context, @NonNull String permission)
5353
return r == PackageManager.PERMISSION_GRANTED;
5454
}
5555

56+
/**
57+
* @return {@code true} if the app may ask this permission,
58+
* {@code false} otherwise.
59+
*/
60+
@SuppressLint("NewApi")
61+
public static boolean allowed(@NonNull Context context, @NonNull String permission) {
62+
return true;
63+
}
64+
5665
}

project/app/src/main/java/com/achep/base/timber/WritingTree.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
*/
1919
package com.achep.base.timber;
2020

21+
import android.Manifest;
22+
import android.content.Context;
2123
import android.os.Environment;
2224
import android.support.annotation.NonNull;
2325
import android.util.Log;
2426

27+
import com.achep.base.AppHeap;
2528
import com.achep.base.Build;
29+
import com.achep.base.permissions.RuntimePermissions;
2630
import com.achep.base.tests.Check;
2731
import com.achep.base.utils.EncryptionUtils;
2832
import com.achep.base.utils.FileUtils;
@@ -90,29 +94,35 @@ public void run() {
9094
synchronized (mMonitor) {
9195
final int length = mBuilder.length();
9296
if (length > 0) {
97+
//noinspection ConstantConditions
9398
if (DEBUG) {
9499
Log.d(TAG, "Writing " + length + "-symbols log to a file.");
95100
}
96101

97-
//noinspection ConstantConditions
98-
CharSequence log = ENCRYPT_LOGS
99-
? EncryptionUtils.x(mBuilder.toString(), Build.LOG_KEY_SALT) + "\n"
100-
: mBuilder;
101-
102-
//noinspection PointlessBooleanExpression,ConstantConditions
103-
if (DEBUG && ENCRYPT_LOGS) {
104-
// Check if we can decrypt it
105-
String encrypted = log.toString().substring(0, log.length() - 2);
106-
Check.getInstance().isTrue(EncryptionUtils
107-
.fromX(encrypted, Build.LOG_KEY_SALT)
108-
.equals(mBuilder.toString()));
109-
}
102+
final Context context = AppHeap.getContext();
103+
final String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
104+
if (RuntimePermissions.has(context, permission)) {
105+
//noinspection ConstantConditions
106+
final CharSequence log;
107+
if (ENCRYPT_LOGS) {
108+
log = EncryptionUtils.x(mBuilder.toString(), Build.LOG_KEY_SALT) + "\n";
109+
if (DEBUG) {
110+
// Check if we can decrypt it
111+
String encrypted = log.toString().substring(0, log.length() - 2);
112+
Check.getInstance().isTrue(EncryptionUtils
113+
.fromX(encrypted, Build.LOG_KEY_SALT)
114+
.equals(mBuilder.toString()));
115+
}
116+
} else log = mBuilder;
110117

111-
try {
112118
final boolean succeed = FileUtils.writeToFileAppend(mFile, log);
113119
if (succeed) mBuilder.delete(0, length - 1);
114-
} catch (SecurityException e) {
115-
// TODO: Add the real permission check here.
120+
} else if (RuntimePermissions.allowed(context, permission)) {
121+
// TODO: Ask the permission
122+
} else {
123+
// We can not archive it, so lets just fall back
124+
// sit and cry.
125+
mBuilder.delete(0, length - 1);
116126
}
117127
}
118128

0 commit comments

Comments
 (0)