Eliminate static variables and hacky workarounds

Avoid retrieving preferences from static function

Unify numerous calls to the same fragment

Not entirely sure why I added more when this is much cleaner

Avoid a redundant call to SharedPreferences

Cleanup, Fix regressions, Finalize design changes

This fixes a few minor issues, including the missing title when options is opened from file browser, the release build product being named debug, and the possibility that JNIdc.config is not set when opening the BIOS without a game selected.
This commit is contained in:
TwistedUmbrella 2018-03-31 19:28:00 -04:00
parent 919c6d6288
commit f71f2ca91f
12 changed files with 113 additions and 138 deletions

View File

@ -1,12 +1,16 @@
package com.android.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.OnScanCompletedListener;
import android.net.Uri;
import android.os.Environment;
import android.preference.PreferenceManager;
import com.reicast.emulator.MainActivity;
import com.reicast.emulator.config.Config;
import java.io.File;
import java.io.FileInputStream;
@ -83,7 +87,9 @@ public class FileUtils {
public static void saveScreenshot(final Context c, int w, int h, GL10 gl){
try {
File dir = new File(MainActivity.home_directory);
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(c);
File dir = new File(mPrefs.getString(Config.pref_home,
Environment.getExternalStorageDirectory().getAbsolutePath()));
SimpleDateFormat s = new SimpleDateFormat("yyyyMMddHHmmss");
String timestamp = s.format(new Date());
File f = new File(dir.getAbsolutePath(), timestamp+".jpeg");

View File

@ -15,6 +15,8 @@ import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
@ -30,6 +32,7 @@ import com.dropbox.client2.exception.DropboxException;
import com.dropbox.client2.session.AccessTokenPair;
import com.dropbox.client2.session.AppKeyPair;
import com.dropbox.client2.session.TokenPair;
import com.reicast.emulator.config.Config;
import java.io.File;
import java.io.FileInputStream;
@ -49,6 +52,7 @@ public class CloudFragment extends Fragment {
boolean actionRequired=false;
public String task = "";
DropBoxClient client = null;
private String home_directory;
String[] vmus = {"vmu_save_A1.bin","vmu_save_A2.bin",
"vmu_save_B1.bin","vmu_save_B2.bin",
@ -62,6 +66,9 @@ public class CloudFragment extends Fragment {
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
home_directory = mPrefs.getString(Config.pref_home,
Environment.getExternalStorageDirectory().getAbsolutePath());
buttonListener();
confirmDialog = new AlertDialog.Builder(getActivity());
setClient();
@ -134,14 +141,16 @@ public class CloudFragment extends Fragment {
for(int k=0;k<vmus.length;k++){
String result = "";
try {
String vmuPath = MainActivity.home_directory+"/"+vmus[k];
String vmuPath = home_directory+"/"+vmus[k];
File vmu = new File(vmuPath);
if(vmu.exists() || task.equals("Download") ){
result = new netOperation(client).execute(task,vmuPath,vmus[k]).get();
result = new netOperation(client, home_directory).execute(
task,vmuPath,vmus[k]).get();
}
else{
result = "Ok"; // The result is still ok, because the vmu bin doesn't exist ;)
Toast.makeText(getActivity(), vmus[k]+" doesn't exist, skipping it!", Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), vmus[k]+ " doesn't exist, skipping it!",
Toast.LENGTH_SHORT).show();
}
} catch (InterruptedException e) {
e.printStackTrace();
@ -230,10 +239,11 @@ class DropBoxClient {
class netOperation extends AsyncTask<String, Void, String> {
DropBoxClient client = null;
private String home_directory;
public netOperation(DropBoxClient client){
public netOperation(DropBoxClient client, String home_directory){
this.client = client;
this.home_directory = home_directory;
}
public boolean uploadFile(String filePath, String fileName) {
@ -310,13 +320,13 @@ class netOperation extends AsyncTask<String, Void, String> {
void createBackupOfVmu(String vmuName){
File backupDir = new File(MainActivity.home_directory+"/VmuBackups/");
File backupDir = new File(home_directory+"/VmuBackups/");
if(!backupDir.exists()) {
backupDir.mkdirs();
}
File source = new File(MainActivity.home_directory+"/"+vmuName);
File destination = new File(MainActivity.home_directory+"/VmuBackups/"+vmuName);
File source = new File(home_directory+"/"+vmuName);
File destination = new File(home_directory+"/VmuBackups/"+vmuName);
if(!destination.exists()) {
try {
destination.createNewFile();
@ -339,9 +349,6 @@ class netOperation extends AsyncTask<String, Void, String> {
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -50,8 +50,7 @@ public class Emulator extends Application {
* Load the user configuration from preferences
*
*/
public void getConfigurationPrefs(Context mContext) {
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
public void getConfigurationPrefs(SharedPreferences mPrefs) {
Emulator.dynarecopt = mPrefs.getBoolean(pref_dynarecopt, dynarecopt);
Emulator.unstableopt = mPrefs.getBoolean(pref_unstable, unstableopt);
Emulator.cable = mPrefs.getInt(pref_cable, cable);

View File

@ -396,7 +396,7 @@ public class FileBrowser extends Fragment {
showToastMessage(getActivity().getString(R.string.config_data, home_directory),
Snackbar.LENGTH_LONG);
}
mPrefs.edit().putString("home_directory", home_directory).apply();
mPrefs.edit().putString(Config.pref_home, home_directory).apply();
mCallback.onFolderSelected(Uri.fromFile(new File(home_directory)));
JNIdc.config(home_directory);
}

View File

@ -58,7 +58,7 @@ public class GL2JNIActivity extends Activity {
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
}
Emulator app = (Emulator)getApplicationContext();
app.getConfigurationPrefs(GL2JNIActivity.this);
app.getConfigurationPrefs(prefs);
menu = new OnScreenMenu(GL2JNIActivity.this, prefs);
pad.isXperiaPlay = pad.IsXperiaPlay();

View File

@ -76,7 +76,7 @@ public class GL2JNINative extends NativeActivity {
RegisterNative(pad.isXperiaPlay);
Emulator app = (Emulator)getApplicationContext();
app.getConfigurationPrefs(GL2JNINative.this);
app.getConfigurationPrefs(prefs);
menu = new OnScreenMenu(GL2JNINative.this, prefs);
String fileName = null;

View File

@ -51,9 +51,6 @@ public class MainActivity extends AppCompatActivity implements
private static final int PERMISSION_REQUEST = 1001;
private SharedPreferences mPrefs;
private static File sdcard = Environment.getExternalStorageDirectory();
public static String home_directory = sdcard.getAbsolutePath();
private boolean hasAndroidMarket = false;
private UncaughtExceptionHandler mUEHandler;
@ -117,8 +114,6 @@ public class MainActivity extends AppCompatActivity implements
PERMISSION_REQUEST);
}
home_directory = mPrefs.getString("home_directory", home_directory);
Intent market = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=dummy"));
if (isCallable(market)) {
hasAndroidMarket = true;
@ -127,7 +122,6 @@ public class MainActivity extends AppCompatActivity implements
if (!getFilesDir().exists()) {
getFilesDir().mkdir();
}
JNIdc.config(home_directory);
// When viewing a resource, pass its URI to the native code for opening
Intent intent = getIntent();
@ -152,22 +146,7 @@ public class MainActivity extends AppCompatActivity implements
return;
}
}
// Create a new Fragment to be placed in the activity layout
FileBrowser firstFragment = new FileBrowser();
Bundle args = new Bundle();
args.putBoolean("ImgBrowse", true);
args.putString("browse_entry", null);
// specify a path for selecting folder options
args.putBoolean("games_entry", false);
// specify if the desired path is for games or data
firstFragment.setArguments(args);
// In case this activity was started with instructions from an intent
// pass the Intent's extras to the fragment as arguments
// firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction().replace(
R.id.fragment_container, firstFragment, "MAIN_BROWSER").commit();
onMainBrowseSelected(true, null, false, null);
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
@ -201,17 +180,9 @@ public class MainActivity extends AppCompatActivity implements
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
FileBrowser fragment = new FileBrowser();
Bundle args = new Bundle();
args.putBoolean("ImgBrowse", true);
args.putString("browse_entry", home_directory);
args.putBoolean("games_entry", true);
args.putString("search_params", query);
fragment.setArguments(args);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragment, "MAIN_BROWSER")
.addToBackStack(null).commit();
setTitle(R.string.browser);
onMainBrowseSelected(true, mPrefs.getString(Config.pref_games,
Environment.getExternalStorageDirectory().getAbsolutePath()),
true, query);
searchView.onActionViewCollapsed();
return false;
}
@ -253,43 +224,41 @@ public class MainActivity extends AppCompatActivity implements
builder.show();
}
public static boolean isBiosExisting(Context context) {
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
home_directory = mPrefs.getString("home_directory", home_directory);
File bios = new File(home_directory, "data/dc_boot.bin");
return bios.exists();
public static boolean isBiosExisting(String home_directory) {
return new File (home_directory, "data/dc_boot.bin").exists();
}
public static boolean isFlashExisting(Context context) {
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
home_directory = mPrefs.getString("home_directory", home_directory);
File flash = new File(home_directory, "data/dc_flash.bin");
return flash.exists();
public static boolean isFlashExisting(String home_directory) {
return new File (home_directory, "data/dc_flash.bin").exists();
}
public void onGameSelected(Uri uri) {
if (Config.readOutput("uname -a").equals(getString(R.string.error_kernel))) {
showToastMessage(getString(R.string.unsupported), Snackbar.LENGTH_SHORT);
}
String msg = null;
if (!isBiosExisting(MainActivity.this))
msg = getString(R.string.missing_bios, home_directory);
else if (!isFlashExisting(MainActivity.this))
msg = getString(R.string.missing_flash, home_directory);
String home_directory = mPrefs.getString(Config.pref_home,
Environment.getExternalStorageDirectory().getAbsolutePath());
if (msg != null) {
launchBIOSdetection();
} else {
Emulator.nativeact = PreferenceManager.getDefaultSharedPreferences(
getApplicationContext()).getBoolean(Emulator.pref_nativeact, Emulator.nativeact);
if (Emulator.nativeact) {
startActivity(new Intent("com.reicast.EMULATOR", uri, getApplicationContext(),
GL2JNINative.class));
} else {
startActivity(new Intent("com.reicast.EMULATOR", uri, getApplicationContext(),
GL2JNIActivity.class));
}
}
if (!isBiosExisting(home_directory)) {
launchBIOSdetection();
return;
}
if (!isFlashExisting(home_directory)) {
launchBIOSdetection();
return;
}
JNIdc.config(home_directory);
Emulator.nativeact = PreferenceManager.getDefaultSharedPreferences(
getApplicationContext()).getBoolean(Emulator.pref_nativeact, Emulator.nativeact);
if (Emulator.nativeact) {
startActivity(new Intent("com.reicast.EMULATOR", uri, getApplicationContext(),
GL2JNINative.class));
} else {
startActivity(new Intent("com.reicast.EMULATOR", uri, getApplicationContext(),
GL2JNIActivity.class));
}
}
private void launchBIOSdetection() {
@ -298,27 +267,9 @@ public class MainActivity extends AppCompatActivity implements
builder.setPositiveButton(R.string.browse,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
FileBrowser firstFragment = new FileBrowser();
Bundle args = new Bundle();
// args.putBoolean("ImgBrowse", false);
// specify ImgBrowse option. true = images, false = folders only
args.putString("browse_entry", sdcard.toString());
// specify a path for selecting folder options
args.putBoolean("games_entry", false);
// selecting a BIOS folder, so this is not games
firstFragment.setArguments(args);
// In case this activity was started with instructions from an intent
// pass the Intent's extras to the fragment as arguments
// firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container,
firstFragment,
"MAIN_BROWSER")
.addToBackStack(null).commit();
onMainBrowseSelected(false,
Environment.getExternalStorageDirectory().getAbsolutePath(),
false, null);
}
});
builder.setNegativeButton(R.string.gdrive,
@ -345,18 +296,33 @@ public class MainActivity extends AppCompatActivity implements
OptionsFragment optsFrag = new OptionsFragment();
getSupportFragmentManager().beginTransaction().replace(
R.id.fragment_container, optsFrag, "OPTIONS_FRAG").commit();
setTitle(R.string.settings);
return;
}
public void onMainBrowseSelected(String path_entry, boolean games) {
/**
* Launch the browser activity with specified parameters
*
* @param browse
* Conditional for image files or folders
* @param path
* The root path of the browser fragment
* @param games
* Conditional for viewing games or BIOS
* @param query
* Search parameters to limit list items
*/
public void onMainBrowseSelected(boolean browse, String path, boolean games, String query) {
FileBrowser firstFragment = new FileBrowser();
Bundle args = new Bundle();
args.putBoolean("ImgBrowse", false);
// args.putBoolean("ImgBrowse", false);
args.putBoolean("ImgBrowse", browse);
// specify ImgBrowse option. true = images, false = folders only
args.putString("browse_entry", path_entry);
args.putString("browse_entry", path);
// specify a path for selecting folder options
args.putBoolean("games_entry", games);
// specify if the desired path is for games or data
args.putString("search_params", query);
firstFragment.setArguments(args);
// In case this activity was started with special instructions from
@ -368,6 +334,7 @@ public class MainActivity extends AppCompatActivity implements
.beginTransaction()
.replace(R.id.fragment_container, firstFragment, "MAIN_BROWSER")
.addToBackStack(null).commit();
setTitle(R.string.browser);
}
@Override
@ -389,11 +356,11 @@ public class MainActivity extends AppCompatActivity implements
if (readyToQuit) {
MainActivity.this.finish();
} else {
launchMainFragment(fragment);
launchMainFragment();
}
return true;
} else {
launchMainFragment(fragment);
launchMainFragment();
return true;
}
@ -402,17 +369,8 @@ public class MainActivity extends AppCompatActivity implements
return super.onKeyDown(keyCode, event);
}
private void launchMainFragment(Fragment fragment) {
fragment = new FileBrowser();
Bundle args = new Bundle();
args.putBoolean("ImgBrowse", true);
args.putString("browse_entry", null);
args.putBoolean("games_entry", false);
fragment.setArguments(args);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragment, "MAIN_BROWSER")
.addToBackStack(null).commit();
setTitle(R.string.browser);
private void launchMainFragment() {
onMainBrowseSelected(true, null, false, null);
}
@Override

View File

@ -56,7 +56,7 @@ public class EditVJoyActivity extends Activity {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
Emulator app = (Emulator)getApplicationContext();
app.getConfigurationPrefs(EditVJoyActivity.this);
app.getConfigurationPrefs(prefs);
// Create the actual GLES view
mView = new GL2JNIView(EditVJoyActivity.this, null, false,

View File

@ -10,6 +10,7 @@ import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Vibrator;
import android.preference.PreferenceManager;
@ -41,7 +42,6 @@ import com.reicast.emulator.periph.MOGAInput;
public class InputFragment extends Fragment {
private Activity parentActivity;
private int listenForButton = 0;
private AlertDialog alertDialogSelectController;
private SharedPreferences sharedPreferences;
@ -66,16 +66,14 @@ public class InputFragment extends Fragment {
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
parentActivity = getActivity();
moga.onCreate(parentActivity, pad);
moga.onCreate(getActivity(), pad);
moga.mListener.setPlayerNum(1);
sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(parentActivity);
.getDefaultSharedPreferences(getActivity());
Config.vibrationDuration = sharedPreferences.getInt(Config.pref_vibrationDuration, 20);
vib = (Vibrator) parentActivity.getSystemService(Context.VIBRATOR_SERVICE);
vib = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ImageView icon_a = (ImageView) getView().findViewById(
@ -96,12 +94,16 @@ public class InputFragment extends Fragment {
R.id.buttonLaunchEditor);
buttonLaunchEditor.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent inte = new Intent(parentActivity, EditVJoyActivity.class);
Intent inte = new Intent(getActivity(), EditVJoyActivity.class);
startActivity(inte);
}
});
if (!MainActivity.isBiosExisting(parentActivity) || !MainActivity.isFlashExisting(parentActivity))
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String home_directory = mPrefs.getString(Config.pref_home,
Environment.getExternalStorageDirectory().getAbsolutePath());
if (!MainActivity.isBiosExisting(home_directory) || !MainActivity.isFlashExisting(home_directory))
buttonLaunchEditor.setEnabled(false);
final TextView duration = (TextView) getView().findViewById(R.id.vibDuration_current);
@ -259,7 +261,7 @@ public class InputFragment extends Fragment {
} else {
TableLayout input_devices = (TableLayout) parentActivity
TableLayout input_devices = (TableLayout) getActivity()
.findViewById(R.id.input_devices);
input_devices.setVisibility(View.GONE);
@ -391,7 +393,7 @@ public class InputFragment extends Fragment {
private void selectController(int playerNum) {
listenForButton = playerNum;
AlertDialog.Builder builder = new AlertDialog.Builder(parentActivity);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.select_controller_title);
builder.setMessage(getString(R.string.select_controller_message,
String.valueOf(listenForButton)));
@ -466,7 +468,7 @@ public class InputFragment extends Fragment {
|| descriptor.equals(deviceDescriptorPlayer2)
|| descriptor.equals(deviceDescriptorPlayer3)
|| descriptor.equals(deviceDescriptorPlayer4)) {
Toast.makeText(parentActivity, R.string.controller_already_in_use,
Toast.makeText(getActivity(), R.string.controller_already_in_use,
Toast.LENGTH_SHORT).show();
return true;
}

View File

@ -59,14 +59,14 @@ public class OptionsFragment extends Fragment {
private SharedPreferences mPrefs;
private File sdcard = Environment.getExternalStorageDirectory();
private String home_directory = sdcard.getAbsolutePath().replace("emulated/0", "sdcard0");
private String game_directory = sdcard.getAbsolutePath().replace("emulated/0", "sdcard0");
private String home_directory = sdcard.getAbsolutePath();
private String game_directory = sdcard.getAbsolutePath();
private String[] codes;
// Container Activity must implement this interface
public interface OnClickListener {
public void onMainBrowseSelected(String path_entry, boolean games);
void onMainBrowseSelected(boolean browse, String path_entry, boolean games, String query);
}
@Override
@ -109,7 +109,7 @@ public class OptionsFragment extends Fragment {
home_directory = mPrefs.getString(Config.pref_home, home_directory);
app = (Emulator) getActivity().getApplicationContext();
app.getConfigurationPrefs(getActivity());
app.getConfigurationPrefs(mPrefs);
// Generate the menu options and fill in existing settings
@ -117,15 +117,14 @@ public class OptionsFragment extends Fragment {
mSpnrThemes = (Spinner) getView().findViewById(R.id.pick_button_theme);
new LocateThemes().execute(home_directory + "/themes");
final EditText editBrowse = (EditText) getView().findViewById(
R.id.main_path);
final EditText editBrowse = (EditText) getView().findViewById(R.id.main_path);
editBrowse.setText(home_directory);
mainBrowse.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mPrefs.edit().remove(Config.pref_home).apply();
hideSoftKeyBoard();
mCallback.onMainBrowseSelected(home_directory, false);
mCallback.onMainBrowseSelected(false, home_directory, false, null);
}
});
@ -197,7 +196,7 @@ public class OptionsFragment extends Fragment {
game_directory = editGames.getText().toString();
}
hideSoftKeyBoard();
mCallback.onMainBrowseSelected(game_directory, true);
mCallback.onMainBrowseSelected(false, game_directory, true, null);
}
});

View File

@ -13,6 +13,7 @@ import android.media.AudioManager;
import android.media.AudioTrack;
import android.opengl.GLSurfaceView;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Vibrator;
import android.preference.PreferenceManager;
@ -150,6 +151,9 @@ public class GL2JNIView extends GLSurfaceView
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
JNIdc.config(prefs.getString(Config.pref_home,
Environment.getExternalStorageDirectory().getAbsolutePath()));
ethd = new EmuThread(!Emulator.nosound);
touchVibrationEnabled = prefs.getBoolean(Config.pref_touchvibe, true);
@ -178,7 +182,7 @@ public class GL2JNIView extends GLSurfaceView
// This is the game we are going to run
fileName = newFileName;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD && Emulator.nativeact) {
if (Emulator.nativeact) {
if (GL2JNINative.syms != null)
JNIdc.data(1, GL2JNINative.syms);
} else {

View File

@ -4,9 +4,9 @@
<string name="menu_settings">Settings</string>
<string name="system_path">System Path (location of the data folder with dc_boot.bin/dc_flash.bin inside)</string>
<string name="system_path">System Path\n (location of data folder with dc_boot.bin/dc_flash.bin inside)</string>
<string name="browser_path">Default System Path</string>
<string name="games_path">Storage Path (location of *.gdi, *.chd or *.cdi images)</string>
<string name="games_path">Storage Path\n (location of *.gdi, *.chd or *.cdi images)</string>
<string name="button_theme">Onscreen Button Theme</string>
<string name="bios_selection">Select a BIOS source</string>