Fix compound button

This commit is contained in:
androiddevkotlin 2021-04-21 20:26:52 +03:00
parent 2ee1b26289
commit 85114b049d
2 changed files with 104 additions and 116 deletions

View file

@ -662,12 +662,12 @@ public class DownloadResources extends DownloadResourceGroup {
return res; return res;
} }
public static List<DownloadItem> findIndexItemsAt(OsmandApplication app, String groupTypeVoice) throws IOException { public List<DownloadItem> getDownloadItemsForGroup(String groupId) {
DownloadIndexesThread downloadThread = app.getDownloadThread(); DownloadResourceGroup group = getSubGroupById(groupId);
DownloadResources indexes = downloadThread.getIndexes(); if (group != null) {
DownloadResourceGroup groupVoice = indexes.getSubGroupById(groupTypeVoice); return group.getIndividualDownloadItems();
List<DownloadItem> items = groupVoice.getIndividualDownloadItems(); }
return new ArrayList<>(items); return Collections.emptyList();
} }
public static List<IndexItem> findIndexItemsAt(OsmandApplication app, public static List<IndexItem> findIndexItemsAt(OsmandApplication app,

View file

@ -9,11 +9,11 @@ import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.DimenRes; import androidx.annotation.DimenRes;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
@ -31,51 +31,56 @@ import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerSpaceItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerSpaceItem;
import net.osmand.plus.download.DownloadIndexesThread; import net.osmand.plus.download.DownloadIndexesThread;
import net.osmand.plus.download.DownloadItem; import net.osmand.plus.download.DownloadItem;
import net.osmand.plus.download.DownloadResources;
import net.osmand.plus.download.DownloadValidationManager; import net.osmand.plus.download.DownloadValidationManager;
import net.osmand.plus.download.IndexItem; import net.osmand.plus.download.IndexItem;
import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.routing.RoutingHelper;
import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.track.TrackSelectSegmentBottomSheet; import net.osmand.plus.track.TrackSelectSegmentBottomSheet;
import net.osmand.plus.widgets.MultiStateToggleButton; import net.osmand.plus.widgets.multistatetoggle.RadioItem;
import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; import net.osmand.plus.widgets.multistatetoggle.RadioItem.OnRadioItemClickListener;
import net.osmand.plus.widgets.multistatetoggle.TextToggleButton;
import net.osmand.plus.widgets.multistatetoggle.TextToggleButton.TextRadioItem;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import java.io.IOException;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import static net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType; import static net.osmand.plus.UiUtilities.CompoundButtonType.PROFILE_DEPENDENT;
import static net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType.OTHER_GROUP; import static net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType.OTHER_GROUP;
import static net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType.VOICE_HEADER_REC; import static net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType.VOICE_HEADER_REC;
import static net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType.VOICE_HEADER_TTS; import static net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType.VOICE_HEADER_TTS;
import static net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType.VOICE_REC;
import static net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType.VOICE_TTS;
import static net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheet.PREFERENCE_ID; import static net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheet.PREFERENCE_ID;
public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragment implements DownloadIndexesThread.DownloadEvents { public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragment implements DownloadIndexesThread.DownloadEvents {
private static final String TAG = TrackSelectSegmentBottomSheet.class.getSimpleName(); private static final String TAG = TrackSelectSegmentBottomSheet.class.getSimpleName();
private static final String VOICE_REC = OTHER_GROUP.getDefaultId() + "#" + DownloadResourceGroupType.VOICE_TTS.getDefaultId() + "#" + VOICE_HEADER_TTS.getDefaultId();
private static final String VOICE_TTS = OTHER_GROUP.getDefaultId() + "#" + DownloadResourceGroupType.VOICE_REC.getDefaultId() + "#" + VOICE_HEADER_REC.getDefaultId();
private static final int DEFAULT_LANGUAGE_POSITION = 6;
private static final Log LOG = PlatformUtil.getLog(VoiceLanguageBottomSheetFragment.class); private static final Log LOG = PlatformUtil.getLog(VoiceLanguageBottomSheetFragment.class);
protected OsmandSettings settings;
private static final String VOICE_REC_KEY = OTHER_GROUP.getDefaultId() + "#" + VOICE_TTS.getDefaultId() + "#" + VOICE_HEADER_TTS.getDefaultId();
private static final String VOICE_TTS_KEY = OTHER_GROUP.getDefaultId() + "#" + VOICE_REC.getDefaultId() + "#" + VOICE_HEADER_REC.getDefaultId();
private static final int DEFAULT_LANGUAGE_POSITION = 6;
private OsmandApplication app; private OsmandApplication app;
private Context context; private OsmandSettings settings;
private InfoType selectedVoiceType = InfoType.TTS;
private boolean isTtsDescription;
private DownloadIndexesThread downloadThread; private DownloadIndexesThread downloadThread;
private InfoType selectedVoiceType = InfoType.TTS;
public static void showInstance(@NonNull FragmentManager fm, Fragment targetFragment, String key, boolean usedOnMap) { public static void showInstance(@NonNull FragmentManager fm, Fragment targetFragment, String key, boolean usedOnMap) {
try { try {
if (!fm.isStateSaved()) { if (!fm.isStateSaved()) {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putString(PREFERENCE_ID, key); args.putString(PREFERENCE_ID, key);
VoiceLanguageBottomSheetFragment fragment = new VoiceLanguageBottomSheetFragment(); VoiceLanguageBottomSheetFragment fragment = new VoiceLanguageBottomSheetFragment();
fragment.setRetainInstance(true);
fragment.setArguments(args); fragment.setArguments(args);
fragment.setUsedOnMap(usedOnMap); fragment.setUsedOnMap(usedOnMap);
fragment.setTargetFragment(targetFragment, 0); fragment.setTargetFragment(targetFragment, 0);
@ -86,6 +91,53 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
} }
} }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = requiredMyApplication();
}
@Override
public void createMenuItems(Bundle savedInstanceState) {
Context context = requireContext();
settings = app.getSettings();
downloadThread = app.getDownloadThread();
int padding = getDimen(R.dimen.content_padding_small);
LayoutInflater inflater = UiUtilities.getInflater(app, nightMode);
BaseBottomSheetItem titleItem = new BottomSheetItemWithDescription.Builder()
.setDescription(getString(R.string.language_description))
.setDescriptionColorId(nightMode ? R.color.text_color_primary_dark : R.color.text_color_primary_light)
.setTitle(getString(R.string.shared_string_language))
.setLayoutId(R.layout.bottom_sheet_item_title_with_description)
.create();
items.add(titleItem);
items.add(new DividerSpaceItem(context, padding));
LinearLayout voiceTypeButtons = (LinearLayout) inflater.inflate(R.layout.custom_radio_buttons, null);
LinearLayout.MarginLayoutParams itemTimeOfDayParams = new LinearLayout.MarginLayoutParams(
LinearLayout.MarginLayoutParams.MATCH_PARENT, LinearLayout.MarginLayoutParams.WRAP_CONTENT);
AndroidUtils.setMargins(itemTimeOfDayParams, padding, 0, padding, 0);
voiceTypeButtons.setLayoutParams(itemTimeOfDayParams);
setupTypeRadioGroup(voiceTypeButtons);
items.add(new BaseBottomSheetItem.Builder()
.setCustomView(voiceTypeButtons)
.create()
);
items.add(new DividerSpaceItem(context, padding));
BaseBottomSheetItem switchStartAndEndItem = new BottomSheetItemWithDescription.Builder()
.setDescription(getString(selectedVoiceType == InfoType.TTS ? R.string.tts_description : R.string.recorded_description))
.setLayoutId(R.layout.bottom_sheet_item_description_long)
.create();
items.add(switchStartAndEndItem);
items.add(createDividerItem());
createVoiceView();
}
@Override @Override
public void newDownloadIndexes() { public void newDownloadIndexes() {
updateItems(); updateItems();
@ -95,6 +147,7 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
Activity activity = getActivity(); Activity activity = getActivity();
View mainView = getView(); View mainView = getView();
if (activity != null && mainView != null) { if (activity != null && mainView != null) {
Context context = requireContext();
LinearLayout itemsContainer = (LinearLayout) mainView.findViewById(useScrollableItemsContainer() LinearLayout itemsContainer = (LinearLayout) mainView.findViewById(useScrollableItemsContainer()
? R.id.scrollable_items_container : R.id.non_scrollable_items_container); ? R.id.scrollable_items_container : R.id.non_scrollable_items_container);
if (itemsContainer != null) { if (itemsContainer != null) {
@ -111,10 +164,6 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
@Override @Override
public void downloadInProgress() { public void downloadInProgress() {
final OsmandApplication app = getMyApplication();
if (app == null) {
return;
}
IndexItem downloadIndexItem = downloadThread.getCurrentDownloadingItem(); IndexItem downloadIndexItem = downloadThread.getCurrentDownloadingItem();
if (downloadIndexItem != null) { if (downloadIndexItem != null) {
for (BaseBottomSheetItem item : items) { for (BaseBottomSheetItem item : items) {
@ -141,63 +190,11 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
updateItems(); updateItems();
} }
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = requiredMyApplication();
}
@Override
public void createMenuItems(Bundle savedInstanceState) {
context = requireContext();
settings = app.getSettings();
int padding = getDimen(R.dimen.content_padding_small);
downloadThread = app.getDownloadThread();
LayoutInflater inflater = UiUtilities.getInflater(app, nightMode);
BaseBottomSheetItem titleItem = new BottomSheetItemWithDescription.Builder()
.setDescription(getString(R.string.language_description))
.setDescriptionColorId(nightMode ? R.color.text_color_primary_dark : R.color.text_color_primary_light)
.setTitle(getString(R.string.shared_string_language))
.setLayoutId(R.layout.bottom_sheet_item_title_with_description)
.create();
items.add(titleItem);
items.add(new DividerSpaceItem(context, padding));
LinearLayout voiceTypeButtons = (LinearLayout) inflater.inflate(R.layout.custom_radio_buttons, null);
LinearLayout.MarginLayoutParams itemTimeOfDayParams = new LinearLayout.MarginLayoutParams(
LinearLayout.MarginLayoutParams.MATCH_PARENT, LinearLayout.MarginLayoutParams.WRAP_CONTENT);
AndroidUtils.setMargins(itemTimeOfDayParams, padding, 0, padding, 0);
voiceTypeButtons.setLayoutParams(itemTimeOfDayParams);
setupTypeRadioGroup(voiceTypeButtons);
items.add(new BaseBottomSheetItem.Builder()
.setCustomView(voiceTypeButtons)
.create()
);
items.add(new DividerSpaceItem(context, padding));
BaseBottomSheetItem switchStartAndEndItem = new BottomSheetItemWithDescription.Builder()
.setDescription(getString(isTtsDescription ? R.string.tts_description : R.string.recorded_description))
.setLayoutId(R.layout.bottom_sheet_item_description_long)
.create();
items.add(switchStartAndEndItem);
items.add(createDividerItem());
createVoiceView();
}
private void setupTypeRadioGroup(LinearLayout buttonsContainer) { private void setupTypeRadioGroup(LinearLayout buttonsContainer) {
RadioItem tts = createRadioButton(InfoType.TTS, R.string.tts_title); TextRadioItem tts = createRadioButton(InfoType.TTS, R.string.tts_title);
RadioItem recorded = createRadioButton(InfoType.RECORDED, R.string.shared_string_recorded); TextRadioItem recorded = createRadioButton(InfoType.RECORDED, R.string.shared_string_recorded);
MultiStateToggleButton radioGroup = new MultiStateToggleButton(app, buttonsContainer, nightMode); TextToggleButton radioGroup = new TextToggleButton(app, buttonsContainer, nightMode);
radioGroup.setItems(tts, recorded); radioGroup.setItems(tts, recorded);
if (selectedVoiceType == InfoType.TTS) { if (selectedVoiceType == InfoType.TTS) {
@ -207,10 +204,10 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
} }
} }
private RadioItem createRadioButton(final InfoType voiceType, int titleId) { private TextRadioItem createRadioButton(final InfoType voiceType, int titleId) {
String title = app.getString(titleId); String title = app.getString(titleId);
RadioItem item = new RadioItem(title); TextRadioItem item = new TextRadioItem(title);
item.setOnClickListener(new MultiStateToggleButton.OnRadioItemClickListener() { item.setOnClickListener(new OnRadioItemClickListener() {
@Override @Override
public boolean onRadioItemClick(RadioItem radioItem, View view) { public boolean onRadioItemClick(RadioItem radioItem, View view) {
selectedVoiceType = voiceType; selectedVoiceType = voiceType;
@ -225,23 +222,23 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
private void createVoiceView() { private void createVoiceView() {
List<DownloadItem> voiceItems; List<DownloadItem> voiceItems;
if (selectedVoiceType == InfoType.TTS) { if (selectedVoiceType == InfoType.TTS) {
voiceItems = getVoiceList(VOICE_TTS); voiceItems = getVoiceList(VOICE_TTS_KEY);
isTtsDescription = true;
} else { } else {
voiceItems = getVoiceList(VOICE_REC); voiceItems = getVoiceList(VOICE_REC_KEY);
isTtsDescription = false;
} }
createSuggestedVoiceItems(voiceItems); createSuggestedVoiceItemsView(voiceItems);
} }
private void createSuggestedVoiceItems(List<DownloadItem> suggestedMaps) { private void createSuggestedVoiceItemsView(List<DownloadItem> suggestedMaps) {
RoutingHelper routingHelper = app.getRoutingHelper(); final Context context = requireContext();
final ApplicationMode applicationMode = routingHelper.getAppMode(); final OsmandSettings settings = app.getSettings();
final ApplicationMode applicationMode = settings.getApplicationMode();
final LayoutInflater inflater = UiUtilities.getInflater(app, nightMode);
for (final DownloadItem indexItem : suggestedMaps) { for (final DownloadItem indexItem : suggestedMaps) {
View view = UiUtilities.getInflater(context, nightMode).inflate(R.layout.list_item_icon_and_download, null); View container = inflater.inflate(R.layout.list_item_icon_and_download, null);
AndroidUtils.setBackground(view, UiUtilities.getSelectableDrawable(context)); AndroidUtils.setBackground(container, UiUtilities.getSelectableDrawable(context));
view.findViewById(R.id.divider).setVisibility(View.GONE); container.findViewById(R.id.divider).setVisibility(View.GONE);
String systemLanguage = Resources.getSystem().getConfiguration().locale.getLanguage(); String systemLanguage = Resources.getSystem().getConfiguration().locale.getLanguage();
DateFormat df = SimpleDateFormat.getDateInstance(DateFormat.DEFAULT); DateFormat df = SimpleDateFormat.getDateInstance(DateFormat.DEFAULT);
@ -251,13 +248,14 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
String description = isDefault ? indexItem.getVisibleName(app, app.getRegions(), false) : indexItem.getSizeDescription(app) + "" + dateUpdate; String description = isDefault ? indexItem.getVisibleName(app, app.getRegions(), false) : indexItem.getSizeDescription(app) + "" + dateUpdate;
int position = isDefault ? DEFAULT_LANGUAGE_POSITION : -1; int position = isDefault ? DEFAULT_LANGUAGE_POSITION : -1;
final ImageView secondaryIcon = view.findViewById(R.id.secondary_icon); final ImageView secondaryIcon = container.findViewById(R.id.secondary_icon);
int activeColorResId = nightMode ? R.color.active_color_primary_dark : R.color.active_color_primary_light; int activeColorResId = nightMode ? R.color.active_color_primary_dark : R.color.active_color_primary_light;
secondaryIcon.setColorFilter(ContextCompat.getColor(context, activeColorResId)); secondaryIcon.setColorFilter(ContextCompat.getColor(context, activeColorResId));
final ProgressBar progressBar = view.findViewById(R.id.ProgressBar); final ProgressBar progressBar = container.findViewById(R.id.ProgressBar);
final TextView textDescription = view.findViewById(R.id.description); final TextView textDescription = container.findViewById(R.id.description);
final TextView compoundButton = view.findViewById(R.id.compound_button); RadioButton radioButton = (RadioButton) container.findViewById(R.id.compound_button);
UiUtilities.setupCompoundButton(radioButton, nightMode, PROFILE_DEPENDENT);
AndroidUiHelper.updateVisibility(secondaryIcon, true); AndroidUiHelper.updateVisibility(secondaryIcon, true);
AndroidUiHelper.updateVisibility(progressBar, downloadThread.isDownloading((IndexItem) indexItem)); AndroidUiHelper.updateVisibility(progressBar, downloadThread.isDownloading((IndexItem) indexItem));
@ -272,17 +270,17 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
} }
if (indexItem.isDownloaded()) { if (indexItem.isDownloaded()) {
AndroidUiHelper.updateVisibility(compoundButton, true); AndroidUiHelper.updateVisibility(radioButton, true);
AndroidUiHelper.updateVisibility(secondaryIcon, false); AndroidUiHelper.updateVisibility(secondaryIcon, false);
} else { } else {
AndroidUiHelper.updateVisibility(compoundButton, false); AndroidUiHelper.updateVisibility(radioButton, false);
AndroidUiHelper.updateVisibility(secondaryIcon, true); AndroidUiHelper.updateVisibility(secondaryIcon, true);
} }
boolean selected = settings.VOICE_PROVIDER.getModeValue(applicationMode).contains(indexItem.getBasename());
final BottomSheetItemWithCompoundButton[] voiceDownloadedItem = new BottomSheetItemWithCompoundButton[1]; final BottomSheetItemWithCompoundButton[] voiceDownloadedItem = new BottomSheetItemWithCompoundButton[1];
voiceDownloadedItem[0] = (BottomSheetItemWithCompoundButton) new BottomSheetItemWithCompoundButton.Builder() voiceDownloadedItem[0] = (BottomSheetItemWithCompoundButton) new BottomSheetItemWithCompoundButton.Builder()
.setCompoundButtonColorId(activeColorResId) .setCompoundButtonColorId(activeColorResId)
.setChecked(settings.VOICE_PROVIDER.getModeValue(applicationMode).contains(indexItem.getBasename())) .setChecked(selected)
.setDescription(description) .setDescription(description)
.setIconHidden(true) .setIconHidden(true)
.setTitle(title) .setTitle(title)
@ -294,7 +292,7 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
boolean checked = !voiceDownloadedItem[0].isChecked(); boolean checked = !voiceDownloadedItem[0].isChecked();
voiceDownloadedItem[0].setChecked(checked); voiceDownloadedItem[0].setChecked(checked);
settings.VOICE_PROVIDER.setModeValue(applicationMode, indexItem.getBasename()); settings.VOICE_PROVIDER.setModeValue(applicationMode, indexItem.getBasename());
updateItems(); dismiss();
} else { } else {
if (downloadThread.isDownloading((IndexItem) indexItem)) { if (downloadThread.isDownloading((IndexItem) indexItem)) {
downloadThread.cancelDownload(indexItem); downloadThread.cancelDownload(indexItem);
@ -311,7 +309,7 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
} }
} }
}) })
.setCustomView(view) .setCustomView(container)
.create(); .create();
items.add(voiceDownloadedItem[0]); items.add(voiceDownloadedItem[0]);
} }
@ -330,8 +328,6 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
} }
public List<DownloadItem> getVoiceList(String type) { public List<DownloadItem> getVoiceList(String type) {
List<DownloadItem> suggestedVoice = new ArrayList<>();
if (!downloadThread.getIndexes().isDownloadedFromInternet && settings.isInternetConnectionAvailable()) { if (!downloadThread.getIndexes().isDownloadedFromInternet && settings.isInternetConnectionAvailable()) {
downloadThread.runReloadIndexFiles(); downloadThread.runReloadIndexFiles();
} }
@ -340,22 +336,14 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
&& !downloadThread.getIndexes().isDownloadedFromInternet && !downloadThread.getIndexes().isDownloadedFromInternet
&& !downloadThread.getIndexes().downloadFromInternetFailed; && !downloadThread.getIndexes().downloadFromInternetFailed;
List<DownloadItem> suggestedVoice = new ArrayList<>();
if (!downloadIndexes) { if (!downloadIndexes) {
suggestedVoice.addAll(getVoiceItems(type)); suggestedVoice.addAll(app.getDownloadThread().getIndexes().getDownloadItemsForGroup(type));
} }
return suggestedVoice; return suggestedVoice;
} }
private List<DownloadItem> getVoiceItems(String type) {
try {
return DownloadResources.findIndexItemsAt(app, type);
} catch (IOException e) {
LOG.error(e);
}
return Collections.emptyList();
}
private enum InfoType { private enum InfoType {
TTS, TTS,
RECORDED RECORDED