Skip to content

Commit 618b2a4

Browse files
committed
Update storage to Android 30+ requirements
1 parent e30ba75 commit 618b2a4

File tree

6 files changed

+261
-65
lines changed

6 files changed

+261
-65
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
applicationId "io.scalaproject.vault"
77
minSdkVersion 21
88
targetSdkVersion 32
9-
versionCode 9
10-
versionName "1.0.8"
9+
versionCode 10
10+
versionName "1.0.9"
1111

1212
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
1313
externalNativeBuild {

app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
<uses-permission android:name="android.permission.INTERNET" />
77
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
8-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
9-
tools:ignore="ScopedStorage" />
108
<uses-permission android:name="android.permission.WAKE_LOCK" />
119
<uses-permission android:name="android.permission.CAMERA" />
1210
<uses-permission android:name="android.permission.USE_BIOMETRIC" />

app/src/main/java/io/scalaproject/vault/LoginActivity.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@
5757
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
5858

5959
import org.acra.ACRA;
60-
import org.json.JSONArray;
61-
import org.json.JSONException;
62-
import org.json.JSONObject;
6360

6461
import io.scalaproject.vault.data.Node;
6562
import io.scalaproject.vault.data.NodeInfo;
@@ -78,6 +75,7 @@
7875
import io.scalaproject.vault.util.LocaleHelper;
7976
import io.scalaproject.vault.util.ScalaThreadPoolExecutor;
8077
import io.scalaproject.vault.widget.Toolbar;
78+
import io.scalaproject.vault.util.LegacyStorageHelper;
8179

8280
import java.io.File;
8381
import java.io.FileInputStream;
@@ -372,7 +370,7 @@ public void onButton(int type) {
372370
if (!processUsbIntent(intent))
373371
processUriIntent(intent);
374372

375-
if (Helper.getWritePermission(this)) {
373+
if (LegacyStorageHelper.getReadPermission(this)) {
376374
if (savedInstanceState == null) {
377375
startLoginFragment();
378376
}
@@ -388,6 +386,26 @@ public void onButton(int type) {
388386
}
389387
}
390388

389+
@Override
390+
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
391+
@NonNull int[] grantResults) {
392+
Timber.d("onRequestPermissionsResult()");
393+
switch (requestCode) {
394+
case LegacyStorageHelper.PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
395+
396+
// If request is cancelled, the result arrays are empty.
397+
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
398+
isPermissionGranted = true;
399+
} else {
400+
String msg = getString(R.string.message_strorage_not_permitted);
401+
Timber.e(msg);
402+
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
403+
}
404+
break;
405+
default:
406+
}
407+
}
408+
391409
boolean checkServiceRunning() {
392410
if (WalletService.Running) {
393411
Toast.makeText(this, getString(R.string.service_busy), Toast.LENGTH_SHORT).show();
@@ -873,25 +891,6 @@ void startReceive(File walletFile, String password) {
873891
startReceiveFragment(b);
874892
}
875893

876-
@Override
877-
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
878-
@NonNull int[] grantResults) {
879-
Timber.d("onRequestPermissionsResult()");
880-
switch (requestCode) {
881-
case Helper.PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE:
882-
// If request is cancelled, the result arrays are empty.
883-
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
884-
isPermissionGranted = true;
885-
} else {
886-
String msg = getString(R.string.message_strorage_not_permitted);
887-
Timber.e(msg);
888-
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
889-
}
890-
break;
891-
default:
892-
}
893-
}
894-
895894
private boolean isPermissionGranted = false;
896895

897896
@Override

app/src/main/java/io/scalaproject/vault/LoginFragment.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import io.scalaproject.vault.model.WalletManager;
5353
import io.scalaproject.vault.util.Helper;
5454
import io.scalaproject.vault.util.KeyStoreHelper;
55+
import io.scalaproject.vault.util.LegacyStorageHelper;
5556
import io.scalaproject.vault.util.Notice;
5657
import io.scalaproject.vault.widget.Toolbar;
5758

@@ -292,6 +293,8 @@ private void filterList() {
292293
public void loadList() {
293294
Timber.d("loadList");
294295

296+
LegacyStorageHelper.migrateWallets(getContext());
297+
295298
WalletManager mgr = WalletManager.getInstance();
296299
List<WalletManager.WalletInfo> walletInfos = mgr.findWallets(activityCallback.getStorageRoot());
297300

app/src/main/java/io/scalaproject/vault/util/Helper.java

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,12 @@
9393
import timber.log.Timber;
9494

9595
public class Helper {
96-
static private final String FLAVOR_SUFFIX =
97-
(BuildConfig.FLAVOR.startsWith("prod") ? "" : "." + BuildConfig.FLAVOR)
98-
+ (BuildConfig.DEBUG ? "-debug" : "");
99-
10096
static public final String NOCRAZYPASS_FLAGFILE = ".nocrazypass";
10197

10298
static public final String BASE_CRYPTO = "XLA";
10399

104-
static private final String WALLET_DIR = "scala" + FLAVOR_SUFFIX;
105-
static private final String HOME_DIR = "scala" + FLAVOR_SUFFIX;
100+
static private final String WALLET_DIR = "wallets";
101+
static private final String SCALA_DIR = "scala";
106102

107103
static public int DISPLAY_DIGITS_INFO = 5;
108104

@@ -111,12 +107,7 @@ static public File getWalletRoot(Context context) {
111107
}
112108

113109
static public File getStorage(Context context, String folderName) {
114-
if (!isExternalStorageWritable()) {
115-
String msg = context.getString(R.string.message_strorage_not_writable);
116-
Timber.e(msg);
117-
throw new IllegalStateException(msg);
118-
}
119-
File dir = new File(Environment.getExternalStorageDirectory(), folderName);
110+
File dir = new File(context.getFilesDir(), folderName);
120111
if (!dir.exists()) {
121112
Timber.i("Creating %s", dir.getAbsolutePath());
122113
dir.mkdirs(); // try to make it
@@ -129,24 +120,6 @@ static public File getStorage(Context context, String folderName) {
129120
return dir;
130121
}
131122

132-
static public final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
133-
134-
static public boolean getWritePermission(Activity context) {
135-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
136-
if (context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
137-
== PackageManager.PERMISSION_DENIED) {
138-
Timber.w("Permission denied to WRITE_EXTERNAL_STORAGE - requesting it");
139-
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
140-
context.requestPermissions(permissions, PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
141-
return false;
142-
} else {
143-
return true;
144-
}
145-
} else {
146-
return true;
147-
}
148-
}
149-
150123
static public final int PERMISSIONS_REQUEST_CAMERA = 7;
151124

152125
static public boolean getCameraPermission(Activity context) {
@@ -172,12 +145,6 @@ static public File getWalletFile(Context context, String aWalletName) {
172145
return f;
173146
}
174147

175-
/* Checks if external storage is available for read and write */
176-
private static boolean isExternalStorageWritable() {
177-
String state = Environment.getExternalStorageState();
178-
return Environment.MEDIA_MOUNTED.equals(state);
179-
}
180-
181148
static public void showKeyboard(Activity act) {
182149
if (act == null) return;
183150
if (act.getCurrentFocus() == null) {
@@ -389,7 +356,7 @@ public static byte[] hexToBytes(String hex) {
389356

390357
static public void setScalaHome(Context context) {
391358
try {
392-
String home = getStorage(context, HOME_DIR).getAbsolutePath();
359+
String home = getStorage(context, SCALA_DIR).getAbsolutePath();
393360
Os.setenv("HOME", home, true);
394361
} catch (ErrnoException ex) {
395362
throw new IllegalStateException(ex);
@@ -405,7 +372,7 @@ static public void initLogger(Context context) {
405372

406373
// TODO make the log levels refer to the WalletManagerFactory::LogLevel enum ?
407374
static public void initLogger(Context context, int level) {
408-
String home = getStorage(context, HOME_DIR).getAbsolutePath();
375+
String home = getStorage(context, SCALA_DIR).getAbsolutePath();
409376
WalletManager.initLogger(home + "/monerujo", "monerujo.log");
410377
if (level >= WalletManager.LOGLEVEL_SILENT)
411378
WalletManager.setLogLevel(level);

0 commit comments

Comments
 (0)