|
18 | 18 | */ |
19 | 19 | package com.achep.base.timber; |
20 | 20 |
|
| 21 | +import android.Manifest; |
| 22 | +import android.content.Context; |
21 | 23 | import android.os.Environment; |
22 | 24 | import android.support.annotation.NonNull; |
23 | 25 | import android.util.Log; |
24 | 26 |
|
| 27 | +import com.achep.base.AppHeap; |
25 | 28 | import com.achep.base.Build; |
| 29 | +import com.achep.base.permissions.RuntimePermissions; |
26 | 30 | import com.achep.base.tests.Check; |
27 | 31 | import com.achep.base.utils.EncryptionUtils; |
28 | 32 | import com.achep.base.utils.FileUtils; |
@@ -90,29 +94,35 @@ public void run() { |
90 | 94 | synchronized (mMonitor) { |
91 | 95 | final int length = mBuilder.length(); |
92 | 96 | if (length > 0) { |
| 97 | + //noinspection ConstantConditions |
93 | 98 | if (DEBUG) { |
94 | 99 | Log.d(TAG, "Writing " + length + "-symbols log to a file."); |
95 | 100 | } |
96 | 101 |
|
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; |
110 | 117 |
|
111 | | - try { |
112 | 118 | final boolean succeed = FileUtils.writeToFileAppend(mFile, log); |
113 | 119 | 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); |
116 | 126 | } |
117 | 127 | } |
118 | 128 |
|
|
0 commit comments