Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rubenwardy committed Oct 15, 2021
1 parent fe7195b commit 6901c5f
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 186 deletions.
9 changes: 5 additions & 4 deletions .clang-format
@@ -1,6 +1,7 @@
BasedOnStyle: LLVM
IndentWidth: 8
IndentWidth: 4
UseTab: Always
TabWidth: 4
BreakBeforeBraces: Custom
Standard: Cpp11
BraceWrapping:
Expand All @@ -16,7 +17,7 @@ BraceWrapping:
FixNamespaceComments: false
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
AccessModifierOffset: -8
AccessModifierOffset: -4
ColumnLimit: 90
AllowShortFunctionsOnASingleLine: InlineOnly
SortIncludes: false
Expand All @@ -26,7 +27,7 @@ IncludeCategories:
- Regex: '^<.*'
Priority: 1
AlignAfterOpenBracket: DontAlign
ContinuationIndentWidth: 16
ConstructorInitializerIndentWidth: 16
ContinuationIndentWidth: 8
ConstructorInitializerIndentWidth: 8
BreakConstructorInitializers: AfterColon
AlwaysBreakTemplateDeclarations: Yes
82 changes: 0 additions & 82 deletions android/app/src/main/java/net/minetest/minetest/CopyZipTask.java

This file was deleted.

Expand Up @@ -171,4 +171,12 @@ public void openURI(String uri) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(browserIntent);
}

public String getUserDataPath() {
return Utils.getUserDataDirectory(this).getAbsolutePath();
}

public String getCachePath() {
return Utils.getCacheDirectory(this).getAbsolutePath();
}
}
63 changes: 47 additions & 16 deletions android/app/src/main/java/net/minetest/minetest/MainActivity.java
Expand Up @@ -29,12 +29,14 @@
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
Expand All @@ -43,11 +45,7 @@
import java.util.Arrays;
import java.util.List;

import static net.minetest.minetest.UnzipService.ACTION_FAILURE;
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;
import static net.minetest.minetest.UnzipService.*;

public class MainActivity extends AppCompatActivity {
private final static int versionCode = BuildConfig.VERSION_CODE;
Expand All @@ -56,26 +54,40 @@ public class MainActivity extends AppCompatActivity {
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
private static final String SETTINGS = "MinetestSettings";
private static final String TAG_VERSION_CODE = "versionCode";

private ProgressBar mProgressBar;
private TextView mTextView;
private SharedPreferences sharedPreferences;

private final BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int progress = 0;
if (intent != null)
@StringRes int message = 0;
if (intent != null) {
progress = intent.getIntExtra(ACTION_PROGRESS, 0);
if (progress >= 0) {
message = intent.getIntExtra(ACTION_PROGRESS_MESSAGE, 0);
}

if (progress == FAILURE) {
Toast.makeText(MainActivity.this, intent.getStringExtra(ACTION_FAILURE), Toast.LENGTH_LONG).show();
finish();
} else if (progress == SUCCESS) {
startNative();
} else {
if (mProgressBar != null) {
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setProgress(progress);
if (progress == INDETERMINATE) {
mProgressBar.setIndeterminate(true);
} else {
mProgressBar.setIndeterminate(false);
mProgressBar.setProgress(progress);
}
}
mTextView.setVisibility(View.VISIBLE);
} else if (progress == FAILURE) {
Toast.makeText(MainActivity.this, intent.getStringExtra(ACTION_FAILURE), Toast.LENGTH_LONG).show();
finish();
} else if (progress == SUCCESS)
startNative();
if (message != 0)
mTextView.setText(message);
}
}
};

Expand All @@ -88,6 +100,7 @@ public void onCreate(Bundle savedInstanceState) {
mProgressBar = findViewById(R.id.progressBar);
mTextView = findViewById(R.id.textView);
sharedPreferences = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
checkPermission();
else
Expand Down Expand Up @@ -120,17 +133,35 @@ public void onRequestPermissionsResult(int requestCode,
if (grantResult != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, R.string.not_granted, Toast.LENGTH_LONG).show();
finish();
return;
}
}
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();
else
new CopyZipTask(this).execute(getCacheDir() + "/Minetest.zip");
} else {
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setIndeterminate(true);
mTextView.setVisibility(View.VISIBLE);

Intent intent = new Intent(this, UnzipService.class);
startService(intent);
}
}

private void startNative() {
Expand Down

0 comments on commit 6901c5f

Please sign in to comment.