Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SettingsAdapter.kt more null pointer exception resistant #1

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,27 +138,23 @@ class SettingsAdapter(
}

override fun onBindViewHolder(holder: SettingViewHolder, position: Int) {
holder.bind(getItem(position))
getItem(position)?.let { holder.bind(it) }
}

private fun getItem(position: Int): SettingsItem {
return settings!![position]
private fun getItem(position: Int): SettingsItem? {
return settings?.get(position)
}

override fun getItemCount(): Int {
return if (settings != null) {
settings!!.size
} else {
0
}
return settings?.size ?: 0
}

override fun getItemViewType(position: Int): Int {
return getItem(position).type
return getItem(position)?.type ?: -1
}

fun setSettingsList(settings: ArrayList<SettingsItem>?) {
this.settings = settings
this.settings = settings ?: arrayListOf()
notifyDataSetChanged()
}

Expand All @@ -184,10 +180,12 @@ class SettingsAdapter(

private fun onStringSingleChoiceClick(item: StringSingleChoiceSetting) {
clickedItem = item
dialog = MaterialAlertDialogBuilder(context)
.setTitle(item.nameId)
.setSingleChoiceItems(item.choices, item.selectValueIndex, this)
.show()
dialog = context?.let {
MaterialAlertDialogBuilder(it)
.setTitle(item.nameId)
.setSingleChoiceItems(item.choices, item.selectValueIndex, this)
.show()
}
}

fun onStringSingleChoiceClick(item: StringSingleChoiceSetting, position: Int) {
Expand Down Expand Up @@ -233,10 +231,10 @@ class SettingsAdapter(
.build()

datePicker.addOnPositiveButtonClickListener {
timePicker.show(
(fragmentView.activityView as AppCompatActivity).supportFragmentManager,
"TimePicker"
)
val activity = fragmentView.activityView as? AppCompatActivity
activity?.supportFragmentManager?.let { fragmentManager ->
timePicker.show(fragmentManager, "TimePicker")
}
}
timePicker.addOnPositiveButtonClickListener {
var epochTime: Long = datePicker.selection!! / 1000
Expand Down Expand Up @@ -268,28 +266,31 @@ class SettingsAdapter(
textInputLayout = sliderBinding.textInput
textSliderValue = sliderBinding.textValue
if (item.setting is FloatSetting) {
textSliderValue!!.setInputType(InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL);
textSliderValue!!.setText(sliderProgress.toString())
textSliderValue?.let {
it.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL
it.setText(sliderProgress.toString())
}
} else {
textSliderValue?.setText(sliderProgress.roundToInt().toString())
}
else
textSliderValue!!.setText(sliderProgress.roundToInt().toString())
textInputLayout!!.suffixText = item.units

textInputLayout?.suffixText = item.units

sliderBinding.slider.apply {
valueFrom = item.min.toFloat()
valueTo = item.max.toFloat()
value = sliderProgress
textSliderValue!!.addTextChangedListener(object : TextWatcher {
textSliderValue?.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable) {

var textValue = s.toString().toFloatOrNull();
if (item.setting !is FloatSetting) {
textValue = textValue!!.roundToInt().toFloat();
textValue = textValue?.roundToInt()?.toFloat();
}
if (textValue == null || textValue < valueFrom || textValue > valueTo) {
textInputLayout!!.error = "Inappropriate value"
textInputLayout?.error = "Inappropriate value"
} else {
textInputLayout!!.error = null
textInputLayout?.error = null
value = textValue
}
}
Expand All @@ -304,16 +305,16 @@ class SettingsAdapter(
var sliderString = sliderProgress.toString()
if (item.setting !is FloatSetting) {
sliderString = sliderProgress.roundToInt().toString()
if (textSliderValue!!.text.toString() != sliderString) {
textSliderValue!!.setText(sliderString)
textSliderValue!!.setSelection(textSliderValue!!.length())
if (textSliderValue?.text.toString() != sliderString) {
textSliderValue?.setText(sliderString)
textSliderValue?.setSelection(textSliderValue?.length() ?: 0 )
}
} else {
val currentText = textSliderValue!!.text.toString()
val currentText = textSliderValue?.text.toString()
val currentTextValue = currentText.toFloat()
if (currentTextValue != sliderProgress) {
textSliderValue!!.setText(sliderString)
textSliderValue!!.setSelection(textSliderValue!!.length())
textSliderValue?.setText(sliderString)
textSliderValue?.setSelection(textSliderValue?.length() ?: 0 )
}
}
}
Expand All @@ -325,14 +326,14 @@ class SettingsAdapter(
.setPositiveButton(android.R.string.ok, this)
.setNegativeButton(android.R.string.cancel, defaultCancelListener)
.setNeutralButton(R.string.slider_default) { dialog: DialogInterface, which: Int ->
sliderBinding.slider.value = when (item.setting) {
sliderBinding.slider?.value = when (item.setting) {
is ScaledFloatSetting -> {
val scaledSetting = item.setting as ScaledFloatSetting
scaledSetting.defaultValue * scaledSetting.scale
}

is FloatSetting -> (item.setting as FloatSetting).defaultValue
else -> item.defaultValue!!
else -> item.defaultValue ?: 0f
}
onClick(dialog, which)
}
Expand Down Expand Up @@ -383,81 +384,85 @@ class SettingsAdapter(
override fun onClick(dialog: DialogInterface, which: Int) {
when (clickedItem) {
is SingleChoiceSetting -> {
val scSetting = clickedItem as SingleChoiceSetting
val setting = when (scSetting.setting) {
is AbstractIntSetting -> {
val value = getValueForSingleChoiceSelection(scSetting, which)
if (scSetting.selectedValue != value) {
fragmentView.onSettingChanged()
val scSetting = clickedItem as? SingleChoiceSetting
scSetting?.let {
val setting = when (it.setting) {
is AbstractIntSetting -> {
val value = getValueForSingleChoiceSelection(it, which)
if (it.selectedValue != value) {
fragmentView?.onSettingChanged()
}
scSetting.setSelectedValue(value)
}

is AbstractShortSetting -> {
val value = getValueForSingleChoiceSelection(scSetting, which).toShort()
if (scSetting.selectedValue.toShort() != value) {
fragmentView.onSettingChanged()
it.setSelectedValue(value)
}
is AbstractShortSetting -> {
val value = getValueForSingleChoiceSelection(it, which).toShort()
if (it.selectedValue.toShort() != value) {
fragmentView?.onSettingChanged()
}
it.setSelectedValue(value)
}
scSetting.setSelectedValue(value)
else -> throw IllegalStateException("Unrecognized type used for SingleChoiceSetting!")
}

else -> throw IllegalStateException("Unrecognized type used for SingleChoiceSetting!")
fragmentView?.putSetting(setting)
closeDialog()
}

fragmentView.putSetting(setting)
closeDialog()
}

is StringSingleChoiceSetting -> {
val scSetting = clickedItem as StringSingleChoiceSetting
val setting = when (scSetting.setting) {
is AbstractStringSetting -> {
val value = scSetting.getValueAt(which)
if (scSetting.selectedValue != value) fragmentView.onSettingChanged()
scSetting.setSelectedValue(value!!)
}
val scSetting = clickedItem as? StringSingleChoiceSetting
scSetting?.let {
val setting = when (it.setting) {
is AbstractStringSetting -> {
val value = it.getValueAt(which)
if (it.selectedValue != value) fragmentView?.onSettingChanged()
it.setSelectedValue(value ?: "")
}

is AbstractShortSetting -> {
if (scSetting.selectValueIndex != which) fragmentView.onSettingChanged()
scSetting.setSelectedValue(scSetting.getValueAt(which)?.toShort() ?: 1)
is AbstractShortSetting -> {
if (it.selectValueIndex != which) fragmentView?.onSettingChanged()
it.setSelectedValue(it.getValueAt(which)?.toShort() ?: 1)
}

else -> throw IllegalStateException("Unrecognized type used for StringSingleChoiceSetting!")
}

else -> throw IllegalStateException("Unrecognized type used for StringSingleChoiceSetting!")
fragmentView?.putSetting(setting)
closeDialog()
}

fragmentView.putSetting(setting)
closeDialog()
}

is SliderSetting -> {
val sliderSetting = clickedItem as SliderSetting
val sliderval = (sliderSetting.selectedFloat * 100).roundToInt().toFloat() / 100;
if (sliderval != sliderProgress) {
fragmentView.onSettingChanged()
}
when (sliderSetting.setting) {
is AbstractIntSetting -> {
val value = sliderProgress.roundToInt()
val setting = sliderSetting.setSelectedValue(value)
fragmentView.putSetting(setting)
}

else -> {
val setting = sliderSetting.setSelectedValue(sliderProgress)
fragmentView.putSetting(setting)
val sliderSetting = clickedItem as? SliderSetting
sliderSetting?.let {
val sliderval = (it.selectedFloat * 100).roundToInt().toFloat() / 100
if (sliderval != sliderProgress) {
fragmentView?.onSettingChanged()
}
when (it.setting) {
is AbstractIntSetting -> {
val value = sliderProgress.roundToInt()
val setting = it.setSelectedValue(value)
fragmentView?.putSetting(setting)
}
else -> {
val setting = it.setSelectedValue(sliderProgress)
fragmentView?.putSetting(setting)
}
}
closeDialog()
}
closeDialog()
}

is StringInputSetting -> {
val inputSetting = clickedItem as StringInputSetting
if (inputSetting.selectedValue != textInputValue) {
fragmentView.onSettingChanged()
}
val setting = inputSetting.setSelectedValue(textInputValue)
fragmentView.putSetting(setting)
closeDialog()
val inputSetting = clickedItem as? StringInputSetting
inputSetting?.let {
if (it.selectedValue != textInputValue) {
fragmentView?.onSettingChanged()
}
val setting = it.setSelectedValue(textInputValue ?: "")
fragmentView?.putSetting(setting)
closeDialog()
}
}
}
clickedItem = null
Expand Down
2 changes: 1 addition & 1 deletion src/common/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ std::string_view GetGraphicsAPIName(GraphicsAPI api) {

std::string_view GetTextureFilterName(TextureFilter filter) {
switch (filter) {
case TextureFilter::None:
case TextureFilter::NoFilter:
return "None";
case TextureFilter::Anime4K:
return "Anime4K";
Expand Down
4 changes: 2 additions & 2 deletions src/common/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ enum class AudioEmulation : u32 {
};

enum class TextureFilter : u32 {
None = 0,
NoFilter = 0,
Anime4K = 1,
Bicubic = 2,
ScaleForce = 3,
Expand Down Expand Up @@ -492,7 +492,7 @@ struct Values {
Setting<bool> use_shader_jit{true, "use_shader_jit"};
SwitchableSetting<u32, true> resolution_factor{1, 0, 10, "resolution_factor"};
SwitchableSetting<u16, true> frame_limit{100, 0, 1000, "frame_limit"};
SwitchableSetting<TextureFilter> texture_filter{TextureFilter::None, "texture_filter"};
SwitchableSetting<TextureFilter> texture_filter{TextureFilter::NoFilter, "texture_filter"};
SwitchableSetting<TextureSampling> texture_sampling{TextureSampling::GameControlled,
"texture_sampling"};
SwitchableSetting<LayoutOption> layout_option{LayoutOption::Default, "layout_option"};
Expand Down
6 changes: 4 additions & 2 deletions src/core/frontend/framebuffer_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,12 @@ FramebufferLayout LargeFrameLayout(u32 width, u32 height, bool swapped, bool upr

float large_height =
swapped ? Core::kScreenBottomHeight * scale_factor : Core::kScreenTopHeight * scale_factor;
float small_height = static_cast<float>(swapped ? Core::kScreenTopHeight : Core::kScreenBottomHeight);
float small_height =
static_cast<float>(swapped ? Core::kScreenTopHeight : Core::kScreenBottomHeight);
float large_width =
swapped ? Core::kScreenBottomWidth * scale_factor : Core::kScreenTopWidth * scale_factor;
float small_width = static_cast<float>(swapped ? Core::kScreenTopWidth : Core::kScreenBottomWidth);
float small_width =
static_cast<float>(swapped ? Core::kScreenTopWidth : Core::kScreenBottomWidth);

float emulation_width, emulation_height;
if (vertical) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/hle/service/cam/cam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1099,8 +1099,8 @@ void Module::Interface::DriverInitialize(Kernel::HLERequestContext& ctx) {
for (int context_id = 0; context_id < 2; ++context_id) {
// Note: the following default values are verified against real 3DS
ContextConfig& context = camera.contexts[context_id];
context.flip = camera_id == 1 ? Flip::Horizontal : Flip::None;
context.effect = Effect::None;
context.flip = camera_id == 1 ? Flip::Horizontal : Flip::NoFlip;
context.effect = Effect::NoEffect;
context.format = OutputFormat::YUV422;
context.resolution =
context_id == 0 ? PRESET_RESOLUTION[5 /*DS_LCD*/] : PRESET_RESOLUTION[0 /*VGA*/];
Expand Down
4 changes: 2 additions & 2 deletions src/core/hle/service/cam/cam.h
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,8 @@ class Module final {
Result SetPackageParameter(const PackageParameterType& package);

struct ContextConfig {
Flip flip{Flip::None};
Effect effect{Effect::None};
Flip flip{Flip::NoFlip};
Effect effect{Effect::NoEffect};
OutputFormat format{OutputFormat::YUV422};
Resolution resolution = {0, 0, 0, 0, 0, 0};

Expand Down
4 changes: 2 additions & 2 deletions src/core/hle/service/cam/cam_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ enum CameraIndex {
};

enum class Effect : u8 {
None = 0,
NoEffect = 0,
Mono = 1,
Sepia = 2,
Negative = 3,
Expand All @@ -26,7 +26,7 @@ enum class Effect : u8 {
};

enum class Flip : u8 {
None = 0,
NoFlip = 0,
Horizontal = 1,
Vertical = 2,
Reverse = 3,
Expand Down
2 changes: 1 addition & 1 deletion src/lime_qt/camera/qt_camera_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void QtCameraInterface::SetFlip(Service::CAM::Flip flip) {
}

void QtCameraInterface::SetEffect(Service::CAM::Effect effect) {
if (effect != Service::CAM::Effect::None) {
if (effect != Service::CAM::Effect::NoEffect) {
LOG_ERROR(Service_CAM, "Unimplemented effect {}", static_cast<int>(effect));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/lime_qt/configuration/configure_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ void ConfigureCamera::StartPreviewing() {
}
previewing_camera->SetResolution(
{static_cast<u16>(preview_width), static_cast<u16>(preview_height)});
previewing_camera->SetEffect(Service::CAM::Effect::None);
previewing_camera->SetFlip(Service::CAM::Flip::None);
previewing_camera->SetEffect(Service::CAM::Effect::NoEffect);
previewing_camera->SetFlip(Service::CAM::Flip::NoFlip);
previewing_camera->SetFormat(Service::CAM::OutputFormat::RGB565);
previewing_camera->SetFrameRate(Service::CAM::FrameRate::Rate_30);
previewing_camera->StartCapture();
Expand Down
2 changes: 1 addition & 1 deletion src/video_core/rasterizer_cache/rasterizer_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ SurfaceId RasterizerCache<T>::GetTextureSurface(const Pica::Texture::TextureInfo
params.levels = max_level + 1;
params.is_tiled = true;
params.pixel_format = PixelFormatFromTextureFormat(info.format);
params.res_scale = filter != Settings::TextureFilter::None ? resolution_scale_factor : 1;
params.res_scale = filter != Settings::TextureFilter::NoFilter ? resolution_scale_factor : 1;
params.UpdateParams();

const u32 min_width = info.width >> max_level;
Expand Down
Loading