Skip to content

Commit

Permalink
fix: App crash on opening settings activity (#1684)
Browse files Browse the repository at this point in the history
  • Loading branch information
codedsun authored and iamareebjamal committed Oct 7, 2018
1 parent 4c26c17 commit 34d8774
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ChatActivity : AppCompatActivity(), IChatView {
private var example: String = ""
private var isConfigurationChanged = false
private val enterAsSend: Boolean by lazy {
PrefManager.getBoolean(Constant.ENTER_SEND, false)
PrefManager.getBoolean(R.string.settings_enterPreference_key, false)
}

private val afChangeListener = AudioManager.OnAudioFocusChangeListener { focusChange ->
Expand Down
20 changes: 10 additions & 10 deletions app/src/main/java/org/fossasia/susi/ai/chat/ChatPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ class ChatPresenter(chatActivity: ChatActivity) : IChatPresenter, IChatModel.OnR
}

override fun checkPreferences() {
micCheck = utilModel.getBooleanPref(Constant.MIC_INPUT, true)
chatView?.checkMicPref(utilModel.getBooleanPref(Constant.MIC_INPUT, true))
chatView?.checkEnterKeyPref(utilModel.getBooleanPref(Constant.ENTER_SEND, false))
micCheck = PrefManager.getBoolean(R.string.setting_mic_key, true)
chatView?.checkMicPref(PrefManager.getBoolean(R.string.setting_mic_key, true))
chatView?.checkEnterKeyPref(PrefManager.getBoolean(R.string.settings_enterPreference_key, false))
}

override fun micCheck(): Boolean {
Expand All @@ -103,10 +103,10 @@ class ChatPresenter(chatActivity: ChatActivity) : IChatPresenter, IChatModel.OnR
chatView?.initHotword()
startHotwordDetection()
} else {
utilModel.putBooleanPref(Constant.HOTWORD_DETECTION, false)
if (utilModel.getBooleanPref(Constant.NOTIFY_USER, true)) {
utilModel.putBooleanPref(R.string.hotword_detection_key, false)
if (utilModel.getBooleanPref(R.string.notify_user_key, true)) {
chatView?.showToast(utilModel.getString(R.string.error_hotword))
utilModel.putBooleanPref(Constant.NOTIFY_USER, false)
utilModel.putBooleanPref(R.string.notify_user_key, false)
}
}
}
Expand All @@ -117,7 +117,7 @@ class ChatPresenter(chatActivity: ChatActivity) : IChatPresenter, IChatModel.OnR
}

override fun startHotwordDetection() {
if (!isDetectionOn && utilModel.getBooleanPref(Constant.HOTWORD_DETECTION, false)) {
if (!isDetectionOn && utilModel.getBooleanPref(R.string.hotword_detection_key, false)) {
chatView?.startRecording()
isDetectionOn = true
}
Expand All @@ -138,10 +138,10 @@ class ChatPresenter(chatActivity: ChatActivity) : IChatPresenter, IChatModel.OnR
override fun disableMicInput(boolean: Boolean) {
if (boolean) {
micCheck = false
PrefManager.putBoolean(Constant.MIC_INPUT, false)
PrefManager.putBoolean(R.string.setting_mic_key, false)
} else {
micCheck = utilModel.checkMicInput()
PrefManager.putBoolean(Constant.MIC_INPUT, utilModel.checkMicInput())
PrefManager.putBoolean(R.string.setting_mic_key, utilModel.checkMicInput())
chatView?.checkMicPref(micCheck)
}
}
Expand Down Expand Up @@ -543,7 +543,7 @@ class ChatPresenter(chatActivity: ChatActivity) : IChatPresenter, IChatModel.OnR
}

if (!(chatView?.checkPermission(permissionsRequired[1]) as Boolean)) {
PrefManager.putBoolean(Constant.MIC_INPUT, utilModel.checkMicInput())
PrefManager.putBoolean(R.string.setting_mic_key, utilModel.checkMicInput())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ private ResponseListener getResponseListener() {
return new ResponseListener() {
@Override
public void onData(MetaData data) {
if (!PrefManager.hasTokenExpired() || PrefManager.getBoolean(Constant.ANONYMOUS_LOGGED_IN, false)) {
if (!PrefManager.hasTokenExpired() || PrefManager.getBoolean(R.string.anonymous_logged_in_key, false)) {
realm.beginTransaction();
Realm realm = Realm.getDefaultInstance();
WebLink link = realm.createObject(WebLink.class);
Expand Down
17 changes: 12 additions & 5 deletions app/src/main/java/org/fossasia/susi/ai/data/UtilModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.Manifest
import ai.kitt.snowboy.AppResCopy
import android.content.Context
import android.os.Build
import org.fossasia.susi.ai.R
import org.fossasia.susi.ai.data.contract.IUtilModel
import org.fossasia.susi.ai.helper.*
import org.fossasia.susi.ai.rest.responses.susi.LoginResponse
Expand All @@ -24,11 +25,11 @@ class UtilModel(val context: Context) : IUtilModel {
}

override fun saveAnonymity(isAnonymous: Boolean) {
PrefManager.putBoolean(Constant.ANONYMOUS_LOGGED_IN, isAnonymous)
PrefManager.putBoolean(R.string.anonymous_logged_in_key, isAnonymous)
}

override fun getAnonymity(): Boolean {
return PrefManager.getBoolean(Constant.ANONYMOUS_LOGGED_IN, false)
return PrefManager.getBoolean(R.string.anonymous_logged_in_key, false)
}

override fun saveEmail(email: String) {
Expand All @@ -53,7 +54,7 @@ class UtilModel(val context: Context) : IUtilModel {
}

override fun setServer(isSusiServer: Boolean) {
PrefManager.putBoolean(Constant.SUSI_SERVER, isSusiServer)
PrefManager.putBoolean(R.string.susi_server_selected_key, isSusiServer)
}

override fun setCustomURL(url: String) {
Expand All @@ -64,11 +65,11 @@ class UtilModel(val context: Context) : IUtilModel {
return context.getString(id)
}

override fun getBooleanPref(prefName: String, defaultValue: Boolean): Boolean {
override fun getBooleanPref(prefName: Int, defaultValue: Boolean): Boolean {
return PrefManager.getBoolean(prefName, defaultValue)
}

override fun putBooleanPref(prefName: String, value: Boolean) {
override fun putBooleanPref(prefName: Int, value: Boolean) {
PrefManager.putBoolean(prefName, value)
}

Expand Down Expand Up @@ -98,4 +99,10 @@ class UtilModel(val context: Context) : IUtilModel {
PrefManager.clearPrefs()
}

override fun putBooleanPref(prefName: String, value: Boolean) {
}

override fun getBooleanPref(prefName: String, defaultValue: Boolean): Boolean {
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ interface IUtilModel {
fun setServer(isSusiServer: Boolean)
fun setCustomURL(url: String)
fun getString(id: Int): String
fun getBooleanPref(prefName: Int, defaultValue: Boolean): Boolean
fun getBooleanPref(prefName: String, defaultValue: Boolean): Boolean
fun putBooleanPref(prefName: String, value: Boolean)
fun putBooleanPref(prefName: Int, value: Boolean)
fun checkMicInput(): Boolean
fun copyAssetstoSD()
fun permissionsToGet(): Array<String>
Expand Down
24 changes: 14 additions & 10 deletions app/src/main/java/org/fossasia/susi/ai/helper/PrefManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.gson.Gson;

import org.fossasia.susi.ai.MainApplication;
import org.fossasia.susi.ai.R;
import org.fossasia.susi.ai.rest.clients.BaseUrl;
import org.fossasia.susi.ai.rest.responses.susi.SusiBaseUrls;

Expand All @@ -25,8 +26,7 @@ public class PrefManager {
private static Gson gson = new Gson();

private static SharedPreferences getPreferences() {
return PreferenceManager.getDefaultSharedPreferences(MainApplication.getInstance()
.getApplicationContext());
return PreferenceManager.getDefaultSharedPreferences(getContext());
}

/**
Expand Down Expand Up @@ -107,8 +107,8 @@ public static void putFloat(String preferenceKey, float preferenceValue) {
* @param preferenceDefaultValue the preference default value
* @return the boolean
*/
public static boolean getBoolean(String preferenceKey, boolean preferenceDefaultValue) {
return getPreferences().getBoolean(preferenceKey, preferenceDefaultValue);
public static boolean getBoolean(int preferenceKey, boolean preferenceDefaultValue) {
return getPreferences().getBoolean(getContext().getString(preferenceKey), preferenceDefaultValue);
}

/**
Expand All @@ -117,8 +117,8 @@ public static boolean getBoolean(String preferenceKey, boolean preferenceDefault
* @param preferenceKey the preference key
* @param preferenceValue the preference value
*/
public static void putBoolean(String preferenceKey, boolean preferenceValue) {
getPreferences().edit().putBoolean(preferenceKey, preferenceValue).apply();
public static void putBoolean(int preferenceKey, boolean preferenceValue) {
getPreferences().edit().putBoolean(getContext().getString(preferenceKey), preferenceValue).apply();
}

/**
Expand Down Expand Up @@ -169,7 +169,7 @@ public static Set<String> getStringSet(String preferencesKey) {
*/
public static String getSusiRunningBaseUrl() {

if (getBoolean("is_susi_server_selected", true)) {
if (getBoolean(R.string.susi_server_selected_key, true)) {
return getString(SUSI_RUNNING_BASE_URL, BaseUrl.SUSI_DEFAULT_BASE_URL);
}
return getString("custom_server", "null");
Expand Down Expand Up @@ -238,7 +238,7 @@ public static void clearToken() {
* @return the boolean
*/
public static boolean checkSpeechOutputPref() {
return PrefManager.getBoolean(Constant.SPEECH_OUTPUT, true);
return PrefManager.getBoolean(R.string.settings_speechPreference_key, true);
}

/**
Expand All @@ -247,7 +247,7 @@ public static boolean checkSpeechOutputPref() {
* @return the boolean
*/
public static boolean checkSpeechAlwaysPref() {
return PrefManager.getBoolean(Constant.SPEECH_ALWAYS, false);
return PrefManager.getBoolean(R.string.settings_speechAlways_key, false);
}

/**
Expand All @@ -256,7 +256,7 @@ public static boolean checkSpeechAlwaysPref() {
* @return the boolean
*/
public static boolean checkHotwordPref() {
return PrefManager.getBoolean(Constant.HOTWORD_DETECTION, false);
return PrefManager.getBoolean(R.string.hotword_detection_key, false);
}

/**
Expand All @@ -267,6 +267,10 @@ public static boolean checkHotwordPref() {
public static boolean checkMicInput(Context context) {
return MediaUtil.INSTANCE.isAvailableForVoiceInput(context);
}

private static Context getContext() {
return MainApplication.getInstance().getApplicationContext();
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class LoginActivity : AppCompatActivity(), ILoginView {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)

PrefManager.putBoolean("activity_executed", true)
PrefManager.putBoolean(R.string.activity_executed_key, true)

if (savedInstanceState != null) {
email.editText?.setText(savedInstanceState.getCharSequenceArray(Constant.SAVED_STATES)[0].toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ class LoginPresenter(loginActivity: LoginActivity) : ILoginPresenter, ILoginMode
val settings: Settings? = response.body().settings

if (settings != null) {
utilModel.putBooleanPref(Constant.ENTER_SEND, settings.enterSend)
utilModel.putBooleanPref(Constant.SPEECH_ALWAYS, settings.speechAlways)
utilModel.putBooleanPref(Constant.SPEECH_OUTPUT, settings.speechOutput)
utilModel.putBooleanPref(R.string.settings_enterPreference_key, settings.enterSend)
utilModel.putBooleanPref(R.string.settings_speechAlways_key, settings.speechAlways)
utilModel.putBooleanPref(R.string.settings_speechPreference_key, settings.speechOutput)
if (settings.language == "default") {
utilModel.setLanguage("en")
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class WelcomeActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (PrefManager.getBoolean("activity_executed", false)) {
if (PrefManager.getBoolean(R.string.activity_executed_key, false)) {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ class ChatSettingsFragment : PreferenceFragmentCompat(), ISettingsView {

rate = preferenceManager.findPreference(Constant.RATE)
server = preferenceManager.findPreference(Constant.SELECT_SERVER)
micSettings = preferenceManager.findPreference(Constant.MIC_INPUT)
micSettings = preferenceManager.findPreference(getString(R.string.setting_mic_key))
hotwordSettings = preferenceManager.findPreference(Constant.HOTWORD_DETECTION)
share = preferenceManager.findPreference(Constant.SHARE)
loginLogout = preferenceManager.findPreference(Constant.LOGIN_LOGOUT)
resetPassword = preferenceManager.findPreference(Constant.RESET_PASSWORD)
enterSend = preferenceManager.findPreference(Constant.ENTER_SEND)
speechOutput = preferenceManager.findPreference(Constant.SPEECH_OUTPUT)
speechAlways = preferenceManager.findPreference(Constant.SPEECH_ALWAYS)
enterSend = preferenceManager.findPreference(getString(R.string.settings_enterPreference_key))
speechOutput = preferenceManager.findPreference(getString(R.string.settings_speechPreference_key))
speechAlways = preferenceManager.findPreference(getString(R.string.settings_speechAlways_key))
displayEmail = preferenceManager.findPreference("display_email")
querylanguage = preferenceManager.findPreference(Constant.LANG_SELECT) as ListPreference
deviceName = preferenceManager.findPreference(Constant.DEVICE)
Expand Down Expand Up @@ -199,22 +199,22 @@ class ChatSettingsFragment : PreferenceFragmentCompat(), ISettingsView {

if (!settingsPresenter.getAnonymity()) {
micSettings.setOnPreferenceClickListener {
settingsPresenter.sendSetting(Constant.MIC_INPUT, (PrefManager.getBoolean(Constant.MIC_INPUT, false)).toString(), 1)
settingsPresenter.sendSetting(getString(R.string.setting_mic_key), (PrefManager.getBoolean(R.string.setting_mic_key, false)).toString(), 1)
true
}

enterSend.setOnPreferenceChangeListener { _, newValue ->
settingsPresenter.sendSetting(Constant.ENTER_SEND, newValue.toString(), 1)
settingsPresenter.sendSetting(getString(R.string.settings_enterPreference_key), newValue.toString(), 1)
true
}

speechAlways.setOnPreferenceChangeListener { _, newValue ->
settingsPresenter.sendSetting(Constant.SPEECH_ALWAYS, newValue.toString(), 1)
settingsPresenter.sendSetting(getString(R.string.settings_speechAlways_key), newValue.toString(), 1)
true
}

speechOutput.setOnPreferenceChangeListener { _, newValue ->
settingsPresenter.sendSetting(Constant.SPEECH_OUTPUT, newValue.toString(), 1)
settingsPresenter.sendSetting(getString(R.string.settings_speechPreference_key), newValue.toString(), 1)
true
}
}
Expand Down Expand Up @@ -251,7 +251,7 @@ class ChatSettingsFragment : PreferenceFragmentCompat(), ISettingsView {
inputUrl = promptsView?.findViewById(R.id.input_url) as TextInputLayout
val inputUrlText = promptsView.findViewById(R.id.input_url_text) as TextInputEditText
val customerServer = promptsView.findViewById(R.id.customer_server) as AppCompatCheckBox
if (PrefManager.getBoolean(Constant.SUSI_SERVER, true)) {
if (PrefManager.getBoolean(R.string.susi_server_selected_key, true)) {
inputUrl.visibility = View.GONE
flag = false
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.fossasia.susi.ai.skills.settings

import org.fossasia.susi.ai.R
import org.fossasia.susi.ai.data.SettingModel
import org.fossasia.susi.ai.data.UtilModel
import org.fossasia.susi.ai.data.contract.ISettingModel
Expand Down Expand Up @@ -35,10 +36,10 @@ class SettingsPresenter(skillsActivity: SkillsActivity) : ISettingsPresenter, IS
override fun enableMic(): Boolean {
return if ((settingView?.micPermission()) as Boolean) {
if (!utilModel.checkMicInput())
utilModel.putBooleanPref(Constant.MIC_INPUT, false)
utilModel.putBooleanPref(R.string.setting_mic_key, false)
utilModel.checkMicInput()
} else {
utilModel.putBooleanPref(Constant.MIC_INPUT, false)
utilModel.putBooleanPref(R.string.setting_mic_key, false)
false
}
}
Expand All @@ -48,11 +49,11 @@ class SettingsPresenter(skillsActivity: SkillsActivity) : ISettingsPresenter, IS
return if (utilModel.checkMicInput() && utilModel.isArmDevice()) {
true
} else {
utilModel.putBooleanPref(Constant.HOTWORD_DETECTION, false)
utilModel.putBooleanPref(R.string.hotword_detection_key, false)
false
}
} else {
utilModel.putBooleanPref(Constant.HOTWORD_DETECTION, false)
utilModel.putBooleanPref(R.string.hotword_detection_key, false)
false
}
}
Expand Down Expand Up @@ -140,7 +141,7 @@ class SettingsPresenter(skillsActivity: SkillsActivity) : ISettingsPresenter, IS
return
}
} else {
utilModel.putBooleanPref(Constant.SUSI_SERVER, true)
utilModel.putBooleanPref(R.string.susi_server_selected_key, true)
}
settingView?.setServerSuccessful()
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
<string name="password_invalid">Das angegebene Passwort ist nicht korrek. Bitte versuche es erneut.</string>
<string name="forgot_password_mail_sent">E-Mail gesendet</string>

<string name="activity_executed_key" translatable="false">activity_executed</string>
<string name="anonymous_logged_in_key" translatable="false">anonymous_logged_in</string>
<string name="susi_server_selected_key" translatable="false">is_susi_server_selected</string>
<string name="hotword_detection_key" translatable="false">hotword_detection</string>
<string name="notify_user_key" translatable="false">notify_user</string>

<string name="websearchnull">Keine Ergebnisse gefunden</string>
<string name="error_email">Diese E-Mail Adresse ist bereits vergeben</string>
<string name="error_msg">Bitte verwenden Sie die vergessen Passwort, wenn Sie sich nicht an Ihr Passwort erinnern</string>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
<string name="hotword_success">Hotword detectado</string>
<string name="custom_server">servidor personalizado</string>

<string name="activity_executed_key" translatable="false">activity_executed</string>
<string name="anonymous_logged_in_key" translatable="false">anonymous_logged_in</string>
<string name="susi_server_selected_key" translatable="false">is_susi_server_selected</string>
<string name="hotword_detection_key" translatable="false">hotword_detection</string>
<string name="notify_user_key" translatable="false">notify_user</string>

<string name="signup">Gracias por registrarte</string>
<string name="signup_msg">Hemos enviado un correo electrónico a la dirección que proporcionó. Por favor, utilice el
Enlace de confirmación para activar su cuenta e iniciar sesión en susi.</string>
Expand Down
Loading

0 comments on commit 34d8774

Please sign in to comment.