diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/activity/MainActivity.java b/ultrasonic/src/main/java/org/moire/ultrasonic/activity/MainActivity.java index 3c1d9427..87722486 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/activity/MainActivity.java +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/activity/MainActivity.java @@ -69,6 +69,14 @@ public class MainActivity extends SubsonicTabActivity { super.onCreate(savedInstanceState); + // Determine first run and migrate server settings to DB as early as possible + boolean showWelcomeScreen = Util.isFirstRun(this); + if (showWelcomeScreen) + { + // If settings were migrated, do not show welcome screen + showWelcomeScreen = !serverSettingsModel.getValue().migrateFromPreferences(); + } + if (getIntent().hasExtra(Constants.INTENT_EXTRA_NAME_EXIT)) { setResult(Constants.RESULT_CLOSE_ALL); @@ -230,10 +238,7 @@ public class MainActivity extends SubsonicTabActivity // Remember the current theme. theme = Util.getTheme(this); - boolean shouldShowDialog = Util.shouldShowWelcomeScreen(this); - // This will convert the server settings from the Preferences to the DB - if (shouldShowDialog) serverSettingsModel.getValue().getServerList(); - showInfoDialog(shouldShowDialog); + showInfoDialog(showWelcomeScreen); } private void loadSettings() diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/util/Constants.java b/ultrasonic/src/main/java/org/moire/ultrasonic/util/Constants.java index b1a83133..4008f7b5 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/util/Constants.java +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/util/Constants.java @@ -130,7 +130,7 @@ public final class Constants public static final String PREFERENCES_KEY_FF_IMAGE_LOADER = "ff_new_image_loader"; public static final String PREFERENCES_KEY_USE_FIVE_STAR_RATING = "use_five_star_rating"; public static final String PREFERENCES_KEY_CATEGORY_NOTIFICATIONS = "notificationsCategory"; - public static final String PREFERENCES_KEY_WELCOME_SCREEN_SHOWN = "welcomeScreenShown"; + public static final String PREFERENCES_KEY_FIRST_RUN_EXECUTED = "firstRunExecuted"; // Number of free trial days for non-licensed servers. public static final int FREE_TRIAL_DAYS = 30; diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/util/Util.java b/ultrasonic/src/main/java/org/moire/ultrasonic/util/Util.java index f880787c..c6c1525d 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/util/Util.java +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/util/Util.java @@ -1437,13 +1437,13 @@ public class Util return typedValue.resourceId; } - public static boolean shouldShowWelcomeScreen(Context context) + public static boolean isFirstRun(Context context) { SharedPreferences preferences = getPreferences(context); - boolean shown = preferences.getBoolean(Constants.PREFERENCES_KEY_WELCOME_SCREEN_SHOWN, false); - if (shown) return false; + boolean firstExecuted = preferences.getBoolean(Constants.PREFERENCES_KEY_FIRST_RUN_EXECUTED, false); + if (firstExecuted) return false; SharedPreferences.Editor editor = preferences.edit(); - editor.putBoolean(Constants.PREFERENCES_KEY_WELCOME_SCREEN_SHOWN, true); + editor.putBoolean(Constants.PREFERENCES_KEY_FIRST_RUN_EXECUTED, true); editor.apply(); return true; } diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/activity/EditServerActivity.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/activity/EditServerActivity.kt index cd77c6e6..354e6d8c 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/activity/EditServerActivity.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/activity/EditServerActivity.kt @@ -97,8 +97,8 @@ internal class EditServerActivity : AppCompatActivity() { } else { // Creating a new server setTitle(R.string.server_editor_new_label) + currentServerSetting = ServerSetting() saveButton!!.setOnClickListener { - currentServerSetting = ServerSetting() if (getFields()) { serverSettingsModel.saveNewItem(currentServerSetting) finish() @@ -115,27 +115,16 @@ internal class EditServerActivity : AppCompatActivity() { override fun onOptionsItemSelected(item: MenuItem): Boolean { if (item.itemId == android.R.id.home) { - if (areFieldsChanged()) { - AlertDialog.Builder(this) - .setIcon(android.R.drawable.ic_dialog_alert) - .setTitle(R.string.common_confirm) - .setMessage(R.string.server_editor_leave_confirmation) - .setPositiveButton(R.string.common_ok) { dialog, _ -> - dialog.dismiss() - finish() - } - .setNegativeButton(R.string.common_cancel) { dialog, _ -> - dialog.dismiss() - } - .show() - } else { - finish() - } + finishActivity() return true } return super.onOptionsItemSelected(item) } + override fun onBackPressed() { + finishActivity() + } + private fun applyTheme() { val theme = Util.getTheme(this) if ( @@ -232,7 +221,7 @@ internal class EditServerActivity : AppCompatActivity() { private fun areFieldsChanged(): Boolean { if (currentServerSetting == null) { return !serverNameEditText!!.editText?.text!!.isBlank() || - !serverAddressEditText!!.editText?.text!!.isBlank() || + serverAddressEditText!!.editText?.text.toString() != "http://" || !userNameEditText!!.editText?.text!!.isBlank() || !passwordEditText!!.editText?.text!!.isBlank() } @@ -324,4 +313,26 @@ internal class EditServerActivity : AppCompatActivity() { throw IOException("Failed to perform request: " + response.code()) } } + + /** + * Finishes the Activity, after confirmation from the user if needed + */ + private fun finishActivity() { + if (areFieldsChanged()) { + AlertDialog.Builder(this) + .setIcon(android.R.drawable.ic_dialog_alert) + .setTitle(R.string.common_confirm) + .setMessage(R.string.server_editor_leave_confirmation) + .setPositiveButton(R.string.common_ok) { dialog, _ -> + dialog.dismiss() + finish() + } + .setNegativeButton(R.string.common_cancel) { dialog, _ -> + dialog.dismiss() + } + .show() + } else { + finish() + } + } } diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/activity/ServerSettingsModel.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/activity/ServerSettingsModel.kt index 0c7fbca9..1c5c84c0 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/activity/ServerSettingsModel.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/activity/ServerSettingsModel.kt @@ -9,6 +9,7 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking import org.moire.ultrasonic.R import org.moire.ultrasonic.data.ActiveServerProvider import org.moire.ultrasonic.data.ServerSetting @@ -39,12 +40,13 @@ class ServerSettingsModel( } /** - * Retrieves the list of the configured servers from the database. - * This function will also try to convert existing settings from the Preferences - * This function is asynchronous, uses LiveData to provide the Setting. + * This function will try and convert settings from the Preferences to the Database + * @return True, if the migration was executed, False otherwise */ - fun getServerList(): LiveData> { - viewModelScope.launch { + fun migrateFromPreferences(): Boolean { + var migrated = true + + runBlocking { val dbServerList = repository.loadAllServerSettings().toMutableList() if (dbServerList.isEmpty()) { @@ -55,15 +57,32 @@ class ServerSettingsModel( if (serverNum != 0) { for (x in 1 until serverNum + 1) { val newServerSetting = loadServerSettingFromPreferences(x, settings) - dbServerList.add(newServerSetting) - repository.insert(newServerSetting) - Log.i( - TAG, - "Imported server from Preferences to Database: ${newServerSetting.name}" - ) + if (newServerSetting != null) { + dbServerList.add(newServerSetting) + repository.insert(newServerSetting) + Log.i( + TAG, + "Imported server from Preferences to Database:" + + " ${newServerSetting.name}" + ) + } } + } else { + migrated = false } } + } + + return migrated + } + + /** + * Retrieves the list of the configured servers from the database. + * This function is asynchronous, uses LiveData to provide the Setting. + */ + fun getServerList(): LiveData> { + viewModelScope.launch { + val dbServerList = repository.loadAllServerSettings().toMutableList() dbServerList.add(0, ServerSetting(context.getString(R.string.main_offline), "")) serverList.value = dbServerList @@ -186,13 +205,18 @@ class ServerSettingsModel( private fun loadServerSettingFromPreferences( id: Int, settings: SharedPreferences - ): ServerSetting { + ): ServerSetting? { + val url = settings.getString(PREFERENCES_KEY_SERVER_URL + id, "") + val userName = settings.getString(PREFERENCES_KEY_USERNAME + id, "") + + if (url.isNullOrEmpty() || userName.isNullOrEmpty()) return null + return ServerSetting( id, id, settings.getString(PREFERENCES_KEY_SERVER_NAME + id, "")!!, - settings.getString(PREFERENCES_KEY_SERVER_URL + id, "")!!, - settings.getString(PREFERENCES_KEY_USERNAME + id, "")!!, + url, + userName, settings.getString(PREFERENCES_KEY_PASSWORD + id, "")!!, settings.getBoolean(PREFERENCES_KEY_JUKEBOX_BY_DEFAULT + id, false), settings.getBoolean(PREFERENCES_KEY_ALLOW_SELF_SIGNED_CERTIFICATE + id, false), diff --git a/ultrasonic/src/main/res/layout/server_edit.xml b/ultrasonic/src/main/res/layout/server_edit.xml index 36c6e7ba..a8f32ccc 100644 --- a/ultrasonic/src/main/res/layout/server_edit.xml +++ b/ultrasonic/src/main/res/layout/server_edit.xml @@ -41,6 +41,7 @@