Skip to content

Commit

Permalink
Add support for games with multiple game files
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrKozlovskiy authored and vkremianskii committed Feb 21, 2020
1 parent c4ce082 commit 268fe70
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 36 deletions.
45 changes: 36 additions & 9 deletions app/src/main/java/com/qsp/player/stock/GameStockActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,45 @@ public void onClick(DialogInterface dialog, int which) {
.show();
}

private void playOrDownloadGame(GameStockItem game) {
if (game.downloaded) {
Intent data = new Intent();
data.putExtra("gameTitle", game.title);
data.putExtra("gameDirUri", game.localDirUri);
data.putExtra("gameFileUri", game.localFileUri);
setResult(RESULT_OK, data);
finish();
private void playOrDownloadGame(final GameStockItem game) {
if (!game.downloaded) {
downloadGame(game);
return;
}
final Intent data = new Intent();
data.putExtra("gameTitle", game.title);
data.putExtra("gameDirUri", game.gameDir.getUri().toString());

int gameFileCount = game.gameFiles.size();
switch (gameFileCount) {
case 0:
Log.w(TAG, "Game has no game files");
return;
case 1:
data.putExtra("gameFileUri", game.gameFiles.get(0).getUri().toString());
setResult(RESULT_OK, data);
finish();
return;
default:
break;
}

downloadGame(game);
ArrayList<String> names = new ArrayList<>();
for (DocumentFile file : game.gameFiles) {
names.add(file.getName());
}
new AlertDialog.Builder(GameStockActivity.this)
.setTitle(getString(R.string.selectGameFile))
.setCancelable(false)
.setItems(names.toArray(new String[0]), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
data.putExtra("gameFileUri", game.gameFiles.get(which).getUri().toString());
setResult(RESULT_OK, data);
finish();
}
})
.show();
}

private void downloadGame(GameStockItem game) {
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/java/com/qsp/player/stock/GameStockItem.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.qsp.player.stock;

import androidx.documentfile.provider.DocumentFile;

import java.util.List;

class GameStockItem {
String gameId = "";
String listId = "";
String author = "";
String portedBy = "";
String version = "";
String title;
String title = "";
String lang = "";
String player = "";
String fileUrl = "";
Expand All @@ -15,6 +19,6 @@ class GameStockItem {
String pubDate = "";
String modDate = "";
boolean downloaded;
String localDirUri = "";
String localFileUri = "";
DocumentFile gameDir;
List<DocumentFile> gameFiles;
}
37 changes: 14 additions & 23 deletions app/src/main/java/com/qsp/player/stock/LocalGameRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,19 @@ List<GameStockItem> getGames() {
ArrayList<GameStockItem> items = new ArrayList<>();
for (GameFolder folder : getGameFolders(gameDirs)) {
String info = getGameInfo(folder);
boolean pack = folder.gameFiles.size() > 1;

for (DocumentFile file : folder.gameFiles) {
GameStockItem item;
if (info != null) {
item = parseGameInfo(info);
} else {
String name = folder.dir.getName();
item = new GameStockItem();
item.title = name;
item.gameId = name;
}
if (pack) {
String name = file.getName();
item.title += " (" + name + ")";
item.gameId += " (" + name + ")";
}
item.downloaded = true;
item.localDirUri = folder.dir.getUri().toString();
item.localFileUri = file.getUri().toString();
items.add(item);
GameStockItem item;
if (info != null) {
item = parseGameInfo(info);
} else {
String name = folder.dir.getName();
item = new GameStockItem();
item.gameId = name;
item.title = name;
}
item.downloaded = true;
item.gameDir = folder.dir;
item.gameFiles = folder.gameFiles;
items.add(item);
}

return items;
Expand Down Expand Up @@ -239,9 +230,9 @@ private void fillGameItemFromCDATA(GameStockItem item, String tagName, String va

private static class GameFolder {
private final DocumentFile dir;
private final Collection<DocumentFile> gameFiles;
private final List<DocumentFile> gameFiles;

private GameFolder(DocumentFile dir, Collection<DocumentFile> gameFiles) {
private GameFolder(DocumentFile dir, List<DocumentFile> gameFiles) {
this.dir = dir;
this.gameFiles = gameFiles;
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/title.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

<ImageButton
android:id="@+id/title_inv_btn"
android:contentDescription="@string/inventory"
android:contentDescription="@string/inventoryContentDesc"
android:layout_width="@dimen/title_height"
android:layout_height="fill_parent"
android:layout_alignWithParentIfMissing="true"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<string name="tabRemote">Сайт</string>
<string name="tabAll">Все</string>
<string name="inventory">Инвентарь</string>
<string name="inventoryContentDesc">Перейти к инвентарю</string>
<string name="mainDesc">Описание</string>
<string name="mainDescContentDesc">Перейти к основному описанию</string>
<string name="varsDesc">Доп. описание</string>
Expand All @@ -93,6 +94,7 @@
<string name="version">Версия: -VERSION-</string>
<string name="fileSize">Размер: -SIZE- КБ</string>
<string name="close">Закрыть</string>
<string name="selectGameFile">Выберите файл игры для запуска</string>
</resources>


Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<string name="tabLocal">Local</string>
<string name="tabRemote">Remote</string>
<string name="inventory">Inventory</string>
<string name="inventoryContentDesc">Go to inventory</string>
<string name="mainDesc">Description</string>
<string name="mainDescContentDesc">Go to main description</string>
<string name="varsDesc">Add. description</string>
Expand All @@ -93,4 +94,5 @@
<string name="fileSize">Size: -SIZE- KB</string>
<string name="deleteGameQuery">Delete game -GAMENAME-?</string>
<string name="close">Close</string>
<string name="selectGameFile">Select a game file to run</string>
</resources>

0 comments on commit 268fe70

Please sign in to comment.