Skip to content

Commit 373bad1

Browse files
Maksimubulem
Maksim
andauthoredMay 4, 2020
Android: some java-part improvements (#9760)
Replace Log to Toast. Start Native only after successful unpacking. Light refactoring in CopyZipTask. Update NDK version. Co-authored-by: ubulem <berkut87@gmail.com>
1 parent 66c1825 commit 373bad1

File tree

6 files changed

+32
-25
lines changed

6 files changed

+32
-25
lines changed
 

Diff for: ‎build/android/app/build.gradle

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ apply plugin: 'com.android.application'
22
android {
33
compileSdkVersion 29
44
buildToolsVersion '29.0.3'
5-
ndkVersion '21.0.6113669'
5+
ndkVersion '21.1.6352462'
66
defaultConfig {
77
applicationId 'net.minetest.minetest'
88
minSdkVersion 16
9-
//noinspection OldTargetApi
10-
targetSdkVersion 28 // Workaround for using `/sdcard` instead of the `data` patch for assets
9+
targetSdkVersion 29
1110
versionName "${versionMajor}.${versionMinor}.${versionPatch}"
1211
versionCode project.versionCode
1312
}

Diff for: ‎build/android/app/src/main/AndroidManifest.xml

+7
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@
77
<uses-permission android:name="android.permission.INTERNET" />
88
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
99

10+
<!--
11+
`android:requestLegacyExternalStorage="true"` is workaround for using `/sdcard`
12+
instead of the `getFilesDir()` patch for assets. Check link below for more information:
13+
https://developer.android.com/training/data-storage/compatibility
14+
-->
15+
1016
<application
1117
android:allowBackup="false"
1218
android:icon="@mipmap/ic_launcher"
1319
android:label="@string/label"
1420
android:resizeableActivity="false"
21+
android:requestLegacyExternalStorage="true"
1522
tools:ignore="UnusedAttribute">
1623

1724
<meta-data

Diff for: ‎build/android/app/src/main/java/net/minetest/minetest/CopyZipTask.java

+4-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import android.content.Context;
2424
import android.content.Intent;
2525
import android.os.AsyncTask;
26-
import android.util.Log;
26+
import android.widget.Toast;
2727

2828
import java.io.FileOutputStream;
2929
import java.io.IOException;
@@ -40,7 +40,7 @@ public class CopyZipTask extends AsyncTask<String, Void, String> {
4040
}
4141

4242
protected String doInBackground(String... params) {
43-
copyAssets(params);
43+
copyAsset(params[0]);
4444
return params[0];
4545
}
4646

@@ -49,20 +49,13 @@ protected void onPostExecute(String result) {
4949
startUnzipService(result);
5050
}
5151

52-
private void copyAsset(String zipName) throws IOException {
52+
private void copyAsset(String zipName) {
5353
String filename = zipName.substring(zipName.lastIndexOf("/") + 1);
5454
try (InputStream in = contextRef.get().getAssets().open(filename);
5555
OutputStream out = new FileOutputStream(zipName)) {
5656
copyFile(in, out);
57-
}
58-
}
59-
60-
private void copyAssets(String[] zips) {
61-
try {
62-
for (String zipName : zips)
63-
copyAsset(zipName);
6457
} catch (IOException e) {
65-
Log.e("CopyZipTask", e.getLocalizedMessage());
58+
Toast.makeText(contextRef.get(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
6659
cancel(true);
6760
}
6861
}

Diff for: ‎build/android/app/src/main/java/net/minetest/minetest/MainActivity.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
import java.util.Arrays;
4444
import java.util.List;
4545

46+
import static net.minetest.minetest.UnzipService.ACTION_PROGRESS;
47+
import static net.minetest.minetest.UnzipService.ACTION_UPDATE;
48+
import static net.minetest.minetest.UnzipService.FAILURE;
49+
import static net.minetest.minetest.UnzipService.SUCCESS;
50+
4651
public class MainActivity extends AppCompatActivity {
4752
private final static int versionCode = BuildConfig.VERSION_CODE;
4853
private final static int PERMISSIONS = 1;
@@ -58,14 +63,16 @@ public class MainActivity extends AppCompatActivity {
5863
public void onReceive(Context context, Intent intent) {
5964
int progress = 0;
6065
if (intent != null)
61-
progress = intent.getIntExtra(UnzipService.ACTION_PROGRESS, 0);
66+
progress = intent.getIntExtra(ACTION_PROGRESS, 0);
6267
if (progress >= 0) {
6368
if (mProgressBar != null) {
6469
mProgressBar.setVisibility(View.VISIBLE);
6570
mProgressBar.setProgress(progress);
6671
}
6772
mTextView.setVisibility(View.VISIBLE);
68-
} else
73+
} else if (progress == FAILURE) {
74+
finish();
75+
} else if (progress == SUCCESS)
6976
startNative();
7077
}
7178
};
@@ -74,7 +81,7 @@ public void onReceive(Context context, Intent intent) {
7481
public void onCreate(Bundle savedInstanceState) {
7582
super.onCreate(savedInstanceState);
7683
setContentView(R.layout.activity_main);
77-
IntentFilter filter = new IntentFilter(UnzipService.ACTION_UPDATE);
84+
IntentFilter filter = new IntentFilter(ACTION_UPDATE);
7885
registerReceiver(myReceiver, filter);
7986
mProgressBar = findViewById(R.id.progressBar);
8087
mTextView = findViewById(R.id.textView);

Diff for: ‎build/android/app/src/main/java/net/minetest/minetest/UnzipService.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@
2828
import android.content.Intent;
2929
import android.os.Build;
3030
import android.os.Environment;
31-
import android.util.Log;
31+
import android.widget.Toast;
3232

3333
import java.io.File;
3434
import java.io.FileInputStream;
35-
import java.io.FileNotFoundException;
3635
import java.io.FileOutputStream;
3736
import java.io.IOException;
3837
import java.io.OutputStream;
@@ -44,9 +43,12 @@ public class UnzipService extends IntentService {
4443
public static final String ACTION_UPDATE = "net.minetest.minetest.UPDATE";
4544
public static final String ACTION_PROGRESS = "net.minetest.minetest.PROGRESS";
4645
public static final String EXTRA_KEY_IN_FILE = "file";
46+
public static final int SUCCESS = -1;
47+
public static final int FAILURE = -2;
4748
private static final String TAG = "UnzipService";
4849
private final int id = 1;
4950
private NotificationManager mNotifyManager;
51+
private boolean isSuccess = true;
5052

5153
public UnzipService() {
5254
super("net.minetest.minetest.UnzipService");
@@ -120,10 +122,9 @@ private void unzip(Intent intent) {
120122
}
121123
zipFile.delete();
122124
}
123-
} catch (FileNotFoundException e) {
124-
Log.e(TAG, e.getLocalizedMessage());
125125
} catch (IOException e) {
126-
Log.e(TAG, e.getLocalizedMessage());
126+
isSuccess = false;
127+
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
127128
}
128129
}
129130

@@ -139,7 +140,7 @@ private int getSummarySize(String zip) {
139140
ZipFile zipSize = new ZipFile(zip);
140141
size += zipSize.size();
141142
} catch (IOException e) {
142-
Log.e(TAG, e.getLocalizedMessage());
143+
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
143144
}
144145
return size;
145146
}
@@ -148,6 +149,6 @@ private int getSummarySize(String zip) {
148149
public void onDestroy() {
149150
super.onDestroy();
150151
mNotifyManager.cancel(id);
151-
publishProgress(-1);
152+
publishProgress(isSuccess ? SUCCESS : FAILURE);
152153
}
153154
}

Diff for: ‎build/android/native/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import org.ajoberstar.grgit.Grgit
44
android {
55
compileSdkVersion 29
66
buildToolsVersion '29.0.3'
7-
ndkVersion '21.0.6113669'
7+
ndkVersion '21.1.6352462'
88
defaultConfig {
99
minSdkVersion 16
1010
targetSdkVersion 29

0 commit comments

Comments
 (0)
Please sign in to comment.