mirror of
https://github.com/love2d/love-android.git
synced 2026-08-12 00:20:57 +02:00
Add null-checking when scanning games.
This commit is contained in:
@@ -29,6 +29,7 @@ import androidx.recyclerview.widget.LinearLayoutManager;
|
|||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
@@ -133,70 +134,73 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
return builder.setMessage(message.toString()).create();
|
return builder.setMessage(message.toString()).create();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressLint("NotifyDataSetChanged")
|
||||||
private void scanGames(GameListAdapter adapter, ConstraintLayout noGameText, SwipeRefreshLayout swipeRefreshLayout) {
|
private void scanGames(GameListAdapter adapter, ConstraintLayout noGameText, SwipeRefreshLayout swipeRefreshLayout) {
|
||||||
executor.execute(() -> {
|
executor.execute(() -> {
|
||||||
File extDir = getExternalFilesDir("games");
|
File extDir = getExternalFilesDir("games");
|
||||||
|
|
||||||
if (!extDir.isDirectory()) {
|
if (extDir != null) {
|
||||||
if (!extDir.mkdir()) {
|
if (!extDir.isDirectory()) {
|
||||||
// Scan failure, abort
|
if (!extDir.mkdir()) {
|
||||||
runOnUiThread(() -> {
|
// Scan failure, abort
|
||||||
if (swipeRefreshLayout != null) {
|
runOnUiThread(() -> {
|
||||||
swipeRefreshLayout.setRefreshing(false);
|
if (swipeRefreshLayout != null) {
|
||||||
}
|
swipeRefreshLayout.setRefreshing(false);
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Log.e(TAG, "Directory creation failure.");
|
Log.e(TAG, "Directory creation failure.");
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
ArrayList<GameListAdapter.Data> validGames = new ArrayList<>();
|
ArrayList<GameListAdapter.Data> validGames = new ArrayList<>();
|
||||||
File[] files = extDir.listFiles();
|
File[] files = extDir.listFiles();
|
||||||
|
|
||||||
if (files != null) {
|
if (files != null) {
|
||||||
for (File file: files) {
|
for (File file : files) {
|
||||||
GameListAdapter.Data gameData = null;
|
GameListAdapter.Data gameData = null;
|
||||||
|
|
||||||
if (file.isDirectory()) {
|
if (file.isDirectory()) {
|
||||||
if (isValidGamedir(file)) {
|
if (isValidGamedir(file)) {
|
||||||
gameData = new GameListAdapter.Data();
|
gameData = new GameListAdapter.Data();
|
||||||
gameData.path = file;
|
gameData.path = file;
|
||||||
gameData.directory = true;
|
gameData.directory = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (isValidLovegame(file)) {
|
||||||
|
gameData = new GameListAdapter.Data();
|
||||||
|
gameData.path = file;
|
||||||
|
gameData.directory = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (gameData != null) {
|
||||||
|
validGames.add(gameData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean empty = validGames.isEmpty();
|
||||||
|
|
||||||
|
runOnUiThread(() -> {
|
||||||
|
if (empty) {
|
||||||
|
adapter.setData(null);
|
||||||
} else {
|
} else {
|
||||||
if (isValidLovegame(file)) {
|
GameListAdapter.Data[] gameDatas = new GameListAdapter.Data[validGames.size()];
|
||||||
gameData = new GameListAdapter.Data();
|
validGames.toArray(gameDatas);
|
||||||
gameData.path = file;
|
adapter.setData(gameDatas);
|
||||||
gameData.directory = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (gameData != null) {
|
adapter.notifyDataSetChanged();
|
||||||
validGames.add(gameData);
|
|
||||||
|
if (swipeRefreshLayout != null) {
|
||||||
|
swipeRefreshLayout.setRefreshing(false);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
noGameText.setVisibility(empty ? View.VISIBLE : View.INVISIBLE);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean empty = validGames.isEmpty();
|
|
||||||
|
|
||||||
runOnUiThread(() -> {
|
|
||||||
if (empty) {
|
|
||||||
adapter.setData(null);
|
|
||||||
} else {
|
|
||||||
GameListAdapter.Data[] gameDatas = new GameListAdapter.Data[validGames.size()];
|
|
||||||
validGames.toArray(gameDatas);
|
|
||||||
adapter.setData(gameDatas);
|
|
||||||
}
|
|
||||||
|
|
||||||
adapter.notifyDataSetChanged();
|
|
||||||
|
|
||||||
if (swipeRefreshLayout != null) {
|
|
||||||
swipeRefreshLayout.setRefreshing(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
noGameText.setVisibility(empty ? View.VISIBLE : View.INVISIBLE);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user