Skip to content

Commit

Permalink
Merge pull request #21406 from osmandapp/potential_widget_type_npe_fix
Browse files Browse the repository at this point in the history
Potential npe widget type fix
  • Loading branch information
Chumva authored Nov 22, 2024
2 parents b91eb34 + 2dad179 commit 27b47fe
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static public void showMenu(@NonNull View view, @NonNull MapActivity mapActivity
args.putString(KEY_WIDGET_ID, widgetInfo.key);
args.putString(KEY_APP_MODE, appMode.getStringKey());
FragmentManager manager = mapActivity.getSupportFragmentManager();
WidgetSettingsBaseFragment.showFragment(manager, args, null, fragment);
WidgetSettingsBaseFragment.showFragment(manager, args, fragment, null);
})
.setIcon(uiUtilities.getPaintedIcon(R.drawable.ic_action_settings_outlined, iconColor))
.showTopDivider(Algorithms.isEmpty(widgetActions))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ private OnClickListener getActionClickListener(@NonNull Action action) {
}

private void openSettingsFragment() {
WidgetSettingsBaseFragment settingsFragment = widgetType == null ? null : widgetType.getSettingsFragment(app, widgetInfo);
if (settingsFragment == null) {
WidgetSettingsBaseFragment fragment = widgetType == null ? null : widgetType.getSettingsFragment(app, widgetInfo);
if (fragment == null) {
throw new IllegalStateException("Widget has no available settings");
}

Expand All @@ -272,7 +272,7 @@ private void openSettingsFragment() {
args.putString(APP_MODE_KEY, appMode.getStringKey());
args.putString(WIDGET_ID_KEY, widgetInfo.key);

WidgetSettingsBaseFragment.showFragment(fragmentManager, args, this, settingsFragment);
WidgetSettingsBaseFragment.showFragment(fragmentManager, args, fragment, this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ private void inflateWidgetItemsViews(@NonNull Set<MapWidgetInfo> widgetsInfo, @N

Fragment target = getParentFragment();
FragmentManager manager = activity.getSupportFragmentManager();
WidgetSettingsBaseFragment.showFragment(manager, args, target, fragment);
WidgetSettingsBaseFragment.showFragment(manager, args, fragment, target);
}
});
UiUtilities.setupListItemBackground(app, settingsButton, selectedAppMode.getProfileColor(nightMode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;

import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.settings.backend.preferences.CommonPreference;
import net.osmand.plus.settings.backend.preferences.OsmandPreference;
import net.osmand.plus.settings.enums.WidgetSize;
import net.osmand.plus.utils.ColorUtilities;
Expand All @@ -33,14 +31,14 @@ public class BaseResizableWidgetSettingFragment extends WidgetSettingsBaseFragme
private static final String SELECTED_WIDGET_SIZE_ID_KEY = "selected_widget_id_size";
protected static final String WIDGET_TYPE_KEY = "widget_type_key";

public OsmandPreference<WidgetSize> widgetSizePref;
private WidgetType widgetType;
protected OsmandPreference<WidgetSize> widgetSizePref;
protected WidgetType widgetType;
@Nullable
private MapWidgetInfo widgetInfo;

private WidgetSize selectedWidgetSize;

public void setWidgetType(WidgetType widgetType) {
public void setWidgetType(@NonNull WidgetType widgetType) {
this.widgetType = widgetType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,20 @@ public class BaseSimpleWidgetSettingsFragment extends BaseResizableWidgetSetting
private boolean isWidgetVertical = false;

public CommonPreference<Boolean> shouldShowIconPref;
private WidgetType widgetType;
@Nullable
private MapWidgetInfo widgetInfo;

private boolean showIcon;

public void setWidgetType(WidgetType widgetType) {
this.widgetType = widgetType;
}

@Override
protected void initParams(@NonNull Bundle bundle) {
super.initParams(bundle);
widgetInfo = widgetRegistry.getWidgetInfoById(widgetId);
if (widgetInfo != null && widgetInfo.widget instanceof SimpleWidget) {
SimpleWidget simpleWidget = (SimpleWidget) widgetInfo.widget;
if (widgetInfo != null && widgetInfo.widget instanceof SimpleWidget simpleWidget) {
isWidgetVertical = simpleWidget.isVerticalWidget();
shouldShowIconPref = simpleWidget.shouldShowIconPref();
showIcon = bundle.containsKey(SHOW_ICON_KEY) ? bundle.getBoolean(SHOW_ICON_KEY) : shouldShowIconPref.get();
}
String type = bundle.getString(WIDGET_TYPE_KEY);
if (widgetType == null && type != null) {
widgetType = WidgetType.getById(type);
}
}

@Override
Expand All @@ -67,12 +57,6 @@ public void onSaveInstanceState(@NonNull Bundle outState) {
outState.putBoolean(SHOW_ICON_KEY, showIcon);
}

@NonNull
@Override
public WidgetType getWidget() {
return widgetType;
}

@Override
protected void setupContent(@NonNull LayoutInflater themedInflater, @NonNull ViewGroup container) {
if (isWidgetVertical) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
import net.osmand.plus.utils.AndroidUtils;
import net.osmand.plus.utils.ColorUtilities;
import net.osmand.plus.utils.UiUtilities;
import net.osmand.plus.widgets.dialogbutton.DialogButtonType;
import net.osmand.plus.views.layers.MapInfoLayer;
import net.osmand.plus.views.mapwidgets.MapWidgetRegistry;
import net.osmand.plus.views.mapwidgets.WidgetType;
import net.osmand.plus.views.mapwidgets.configure.panel.WidgetsConfigurationChangeListener;
import net.osmand.plus.widgets.dialogbutton.DialogButton;
import net.osmand.plus.widgets.dialogbutton.DialogButtonType;

public abstract class WidgetSettingsBaseFragment extends BaseOsmAndFragment {

Expand Down Expand Up @@ -165,12 +165,10 @@ protected MapActivity getMapActivity() {
return activity != null ? ((MapActivity) activity) : null;
}

public static void showFragment(@NonNull FragmentManager manager,
@NonNull Bundle args,
@Nullable Fragment target,
@NonNull WidgetSettingsBaseFragment fragment) {
public static void showFragment(@NonNull FragmentManager manager, @NonNull Bundle args,
@NonNull WidgetSettingsBaseFragment fragment, @Nullable Fragment target) {
String tag = fragment.getClass().getSimpleName();
if (AndroidUtils.isFragmentCanBeAdded(manager, tag) && manager.findFragmentByTag(tag) == null) {
if (AndroidUtils.isFragmentCanBeAdded(manager, tag, true)) {
fragment.setArguments(args);
fragment.setTargetFragment(target, 0);

Expand Down

0 comments on commit 27b47fe

Please sign in to comment.