From 0250c5cfe2cda05b8e678ece7b8a9ff34112db3a Mon Sep 17 00:00:00 2001 From: dgis Date: Mon, 11 Feb 2019 22:32:12 +0000 Subject: [PATCH] --- app/src/main/cpp/emu48-jni.c | 38 +++- app/src/main/cpp/win32-layer.c | 2 +- app/src/main/cpp/win32-layer.h | 3 +- .../emulator/forty/eight/MainActivity.java | 168 ++++++++++++------ .../org/emulator/forty/eight/NativeLib.java | 2 +- .../forty/eight/SettingsActivity.java | 32 ++-- 6 files changed, 164 insertions(+), 81 deletions(-) diff --git a/app/src/main/cpp/emu48-jni.c b/app/src/main/cpp/emu48-jni.c index e88ac8a..75c19ed 100644 --- a/app/src/main/cpp/emu48-jni.c +++ b/app/src/main/cpp/emu48-jni.c @@ -21,6 +21,7 @@ AndroidBitmapInfo androidBitmapInfo; enum ChooseKmlMode chooseCurrentKmlMode; TCHAR szChosenCurrentKml[MAX_PATH]; TCHAR szKmlLog[10240]; +TCHAR szKmlLogBackup[10240]; TCHAR szKmlTitle[10240]; BOOL settingsPort2en; BOOL settingsPort2wr; @@ -826,23 +827,25 @@ JNIEXPORT void JNICALL Java_org_emulator_forty_eight_NativeLib_onViewReset(JNIEn SwitchToState(SM_RUN); } -JNIEXPORT void JNICALL Java_org_emulator_forty_eight_NativeLib_onViewScript(JNIEnv *env, jobject thisz, jstring kmlFilename) { +JNIEXPORT int JNICALL Java_org_emulator_forty_eight_NativeLib_onViewScript(JNIEnv *env, jobject thisz, jstring kmlFilename) { TCHAR szKmlFile[MAX_PATH]; - BOOL bKMLChanged,bSucc; +// BOOL bKMLChanged,bSucc; BYTE cType = cCurrentRomType; SwitchToState(SM_INVALID); - const char *filenameUTF8 = (*env)->GetStringUTFChars(env, kmlFilename , NULL) ; - _tcscpy(szCurrentKml, filenameUTF8); - (*env)->ReleaseStringUTFChars(env, kmlFilename, filenameUTF8); - // make a copy of the current KML script file name _ASSERT(sizeof(szKmlFile) == sizeof(szCurrentKml)); lstrcpyn(szKmlFile,szCurrentKml,ARRAYSIZEOF(szKmlFile)); - bKMLChanged = FALSE; // KML script not changed - bSucc = TRUE; // KML script successful loaded + const char *filenameUTF8 = (*env)->GetStringUTFChars(env, kmlFilename , NULL) ; + _tcscpy(szCurrentKml, filenameUTF8); + (*env)->ReleaseStringUTFChars(env, kmlFilename, filenameUTF8); + +// bKMLChanged = FALSE; // KML script not changed + BOOL bSucc = TRUE; // KML script successful loaded + + chooseCurrentKmlMode = ChooseKmlMode_CHANGE_KML; // do // { @@ -866,12 +869,27 @@ JNIEXPORT void JNICALL Java_org_emulator_forty_eight_NativeLib_onViewScript(JNIE // } // else // quit with Ok // { - bKMLChanged = TRUE; // KML script changed +// bKMLChanged = TRUE; // KML script changed bSucc = InitKML(szCurrentKml,FALSE); // } // } // while (!bSucc); // retry if KML script is invalid + BOOL result = bSucc; + + if(!bSucc) { + // restore KML script file name + lstrcpyn(szCurrentKml,szKmlFile,ARRAYSIZEOF(szCurrentKml)); + + _tcsncpy(szKmlLogBackup, szKmlLog, sizeof(szKmlLog) / sizeof(TCHAR)); + + // try to restore old KML script + bSucc = InitKML(szCurrentKml,FALSE); + + _tcsncpy(szKmlLog, szKmlLogBackup, sizeof(szKmlLog) / sizeof(TCHAR)); + } + chooseCurrentKmlMode = ChooseKmlMode_UNKNOWN; + if (bSucc) { if (Chipset.wRomCrc != wRomCrc) // ROM changed @@ -890,6 +908,8 @@ JNIEXPORT void JNICALL Java_org_emulator_forty_eight_NativeLib_onViewScript(JNIE } mainViewResizeCallback(nBackgroundW, nBackgroundH); draw(); + + return result; } JNIEXPORT void JNICALL Java_org_emulator_forty_eight_NativeLib_onBackupSave(JNIEnv *env, jobject thisz) { diff --git a/app/src/main/cpp/win32-layer.c b/app/src/main/cpp/win32-layer.c index 60cbb45..1a19d47 100644 --- a/app/src/main/cpp/win32-layer.c +++ b/app/src/main/cpp/win32-layer.c @@ -97,7 +97,7 @@ HANDLE CreateFile(LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, TCHAR * foundDocumentScheme = _tcsstr(lpFileName, documentScheme); - if(chooseCurrentKmlMode == ChooseKmlMode_FILE_OPEN) { + if(chooseCurrentKmlMode == ChooseKmlMode_FILE_OPEN || chooseCurrentKmlMode == ChooseKmlMode_CHANGE_KML) { // When we open a new E48 state document TCHAR * fileExtension = _tcsrchr(lpFileName, _T('.')); if(fileExtension && ((fileExtension[1] == 'K' && fileExtension[2] == 'M' && fileExtension[3] == 'L') || diff --git a/app/src/main/cpp/win32-layer.h b/app/src/main/cpp/win32-layer.h index 3073509..95fbc00 100644 --- a/app/src/main/cpp/win32-layer.h +++ b/app/src/main/cpp/win32-layer.h @@ -1128,7 +1128,8 @@ extern TCHAR szChosenCurrentKml[MAX_PATH]; enum ChooseKmlMode { ChooseKmlMode_UNKNOWN, ChooseKmlMode_FILE_NEW, - ChooseKmlMode_FILE_OPEN + ChooseKmlMode_FILE_OPEN, + ChooseKmlMode_CHANGE_KML }; extern enum ChooseKmlMode chooseCurrentKmlMode; BOOL getFirstKMLFilenameForType(BYTE chipsetType, TCHAR * firstKMLFilename, size_t firstKMLFilenameSize); diff --git a/app/src/main/java/org/emulator/forty/eight/MainActivity.java b/app/src/main/java/org/emulator/forty/eight/MainActivity.java index a8b7a31..d211186 100644 --- a/app/src/main/java/org/emulator/forty/eight/MainActivity.java +++ b/app/src/main/java/org/emulator/forty/eight/MainActivity.java @@ -66,8 +66,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On public static final int INTENT_OBJECT_SAVE = 4; public static final int INTENT_SETTINGS = 5; public static final int INTENT_PORT2LOAD = 6; - public static final int INTENT_PICK_KML_FOLDER = 7; - //public static final int INTENT_PICK_KML_FILE = 8; + public static final int INTENT_PICK_KML_FOLDER_FOR_NEW_FILE = 7; + public static final int INTENT_PICK_KML_FOLDER_FOR_CHANGING = 8; + public static final int INTENT_PICK_KML_FOLDER_FOR_SETTINGS = 9; + //public static final int INTENT_PICK_KML_FILE = 10; public static MainActivity mainActivity; @@ -630,44 +632,52 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On // By default Port1 is set setPort1Settings(true, true); - extractKMLScripts(); - ensureDocumentSaved(new Runnable() { @Override public void run() { - final int lastIndex = kmlScripts.size(); - final String[] kmlScriptTitles = new String[lastIndex + 2]; - for (int i = 0; i < kmlScripts.size(); i++) - kmlScriptTitles[i] = kmlScripts.get(i).title; - kmlScriptTitles[lastIndex] = getResources().getString(R.string.load_custom_kml); - kmlScriptTitles[lastIndex + 1] = getResources().getString(R.string.load_default_kml); - new AlertDialog.Builder(MainActivity.this) - .setTitle(getResources().getString(R.string.pick_calculator)) - .setItems(kmlScriptTitles, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - if(which == lastIndex) { -// Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); -// intent.addCategory(Intent.CATEGORY_OPENABLE); -// intent.setType(kmlMimeType); -// intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); -// startActivityForResult(intent, INTENT_PICK_KML_FILE); - Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); - startActivityForResult(intent, INTENT_PICK_KML_FOLDER); - } else if(which == lastIndex + 1) { - // Reset to default KML folder - SharedPreferences.Editor editor = sharedPreferences.edit(); - editor.putBoolean("settings_kml_default", true); - //editor.putString("settings_kml_folder", url); - editor.apply(); - OnFileNew(); - } else { - String kmlScriptFilename = kmlScripts.get(which).filename; - newFileFromKML(kmlScriptFilename); - } - } - }).show(); +// extractKMLScripts(); +// +// final ArrayList kmlScriptsForCurrentModel = kmlScripts; +// +// final int lastIndex = kmlScriptsForCurrentModel.size(); +// final String[] kmlScriptTitles = new String[lastIndex + 2]; +// for (int i = 0; i < kmlScriptsForCurrentModel.size(); i++) +// kmlScriptTitles[i] = kmlScriptsForCurrentModel.get(i).title; +// kmlScriptTitles[lastIndex] = getResources().getString(R.string.load_custom_kml); +// kmlScriptTitles[lastIndex + 1] = getResources().getString(R.string.load_default_kml); +// new AlertDialog.Builder(MainActivity.this) +// .setTitle(getResources().getString(R.string.pick_calculator)) +// .setItems(kmlScriptTitles, new DialogInterface.OnClickListener() { +// @Override +// public void onClick(DialogInterface dialog, int which) { +// if(which == lastIndex) { +//// Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); +//// intent.addCategory(Intent.CATEGORY_OPENABLE); +//// intent.setType(kmlMimeType); +//// intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); +//// startActivityForResult(intent, INTENT_PICK_KML_FILE); +// +// Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); +// Bundle bundle = new Bundle(); +// bundle.putString("mode", "new-kml"); +// intent.putExtras(bundle); +// startActivityForResult(intent, INTENT_PICK_KML_FOLDER); +// } else if(which == lastIndex + 1) { +// // Reset to default KML folder +// SharedPreferences.Editor editor = sharedPreferences.edit(); +// editor.putBoolean("settings_kml_default", true); +// //editor.putString("settings_kml_folder", url); +// editor.apply(); +// OnFileNew(); +// //OnViewScript(); +// } else { +// String kmlScriptFilename = kmlScriptsForCurrentModel.get(which).filename; +// newFileFromKML(kmlScriptFilename); +// } +// } +// }).show(); + showKMLPicker(false); } }); } @@ -842,38 +852,71 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On } private void OnViewScript() { - extractKMLScripts(); - - if (NativeLib.getState() != 0 /*SM_RUN*/) - { + if (NativeLib.getState() != 0 /*SM_RUN*/) { showAlert("You cannot change the KML script when Emu48 is not running.\n" + "Use the File,New menu item to create a new calculator."); return; } - final ArrayList kmlScriptsForCurrentModel = new ArrayList<>(); - char m = (char)NativeLib.getCurrentModel(); - for (int i = 0; i < kmlScripts.size(); i++) { - KMLScriptItem kmlScriptItem = kmlScripts.get(i); - if (kmlScriptItem.model.charAt(0) == m) - kmlScriptsForCurrentModel.add(kmlScriptItem); - } - final String[] kmlScriptTitles = new String[kmlScriptsForCurrentModel.size()]; + showKMLPicker(true); + } + + private void showKMLPicker(final boolean changeKML) { + extractKMLScripts(); + + final ArrayList kmlScriptsForCurrentModel; + if(changeKML) { + kmlScriptsForCurrentModel = new ArrayList(); + char m = (char) NativeLib.getCurrentModel(); + for (int i = 0; i < kmlScripts.size(); i++) { + KMLScriptItem kmlScriptItem = kmlScripts.get(i); + if (kmlScriptItem.model.charAt(0) == m) + kmlScriptsForCurrentModel.add(kmlScriptItem); + } + } else + kmlScriptsForCurrentModel = kmlScripts; + + final int lastIndex = kmlScriptsForCurrentModel.size(); + final String[] kmlScriptTitles = new String[lastIndex + 2]; for (int i = 0; i < kmlScriptsForCurrentModel.size(); i++) kmlScriptTitles[i] = kmlScriptsForCurrentModel.get(i).title; + kmlScriptTitles[lastIndex] = getResources().getString(R.string.load_custom_kml); + kmlScriptTitles[lastIndex + 1] = getResources().getString(R.string.load_default_kml); new AlertDialog.Builder(MainActivity.this) - .setTitle("Pick a calculator") + .setTitle(getResources().getString(R.string.pick_calculator)) .setItems(kmlScriptTitles, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - String kmlScriptFilename = kmlScriptsForCurrentModel.get(which).filename; - NativeLib.onViewScript(kmlScriptFilename); - displayKMLTitle(); - showKMLLog(); - updateNavigationDrawerItems(); + if(which == lastIndex) { + Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); + startActivityForResult(intent, changeKML ? INTENT_PICK_KML_FOLDER_FOR_CHANGING : INTENT_PICK_KML_FOLDER_FOR_NEW_FILE); + } else if(which == lastIndex + 1) { + // Reset to default KML folder + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putBoolean("settings_kml_default", true); + //editor.putString("settings_kml_folder", url); + editor.apply(); + if(changeKML) + OnViewScript(); + else + OnFileNew(); + } else { + String kmlScriptFilename = kmlScriptsForCurrentModel.get(which).filename; + if(changeKML) { + int result = NativeLib.onViewScript(kmlScriptFilename); + if(result > 0) { + displayKMLTitle(); + showKMLLog(); + } else + showKMLLogForce(); + updateNavigationDrawerItems(); + } else + newFileFromKML(kmlScriptFilename); + } } }).show(); } + private void OnTopics() { startActivity(new Intent(this, InfoWebActivity.class)); } @@ -930,14 +973,27 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On // DocumentFile parentDocumentFile = documentFile.getParentFile(); // break; // } - case INTENT_PICK_KML_FOLDER: { + case INTENT_PICK_KML_FOLDER_FOR_NEW_FILE: + case INTENT_PICK_KML_FOLDER_FOR_CHANGING: + case INTENT_PICK_KML_FOLDER_FOR_SETTINGS: { Log.d(TAG, "onActivityResult INTENT_PICK_KML_FOLDER " + url); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean("settings_kml_default", false); editor.putString("settings_kml_folder", url); editor.apply(); makeUriPersistableReadOnly(data, uri); - OnFileNew(); + + switch (requestCode) { + case INTENT_PICK_KML_FOLDER_FOR_NEW_FILE: + OnFileNew(); + break; + case INTENT_PICK_KML_FOLDER_FOR_CHANGING: + OnViewScript(); + break; + case INTENT_PICK_KML_FOLDER_FOR_SETTINGS: + + break; + } break; } default: diff --git a/app/src/main/java/org/emulator/forty/eight/NativeLib.java b/app/src/main/java/org/emulator/forty/eight/NativeLib.java index 998c823..9412262 100644 --- a/app/src/main/java/org/emulator/forty/eight/NativeLib.java +++ b/app/src/main/java/org/emulator/forty/eight/NativeLib.java @@ -38,7 +38,7 @@ public class NativeLib { public static native void onStackCopy(); public static native void onStackPaste(); public static native void onViewReset(); - public static native void onViewScript(String kmlFilename); + public static native int onViewScript(String kmlFilename); public static native void onBackupSave(); public static native void onBackupRestore(); public static native void onBackupDelete(); diff --git a/app/src/main/java/org/emulator/forty/eight/SettingsActivity.java b/app/src/main/java/org/emulator/forty/eight/SettingsActivity.java index 0378e29..9bf125d 100644 --- a/app/src/main/java/org/emulator/forty/eight/SettingsActivity.java +++ b/app/src/main/java/org/emulator/forty/eight/SettingsActivity.java @@ -305,7 +305,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity { @Override public boolean onPreferenceClick(Preference preference) { Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); - startActivityForResult(intent, MainActivity.INTENT_PICK_KML_FOLDER); + startActivityForResult(intent, MainActivity.INTENT_PICK_KML_FOLDER_FOR_SETTINGS); return true; } }); @@ -413,22 +413,28 @@ public class SettingsActivity extends AppCompatPreferenceActivity { @Override public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { - if(resultCode == Activity.RESULT_OK) { + if(resultCode == Activity.RESULT_OK && data != null) { if(requestCode == MainActivity.INTENT_PORT2LOAD) { Uri uri = data.getData(); //Log.d(TAG, "onActivityResult INTENT_PORT2LOAD " + uri.toString()); - String url = uri.toString(); - SharedPreferences.Editor editor = sharedPreferences.edit(); - editor.putString("settings_port2load", url); - editor.apply(); - makeUriPersistable(data, uri); - } else if(requestCode == MainActivity.INTENT_PICK_KML_FOLDER) { + String url = null; + if (uri != null) { + url = uri.toString(); + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putString("settings_port2load", url); + editor.apply(); + makeUriPersistable(data, uri); + } + } else if(requestCode == MainActivity.INTENT_PICK_KML_FOLDER_FOR_SETTINGS) { Uri uri = data.getData(); - String url = uri.toString(); - SharedPreferences.Editor editor = sharedPreferences.edit(); - editor.putString("settings_kml_folder", url); - editor.apply(); - makeUriPersistableReadOnly(data, uri); + String url = null; + if (uri != null) { + url = uri.toString(); + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putString("settings_kml_folder", url); + editor.apply(); + makeUriPersistableReadOnly(data, uri); + } } } super.onActivityResult(requestCode, resultCode, data);