diff options
| author | JosJuice <josjuice@gmail.com> | 2018-06-01 09:36:29 +0200 |
|---|---|---|
| committer | JosJuice <josjuice@gmail.com> | 2018-06-06 17:34:07 +0200 |
| commit | 1c027bc148574cf0b05cd04f8ce15132e8115170 (patch) | |
| tree | cf7d352f8cc68bc26b37168f86c8314d448903a7 /Source/Android/app/src/main/java | |
| parent | daee5a4b43e95f50a1b024348a19d7f2bb48bdbc (diff) | |
Use UICommon's game list code on Android
Deduplicates code, and gets rid of some problems the old code had
(such as: bad performance when calling native functions, only one
disc showing up for multi-disc games, Wii banners being low-res,
unnecessarily much effort being needed for adding more metadata).
Diffstat (limited to 'Source/Android/app/src/main/java')
27 files changed, 456 insertions, 1181 deletions
diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/DolphinApplication.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/DolphinApplication.java index ee051a68a7..22ee55cb70 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/DolphinApplication.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/DolphinApplication.java @@ -2,22 +2,19 @@ package org.dolphinemu.dolphinemu; import android.app.Application; -import org.dolphinemu.dolphinemu.model.GameDatabase; import org.dolphinemu.dolphinemu.services.DirectoryInitializationService; import org.dolphinemu.dolphinemu.utils.PermissionsHandler; public class DolphinApplication extends Application { - public static GameDatabase databaseHelper; - @Override public void onCreate() { super.onCreate(); + System.loadLibrary("main"); + if (PermissionsHandler.hasWriteAccess(getApplicationContext())) DirectoryInitializationService.startService(getApplicationContext()); - - databaseHelper = new GameDatabase(this); } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.java index 413d84b596..c986857e75 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.java @@ -261,34 +261,6 @@ public final class NativeLibrary public static native void SetConfig(String configFile, String Section, String Key, String Value); /** - * Gets the embedded banner within the given ISO/ROM. - * - * @param filename the file path to the ISO/ROM. - * - * @return an integer array containing the color data for the banner. - */ - public static native int[] GetBanner(String filename); - - /** - * Gets the embedded title of the given ISO/ROM. - * - * @param filename The file path to the ISO/ROM. - * - * @return the embedded title of the ISO/ROM. - */ - public static native String GetTitle(String filename); - - public static native String GetDescription(String filename); - public static native String GetGameId(String filename); - - public static native int GetCountry(String filename); - - public static native String GetCompany(String filename); - public static native long GetFilesize(String filename); - - public static native int GetPlatform(String filename); - - /** * Gets the Dolphin version string. * * @return the Dolphin version string. @@ -394,27 +366,6 @@ public final class NativeLibrary */ public static native void RefreshWiimotes(); - /** - * The methods C++ uses to find references to Java classes and methods - * are really expensive. Rather than calling them every time we want to - * run them, do it once when we load the native library. - */ - private static native void CacheClassesAndMethods(); - - static - { - try - { - System.loadLibrary("main"); - } - catch (UnsatisfiedLinkError ex) - { - Log.error("[NativeLibrary] " + ex.toString()); - } - - CacheClassesAndMethods(); - } - private static boolean alertResult = false; public static boolean displayAlertMsg(final String caption, final String text, final boolean yesNo) { diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java index 217ba73336..4a02cfc89c 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java @@ -36,6 +36,7 @@ import org.dolphinemu.dolphinemu.R; import org.dolphinemu.dolphinemu.fragments.EmulationFragment; import org.dolphinemu.dolphinemu.fragments.MenuFragment; import org.dolphinemu.dolphinemu.fragments.SaveLoadStateFragment; +import org.dolphinemu.dolphinemu.model.GameFile; import org.dolphinemu.dolphinemu.ui.main.MainActivity; import org.dolphinemu.dolphinemu.ui.main.MainPresenter; import org.dolphinemu.dolphinemu.ui.platform.Platform; @@ -74,10 +75,12 @@ public final class EmulationActivity extends AppCompatActivity private boolean activityRecreated; private String mScreenPath; private String mSelectedTitle; + private int mPlatform; private String mPath; public static final String EXTRA_SELECTED_GAME = "SelectedGame"; public static final String EXTRA_SELECTED_TITLE = "SelectedTitle"; + public static final String EXTRA_PLATFORM = "Platform"; public static final String EXTRA_SCREEN_PATH = "ScreenPath"; public static final String EXTRA_GRID_POSITION = "GridPosition"; @@ -146,13 +149,14 @@ public final class EmulationActivity extends AppCompatActivity buttonsActionsMap.append(R.id.menu_exit, EmulationActivity.MENU_ACTION_EXIT); } - public static void launch(FragmentActivity activity, String path, String title, String screenshotPath, int position, View sharedView) + public static void launch(FragmentActivity activity, GameFile gameFile, int position, View sharedView) { Intent launcher = new Intent(activity, EmulationActivity.class); - launcher.putExtra(EXTRA_SELECTED_GAME, path); - launcher.putExtra(EXTRA_SELECTED_TITLE, title); - launcher.putExtra(EXTRA_SCREEN_PATH, screenshotPath); + launcher.putExtra(EXTRA_SELECTED_GAME, gameFile.getPath()); + launcher.putExtra(EXTRA_SELECTED_TITLE, gameFile.getTitle()); + launcher.putExtra(EXTRA_PLATFORM, gameFile.getPlatform()); + launcher.putExtra(EXTRA_SCREEN_PATH, gameFile.getScreenshotPath()); launcher.putExtra(EXTRA_GRID_POSITION, position); ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation( @@ -176,6 +180,7 @@ public final class EmulationActivity extends AppCompatActivity Intent gameToEmulate = getIntent(); mPath = gameToEmulate.getStringExtra(EXTRA_SELECTED_GAME); mSelectedTitle = gameToEmulate.getStringExtra(EXTRA_SELECTED_TITLE); + mPlatform = gameToEmulate.getIntExtra(EXTRA_PLATFORM, 0); mScreenPath = gameToEmulate.getStringExtra(EXTRA_SCREEN_PATH); mPosition = gameToEmulate.getIntExtra(EXTRA_GRID_POSITION, -1); activityRecreated = false; @@ -186,7 +191,9 @@ public final class EmulationActivity extends AppCompatActivity restoreState(savedInstanceState); } - sIsGameCubeGame = Platform.fromNativeInt(NativeLibrary.GetPlatform(mPath)) == Platform.GAMECUBE; + // TODO: The accurate way to find out which console we're emulating is to + // first launch emulation and then ask the core which console we're emulating + sIsGameCubeGame = Platform.fromNativeInt(mPlatform) == Platform.GAMECUBE; mDeviceHasTouchScreen = getPackageManager().hasSystemFeature("android.hardware.touchscreen"); mControllerMappingHelper = new ControllerMappingHelper(); @@ -284,6 +291,7 @@ public final class EmulationActivity extends AppCompatActivity mEmulationFragment.saveTemporaryState(); outState.putString(EXTRA_SELECTED_GAME, mPath); outState.putString(EXTRA_SELECTED_TITLE, mSelectedTitle); + outState.putInt(EXTRA_PLATFORM, mPlatform); outState.putString(EXTRA_SCREEN_PATH, mScreenPath); outState.putInt(EXTRA_GRID_POSITION, mPosition); super.onSaveInstanceState(outState); @@ -293,6 +301,7 @@ public final class EmulationActivity extends AppCompatActivity { mPath = savedInstanceState.getString(EXTRA_SELECTED_GAME); mSelectedTitle = savedInstanceState.getString(EXTRA_SELECTED_TITLE); + mPlatform = savedInstanceState.getInt(EXTRA_PLATFORM); mScreenPath = savedInstanceState.getString(EXTRA_SCREEN_PATH); mPosition = savedInstanceState.getInt(EXTRA_GRID_POSITION); } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/adapters/GameAdapter.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/adapters/GameAdapter.java index 63f1289c97..1ff7a13a13 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/adapters/GameAdapter.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/adapters/GameAdapter.java @@ -2,8 +2,6 @@ package org.dolphinemu.dolphinemu.adapters; import android.app.AlertDialog; import android.content.DialogInterface; -import android.database.Cursor; -import android.database.DataSetObserver; import android.graphics.Rect; import android.support.v4.app.FragmentActivity; import android.support.v7.widget.RecyclerView; @@ -14,38 +12,30 @@ import android.widget.Toast; import org.dolphinemu.dolphinemu.R; import org.dolphinemu.dolphinemu.activities.EmulationActivity; -import org.dolphinemu.dolphinemu.model.GameDatabase; +import org.dolphinemu.dolphinemu.model.GameFile; import org.dolphinemu.dolphinemu.services.DirectoryInitializationService; import org.dolphinemu.dolphinemu.ui.settings.SettingsActivity; -import org.dolphinemu.dolphinemu.utils.Log; import org.dolphinemu.dolphinemu.utils.PicassoUtils; import org.dolphinemu.dolphinemu.utils.SettingsFile; import org.dolphinemu.dolphinemu.viewholders.GameViewHolder; import java.io.File; +import java.util.ArrayList; +import java.util.List; -/** - * This adapter gets its information from a database Cursor. This fact, paired with the usage of - * ContentProviders and Loaders, allows for efficient display of a limited view into a (possibly) - * large dataset. - */ public final class GameAdapter extends RecyclerView.Adapter<GameViewHolder> implements View.OnClickListener, View.OnLongClickListener { - private Cursor mCursor; - private GameDataSetObserver mObserver; - - private boolean mDatasetValid; + private List<GameFile> mGameFiles; /** * Initializes the adapter's observer, which watches for changes to the dataset. The adapter will - * display no data until a Cursor is supplied by a CursorLoader. + * display no data until swapDataSet is called. */ public GameAdapter() { - mDatasetValid = false; - mObserver = new GameDataSetObserver(); + mGameFiles = new ArrayList<>(); } /** @@ -80,34 +70,13 @@ public final class GameAdapter extends RecyclerView.Adapter<GameViewHolder> impl @Override public void onBindViewHolder(GameViewHolder holder, int position) { - if (mDatasetValid) - { - if (mCursor.moveToPosition(position)) - { - String screenPath = mCursor.getString(GameDatabase.GAME_COLUMN_SCREENSHOT_PATH); - PicassoUtils.loadGameBanner(holder.imageScreenshot, screenPath, mCursor.getString(GameDatabase.GAME_COLUMN_PATH)); + GameFile gameFile = mGameFiles.get(position); + PicassoUtils.loadGameBanner(holder.imageScreenshot, gameFile); - holder.textGameTitle.setText(mCursor.getString(GameDatabase.GAME_COLUMN_TITLE)); - holder.textCompany.setText(mCursor.getString(GameDatabase.GAME_COLUMN_COMPANY)); + holder.textGameTitle.setText(gameFile.getTitle()); + holder.textCompany.setText(gameFile.getCompany()); - // TODO These shouldn't be necessary once the move to a DB-based model is complete. - holder.gameId = mCursor.getString(GameDatabase.GAME_COLUMN_GAME_ID); - holder.path = mCursor.getString(GameDatabase.GAME_COLUMN_PATH); - holder.title = mCursor.getString(GameDatabase.GAME_COLUMN_TITLE); - holder.description = mCursor.getString(GameDatabase.GAME_COLUMN_DESCRIPTION); - holder.country = mCursor.getInt(GameDatabase.GAME_COLUMN_COUNTRY); - holder.company = mCursor.getString(GameDatabase.GAME_COLUMN_COMPANY); - holder.screenshotPath = mCursor.getString(GameDatabase.GAME_COLUMN_SCREENSHOT_PATH); - } - else - { - Log.error("[GameAdapter] Can't bind view; Cursor is not valid."); - } - } - else - { - Log.error("[GameAdapter] Can't bind view; dataset is not valid."); - } + holder.gameFile = gameFile; } /** @@ -118,84 +87,27 @@ public final class GameAdapter extends RecyclerView.Adapter<GameViewHolder> impl @Override public int getItemCount() { - if (mDatasetValid && mCursor != null) - { - return mCursor.getCount(); - } - Log.error("[GameAdapter] Dataset is not valid."); - return 0; - } - - /** - * Return the contents of the _id column for a given row. - * - * @param position The row for which Android wants an ID. - * @return A valid ID from the database, or 0 if not available. - */ - @Override - public long getItemId(int position) - { - if (mDatasetValid && mCursor != null) - { - if (mCursor.moveToPosition(position)) - { - return mCursor.getLong(GameDatabase.COLUMN_DB_ID); - } - } - - Log.error("[GameAdapter] Dataset is not valid."); - return 0; + return mGameFiles.size(); } /** * Tell Android whether or not each item in the dataset has a stable identifier. - * Which it does, because it's a database, so always tell Android 'true'. * * @param hasStableIds ignored. */ @Override public void setHasStableIds(boolean hasStableIds) { - super.setHasStableIds(true); + super.setHasStableIds(false); } /** - * When a load is finished, call this to replace the existing data with the newly-loaded - * data. - * - * @param cursor The newly-loaded Cursor. + * When a load is finished, call this to replace the existing data + * with the newly-loaded data. */ - public void swapCursor(Cursor cursor) + public void swapDataSet(List<GameFile> gameFiles) { - // Sanity check. - if (cursor == mCursor) - { - return; - } - - // Before getting rid of the old cursor, disassociate it from the Observer. - final Cursor oldCursor = mCursor; - if (oldCursor != null && mObserver != null) - { - oldCursor.unregisterDataSetObserver(mObserver); - } - - mCursor = cursor; - if (mCursor != null) - { - // Attempt to associate the new Cursor with the Observer. - if (mObserver != null) - { - mCursor.registerDataSetObserver(mObserver); - } - - mDatasetValid = true; - } - else - { - mDatasetValid = false; - } - + mGameFiles = gameFiles; notifyDataSetChanged(); } @@ -210,9 +122,7 @@ public final class GameAdapter extends RecyclerView.Adapter<GameViewHolder> impl GameViewHolder holder = (GameViewHolder) view.getTag(); EmulationActivity.launch((FragmentActivity) view.getContext(), - holder.path, - holder.title, - holder.screenshotPath, + holder.gameFile, holder.getAdapterPosition(), holder.imageScreenshot); } @@ -227,13 +137,10 @@ public final class GameAdapter extends RecyclerView.Adapter<GameViewHolder> impl public boolean onLongClick(View view) { GameViewHolder holder = (GameViewHolder) view.getTag(); - - // Get the ID of the game we want to look at. - String gameId = (String) holder.gameId; + String gameId = holder.gameFile.getGameId(); FragmentActivity activity = (FragmentActivity) view.getContext(); - AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setTitle("Game Settings") .setItems(R.array.gameSettingsMenus, new DialogInterface.OnClickListener() { @@ -290,25 +197,4 @@ public final class GameAdapter extends RecyclerView.Adapter<GameViewHolder> impl outRect.top = space; } } - - private final class GameDataSetObserver extends DataSetObserver - { - @Override - public void onChanged() - { - super.onChanged(); - - mDatasetValid = true; - notifyDataSetChanged(); - } - - @Override - public void onInvalidated() - { - super.onInvalidated(); - - mDatasetValid = false; - notifyDataSetChanged(); - } - } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/adapters/GameRowPresenter.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/adapters/GameRowPresenter.java index 81234904da..79c1fabf9b 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/adapters/GameRowPresenter.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/adapters/GameRowPresenter.java @@ -14,8 +14,9 @@ import android.widget.ImageView; import android.widget.Toast; import org.dolphinemu.dolphinemu.R; -import org.dolphinemu.dolphinemu.model.Game; +import org.dolphinemu.dolphinemu.model.GameFile; import org.dolphinemu.dolphinemu.services.DirectoryInitializationService; +import org.dolphinemu.dolphinemu.ui.platform.Platform; import org.dolphinemu.dolphinemu.ui.settings.SettingsActivity; import org.dolphinemu.dolphinemu.utils.PicassoUtils; import org.dolphinemu.dolphinemu.utils.SettingsFile; @@ -50,28 +51,19 @@ public final class GameRowPresenter extends Presenter public void onBindViewHolder(ViewHolder viewHolder, Object item) { TvGameViewHolder holder = (TvGameViewHolder) viewHolder; - Game game = (Game) item; - - String screenPath = game.getScreenshotPath(); + GameFile gameFile = (GameFile) item; holder.imageScreenshot.setImageDrawable(null); - PicassoUtils.loadGameBanner(holder.imageScreenshot, screenPath, game.getPath()); + PicassoUtils.loadGameBanner(holder.imageScreenshot, gameFile); - holder.cardParent.setTitleText(game.getTitle()); - holder.cardParent.setContentText(game.getCompany()); + holder.cardParent.setTitleText(gameFile.getTitle()); + holder.cardParent.setContentText(gameFile.getCompany()); - // TODO These shouldn't be necessary once the move to a DB-based model is complete. - holder.gameId = game.getGameId(); - holder.path = game.getPath(); - holder.title = game.getTitle(); - holder.description = game.getDescription(); - holder.country = game.getCountry(); - holder.company = game.getCompany(); - holder.screenshotPath = game.getScreenshotPath(); + holder.gameFile = gameFile; // Set the platform-dependent background color of the card int backgroundId; - switch (game.getPlatform()) + switch (Platform.fromNativeInt(gameFile.getPlatform())) { case GAMECUBE: backgroundId = R.drawable.tv_card_background_gamecube; @@ -93,7 +85,7 @@ public final class GameRowPresenter extends Presenter public boolean onLongClick(View view) { FragmentActivity activity = (FragmentActivity) view.getContext(); - + String gameId = gameFile.getGameId(); AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setTitle("Game Settings") @@ -101,23 +93,23 @@ public final class GameRowPresenter extends Presenter public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: - SettingsActivity.launch(activity, SettingsFile.FILE_NAME_DOLPHIN, game.getGameId()); + SettingsActivity.launch(activity, SettingsFile.FILE_NAME_DOLPHIN, gameId); break; case 1: - SettingsActivity.launch(activity, SettingsFile.FILE_NAME_GFX, game.getGameId()); + SettingsActivity.launch(activity, SettingsFile.FILE_NAME_GFX, gameId); break; case 2: - String path = DirectoryInitializationService.getUserDirectory() + "/GameSettings/" + game.getGameId() + ".ini"; + String path = DirectoryInitializationService.getUserDirectory() + "/GameSettings/" + gameId + ".ini"; File gameSettingsFile = new File(path); if (gameSettingsFile.exists()) { if (gameSettingsFile.delete()) { - Toast.makeText(view.getContext(), "Cleared settings for " + game.getGameId(), Toast.LENGTH_SHORT).show(); + Toast.makeText(view.getContext(), "Cleared settings for " + gameId, Toast.LENGTH_SHORT).show(); } else { - Toast.makeText(view.getContext(), "Unable to clear settings for " + game.getGameId(), Toast.LENGTH_SHORT).show(); + Toast.makeText(view.getContext(), "Unable to clear settings for " + gameId, Toast.LENGTH_SHORT).show(); } } else diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/dialogs/GameDetailsDialog.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/dialogs/GameDetailsDialog.java index b3dc13de22..718c03152d 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/dialogs/GameDetailsDialog.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/dialogs/GameDetailsDialog.java @@ -12,32 +12,24 @@ import android.widget.TextView; import com.squareup.picasso.Picasso; +import org.dolphinemu.dolphinemu.DolphinApplication; import org.dolphinemu.dolphinemu.R; import org.dolphinemu.dolphinemu.activities.EmulationActivity; +import org.dolphinemu.dolphinemu.model.GameFile; +import org.dolphinemu.dolphinemu.services.GameFileCacheService; import de.hdodenhof.circleimageview.CircleImageView; public final class GameDetailsDialog extends DialogFragment { - private static final String ARG_GAME_TITLE = "game_title"; - private static final String ARG_GAME_DESCRIPTION = "game_description"; - private static final String ARG_GAME_COUNTRY = "game_country"; - private static final String ARG_GAME_DATE = "game_date"; private static final String ARG_GAME_PATH = "game_path"; - private static final String ARG_GAME_SCREENSHOT_PATH = "game_screenshot_path"; - // TODO Add all of this to the Loader in GameActivity.java - public static GameDetailsDialog newInstance(String title, String description, int country, String company, String path, String screenshotPath) + public static GameDetailsDialog newInstance(String gamePath) { GameDetailsDialog fragment = new GameDetailsDialog(); Bundle arguments = new Bundle(); - arguments.putString(ARG_GAME_TITLE, title); - arguments.putString(ARG_GAME_DESCRIPTION, description); - arguments.putInt(ARG_GAME_COUNTRY, country); - arguments.putString(ARG_GAME_DATE, company); - arguments.putString(ARG_GAME_PATH, path); - arguments.putString(ARG_GAME_SCREENSHOT_PATH, screenshotPath); + arguments.putString(ARG_GAME_PATH, gamePath); fragment.setArguments(arguments); return fragment; @@ -46,42 +38,38 @@ public final class GameDetailsDialog extends DialogFragment @Override public Dialog onCreateDialog(Bundle savedInstanceState) { + GameFile gameFile = GameFileCacheService.addOrGet(getArguments().getString(ARG_GAME_PATH)); + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); ViewGroup contents = (ViewGroup) getActivity().getLayoutInflater().inflate(R.layout.dialog_game_details, null); - final ImageView imageGameScreen = (ImageView) contents.findViewById(R.id.image_game_screen); - CircleImageView circleBanner = (CircleImageView) contents.findViewById(R.id.circle_banner); + final ImageView imageGameScreen = contents.findViewById(R.id.image_game_screen); + CircleImageView circleBanner = contents.findViewById(R.id.circle_banner); - TextView textTitle = (TextView) contents.findViewById(R.id.text_game_title); - TextView textDescription = (TextView) contents.findViewById(R.id.text_company); + TextView textTitle = contents.findViewById(R.id.text_game_title); + TextView textDescription = contents.findViewById(R.id.text_description); - TextView textCountry = (TextView) contents.findViewById(R.id.text_country); - TextView textDate = (TextView) contents.findViewById(R.id.text_date); + TextView textCountry = contents.findViewById(R.id.text_country); + TextView textCompany = contents.findViewById(R.id.text_company); - FloatingActionButton buttonLaunch = (FloatingActionButton) contents.findViewById(R.id.button_launch); + FloatingActionButton buttonLaunch = contents.findViewById(R.id.button_launch); - int countryIndex = getArguments().getInt(ARG_GAME_COUNTRY); - String country = getResources().getStringArray(R.array.countryNames)[countryIndex]; + String country = getResources().getStringArray(R.array.countryNames)[gameFile.getCountry()]; - textTitle.setText(getArguments().getString(ARG_GAME_TITLE)); - textDescription.setText(getArguments().getString(ARG_GAME_DESCRIPTION)); + textTitle.setText(gameFile.getTitle()); + textDescription.setText(gameFile.getDescription()); textCountry.setText(country); - textDate.setText(getArguments().getString(ARG_GAME_DATE)); + textCompany.setText(gameFile.getCompany()); buttonLaunch.setOnClickListener(view -> { // Start the emulation activity and send the path of the clicked ROM to it. - EmulationActivity.launch(getActivity(), - getArguments().getString(ARG_GAME_PATH), - getArguments().getString(ARG_GAME_TITLE), - getArguments().getString(ARG_GAME_SCREENSHOT_PATH), - -1, - imageGameScreen); + EmulationActivity.launch(getActivity(), gameFile, -1, imageGameScreen); }); // Fill in the view contents. Picasso.with(imageGameScreen.getContext()) - .load(getArguments().getString(ARG_GAME_SCREENSHOT_PATH)) + .load(getArguments().getString(gameFile.getScreenshotPath())) .fit() .centerCrop() .noFade() diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/FileListItem.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/FileListItem.java deleted file mode 100644 index 4460a1e155..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/FileListItem.java +++ /dev/null @@ -1,95 +0,0 @@ -package org.dolphinemu.dolphinemu.model; - -import org.dolphinemu.dolphinemu.NativeLibrary; - -import java.io.File; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - -public class FileListItem implements Comparable<FileListItem> -{ - public static final int TYPE_FOLDER = 0; - public static final int TYPE_GC = 1; - public static final int TYPE_WII = 2; - public static final int TYPE_WII_WARE = 3; - public static final int TYPE_OTHER = 4; - - private int mType; - private String mFilename; - private String mPath; - - public FileListItem(File file) - { - mPath = file.getAbsolutePath(); - mFilename = file.getName(); - - if (file.isDirectory()) - { - mType = TYPE_FOLDER; - } - else - { - int extensionStart = mPath.lastIndexOf('.'); - if (extensionStart < 1) - { - // Ignore hidden files & files without extensions. - mType = TYPE_OTHER; - } - else - { - String fileExtension = mPath.substring(extensionStart); - - // The extensions we care about. - Set<String> allowedExtensions = new HashSet<String>(Arrays.asList( - ".ciso", ".dff", ".dol", ".elf", ".gcm", ".gcz", ".iso", ".tgc", ".wad", ".wbfs")); - - // Check that the file has an extension we care about before trying to read out of it. - if (allowedExtensions.contains(fileExtension.toLowerCase())) - { - // Add 1 because 0 = TYPE_FOLDER - mType = NativeLibrary.GetPlatform(mPath) + 1; - } - else - { - mType = TYPE_OTHER; - } - } - } - } - - public int getType() - { - return mType; - } - - public String getFilename() - { - return mFilename; - } - - public String getPath() - { - return mPath; - } - - @Override - public int compareTo(FileListItem theOther) - { - if (theOther.getType() == getType()) - { - return getFilename().toLowerCase().compareTo(theOther.getFilename().toLowerCase()); - } - else - { - if (getType() > theOther.getType()) - { - return 1; - } - else - { - return -1; - } - } - } -} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/Game.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/Game.java deleted file mode 100644 index c38bcef2e1..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/Game.java +++ /dev/null @@ -1,120 +0,0 @@ -package org.dolphinemu.dolphinemu.model; - -import android.content.ContentValues; -import android.database.Cursor; -import android.os.Environment; - -import org.dolphinemu.dolphinemu.ui.platform.Platform; - -public final class Game -{ - // Copied from IVolume::ECountry. Update these if that is ever modified. - public static final int COUNTRY_EUROPE = 0; - public static final int COUNTRY_JAPAN = 1; - public static final int COUNTRY_USA = 2; - public static final int COUNTRY_AUSTRALIA = 3; - public static final int COUNTRY_FRANCE = 4; - public static final int COUNTRY_GERMANY = 5; - public static final int COUNTRY_ITALY = 6; - public static final int COUNTRY_KOREA = 7; - public static final int COUNTRY_NETHERLANDS = 8; - public static final int COUNTRY_RUSSIA = 9; - public static final int COUNTRY_SPAIN = 10; - public static final int COUNTRY_TAIWAN = 11; - public static final int COUNTRY_WORLD = 12; - public static final int COUNTRY_UNKNOWN = 13; - - private static final String PATH_SCREENSHOT_FOLDER = "file://" + Environment.getExternalStorageDirectory().getPath() + "/dolphin-emu/ScreenShots/"; - - private String mTitle; - private String mDescription; - private String mPath; - private String mGameId; - private String mScreenshotPath; - private String mCompany; - - private Platform mPlatform; - private int mCountry; - - public Game(Platform platform, String title, String description, int country, String path, String gameId, String company, String screenshotPath) - { - mPlatform = platform; - mTitle = title; - mDescription = description; - mCountry = country; - mPath = path; - mGameId = gameId; - mCompany = company; - mScreenshotPath = screenshotPath; - } - - public Platform getPlatform() - { - return mPlatform; - } - - public String getTitle() - { - return mTitle; - } - - public String getDescription() - { - return mDescription; - } - - public String getCompany() - { - return mCompany; - } - - public int getCountry() - { - return mCountry; - } - - public String getPath() - { - return mPath; - } - - public String getGameId() - { - return mGameId; - } - - public String getScreenshotPath() - { - return mScreenshotPath; - } - - public static ContentValues asContentValues(Platform platform, String title, String description, int country, String path, String gameId, String company) - { - ContentValues values = new ContentValues(); - - String screenPath = PATH_SCREENSHOT_FOLDER + gameId + "/" + gameId + "-1.png"; - - values.put(GameDatabase.KEY_GAME_PLATFORM, platform.toInt()); - values.put(GameDatabase.KEY_GAME_TITLE, title); - values.put(GameDatabase.KEY_GAME_DESCRIPTION, description); - values.put(GameDatabase.KEY_GAME_COUNTRY, company); - values.put(GameDatabase.KEY_GAME_PATH, path); - values.put(GameDatabase.KEY_GAME_ID, gameId); - values.put(GameDatabase.KEY_GAME_COMPANY, company); - values.put(GameDatabase.KEY_GAME_SCREENSHOT_PATH, screenPath); - - return values; - } - - public static Game fromCursor(Cursor cursor) - { - return new Game(Platform.fromInt(cursor.getInt(GameDatabase.GAME_COLUMN_PLATFORM)), - cursor.getString(GameDatabase.GAME_COLUMN_TITLE), - cursor.getString(GameDatabase.GAME_COLUMN_DESCRIPTION), - cursor.getInt(GameDatabase.GAME_COLUMN_COUNTRY), - cursor.getString(GameDatabase.GAME_COLUMN_PATH), - cursor.getString(GameDatabase.GAME_COLUMN_GAME_ID), - cursor.getString(GameDatabase.GAME_COLUMN_COMPANY), - cursor.getString(GameDatabase.GAME_COLUMN_SCREENSHOT_PATH)); - } -} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameDatabase.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameDatabase.java deleted file mode 100644 index 9c152d90ad..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameDatabase.java +++ /dev/null @@ -1,299 +0,0 @@ -package org.dolphinemu.dolphinemu.model; - -import android.content.ContentValues; -import android.content.Context; -import android.content.SharedPreferences; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteOpenHelper; -import android.preference.PreferenceManager; - -import org.dolphinemu.dolphinemu.NativeLibrary; -import org.dolphinemu.dolphinemu.ui.platform.Platform; -import org.dolphinemu.dolphinemu.utils.Log; - -import java.io.File; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - -import rx.Observable; -import rx.Subscriber; - -/** - * A helper class that provides several utilities simplifying interaction with - * the SQLite database. - */ -public final class GameDatabase extends SQLiteOpenHelper -{ - private static final int DB_VERSION = 1; - - public static final int COLUMN_DB_ID = 0; - - public static final int GAME_COLUMN_PATH = 1; - public static final int GAME_COLUMN_PLATFORM = 2; - public static final int GAME_COLUMN_TITLE = 3; - public static final int GAME_COLUMN_DESCRIPTION = 4; - public static final int GAME_COLUMN_COUNTRY = 5; - public static final int GAME_COLUMN_GAME_ID = 6; - public static final int GAME_COLUMN_COMPANY = 7; - public static final int GAME_COLUMN_SCREENSHOT_PATH = 8; - - public static final String KEY_DB_ID = "_id"; - - public static final String KEY_GAME_PATH = "path"; - public static final String KEY_GAME_PLATFORM = "platform"; - public static final String KEY_GAME_TITLE = "title"; - public static final String KEY_GAME_DESCRIPTION = "description"; - public static final String KEY_GAME_COUNTRY = "country"; - public static final String KEY_GAME_ID = "game_id"; - public static final String KEY_GAME_COMPANY = "company"; - public static final String KEY_GAME_SCREENSHOT_PATH = "screenshot_path"; - public static final String TABLE_NAME_GAMES = "games"; - - private static final String TYPE_PRIMARY = " INTEGER PRIMARY KEY"; - private static final String TYPE_INTEGER = " INTEGER"; - private static final String TYPE_STRING = " TEXT"; - - private static final String SEPARATOR = ", "; - - private static final String SQL_CREATE_GAMES = "CREATE TABLE " + TABLE_NAME_GAMES + "(" - + KEY_DB_ID + TYPE_PRIMARY + SEPARATOR - + KEY_GAME_PATH + TYPE_STRING + SEPARATOR - + KEY_GAME_PLATFORM + TYPE_STRING + SEPARATOR - + KEY_GAME_TITLE + TYPE_STRING + SEPARATOR - + KEY_GAME_DESCRIPTION + TYPE_STRING + SEPARATOR - + KEY_GAME_COUNTRY + TYPE_INTEGER + SEPARATOR - + KEY_GAME_ID + TYPE_STRING + SEPARATOR - + KEY_GAME_COMPANY + TYPE_STRING + SEPARATOR - + KEY_GAME_SCREENSHOT_PATH + TYPE_STRING + ")"; - - private static final String SQL_DELETE_GAMES = "DROP TABLE IF EXISTS " + TABLE_NAME_GAMES; - - private static final String GAME_FOLDER_PATHS_PREFERENCE = "gameFolderPaths"; - - private static final Set<String> EMPTY_SET = new HashSet<>(); - - private Context mContext; - - public GameDatabase(Context context) - { - // Superclass constructor builds a database or uses an existing one. - super(context, "games.db", null, DB_VERSION); - mContext = context; - } - - @Override - public void onCreate(SQLiteDatabase database) - { - Log.debug("[GameDatabase] GameDatabase - Creating database..."); - - execSqlAndLog(database, SQL_CREATE_GAMES); - } - - @Override - public void onDowngrade(SQLiteDatabase database, int oldVersion, int newVersion) - { - Log.verbose("[GameDatabase] Downgrades not supporting, clearing databases.."); - execSqlAndLog(database, SQL_DELETE_GAMES); - execSqlAndLog(database, SQL_CREATE_GAMES); - } - - @Override - public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) - { - Log.info("[GameDatabase] Upgrading database from schema version " + oldVersion + " to " + newVersion); - - // Delete all the games - execSqlAndLog(database, SQL_DELETE_GAMES); - execSqlAndLog(database, SQL_CREATE_GAMES); - - Log.verbose("[GameDatabase] Re-scanning library with new schema."); - scanLibrary(database); - } - - public void addGameFolder(String path) - { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext); - Set<String> folderPaths = preferences.getStringSet(GAME_FOLDER_PATHS_PREFERENCE, EMPTY_SET); - Set<String> newFolderPaths = new HashSet<>(folderPaths); - newFolderPaths.add(path); - SharedPreferences.Editor editor = preferences.edit(); - editor.putStringSet(GAME_FOLDER_PATHS_PREFERENCE, newFolderPaths); - editor.apply(); - - scanLibrary(getWritableDatabase()); - } - - public void scanLibrary(SQLiteDatabase database) - { - // Before scanning known folders, go through the game table and remove any entries for which the file itself is missing. - Cursor fileCursor = database.query(TABLE_NAME_GAMES, - null, // Get all columns. - null, // Get all rows. - null, - null, // No grouping. - null, - null); // Order of games is irrelevant. - - // Possibly overly defensive, but ensures that moveToNext() does not skip a row. - fileCursor.moveToPosition(-1); - - while (fileCursor.moveToNext()) - { - String gamePath = fileCursor.getString(GAME_COLUMN_PATH); - File game = new File(gamePath); - - if (!game.exists()) - { - Log.error("[GameDatabase] Game file no longer exists. Removing from the library: " + gamePath); - database.delete(TABLE_NAME_GAMES, - KEY_DB_ID + " = ?", - new String[]{Long.toString(fileCursor.getLong(COLUMN_DB_ID))}); - } - } - - Set<String> allowedExtensions = new HashSet<String>(Arrays.asList( - ".ciso", ".dff", ".dol", ".elf", ".gcm", ".gcz", ".iso", ".tgc", ".wad", ".wbfs")); - - // Iterate through all results of the DB query (i.e. all folders in the library.) - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext); - Set<String> folderPaths = preferences.getStringSet(GAME_FOLDER_PATHS_PREFERENCE, EMPTY_SET); - Set<String> newFolderPaths = new HashSet<>(); - for (String folderPath : folderPaths) - { - File folder = new File(folderPath); - boolean deleteFolder = false; - - Log.info("[GameDatabase] Reading files from library folder: " + folderPath); - - // Iterate through every file in the folder. - File[] children = folder.listFiles(); - - if (children != null) - { - for (File file : children) - { - if (!file.isHidden() && !file.isDirectory()) - { - String filePath = file.getPath(); - - int extensionStart = filePath.lastIndexOf('.'); - if (extensionStart > 0) - { - String fileExtension = filePath.substring(extensionStart); - - // Check that the file has an extension we care about before trying to read out of it. - if (allowedExtensions.contains(fileExtension.toLowerCase())) - { - String name = NativeLibrary.GetTitle(filePath); - - // If the game's title field is empty, use the filename. - if (name.isEmpty()) - { - name = filePath.substring(filePath.lastIndexOf("/") + 1); - } - - String gameId = NativeLibrary.GetGameId(filePath); - - // If the game's ID field is empty, use the filename without extension. - if (gameId.isEmpty()) - { - gameId = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.lastIndexOf(".")); - } - - Platform platform = Platform.fromNativeInt(NativeLibrary.GetPlatform(filePath)); - - ContentValues game = Game.asContentValues(platform, - name, - NativeLibrary.GetDescription(filePath).replace("\n", " "), - NativeLibrary.GetCountry(filePath), - filePath, - gameId, - NativeLibrary.GetCompany(filePath)); - - // Try to update an existing game first. - int rowsMatched = database.update(TABLE_NAME_GAMES, // Which table to update. - game, // The values to fill the row with. - KEY_GAME_ID + " = ?", // The WHERE clause used to find the right row. - new String[]{game.getAsString(KEY_GAME_ID)}); // The ? in WHERE clause is replaced with this, - // which is provided as an array because there - // could potentially be more than one argument. - - // If update fails, insert a new game instead. - if (rowsMatched == 0) - { - Log.verbose("[GameDatabase] Adding game: " + game.getAsString(KEY_GAME_TITLE)); - database.insert(TABLE_NAME_GAMES, null, game); - } - else - { - Log.verbose("[GameDatabase] Updated game: " + game.getAsString(KEY_GAME_TITLE)); - } - } - } - } - } - } - // If the folder is empty because it no longer exists, remove it from the library. - else if (!folder.exists()) - { - Log.error("[GameDatabase] Folder no longer exists. Removing from the library: " + folderPath); - deleteFolder = true; - } - else - { - Log.error("[GameDatabase] Folder contains no games: " + folderPath); - } - - if (!deleteFolder) - { - newFolderPaths.add(folderPath); - } - } - - fileCursor.close(); - database.close(); - - if (folderPaths.size() != newFolderPaths.size()) - { - // One or more folders are being deleted - SharedPreferences.Editor editor = preferences.edit(); - editor.putStringSet(GAME_FOLDER_PATHS_PREFERENCE, newFolderPaths); - editor.apply(); - } - } - - public Observable<Cursor> getGamesForPlatform(final Platform platform) - { - return Observable.create(subscriber -> - { - Log.info("[GameDatabase] Reading games list..."); - - String[] whereArgs = new String[]{Integer.toString(platform.toInt())}; - - SQLiteDatabase database = getReadableDatabase(); - Cursor resultCursor = database.query( - TABLE_NAME_GAMES, - null, - KEY_GAME_PLATFORM + " = ?", - whereArgs, - null, - null, - KEY_GAME_TITLE + " ASC" - ); - - // Pass the result cursor to the consumer. - subscriber.onNext(resultCursor); - - // Tell the consumer we're done; it will unsubscribe implicitly. - subscriber.onCompleted(); - }); - } - - private void execSqlAndLog(SQLiteDatabase database, String sql) - { - Log.verbose("[GameDatabase] Executing SQL: " + sql); - database.execSQL(sql); - } -} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameFile.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameFile.java new file mode 100644 index 0000000000..7e8628d4d2 --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameFile.java @@ -0,0 +1,34 @@ +package org.dolphinemu.dolphinemu.model; + +import android.os.Environment; + +public class GameFile +{ + private long mPointer; // Do not rename or move without editing the native code + + private GameFile(long pointer) + { + mPointer = pointer; + } + + @Override + public native void finalize(); + + public native int getPlatform(); + public native String getTitle(); + public native String getDescription(); + public native String getCompany(); + public native int getCountry(); + public native String getPath(); + public native String getGameId(); + public native int[] getBanner(); + public native int getBannerWidth(); + public native int getBannerHeight(); + + public String getScreenshotPath() + { + String gameId = getGameId(); + return "file://" + Environment.getExternalStorageDirectory().getPath() + + "/dolphin-emu/ScreenShots/" + gameId + "/" + gameId + "-1.png"; + } +} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameFileCache.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameFileCache.java new file mode 100644 index 0000000000..6d8cdaa718 --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameFileCache.java @@ -0,0 +1,89 @@ +package org.dolphinemu.dolphinemu.model; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import java.io.File; +import java.util.HashSet; +import java.util.Set; + +public class GameFileCache +{ + private static final String GAME_FOLDER_PATHS_PREFERENCE = "gameFolderPaths"; + private static final Set<String> EMPTY_SET = new HashSet<>(); + + private long mPointer; // Do not rename or move without editing the native code + + public GameFileCache(String path) + { + mPointer = newGameFileCache(path); + } + + private static native long newGameFileCache(String path); + + @Override + public native void finalize(); + + public static void addGameFolder(String path, Context context) + { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + Set<String> folderPaths = preferences.getStringSet(GAME_FOLDER_PATHS_PREFERENCE, EMPTY_SET); + Set<String> newFolderPaths = new HashSet<>(folderPaths); + newFolderPaths.add(path); + SharedPreferences.Editor editor = preferences.edit(); + editor.putStringSet(GAME_FOLDER_PATHS_PREFERENCE, newFolderPaths); + editor.apply(); + } + + private void removeNonExistentGameFolders(Context context) + { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + Set<String> folderPaths = preferences.getStringSet(GAME_FOLDER_PATHS_PREFERENCE, EMPTY_SET); + Set<String> newFolderPaths = new HashSet<>(); + for (String folderPath : folderPaths) + { + File folder = new File(folderPath); + if (folder.exists()) + { + newFolderPaths.add(folderPath); + } + } + + if (folderPaths.size() != newFolderPaths.size()) + { + // One or more folders are being deleted + SharedPreferences.Editor editor = preferences.edit(); + editor.putStringSet(GAME_FOLDER_PATHS_PREFERENCE, newFolderPaths); + editor.apply(); + } + } + + /** + * Scans through the file system and updates the cache to match. + * @return true if the cache was modified + */ + public boolean scanLibrary(Context context) + { + removeNonExistentGameFolders(context); + + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + Set<String> folderPathsSet = preferences.getStringSet(GAME_FOLDER_PATHS_PREFERENCE, EMPTY_SET); + String[] folderPaths = folderPathsSet.toArray(new String[folderPathsSet.size()]); + + boolean cacheChanged = update(folderPaths); + cacheChanged |= updateAdditionalMetadata(); + if (cacheChanged) + { + save(); + } + return cacheChanged; + } + + public native GameFile[] getAllGames(); + public native GameFile addOrGet(String gamePath); + private native boolean update(String[] folderPaths); + private native boolean updateAdditionalMetadata(); + public native boolean load(); + private native boolean save(); +} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameProvider.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameProvider.java deleted file mode 100644 index ffb8e5b5ec..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameProvider.java +++ /dev/null @@ -1,139 +0,0 @@ -package org.dolphinemu.dolphinemu.model; - -import android.content.ContentProvider; -import android.content.ContentValues; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; -import android.net.Uri; -import android.support.annotation.NonNull; - -import org.dolphinemu.dolphinemu.BuildConfig; -import org.dolphinemu.dolphinemu.utils.Log; - -/** - * Provides an interface allowing Activities to interact with the SQLite database. - * CRUD methods in this class can be called by Activities using getContentResolver(). - */ -public final class GameProvider extends ContentProvider -{ - public static final String REFRESH_LIBRARY = "refresh"; - - public static final String AUTHORITY = "content://" + BuildConfig.APPLICATION_ID + ".provider"; - public static final Uri URI_GAME = Uri.parse(AUTHORITY + "/" + GameDatabase.TABLE_NAME_GAMES + "/"); - public static final Uri URI_REFRESH = Uri.parse(AUTHORITY + "/" + REFRESH_LIBRARY + "/"); - - public static final String MIME_TYPE_FOLDER = "vnd.android.cursor.item/vnd.dolphin.folder"; - public static final String MIME_TYPE_GAME = "vnd.android.cursor.item/vnd.dolphin.game"; - - - private GameDatabase mDbHelper; - - @Override - public boolean onCreate() - { - Log.info("[GameProvider] Creating Content Provider..."); - - mDbHelper = new GameDatabase(getContext()); - - return true; - } - - @Override - public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) - { - Log.info("[GameProvider] Querying URI: " + uri); - - SQLiteDatabase db = mDbHelper.getReadableDatabase(); - - String table = uri.getLastPathSegment(); - - if (table == null) - { - Log.error("[GameProvider] Badly formatted URI: " + uri); - return null; - } - - Cursor cursor = db.query(table, projection, selection, selectionArgs, null, null, sortOrder); - cursor.setNotificationUri(getContext().getContentResolver(), uri); - - return cursor; - } - - @Override - public String getType(@NonNull Uri uri) - { - Log.verbose("[GameProvider] Getting MIME type for URI: " + uri); - String lastSegment = uri.getLastPathSegment(); - - if (lastSegment == null) - { - Log.error("[GameProvider] Badly formatted URI: " + uri); - return null; - } - - if (lastSegment.equals(GameDatabase.TABLE_NAME_GAMES)) - { - return MIME_TYPE_GAME; - } - - Log.error("[GameProvider] Unknown MIME type for URI: " + uri); - return null; - } - - @Override - public Uri insert(@NonNull Uri uri, ContentValues values) - { - Log.info("[GameProvider] Inserting row at URI: " + uri); - - SQLiteDatabase database = mDbHelper.getWritableDatabase(); - String table = uri.getLastPathSegment(); - - long id = -1; - - if (table != null) - { - if (table.equals(REFRESH_LIBRARY)) - { - Log.info("[GameProvider] URI specified table REFRESH_LIBRARY. No insertion necessary; refreshing library contents..."); - mDbHelper.scanLibrary(database); - return uri; - } - - id = database.insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_IGNORE); - - // If insertion was successful... - if (id > 0) - { - // Notify the UI that its contents should be refreshed. - getContext().getContentResolver().notifyChange(uri, null); - uri = Uri.withAppendedPath(uri, Long.toString(id)); - } - else - { - Log.error("[GameProvider] Row already exists: " + uri + " id: " + id); - } - } - else - { - Log.error("[GameProvider] Badly formatted URI: " + uri); - } - - database.close(); - - return uri; - } - - @Override - public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) - { - Log.error("[GameProvider] Delete operations unsupported. URI: " + uri); - return 0; - } - - @Override - public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) - { - Log.error("[GameProvider] Update operations unsupported. URI: " + uri); - return 0; - } -} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/DirectoryInitializationService.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/DirectoryInitializationService.java index d23b478065..9dde847d35 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/DirectoryInitializationService.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/DirectoryInitializationService.java @@ -32,7 +32,7 @@ import java.util.concurrent.atomic.AtomicBoolean; */ public final class DirectoryInitializationService extends IntentService { - public static final String BROADCAST_ACTION = "org.dolphinemu.dolphinemu.BROADCAST"; + public static final String BROADCAST_ACTION = "org.dolphinemu.dolphinemu.DIRECTORY_INITIALIZATION"; public static final String EXTRA_STATE = "directoryState"; private static volatile DirectoryInitializationState directoryState = null; diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/GameFileCacheService.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/GameFileCacheService.java new file mode 100644 index 0000000000..c4aa8e5715 --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/GameFileCacheService.java @@ -0,0 +1,125 @@ +package org.dolphinemu.dolphinemu.services;
+
+import android.app.IntentService;
+import android.content.Context;
+import android.content.Intent;
+import android.support.v4.content.LocalBroadcastManager;
+
+import org.dolphinemu.dolphinemu.model.GameFile;
+import org.dolphinemu.dolphinemu.model.GameFileCache;
+import org.dolphinemu.dolphinemu.ui.platform.Platform;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * A service that loads game list data on a separate thread.
+ */
+public final class GameFileCacheService extends IntentService
+{
+ public static final String BROADCAST_ACTION = "org.dolphinemu.dolphinemu.GAME_FILE_CACHE_UPDATED";
+
+ private static final String ACTION_LOAD = "org.dolphinemu.dolphinemu.LOAD_GAME_FILE_CACHE";
+ private static final String ACTION_RESCAN = "org.dolphinemu.dolphinemu.RESCAN_GAME_FILE_CACHE";
+
+ private static GameFileCache gameFileCache = null;
+ private static AtomicReference<GameFile[]> gameFiles = new AtomicReference<>(new GameFile[]{});
+
+ public GameFileCacheService()
+ {
+ // Superclass constructor is called to name the thread on which this service executes.
+ super("GameFileCacheService");
+ }
+
+ public static List<GameFile> getGameFilesForPlatform(Platform platform)
+ {
+ GameFile[] allGames = gameFiles.get();
+ ArrayList<GameFile> platformGames = new ArrayList<>();
+ for (GameFile game : allGames)
+ {
+ if (Platform.fromNativeInt(game.getPlatform()) == platform)
+ {
+ platformGames.add(game);
+ }
+ }
+ return platformGames;
+ }
+
+ private static void startService(Context context, String action)
+ {
+ Intent intent = new Intent(context, GameFileCacheService.class);
+ intent.setAction(action);
+ context.startService(intent);
+ }
+
+ /**
+ * Asynchronously loads the game file cache from disk without checking
+ * which games are present on the file system.
+ */
+ public static void startLoad(Context context)
+ {
+ startService(context, ACTION_LOAD);
+ }
+
+ /**
+ * Asynchronously scans for games in the user's configured folders,
+ * updating the game file cache with the results.
+ * If startLoad hasn't been called before this, this has no effect.
+ */
+ public static void startRescan(Context context)
+ {
+ startService(context, ACTION_RESCAN);
+ }
+
+ public static GameFile addOrGet(String gamePath)
+ {
+ // The existence of this one function, which is called from one
+ // single place, forces us to use synchronization in onHandleIntent...
+ // A bit annoying, but should be good enough for now
+ synchronized (gameFileCache)
+ {
+ return gameFileCache.addOrGet(gamePath);
+ }
+ }
+
+ @Override
+ protected void onHandleIntent(Intent intent)
+ {
+ // Load the game list cache if it isn't already loaded, otherwise do nothing
+ if (ACTION_LOAD.equals(intent.getAction()) && gameFileCache == null)
+ {
+ GameFileCache temp = new GameFileCache(getCacheDir() + File.separator + "gamelist.cache");
+ synchronized (temp)
+ {
+ gameFileCache = temp;
+ gameFileCache.load();
+ updateGameFileArray();
+ }
+ }
+
+ // Rescan the file system and update the game list cache with the results
+ if (ACTION_RESCAN.equals(intent.getAction()) && gameFileCache != null)
+ {
+ synchronized (gameFileCache)
+ {
+ if (gameFileCache.scanLibrary(this))
+ {
+ updateGameFileArray();
+ }
+ }
+ }
+ }
+
+ private void updateGameFileArray()
+ {
+ GameFile[] gameFilesTemp = gameFileCache.getAllGames();
+ Arrays.sort(gameFilesTemp, (lhs, rhs) -> lhs.getTitle().compareTo(rhs.getTitle()));
+ gameFiles.set(gameFilesTemp);
+ LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(BROADCAST_ACTION));
+ }
+}
diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java index 97ad22bd7a..57b9814a75 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java @@ -2,7 +2,6 @@ package org.dolphinemu.dolphinemu.ui.main; import android.content.Intent; import android.content.pm.PackageManager; -import android.database.Cursor; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.design.widget.FloatingActionButton; @@ -18,12 +17,11 @@ import android.widget.Toast; import org.dolphinemu.dolphinemu.R; import org.dolphinemu.dolphinemu.adapters.PlatformPagerAdapter; -import org.dolphinemu.dolphinemu.model.GameProvider; import org.dolphinemu.dolphinemu.services.DirectoryInitializationService; +import org.dolphinemu.dolphinemu.services.GameFileCacheService; import org.dolphinemu.dolphinemu.ui.platform.Platform; import org.dolphinemu.dolphinemu.ui.platform.PlatformGamesView; import org.dolphinemu.dolphinemu.ui.settings.SettingsActivity; -import org.dolphinemu.dolphinemu.utils.AddDirectoryHelper; import org.dolphinemu.dolphinemu.utils.FileBrowserHelper; import org.dolphinemu.dolphinemu.utils.PermissionsHandler; import org.dolphinemu.dolphinemu.utils.StartupHandler; @@ -39,7 +37,7 @@ public final class MainActivity extends AppCompatActivity implements MainView private TabLayout mTabLayout; private FloatingActionButton mFab; - private MainPresenter mPresenter = new MainPresenter(this); + private MainPresenter mPresenter = new MainPresenter(this, this); @Override protected void onCreate(Bundle savedInstanceState) @@ -67,7 +65,11 @@ public final class MainActivity extends AppCompatActivity implements MainView PlatformPagerAdapter platformPagerAdapter = new PlatformPagerAdapter( getSupportFragmentManager(), this); mViewPager.setAdapter(platformPagerAdapter); - } else { + showGames(); + GameFileCacheService.startLoad(this); + } + else + { mViewPager.setVisibility(View.INVISIBLE); } } @@ -76,7 +78,14 @@ public final class MainActivity extends AppCompatActivity implements MainView protected void onResume() { super.onResume(); - mPresenter.addDirIfNeeded(new AddDirectoryHelper(this)); + mPresenter.addDirIfNeeded(this); + } + + @Override + protected void onDestroy() + { + super.onDestroy(); + mPresenter.onDestroy(); } // TODO: Replace with a ButterKnife injection. @@ -107,13 +116,6 @@ public final class MainActivity extends AppCompatActivity implements MainView } @Override - public void refresh() - { - getContentResolver().insert(GameProvider.URI_REFRESH, null); - refreshAllFragments(); - } - - @Override public void refreshFragmentScreenshot(int fragmentPosition) { // Invalidate Picasso image so that the new screenshot is animated in. @@ -138,12 +140,6 @@ public final class MainActivity extends AppCompatActivity implements MainView FileBrowserHelper.openDirectoryPicker(this); } - @Override - public void showGames(Platform platform, Cursor games) - { - // no-op. Handled by PlatformGamesFragment. - } - /** * @param requestCode An int describing whether the Activity that is returning did so successfully. * @param resultCode An int describing what Activity is giving us this callback. @@ -174,12 +170,12 @@ public final class MainActivity extends AppCompatActivity implements MainView case PermissionsHandler.REQUEST_CODE_WRITE_PERMISSION: if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { DirectoryInitializationService.startService(this); - PlatformPagerAdapter platformPagerAdapter = new PlatformPagerAdapter( getSupportFragmentManager(), this); mViewPager.setAdapter(platformPagerAdapter); mTabLayout.setupWithViewPager(mViewPager); mViewPager.setVisibility(View.VISIBLE); + GameFileCacheService.startLoad(this); } else { Toast.makeText(this, R.string.write_permission_needed, Toast.LENGTH_SHORT) .show(); @@ -200,17 +196,17 @@ public final class MainActivity extends AppCompatActivity implements MainView @Override public boolean onOptionsItemSelected(MenuItem item) { - return mPresenter.handleOptionSelection(item.getItemId()); + return mPresenter.handleOptionSelection(item.getItemId(), this); } - private void refreshAllFragments() + public void showGames() { for (Platform platform : Platform.values()) { PlatformGamesView fragment = getPlatformGamesView(platform); if (fragment != null) { - fragment.refresh(); + fragment.showGames(); } } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainPresenter.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainPresenter.java index 4ed97b6ecb..9ae48acb0f 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainPresenter.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainPresenter.java @@ -1,34 +1,57 @@ package org.dolphinemu.dolphinemu.ui.main; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.support.v4.content.LocalBroadcastManager; import org.dolphinemu.dolphinemu.BuildConfig; -import org.dolphinemu.dolphinemu.DolphinApplication; import org.dolphinemu.dolphinemu.R; -import org.dolphinemu.dolphinemu.model.GameDatabase; -import org.dolphinemu.dolphinemu.ui.platform.Platform; -import org.dolphinemu.dolphinemu.utils.AddDirectoryHelper; +import org.dolphinemu.dolphinemu.model.GameFileCache; +import org.dolphinemu.dolphinemu.services.GameFileCacheService; import org.dolphinemu.dolphinemu.utils.SettingsFile; -import rx.android.schedulers.AndroidSchedulers; -import rx.schedulers.Schedulers; - public final class MainPresenter { public static final int REQUEST_ADD_DIRECTORY = 1; public static final int REQUEST_EMULATE_GAME = 2; private final MainView mView; + private final Context mContext; + private BroadcastReceiver mBroadcastReceiver = null; private String mDirToAdd; - public MainPresenter(MainView view) + public MainPresenter(MainView view, Context context) { mView = view; + mContext = context; } public void onCreate() { String versionName = BuildConfig.VERSION_NAME; mView.setVersionString(versionName); + + IntentFilter filter = new IntentFilter(); + filter.addAction(GameFileCacheService.BROADCAST_ACTION); + mBroadcastReceiver = new BroadcastReceiver() + { + @Override + public void onReceive(Context context, Intent intent) + { + mView.showGames(); + } + }; + LocalBroadcastManager.getInstance(mContext).registerReceiver(mBroadcastReceiver, filter); + } + + public void onDestroy() + { + if (mBroadcastReceiver != null) + { + LocalBroadcastManager.getInstance(mContext).unregisterReceiver(mBroadcastReceiver); + } } public void onFabClick() @@ -36,7 +59,7 @@ public final class MainPresenter mView.launchFileListActivity(); } - public boolean handleOptionSelection(int itemId) + public boolean handleOptionSelection(int itemId, Context context) { switch (itemId) { @@ -57,9 +80,7 @@ public final class MainPresenter return true; case R.id.menu_refresh: - GameDatabase databaseHelper = DolphinApplication.databaseHelper; - databaseHelper.scanLibrary(databaseHelper.getWritableDatabase()); - mView.refresh(); + GameFileCacheService.startRescan(context); return true; case R.id.button_add_directory: @@ -70,13 +91,13 @@ public final class MainPresenter return false; } - public void addDirIfNeeded(AddDirectoryHelper helper) + public void addDirIfNeeded(Context context) { if (mDirToAdd != null) { - helper.addDirectory(mDirToAdd, mView::refresh); - + GameFileCache.addGameFolder(mDirToAdd, context); mDirToAdd = null; + GameFileCacheService.startRescan(context); } } @@ -89,15 +110,4 @@ public final class MainPresenter { mView.refreshFragmentScreenshot(resultCode); } - - - public void loadGames(final Platform platform) - { - GameDatabase databaseHelper = DolphinApplication.databaseHelper; - - databaseHelper.getGamesForPlatform(platform) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(games -> mView.showGames(platform, games)); - } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainView.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainView.java index fdaef5eefa..cb21b7b084 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainView.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainView.java @@ -2,6 +2,7 @@ package org.dolphinemu.dolphinemu.ui.main; import android.database.Cursor; +import org.dolphinemu.dolphinemu.model.GameFile; import org.dolphinemu.dolphinemu.ui.platform.Platform; /** @@ -20,11 +21,6 @@ public interface MainView void setVersionString(String version); /** - * Tell the view to refresh its contents. - */ - void refresh(); - - /** * Tell the view to tell the currently displayed {@link android.support.v4.app.Fragment} * to refresh the screenshot at the given position in its list of games. * @@ -38,11 +34,7 @@ public interface MainView void launchFileListActivity(); /** - * To be called when an asynchronous database read completes. Passes the - * result, in this case a {@link Cursor} to the view. - * - * @param platform Which platform to show games for. - * @param games A Cursor containing the games read from the database. + * To be called when the game file cache is updated. */ - void showGames(Platform platform, Cursor games); + void showGames(); } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java index 659ee97d37..10187df402 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java @@ -2,13 +2,10 @@ package org.dolphinemu.dolphinemu.ui.main; import android.content.Intent; import android.content.pm.PackageManager; -import android.database.Cursor; import android.os.Bundle; import android.support.v17.leanback.app.BrowseFragment; import android.support.v17.leanback.app.BrowseSupportFragment; -import android.support.v17.leanback.database.CursorMapper; import android.support.v17.leanback.widget.ArrayObjectAdapter; -import android.support.v17.leanback.widget.CursorObjectAdapter; import android.support.v17.leanback.widget.HeaderItem; import android.support.v17.leanback.widget.ListRow; import android.support.v17.leanback.widget.ListRowPresenter; @@ -21,20 +18,22 @@ import org.dolphinemu.dolphinemu.R; import org.dolphinemu.dolphinemu.activities.EmulationActivity; import org.dolphinemu.dolphinemu.adapters.GameRowPresenter; import org.dolphinemu.dolphinemu.adapters.SettingsRowPresenter; -import org.dolphinemu.dolphinemu.model.Game; +import org.dolphinemu.dolphinemu.model.GameFile; import org.dolphinemu.dolphinemu.model.TvSettingsItem; import org.dolphinemu.dolphinemu.services.DirectoryInitializationService; +import org.dolphinemu.dolphinemu.services.GameFileCacheService; import org.dolphinemu.dolphinemu.ui.platform.Platform; import org.dolphinemu.dolphinemu.ui.settings.SettingsActivity; -import org.dolphinemu.dolphinemu.utils.AddDirectoryHelper; import org.dolphinemu.dolphinemu.utils.FileBrowserHelper; import org.dolphinemu.dolphinemu.utils.PermissionsHandler; import org.dolphinemu.dolphinemu.utils.StartupHandler; import org.dolphinemu.dolphinemu.viewholders.TvGameViewHolder; +import java.util.Collection; + public final class TvMainActivity extends FragmentActivity implements MainView { - private MainPresenter mPresenter = new MainPresenter(this); + private MainPresenter mPresenter = new MainPresenter(this, this); private BrowseSupportFragment mBrowseFragment; @@ -59,7 +58,14 @@ public final class TvMainActivity extends FragmentActivity implements MainView protected void onResume() { super.onResume(); - mPresenter.addDirIfNeeded(new AddDirectoryHelper(this)); + mPresenter.addDirIfNeeded(this); + } + + @Override + protected void onDestroy() + { + super.onDestroy(); + mPresenter.onDestroy(); } void setupUI() { @@ -82,7 +88,7 @@ public final class TvMainActivity extends FragmentActivity implements MainView if (item instanceof TvSettingsItem) { TvSettingsItem settingsItem = (TvSettingsItem) item; - mPresenter.handleOptionSelection(settingsItem.getItemId()); + mPresenter.handleOptionSelection(settingsItem.getItemId(), this); } else { @@ -90,9 +96,7 @@ public final class TvMainActivity extends FragmentActivity implements MainView // Start the emulation activity and send the path of the clicked ISO to it. EmulationActivity.launch(TvMainActivity.this, - holder.path, - holder.title, - holder.screenshotPath, + holder.gameFile, -1, holder.imageScreenshot); } @@ -109,12 +113,6 @@ public final class TvMainActivity extends FragmentActivity implements MainView } @Override - public void refresh() - { - recreate(); - } - - @Override public void refreshFragmentScreenshot(int fragmentPosition) { mRowsAdapter.notifyArrayItemRangeChanged(0, mRowsAdapter.size()); @@ -133,15 +131,9 @@ public final class TvMainActivity extends FragmentActivity implements MainView } @Override - public void showGames(Platform platform, Cursor games) + public void showGames() { - ListRow row = buildGamesRow(platform, games); - - // Add row to the adapter only if it is not empty. - if (row != null) - { - mRowsAdapter.add(row); - } + recreate(); } /** @@ -176,7 +168,7 @@ public final class TvMainActivity extends FragmentActivity implements MainView case PermissionsHandler.REQUEST_CODE_WRITE_PERMISSION: if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { DirectoryInitializationService.startService(this); - loadGames(); + GameFileCacheService.startLoad(this); } else { Toast.makeText(this, R.string.write_permission_needed, Toast.LENGTH_SHORT) .show(); @@ -194,48 +186,36 @@ public final class TvMainActivity extends FragmentActivity implements MainView if (PermissionsHandler.hasWriteAccess(this)) { - loadGames(); + GameFileCacheService.startLoad(this); } mRowsAdapter.add(buildSettingsRow()); - mBrowseFragment.setAdapter(mRowsAdapter); - } + for (Platform platform : Platform.values()) + { + ListRow row = buildGamesRow(platform, GameFileCacheService.getGameFilesForPlatform(platform)); - private void loadGames() { - for (Platform platform : Platform.values()) { - mPresenter.loadGames(platform); + // Add row to the adapter only if it is not empty. + if (row != null) + { + mRowsAdapter.add(row); + } } + + mBrowseFragment.setAdapter(mRowsAdapter); } - private ListRow buildGamesRow(Platform platform, Cursor games) + private ListRow buildGamesRow(Platform platform, Collection<GameFile> gameFiles) { - // Create an adapter for this row. - CursorObjectAdapter row = new CursorObjectAdapter(new GameRowPresenter()); - - // If cursor is empty, don't return a Row. - if (!games.moveToFirst()) + // If there are no games, don't return a Row. + if (gameFiles.size() == 0) { return null; } - row.changeCursor(games); - row.setMapper(new CursorMapper() - { - @Override - protected void bindColumns(Cursor cursor) - { - // No-op? Not sure what this does. - } - - @Override - protected Object bind(Cursor cursor) - { - return Game.fromCursor(cursor); - } - }); - - String headerName = platform.getHeaderName(); + // Create an adapter for this row. + ArrayObjectAdapter row = new ArrayObjectAdapter(new GameRowPresenter()); + row.addAll(0, gameFiles); // Create a header for this row. HeaderItem header = new HeaderItem(platform.toInt(), platform.getHeaderName()); diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/Platform.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/Platform.java index b161a1cde4..64f1cc71cd 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/Platform.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/Platform.java @@ -23,11 +23,9 @@ public enum Platform public static Platform fromNativeInt(int i) { - // If the game's platform field is empty, file under Wiiware. // TODO Something less dum - if (i == -1) { - return Platform.WIIWARE; - } - return values()[i]; + // TODO: Proper support for DOL and ELF files + boolean in_range = i >= 0 && i < values().length; + return values()[in_range ? i : WIIWARE.value]; } public static Platform fromPosition(int position) @@ -44,4 +42,4 @@ public enum Platform { return headerName; } -}
\ No newline at end of file +} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/PlatformGamesFragment.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/PlatformGamesFragment.java index 3325a1ca23..7d15609055 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/PlatformGamesFragment.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/PlatformGamesFragment.java @@ -1,6 +1,5 @@ package org.dolphinemu.dolphinemu.ui.platform; -import android.database.Cursor; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; @@ -12,13 +11,15 @@ import android.view.ViewGroup; import org.dolphinemu.dolphinemu.R; import org.dolphinemu.dolphinemu.adapters.GameAdapter; +import org.dolphinemu.dolphinemu.model.GameFile; +import org.dolphinemu.dolphinemu.services.GameFileCacheService; + +import java.util.List; public final class PlatformGamesFragment extends Fragment implements PlatformGamesView { private static final String ARG_PLATFORM = "platform"; - private PlatformGamesPresenter mPresenter = new PlatformGamesPresenter(this); - private GameAdapter mAdapter; private RecyclerView mRecyclerView; @@ -37,8 +38,6 @@ public final class PlatformGamesFragment extends Fragment implements PlatformGam public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - - mPresenter.onCreate((Platform) getArguments().getSerializable(ARG_PLATFORM)); } @Nullable @@ -49,8 +48,6 @@ public final class PlatformGamesFragment extends Fragment implements PlatformGam findViews(rootView); - mPresenter.onCreateView(); - return rootView; } @@ -65,6 +62,8 @@ public final class PlatformGamesFragment extends Fragment implements PlatformGam mRecyclerView.setAdapter(mAdapter); mRecyclerView.addItemDecoration(new GameAdapter.SpacesItemDecoration(8)); + + showGames(); } @Override @@ -74,23 +73,18 @@ public final class PlatformGamesFragment extends Fragment implements PlatformGam } @Override - public void refresh() - { - mPresenter.refresh(); - } - - @Override public void onItemClick(String gameId) { // No-op for now } @Override - public void showGames(Cursor games) + public void showGames() { if (mAdapter != null) { - mAdapter.swapCursor(games); + Platform platform = (Platform) getArguments().getSerializable(ARG_PLATFORM); + mAdapter.swapDataSet(GameFileCacheService.getGameFilesForPlatform(platform)); } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/PlatformGamesPresenter.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/PlatformGamesPresenter.java deleted file mode 100644 index 4c6782dd5f..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/PlatformGamesPresenter.java +++ /dev/null @@ -1,57 +0,0 @@ -package org.dolphinemu.dolphinemu.ui.platform; - - -import android.database.Cursor; - -import org.dolphinemu.dolphinemu.DolphinApplication; -import org.dolphinemu.dolphinemu.model.GameDatabase; -import org.dolphinemu.dolphinemu.utils.Log; - -import rx.android.schedulers.AndroidSchedulers; -import rx.functions.Action1; -import rx.schedulers.Schedulers; - -public final class PlatformGamesPresenter -{ - private final PlatformGamesView mView; - - private Platform mPlatform; - - public PlatformGamesPresenter(PlatformGamesView view) - { - mView = view; - } - - public void onCreate(Platform platform) - { - mPlatform = platform; - } - - public void onCreateView() - { - loadGames(); - } - - public void refresh() - { - Log.debug("[PlatformGamesPresenter] " + mPlatform + ": Refreshing..."); - loadGames(); - } - - private void loadGames() - { - Log.debug("[PlatformGamesPresenter] " + mPlatform + ": Loading games..."); - - GameDatabase databaseHelper = DolphinApplication.databaseHelper; - - databaseHelper.getGamesForPlatform(mPlatform) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(games -> - { - Log.debug("[PlatformGamesPresenter] " + mPlatform + ": Load finished, swapping cursor..."); - - mView.showGames(games); - }); - } -} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/PlatformGamesView.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/PlatformGamesView.java index 361d16dfbc..b584b8090f 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/PlatformGamesView.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/PlatformGamesView.java @@ -1,6 +1,8 @@ package org.dolphinemu.dolphinemu.ui.platform; -import android.database.Cursor; +import org.dolphinemu.dolphinemu.model.GameFile; + +import java.util.List; /** * Abstraction for a screen representing a single platform's games. @@ -8,11 +10,6 @@ import android.database.Cursor; public interface PlatformGamesView { /** - * Tell the view to refresh its contents. - */ - void refresh(); - - /** * Tell the view that a certain game's screenshot has been updated, * and should be redrawn on-screen. * @@ -29,10 +26,7 @@ public interface PlatformGamesView void onItemClick(String gameId); /** - * To be called when an asynchronous database read completes. Passes the - * result, in this case a {@link Cursor}, to the view. - * - * @param games A Cursor containing the games read from the database. + * To be called when the game file cache is updated. */ - void showGames(Cursor games); + void showGames(); } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/AddDirectoryHelper.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/AddDirectoryHelper.java deleted file mode 100644 index 7678779707..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/AddDirectoryHelper.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.dolphinemu.dolphinemu.utils; - -import android.content.Context; -import android.os.AsyncTask; - -import org.dolphinemu.dolphinemu.DolphinApplication; -import org.dolphinemu.dolphinemu.model.GameDatabase; - -public class AddDirectoryHelper -{ - private Context mContext; - - public interface AddDirectoryListener - { - void onDirectoryAdded(); - } - - public AddDirectoryHelper(Context context) - { - this.mContext = context; - } - - public void addDirectory(String dir, AddDirectoryListener addDirectoryListener) - { - new AsyncTask<String, Void, Void>() - { - @Override - protected Void doInBackground(String... params) - { - for (String path : params) - { - DolphinApplication.databaseHelper.addGameFolder(path); - } - return null; - } - - @Override - protected void onPostExecute(Void result) - { - addDirectoryListener.onDirectoryAdded(); - } - }.execute(dir); - } -} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/GameBannerRequestHandler.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/GameBannerRequestHandler.java index ef95cd7712..b8afcf6a52 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/GameBannerRequestHandler.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/GameBannerRequestHandler.java @@ -7,22 +7,28 @@ import com.squareup.picasso.Request; import com.squareup.picasso.RequestHandler; import org.dolphinemu.dolphinemu.NativeLibrary; +import org.dolphinemu.dolphinemu.model.GameFile; import java.io.IOException; import java.nio.IntBuffer; public class GameBannerRequestHandler extends RequestHandler { + GameFile mGameFile; + + public GameBannerRequestHandler(GameFile gameFile) + { + mGameFile = gameFile; + } @Override public boolean canHandleRequest(Request data) { - return "iso".equals(data.uri.getScheme()); + return true; } @Override - public Result load(Request request, int networkPolicy) throws IOException { - String url = request.uri.getHost() + request.uri.getPath(); - int[] vector = NativeLibrary.GetBanner(url); - int width = 96; - int height = 32; + public Result load(Request request, int networkPolicy) { + int[] vector = mGameFile.getBanner(); + int width = mGameFile.getBannerWidth(); + int height = mGameFile.getBannerHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(vector, 0, width, 0, 0, width, height); return new Result(bitmap, Picasso.LoadedFrom.DISK); diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/PicassoUtils.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/PicassoUtils.java index 36a00d884d..4630a3cddc 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/PicassoUtils.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/PicassoUtils.java @@ -7,17 +7,18 @@ import android.widget.ImageView; import com.squareup.picasso.Picasso; import org.dolphinemu.dolphinemu.R; +import org.dolphinemu.dolphinemu.model.GameFile; import java.io.File; import java.net.URI; public class PicassoUtils { - public static void loadGameBanner(ImageView imageView, String screenshotPath, String gamePath) { - File file = new File(URI.create(screenshotPath)); - if (file.exists()) { + public static void loadGameBanner(ImageView imageView, GameFile gameFile) { + File screenshotFile = new File(URI.create(gameFile.getScreenshotPath())); + if (screenshotFile.exists()) { // Fill in the view contents. Picasso.with(imageView.getContext()) - .load(screenshotPath) + .load(gameFile.getScreenshotPath()) .fit() .centerCrop() .noFade() @@ -27,11 +28,11 @@ public class PicassoUtils { .into(imageView); } else { Picasso picassoInstance = new Picasso.Builder(imageView.getContext()) - .addRequestHandler(new GameBannerRequestHandler()) + .addRequestHandler(new GameBannerRequestHandler(gameFile)) .build(); picassoInstance - .load(Uri.parse("iso:/" + gamePath)) + .load(Uri.parse("iso:/" + gameFile.getPath())) .fit() .noFade() .noPlaceholder() diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/viewholders/GameViewHolder.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/viewholders/GameViewHolder.java index 592a2ecf32..e993ba7961 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/viewholders/GameViewHolder.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/viewholders/GameViewHolder.java @@ -6,6 +6,7 @@ import android.widget.ImageView; import android.widget.TextView; import org.dolphinemu.dolphinemu.R; +import org.dolphinemu.dolphinemu.model.GameFile; /** * A simple class that stores references to views so that the GameAdapter doesn't need to @@ -17,15 +18,7 @@ public class GameViewHolder extends RecyclerView.ViewHolder public TextView textGameTitle; public TextView textCompany; - public String gameId; - - // TODO Not need any of this stuff. Currently only the properties dialog needs it. - public String path; - public String title; - public String description; - public int country; - public String company; - public String screenshotPath; + public GameFile gameFile; public GameViewHolder(View itemView) { @@ -33,8 +26,8 @@ public class GameViewHolder extends RecyclerView.ViewHolder itemView.setTag(this); - imageScreenshot = (ImageView) itemView.findViewById(R.id.image_game_screen); - textGameTitle = (TextView) itemView.findViewById(R.id.text_game_title); - textCompany = (TextView) itemView.findViewById(R.id.text_company); + imageScreenshot = itemView.findViewById(R.id.image_game_screen); + textGameTitle = itemView.findViewById(R.id.text_game_title); + textCompany = itemView.findViewById(R.id.text_company); } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/viewholders/TvGameViewHolder.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/viewholders/TvGameViewHolder.java index dbd018be78..54615c6710 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/viewholders/TvGameViewHolder.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/viewholders/TvGameViewHolder.java @@ -5,6 +5,8 @@ import android.support.v17.leanback.widget.Presenter; import android.view.View; import android.widget.ImageView; +import org.dolphinemu.dolphinemu.model.GameFile; + /** * A simple class that stores references to views so that the GameAdapter doesn't need to * keep calling findViewById(), which is expensive. @@ -15,15 +17,7 @@ public final class TvGameViewHolder extends Presenter.ViewHolder public ImageView imageScreenshot; - public String gameId; - - // TODO Not need any of this stuff. Currently only the properties dialog needs it. - public String path; - public String title; - public String description; - public int country; - public String company; - public String screenshotPath; + public GameFile gameFile; public TvGameViewHolder(View itemView) { |
