Use scoped app storage on Android (#11466)

From November 2021, the Play Store will no longer be accepting
apps which use the deprecated getExternalStorageDirectory() API.

Therefore, this commit replaces uses of deprecated API with the new
scoped API (`getExternalFilesDir()` and `getExternalCacheDir()`).
It also provides a temporary migration to move user data from the
shared external directory to new storage.

Fixes #2097,  #11417 and #11118
This commit is contained in:
rubenwardy 2021-10-15 17:14:48 +01:00 committed by GitHub
parent fe7195badb
commit 6901c5fae5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 276 additions and 186 deletions

@ -1,6 +1,7 @@
BasedOnStyle: LLVM BasedOnStyle: LLVM
IndentWidth: 8 IndentWidth: 4
UseTab: Always UseTab: Always
TabWidth: 4
BreakBeforeBraces: Custom BreakBeforeBraces: Custom
Standard: Cpp11 Standard: Cpp11
BraceWrapping: BraceWrapping:
@ -16,7 +17,7 @@ BraceWrapping:
FixNamespaceComments: false FixNamespaceComments: false
AllowShortIfStatementsOnASingleLine: false AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false IndentCaseLabels: false
AccessModifierOffset: -8 AccessModifierOffset: -4
ColumnLimit: 90 ColumnLimit: 90
AllowShortFunctionsOnASingleLine: InlineOnly AllowShortFunctionsOnASingleLine: InlineOnly
SortIncludes: false SortIncludes: false
@ -26,7 +27,7 @@ IncludeCategories:
- Regex: '^<.*' - Regex: '^<.*'
Priority: 1 Priority: 1
AlignAfterOpenBracket: DontAlign AlignAfterOpenBracket: DontAlign
ContinuationIndentWidth: 16 ContinuationIndentWidth: 8
ConstructorInitializerIndentWidth: 16 ConstructorInitializerIndentWidth: 8
BreakConstructorInitializers: AfterColon BreakConstructorInitializers: AfterColon
AlwaysBreakTemplateDeclarations: Yes AlwaysBreakTemplateDeclarations: Yes

@ -1,82 +0,0 @@
/*
Minetest
Copyright (C) 2014-2020 MoNTE48, Maksim Gamarnik <MoNTE48@mail.ua>
Copyright (C) 2014-2020 ubulem, Bektur Mambetov <berkut87@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package net.minetest.minetest;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.WeakReference;
public class CopyZipTask extends AsyncTask<String, Void, String> {
private final WeakReference<AppCompatActivity> activityRef;
CopyZipTask(AppCompatActivity activity) {
activityRef = new WeakReference<>(activity);
}
protected String doInBackground(String... params) {
copyAsset(params[0]);
return params[0];
}
@Override
protected void onPostExecute(String result) {
startUnzipService(result);
}
private void copyAsset(String zipName) {
String filename = zipName.substring(zipName.lastIndexOf("/") + 1);
try (InputStream in = activityRef.get().getAssets().open(filename);
OutputStream out = new FileOutputStream(zipName)) {
copyFile(in, out);
} catch (IOException e) {
AppCompatActivity activity = activityRef.get();
if (activity != null) {
activity.runOnUiThread(() -> Toast.makeText(activityRef.get(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show());
}
cancel(true);
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
out.write(buffer, 0, read);
}
private void startUnzipService(String file) {
Intent intent = new Intent(activityRef.get(), UnzipService.class);
intent.putExtra(UnzipService.EXTRA_KEY_IN_FILE, file);
AppCompatActivity activity = activityRef.get();
if (activity != null) {
activity.startService(intent);
}
}
}

@ -171,4 +171,12 @@ public class GameActivity extends NativeActivity {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(browserIntent); startActivity(browserIntent);
} }
public String getUserDataPath() {
return Utils.getUserDataDirectory(this).getAbsolutePath();
}
public String getCachePath() {
return Utils.getCacheDirectory(this).getAbsolutePath();
}
} }

@ -29,12 +29,14 @@ import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.view.View; import android.view.View;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
@ -43,11 +45,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import static net.minetest.minetest.UnzipService.ACTION_FAILURE; import static net.minetest.minetest.UnzipService.*;
import static net.minetest.minetest.UnzipService.ACTION_PROGRESS;
import static net.minetest.minetest.UnzipService.ACTION_UPDATE;
import static net.minetest.minetest.UnzipService.FAILURE;
import static net.minetest.minetest.UnzipService.SUCCESS;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private final static int versionCode = BuildConfig.VERSION_CODE; private final static int versionCode = BuildConfig.VERSION_CODE;
@ -56,26 +54,40 @@ public class MainActivity extends AppCompatActivity {
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}; new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
private static final String SETTINGS = "MinetestSettings"; private static final String SETTINGS = "MinetestSettings";
private static final String TAG_VERSION_CODE = "versionCode"; private static final String TAG_VERSION_CODE = "versionCode";
private ProgressBar mProgressBar; private ProgressBar mProgressBar;
private TextView mTextView; private TextView mTextView;
private SharedPreferences sharedPreferences; private SharedPreferences sharedPreferences;
private final BroadcastReceiver myReceiver = new BroadcastReceiver() { private final BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
int progress = 0; int progress = 0;
if (intent != null) @StringRes int message = 0;
if (intent != null) {
progress = intent.getIntExtra(ACTION_PROGRESS, 0); progress = intent.getIntExtra(ACTION_PROGRESS, 0);
if (progress >= 0) { message = intent.getIntExtra(ACTION_PROGRESS_MESSAGE, 0);
if (mProgressBar != null) {
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setProgress(progress);
} }
mTextView.setVisibility(View.VISIBLE);
} else if (progress == FAILURE) { if (progress == FAILURE) {
Toast.makeText(MainActivity.this, intent.getStringExtra(ACTION_FAILURE), Toast.LENGTH_LONG).show(); Toast.makeText(MainActivity.this, intent.getStringExtra(ACTION_FAILURE), Toast.LENGTH_LONG).show();
finish(); finish();
} else if (progress == SUCCESS) } else if (progress == SUCCESS) {
startNative(); startNative();
} else {
if (mProgressBar != null) {
mProgressBar.setVisibility(View.VISIBLE);
if (progress == INDETERMINATE) {
mProgressBar.setIndeterminate(true);
} else {
mProgressBar.setIndeterminate(false);
mProgressBar.setProgress(progress);
}
}
mTextView.setVisibility(View.VISIBLE);
if (message != 0)
mTextView.setText(message);
}
} }
}; };
@ -88,6 +100,7 @@ public class MainActivity extends AppCompatActivity {
mProgressBar = findViewById(R.id.progressBar); mProgressBar = findViewById(R.id.progressBar);
mTextView = findViewById(R.id.textView); mTextView = findViewById(R.id.textView);
sharedPreferences = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE); sharedPreferences = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
checkPermission(); checkPermission();
else else
@ -120,6 +133,7 @@ public class MainActivity extends AppCompatActivity {
if (grantResult != PackageManager.PERMISSION_GRANTED) { if (grantResult != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, R.string.not_granted, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.not_granted, Toast.LENGTH_LONG).show();
finish(); finish();
return;
} }
} }
checkAppVersion(); checkAppVersion();
@ -127,10 +141,27 @@ public class MainActivity extends AppCompatActivity {
} }
private void checkAppVersion() { private void checkAppVersion() {
if (sharedPreferences.getInt(TAG_VERSION_CODE, 0) == versionCode) if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
Toast.makeText(this, R.string.no_external_storage, Toast.LENGTH_LONG).show();
finish();
return;
}
if (UnzipService.getIsRunning()) {
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setIndeterminate(true);
mTextView.setVisibility(View.VISIBLE);
} else if (sharedPreferences.getInt(TAG_VERSION_CODE, 0) == versionCode &&
Utils.isInstallValid(this)) {
startNative(); startNative();
else } else {
new CopyZipTask(this).execute(getCacheDir() + "/Minetest.zip"); mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setIndeterminate(true);
mTextView.setVisibility(View.VISIBLE);
Intent intent = new Intent(this, UnzipService.class);
startService(intent);
}
} }
private void startNative() { private void startNative() {

@ -24,16 +24,21 @@ import android.app.IntentService;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.os.Environment; import android.os.Environment;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
@ -42,32 +47,61 @@ import java.util.zip.ZipInputStream;
public class UnzipService extends IntentService { public class UnzipService extends IntentService {
public static final String ACTION_UPDATE = "net.minetest.minetest.UPDATE"; public static final String ACTION_UPDATE = "net.minetest.minetest.UPDATE";
public static final String ACTION_PROGRESS = "net.minetest.minetest.PROGRESS"; public static final String ACTION_PROGRESS = "net.minetest.minetest.PROGRESS";
public static final String ACTION_PROGRESS_MESSAGE = "net.minetest.minetest.PROGRESS_MESSAGE";
public static final String ACTION_FAILURE = "net.minetest.minetest.FAILURE"; public static final String ACTION_FAILURE = "net.minetest.minetest.FAILURE";
public static final String EXTRA_KEY_IN_FILE = "file";
public static final int SUCCESS = -1; public static final int SUCCESS = -1;
public static final int FAILURE = -2; public static final int FAILURE = -2;
public static final int INDETERMINATE = -3;
private final int id = 1; private final int id = 1;
private NotificationManager mNotifyManager; private NotificationManager mNotifyManager;
private boolean isSuccess = true; private boolean isSuccess = true;
private String failureMessage; private String failureMessage;
private static boolean isRunning = false;
public static synchronized boolean getIsRunning() {
return isRunning;
}
private static synchronized void setIsRunning(boolean v) {
isRunning = v;
}
public UnzipService() { public UnzipService() {
super("net.minetest.minetest.UnzipService"); super("net.minetest.minetest.UnzipService");
} }
private void isDir(String dir, String location) {
File f = new File(location, dir);
if (!f.isDirectory())
f.mkdirs();
}
@Override @Override
protected void onHandleIntent(Intent intent) { protected void onHandleIntent(Intent intent) {
createNotification(); Notification.Builder notificationBuilder = createNotification();
unzip(intent); final File zipFile = new File(getCacheDir(), "Minetest.zip");
try {
setIsRunning(true);
File userDataDirectory = Utils.getUserDataDirectory(this);
if (userDataDirectory == null) {
throw new IOException("Unable to find user data directory");
} }
private void createNotification() { try (InputStream in = this.getAssets().open(zipFile.getName())) {
try (OutputStream out = new FileOutputStream(zipFile)) {
int readLen;
byte[] readBuffer = new byte[16384];
while ((readLen = in.read(readBuffer)) != -1) {
out.write(readBuffer, 0, readLen);
}
}
}
migrate(notificationBuilder, userDataDirectory);
unzip(notificationBuilder, zipFile, userDataDirectory);
} catch (IOException e) {
isSuccess = false;
failureMessage = e.getLocalizedMessage();
} finally {
setIsRunning(false);
zipFile.delete();
}
}
private Notification.Builder createNotification() {
String name = "net.minetest.minetest"; String name = "net.minetest.minetest";
String channelId = "Minetest channel"; String channelId = "Minetest channel";
String description = "notifications from Minetest"; String description = "notifications from Minetest";
@ -92,66 +126,129 @@ public class UnzipService extends IntentService {
} else { } else {
builder = new Notification.Builder(this); builder = new Notification.Builder(this);
} }
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
builder.setContentTitle(getString(R.string.notification_title)) builder.setContentTitle(getString(R.string.notification_title))
.setSmallIcon(R.mipmap.ic_launcher) .setSmallIcon(R.mipmap.ic_launcher)
.setContentText(getString(R.string.notification_description)); .setContentText(getString(R.string.notification_description))
.setContentIntent(intent)
.setOngoing(true)
.setProgress(0, 0, true);
mNotifyManager.notify(id, builder.build()); mNotifyManager.notify(id, builder.build());
return builder;
} }
private void unzip(Intent intent) { private void unzip(Notification.Builder notificationBuilder, File zipFile, File userDataDirectory) throws IOException {
String zip = intent.getStringExtra(EXTRA_KEY_IN_FILE);
isDir("Minetest", Environment.getExternalStorageDirectory().toString());
String location = Environment.getExternalStorageDirectory() + File.separator + "Minetest" + File.separator;
int per = 0; int per = 0;
int size = getSummarySize(zip);
File zipFile = new File(zip); int size;
try (ZipFile zipSize = new ZipFile(zipFile)) {
size = zipSize.size();
}
int readLen; int readLen;
byte[] readBuffer = new byte[8192]; byte[] readBuffer = new byte[16384];
try (FileInputStream fileInputStream = new FileInputStream(zipFile); try (FileInputStream fileInputStream = new FileInputStream(zipFile);
ZipInputStream zipInputStream = new ZipInputStream(fileInputStream)) { ZipInputStream zipInputStream = new ZipInputStream(fileInputStream)) {
ZipEntry ze; ZipEntry ze;
while ((ze = zipInputStream.getNextEntry()) != null) { while ((ze = zipInputStream.getNextEntry()) != null) {
if (ze.isDirectory()) { if (ze.isDirectory()) {
++per; ++per;
isDir(ze.getName(), location); Utils.createDirs(userDataDirectory, ze.getName());
} else { continue;
publishProgress(100 * ++per / size); }
try (OutputStream outputStream = new FileOutputStream(location + ze.getName())) { publishProgress(notificationBuilder, R.string.loading, 100 * ++per / size);
try (OutputStream outputStream = new FileOutputStream(
new File(userDataDirectory, ze.getName()))) {
while ((readLen = zipInputStream.read(readBuffer)) != -1) { while ((readLen = zipInputStream.read(readBuffer)) != -1) {
outputStream.write(readBuffer, 0, readLen); outputStream.write(readBuffer, 0, readLen);
} }
} }
} }
zipFile.delete();
}
} catch (IOException e) {
isSuccess = false;
failureMessage = e.getLocalizedMessage();
} }
} }
private void publishProgress(int progress) { void moveFileOrDir(@NonNull File src, @NonNull File dst) throws IOException {
try {
Process p = new ProcessBuilder("/system/bin/mv",
src.getAbsolutePath(), dst.getAbsolutePath()).start();
int exitcode = p.waitFor();
if (exitcode != 0)
throw new IOException("Move failed with exit code " + exitcode);
} catch (InterruptedException e) {
throw new IOException("Move operation interrupted");
}
}
boolean recursivelyDeleteDirectory(@NonNull File loc) {
try {
Process p = new ProcessBuilder("/system/bin/rm", "-rf",
loc.getAbsolutePath()).start();
return p.waitFor() == 0;
} catch (IOException | InterruptedException e) {
return false;
}
}
/**
* Migrates user data from deprecated external storage to app scoped storage
*/
private void migrate(Notification.Builder notificationBuilder, File newLocation) throws IOException {
File oldLocation = new File(Environment.getExternalStorageDirectory(), "Minetest");
if (!oldLocation.isDirectory())
return;
publishProgress(notificationBuilder, R.string.migrating, 0);
newLocation.mkdir();
String[] dirs = new String[] { "worlds", "games", "mods", "textures", "client" };
for (int i = 0; i < dirs.length; i++) {
publishProgress(notificationBuilder, R.string.migrating, 100 * i / dirs.length);
File dir = new File(oldLocation, dirs[i]), dir2 = new File(newLocation, dirs[i]);
if (dir.isDirectory() && !dir2.isDirectory()) {
moveFileOrDir(dir, dir2);
}
}
for (String filename : new String[] { "minetest.conf" }) {
File file = new File(oldLocation, filename), file2 = new File(newLocation, filename);
if (file.isFile() && !file2.isFile()) {
moveFileOrDir(file, file2);
}
}
recursivelyDeleteDirectory(oldLocation);
}
private void publishProgress(@Nullable Notification.Builder notificationBuilder, @StringRes int message, int progress) {
Intent intentUpdate = new Intent(ACTION_UPDATE); Intent intentUpdate = new Intent(ACTION_UPDATE);
intentUpdate.putExtra(ACTION_PROGRESS, progress); intentUpdate.putExtra(ACTION_PROGRESS, progress);
if (!isSuccess) intentUpdate.putExtra(ACTION_FAILURE, failureMessage); intentUpdate.putExtra(ACTION_PROGRESS_MESSAGE, message);
if (!isSuccess)
intentUpdate.putExtra(ACTION_FAILURE, failureMessage);
sendBroadcast(intentUpdate); sendBroadcast(intentUpdate);
}
private int getSummarySize(String zip) { if (notificationBuilder != null) {
int size = 0; notificationBuilder.setContentText(getString(message));
try { if (progress == INDETERMINATE) {
ZipFile zipSize = new ZipFile(zip); notificationBuilder.setProgress(100, 50, true);
size += zipSize.size(); } else {
} catch (IOException e) { notificationBuilder.setProgress(100, progress, false);
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show(); }
mNotifyManager.notify(id, notificationBuilder.build());
} }
return size;
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
mNotifyManager.cancel(id); mNotifyManager.cancel(id);
publishProgress(isSuccess ? SUCCESS : FAILURE); publishProgress(null, R.string.loading, isSuccess ? SUCCESS : FAILURE);
} }
} }

@ -0,0 +1,39 @@
package net.minetest.minetest;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.File;
public class Utils {
public static @NonNull File createDirs(File root, String dir) {
File f = new File(root, dir);
if (!f.isDirectory())
f.mkdirs();
return f;
}
public static @Nullable File getUserDataDirectory(Context context) {
File extDir = context.getExternalFilesDir(null);
if (extDir == null) {
return null;
}
return createDirs(extDir, "Minetest");
}
public static @Nullable File getCacheDirectory(Context context) {
return context.getCacheDir();
}
public static boolean isInstallValid(Context context) {
File userDataDirectory = getUserDataDirectory(context);
return userDataDirectory != null && userDataDirectory.isDirectory() &&
new File(userDataDirectory, "games").isDirectory() &&
new File(userDataDirectory, "builtin").isDirectory() &&
new File(userDataDirectory, "client").isDirectory() &&
new File(userDataDirectory, "textures").isDirectory();
}
}

@ -1,4 +1,5 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@ -14,7 +15,8 @@
android:layout_marginRight="90dp" android:layout_marginRight="90dp"
android:indeterminate="false" android:indeterminate="false"
android:max="100" android:max="100"
android:visibility="gone" /> android:visibility="gone"
tools:visibility="visible" />
<TextView <TextView
android:id="@+id/textView" android:id="@+id/textView"
@ -25,6 +27,7 @@
android:background="@android:color/transparent" android:background="@android:color/transparent"
android:text="@string/loading" android:text="@string/loading"
android:textColor="#FEFEFE" android:textColor="#FEFEFE"
android:visibility="gone" /> android:visibility="gone"
tools:visibility="visible" />
</RelativeLayout> </RelativeLayout>

@ -3,9 +3,11 @@
<string name="label">Minetest</string> <string name="label">Minetest</string>
<string name="loading">Loading&#8230;</string> <string name="loading">Loading&#8230;</string>
<string name="migrating">Migrating save data from old install&#8230; (this may take a while)</string>
<string name="not_granted">Required permission wasn\'t granted, Minetest can\'t run without it</string> <string name="not_granted">Required permission wasn\'t granted, Minetest can\'t run without it</string>
<string name="notification_title">Loading Minetest</string> <string name="notification_title">Loading Minetest</string>
<string name="notification_description">Less than 1 minute&#8230;</string> <string name="notification_description">Less than 1 minute&#8230;</string>
<string name="ime_dialog_done">Done</string> <string name="ime_dialog_done">Done</string>
<string name="no_external_storage">External storage isn\'t available. If you use an SDCard, please reinsert it. Otherwise, try restarting your phone or contacting the Minetest developers</string>
</resources> </resources>

@ -41,6 +41,10 @@ android {
arguments 'NDEBUG=1' arguments 'NDEBUG=1'
} }
} }
ndk {
debugSymbolLevel 'SYMBOL_TABLE'
}
} }
} }
} }

@ -152,49 +152,36 @@ static std::string javaStringToUTF8(jstring js)
return str; return str;
} }
// Calls static method if obj is NULL
static std::string getAndroidPath(
jclass cls, jobject obj, jmethodID mt_getAbsPath, const char *getter)
{
// Get getter method
jmethodID mt_getter;
if (obj)
mt_getter = jnienv->GetMethodID(cls, getter, "()Ljava/io/File;");
else
mt_getter = jnienv->GetStaticMethodID(cls, getter, "()Ljava/io/File;");
// Call getter
jobject ob_file;
if (obj)
ob_file = jnienv->CallObjectMethod(obj, mt_getter);
else
ob_file = jnienv->CallStaticObjectMethod(cls, mt_getter);
// Call getAbsolutePath
auto js_path = (jstring) jnienv->CallObjectMethod(ob_file, mt_getAbsPath);
return javaStringToUTF8(js_path);
}
void initializePathsAndroid() void initializePathsAndroid()
{ {
// Get Environment class // Set user and share paths
jclass cls_Env = jnienv->FindClass("android/os/Environment"); {
// Get File class jmethodID getUserDataPath = jnienv->GetMethodID(nativeActivity,
jclass cls_File = jnienv->FindClass("java/io/File"); "getUserDataPath", "()Ljava/lang/String;");
// Get getAbsolutePath method FATAL_ERROR_IF(getUserDataPath==nullptr,
jmethodID mt_getAbsPath = jnienv->GetMethodID(cls_File, "porting::initializePathsAndroid unable to find Java getUserDataPath method");
"getAbsolutePath", "()Ljava/lang/String;"); jobject result = jnienv->CallObjectMethod(app_global->activity->clazz, getUserDataPath);
std::string path_storage = getAndroidPath(cls_Env, nullptr, const char *javachars = jnienv->GetStringUTFChars((jstring) result, nullptr);
mt_getAbsPath, "getExternalStorageDirectory"); path_user = javachars;
path_share = javachars;
path_user = path_storage + DIR_DELIM + PROJECT_NAME_C;
path_share = path_storage + DIR_DELIM + PROJECT_NAME_C;
path_locale = path_share + DIR_DELIM + "locale"; path_locale = path_share + DIR_DELIM + "locale";
path_cache = getAndroidPath(nativeActivity, jnienv->ReleaseStringUTFChars((jstring) result, javachars);
app_global->activity->clazz, mt_getAbsPath, "getCacheDir"); }
// Set cache path
{
jmethodID getCachePath = jnienv->GetMethodID(nativeActivity,
"getCachePath", "()Ljava/lang/String;");
FATAL_ERROR_IF(getCachePath==nullptr,
"porting::initializePathsAndroid unable to find Java getCachePath method");
jobject result = jnienv->CallObjectMethod(app_global->activity->clazz, getCachePath);
const char *javachars = jnienv->GetStringUTFChars((jstring) result, nullptr);
path_cache = javachars;
jnienv->ReleaseStringUTFChars((jstring) result, javachars);
migrateCachePath(); migrateCachePath();
} }
}
void showInputDialog(const std::string &acceptButton, const std::string &hint, void showInputDialog(const std::string &acceptButton, const std::string &hint,
const std::string &current, int editType) const std::string &current, int editType)