From 07db107b7ac97a63d624f25a88fb659d88958266 Mon Sep 17 00:00:00 2001 From: madwasp79 Date: Wed, 29 May 2019 15:30:41 +0300 Subject: [PATCH] Added new profile selection dialog to navigational settings menu ui tweaks --- ...om_sheet_item_with_descr_and_radio_btn.xml | 2 +- OsmAnd/res/layout/profile_list_item.xml | 2 + .../src/net/osmand/plus/ApplicationMode.java | 2 +- .../plus/activities/SettingsBaseActivity.java | 126 +++++++++--------- .../SettingsNavigationActivity.java | 2 +- .../SettingsMonitoringActivity.java | 2 +- .../AppModesBottomSheetDialogFragment.java | 1 - .../plus/profiles/AppProfileArrayAdapter.java | 95 +++++++++++++ 8 files changed, 163 insertions(+), 69 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/profiles/AppProfileArrayAdapter.java diff --git a/OsmAnd/res/layout/bottom_sheet_item_with_descr_and_radio_btn.xml b/OsmAnd/res/layout/bottom_sheet_item_with_descr_and_radio_btn.xml index a6eb4639e0..56104cd24a 100644 --- a/OsmAnd/res/layout/bottom_sheet_item_with_descr_and_radio_btn.xml +++ b/OsmAnd/res/layout/bottom_sheet_item_with_descr_and_radio_btn.xml @@ -44,7 +44,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" - android:maxLines="1" + android:maxLines="2" android:textAppearance="@style/TextAppearance.ListItemTitle" android:textColor="?attr/main_font_color_basic" tools:text="Item Title"/> diff --git a/OsmAnd/res/layout/profile_list_item.xml b/OsmAnd/res/layout/profile_list_item.xml index e6225b97ce..437d91c17f 100644 --- a/OsmAnd/res/layout/profile_list_item.xml +++ b/OsmAnd/res/layout/profile_list_item.xml @@ -38,6 +38,8 @@ android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" + android:maxLines="2" + android:ellipsize="end" android:textColor="@color/main_font_dark" android:textSize="@dimen/default_list_text_size" tools:text="Bicycle"/> diff --git a/OsmAnd/src/net/osmand/plus/ApplicationMode.java b/OsmAnd/src/net/osmand/plus/ApplicationMode.java index a305bc92a3..b304f83be1 100644 --- a/OsmAnd/src/net/osmand/plus/ApplicationMode.java +++ b/OsmAnd/src/net/osmand/plus/ApplicationMode.java @@ -43,7 +43,7 @@ public class ApplicationMode { icon(R.drawable.map_action_car_dark, R.drawable.ic_action_car_dark, "ic_action_car_dark").setRoutingProfile("car").reg(); public static final ApplicationMode BICYCLE = create(R.string.app_mode_bicycle, "bicycle").speed(5.5f, 15).arrivalDistance(60).offRouteDistance(50).bicycleLocation(). - icon(R.drawable.map_action_bicycle_dark, R.drawable.ic_action_bicycle_dark, "").setRoutingProfile("bicycle").reg(); + icon(R.drawable.map_action_bicycle_dark, R.drawable.ic_action_bicycle_dark, "ic_action_bicycle_dark").setRoutingProfile("bicycle").reg(); public static final ApplicationMode PEDESTRIAN = create(R.string.app_mode_pedestrian, "pedestrian").speed(1.5f, 5).arrivalDistance(45).offRouteDistance(20). icon(R.drawable.map_action_pedestrian_dark, R.drawable.ic_action_pedestrian_dark, "ic_action_pedestrian_dark").setRoutingProfile("pedestrian").reg(); diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsBaseActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsBaseActivity.java index 39e9fd986f..028f1aa8df 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsBaseActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsBaseActivity.java @@ -18,9 +18,6 @@ import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog.Builder; import android.view.MenuItem; import android.view.View; -import android.view.ViewGroup; -import android.widget.AdapterView; -import android.widget.ArrayAdapter; import android.widget.Toast; import net.osmand.plus.ApplicationMode; @@ -29,17 +26,16 @@ import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings.CommonPreference; import net.osmand.plus.OsmandSettings.OsmandPreference; import net.osmand.plus.R; -import net.osmand.plus.activities.actions.AppModeDialog; +import net.osmand.plus.profiles.AppProfileArrayAdapter; +import net.osmand.plus.profiles.ProfileDataObject; import net.osmand.plus.views.SeekBarPreference; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.LinkedHashMap; -import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Set; import net.osmand.util.Algorithms; @@ -60,12 +56,13 @@ public abstract class SettingsBaseActivity extends ActionBarPreferenceActivity private Map> seekBarPreferences = new LinkedHashMap>(); private Map> listPrefValues = new LinkedHashMap>(); - private AlertDialog profileDialog; - + public SettingsBaseActivity() { this(false); } - + + private boolean isModeSelected = false; + public SettingsBaseActivity(boolean profile) { profileSettings = profile; } @@ -343,29 +340,7 @@ public abstract class SettingsBaseActivity extends ActionBarPreferenceActivity getTypeButton().setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - AlertDialog.Builder singleSelectDialogBuilder = new Builder(SettingsBaseActivity.this); - singleSelectDialogBuilder.setTitle("Select App Profile"); - final ArrayAdapter modeNames = new ArrayAdapter<>( - SettingsBaseActivity.this, android.R.layout.select_dialog_singlechoice); - for (ApplicationMode am : modes) { - modeNames.add(am.toHumanString(SettingsBaseActivity.this)); - } - singleSelectDialogBuilder.setNegativeButton(R.string.shared_string_cancel, - new OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); - } - }); - singleSelectDialogBuilder.setAdapter(modeNames, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - settings.APPLICATION_MODE.set(modes.get(which)); - updateModeButton(modes.get(which)); - updateAllSettings(); - } - }); - singleSelectDialogBuilder.show(); + selectAppModeDialog().show(); } }); @@ -373,6 +348,60 @@ public abstract class SettingsBaseActivity extends ActionBarPreferenceActivity setPreferenceScreen(getPreferenceManager().createPreferenceScreen(this)); } + protected AlertDialog.Builder selectAppModeDialog() { + AlertDialog.Builder singleSelectDialogBuilder = new Builder(SettingsBaseActivity.this); + singleSelectDialogBuilder.setTitle(R.string.profile_settings); + + final List activeModes = new ArrayList<>(); + for (ApplicationMode am : ApplicationMode.values(getMyApplication())) { + boolean isSelected = false; + if (previousAppMode != null && previousAppMode == am) { + isSelected = true; + } + if (am != ApplicationMode.DEFAULT) { + activeModes.add(new ProfileDataObject( + am.toHumanString(getMyApplication()), + am.getParent() == null + ? getString(R.string.profile_type_base_string) + : String.format(getString(R.string.profile_type_descr_string), + am.getParent().toHumanString(getMyApplication())), + am.getStringKey(), + ApplicationMode.getIconResFromName(getMyApplication(), am.getIconName(), getApplication().getPackageName()), + isSelected + )); + } + } + + final AppProfileArrayAdapter modeNames = new AppProfileArrayAdapter( + SettingsBaseActivity.this, R.layout.bottom_sheet_item_with_descr_and_radio_btn, activeModes); + + singleSelectDialogBuilder.setNegativeButton(R.string.shared_string_cancel, + new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }); + singleSelectDialogBuilder.setOnDismissListener(new OnDismissListener() { + @Override + public void onDismiss(DialogInterface dialog) { + if (!isModeSelected) { + List m = ApplicationMode.values(getMyApplication()); + setSelectedAppMode(m.get(m.size() > 1 ? 1 : 0)); + } + } + }); + singleSelectDialogBuilder.setAdapter(modeNames, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + settings.APPLICATION_MODE.set(modes.get(which)); + updateModeButton(modes.get(which)); + updateAllSettings(); + } + }); + return singleSelectDialogBuilder; + } + void updateModeButton(ApplicationMode mode) { String title = Algorithms.isEmpty(mode.getUserProfileName()) ? mode.toHumanString(SettingsBaseActivity.this) @@ -393,6 +422,7 @@ public abstract class SettingsBaseActivity extends ActionBarPreferenceActivity ? R.color.active_buttons_and_links_light : R.color.active_buttons_and_links_dark)); settings.APPLICATION_MODE.set(mode); + isModeSelected = true; updateAllSettings(); } @@ -416,39 +446,8 @@ public abstract class SettingsBaseActivity extends ActionBarPreferenceActivity updateAllSettings(); } } - - protected void profileDialog() { - AlertDialog.Builder b = new AlertDialog.Builder(this); - final Set selected = new LinkedHashSet(); - View v = AppModeDialog.prepareAppModeView(this, selected, false, null, true, true, false, - new View.OnClickListener() { - @Override - public void onClick(View v) { - if(selected.size() > 0) { - // test - setSelectedAppMode(selected.iterator().next()); - } - if(profileDialog != null && profileDialog.isShowing()) { - profileDialog.dismiss(); - } - profileDialog = null; - } - }); - b.setOnDismissListener(new OnDismissListener() { - @Override - public void onDismiss(DialogInterface dialog) { - if (selected.size() == 0) { - setSelectedAppMode(ApplicationMode.CAR); - } - } - }); - b.setTitle(R.string.profile_settings); - b.setView(v); - profileDialog = b.show(); - } protected boolean setSelectedAppMode(ApplicationMode am) { - int ind = 0; boolean found = false; for (ApplicationMode a : modes) { if (am == a) { @@ -456,7 +455,6 @@ public abstract class SettingsBaseActivity extends ActionBarPreferenceActivity found = true; break; } - ind++; } return found; } diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java index 6adac1a39b..77db68780b 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java @@ -180,7 +180,7 @@ public class SettingsNavigationActivity extends SettingsBaseActivity { if(getIntent() != null && getIntent().hasExtra(INTENT_SKIP_DIALOG)) { setSelectedAppMode(settings.getApplicationMode()); } else { - profileDialog(); + selectAppModeDialog().show(); } addVoicePrefs((PreferenceGroup) screen.findPreference("voice")); diff --git a/OsmAnd/src/net/osmand/plus/monitoring/SettingsMonitoringActivity.java b/OsmAnd/src/net/osmand/plus/monitoring/SettingsMonitoringActivity.java index 2ea999fcb5..5a7cb43cd4 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/SettingsMonitoringActivity.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/SettingsMonitoringActivity.java @@ -43,7 +43,7 @@ public class SettingsMonitoringActivity extends SettingsBaseActivity { createLoggingSection(grp); createLiveSection(grp); createNotificationSection(grp); - profileDialog(); + selectAppModeDialog().show(); } diff --git a/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java index d6b4d3a086..54a278db3a 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java @@ -103,7 +103,6 @@ public class AppModesBottomSheetDialogFragment extends MenuBottomSheetDialogFrag adapter.updateItemsList(allModes, new LinkedHashSet<>(ApplicationMode.values(getMyApplication()))); setupHeightAndBackground(getView()); - } private void getData() { diff --git a/OsmAnd/src/net/osmand/plus/profiles/AppProfileArrayAdapter.java b/OsmAnd/src/net/osmand/plus/profiles/AppProfileArrayAdapter.java new file mode 100644 index 0000000000..dc818e2cfb --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/profiles/AppProfileArrayAdapter.java @@ -0,0 +1,95 @@ +package net.osmand.plus.profiles; + +import android.app.Activity; +import android.app.Application; +import android.graphics.drawable.Drawable; +import android.support.annotation.NonNull; +import android.system.Os; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.CompoundButton; +import android.widget.ImageView; +import android.widget.TextView; +import java.util.List; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; + +public class AppProfileArrayAdapter extends ArrayAdapter { + + private Activity context; + private List modes; + private int layout; + private int colorRes; + + public AppProfileArrayAdapter(@NonNull Activity context, int resource, @NonNull List objects) { + super(context, resource, objects); + this.context = context; + this.modes = objects; + this.layout = resource; + } + + public long getItemId(int position) { + return position; + } + + static class ViewHolder { + public TextView title; + public TextView description; + public ImageView icon; + public CompoundButton compoundButton; + } + + @Override + public @NonNull View getView(int position, View convertView, @NonNull ViewGroup parent) { + ViewHolder viewHolder; + View rowView = convertView; + if (rowView == null) { + LayoutInflater layoutInflater = context.getLayoutInflater(); + rowView = layoutInflater.inflate(layout, null, true); + viewHolder = new ViewHolder(); + viewHolder.title = (TextView) rowView.findViewById(R.id.title); + viewHolder.description = (TextView) rowView.findViewById(R.id.description); + viewHolder.icon = (ImageView) rowView.findViewById(R.id.icon); + viewHolder.compoundButton = (CompoundButton) rowView.findViewById(R.id.compound_button); + rowView.setTag(viewHolder); + } else { + viewHolder = (ViewHolder) rowView.getTag(); + } + + ProfileDataObject mode = modes.get(position); + + Drawable iconDrawable; + if (getMyApp(context) != null) { + if (mode.isSelected()) { + iconDrawable = getMyApp(context).getUIUtilities().getIcon(mode.getIconRes(), + getMyApp(context).getSettings().isLightContent() + ? R.color.active_buttons_and_links_dark + : R.color.active_buttons_and_links_light + ); + } else { + iconDrawable = getMyApp(context).getUIUtilities() + .getIcon(mode.getIconRes(), R.color.icon_color); + } + } else { + iconDrawable = context.getDrawable(mode.getIconRes()); + } + + viewHolder.title.setText(mode.getName()); + viewHolder.description.setText(mode.getDescription()); + viewHolder.icon.setImageDrawable(iconDrawable); + viewHolder.compoundButton.setChecked(mode.isSelected()); + + return rowView; + } + + private OsmandApplication getMyApp(Activity context) { + Application app = context.getApplication(); + if (app instanceof OsmandApplication) { + return (OsmandApplication) app; + } else { + return null; + } + } +}