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;
}
public static List<DownloadItem> findIndexItemsAt(OsmandApplication app, String groupTypeVoice) throws IOException {
DownloadIndexesThread downloadThread = app.getDownloadThread();
DownloadResources indexes = downloadThread.getIndexes();
DownloadResourceGroup groupVoice = indexes.getSubGroupById(groupTypeVoice);
List<DownloadItem> items = groupVoice.getIndividualDownloadItems();
return new ArrayList<>(items);
public List<DownloadItem> getDownloadItemsForGroup(String groupId) {
DownloadResourceGroup group = getSubGroupById(groupId);
if (group != null) {
return group.getIndividualDownloadItems();
}
return Collections.emptyList();
}
public static List<IndexItem> findIndexItemsAt(OsmandApplication app,

View file

@ -9,11 +9,11 @@ import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.TextView;
import androidx.annotation.DimenRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
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.download.DownloadIndexesThread;
import net.osmand.plus.download.DownloadItem;
import net.osmand.plus.download.DownloadResources;
import net.osmand.plus.download.DownloadValidationManager;
import net.osmand.plus.download.IndexItem;
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.OsmandSettings;
import net.osmand.plus.track.TrackSelectSegmentBottomSheet;
import net.osmand.plus.widgets.MultiStateToggleButton;
import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem;
import net.osmand.plus.widgets.multistatetoggle.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 java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
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.VOICE_HEADER_REC;
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;
public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragment implements DownloadIndexesThread.DownloadEvents {
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);
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 Context context;
private InfoType selectedVoiceType = InfoType.TTS;
private boolean isTtsDescription;
private OsmandSettings settings;
private DownloadIndexesThread downloadThread;
private InfoType selectedVoiceType = InfoType.TTS;
public static void showInstance(@NonNull FragmentManager fm, Fragment targetFragment, String key, boolean usedOnMap) {
try {
if (!fm.isStateSaved()) {
Bundle args = new Bundle();
args.putString(PREFERENCE_ID, key);
VoiceLanguageBottomSheetFragment fragment = new VoiceLanguageBottomSheetFragment();
fragment.setRetainInstance(true);
fragment.setArguments(args);
fragment.setUsedOnMap(usedOnMap);
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
public void newDownloadIndexes() {
updateItems();
@ -95,6 +147,7 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
Activity activity = getActivity();
View mainView = getView();
if (activity != null && mainView != null) {
Context context = requireContext();
LinearLayout itemsContainer = (LinearLayout) mainView.findViewById(useScrollableItemsContainer()
? R.id.scrollable_items_container : R.id.non_scrollable_items_container);
if (itemsContainer != null) {
@ -111,10 +164,6 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
@Override
public void downloadInProgress() {
final OsmandApplication app = getMyApplication();
if (app == null) {
return;
}
IndexItem downloadIndexItem = downloadThread.getCurrentDownloadingItem();
if (downloadIndexItem != null) {
for (BaseBottomSheetItem item : items) {
@ -141,63 +190,11 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
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) {
RadioItem tts = createRadioButton(InfoType.TTS, R.string.tts_title);
RadioItem recorded = createRadioButton(InfoType.RECORDED, R.string.shared_string_recorded);
TextRadioItem tts = createRadioButton(InfoType.TTS, R.string.tts_title);
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);
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);
RadioItem item = new RadioItem(title);
item.setOnClickListener(new MultiStateToggleButton.OnRadioItemClickListener() {
TextRadioItem item = new TextRadioItem(title);
item.setOnClickListener(new OnRadioItemClickListener() {
@Override
public boolean onRadioItemClick(RadioItem radioItem, View view) {
selectedVoiceType = voiceType;
@ -225,23 +222,23 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
private void createVoiceView() {
List<DownloadItem> voiceItems;
if (selectedVoiceType == InfoType.TTS) {
voiceItems = getVoiceList(VOICE_TTS);
isTtsDescription = true;
voiceItems = getVoiceList(VOICE_TTS_KEY);
} else {
voiceItems = getVoiceList(VOICE_REC);
isTtsDescription = false;
voiceItems = getVoiceList(VOICE_REC_KEY);
}
createSuggestedVoiceItems(voiceItems);
createSuggestedVoiceItemsView(voiceItems);
}
private void createSuggestedVoiceItems(List<DownloadItem> suggestedMaps) {
RoutingHelper routingHelper = app.getRoutingHelper();
final ApplicationMode applicationMode = routingHelper.getAppMode();
private void createSuggestedVoiceItemsView(List<DownloadItem> suggestedMaps) {
final Context context = requireContext();
final OsmandSettings settings = app.getSettings();
final ApplicationMode applicationMode = settings.getApplicationMode();
final LayoutInflater inflater = UiUtilities.getInflater(app, nightMode);
for (final DownloadItem indexItem : suggestedMaps) {
View view = UiUtilities.getInflater(context, nightMode).inflate(R.layout.list_item_icon_and_download, null);
AndroidUtils.setBackground(view, UiUtilities.getSelectableDrawable(context));
view.findViewById(R.id.divider).setVisibility(View.GONE);
View container = inflater.inflate(R.layout.list_item_icon_and_download, null);
AndroidUtils.setBackground(container, UiUtilities.getSelectableDrawable(context));
container.findViewById(R.id.divider).setVisibility(View.GONE);
String systemLanguage = Resources.getSystem().getConfiguration().locale.getLanguage();
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;
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;
secondaryIcon.setColorFilter(ContextCompat.getColor(context, activeColorResId));
final ProgressBar progressBar = view.findViewById(R.id.ProgressBar);
final TextView textDescription = view.findViewById(R.id.description);
final TextView compoundButton = view.findViewById(R.id.compound_button);
final ProgressBar progressBar = container.findViewById(R.id.ProgressBar);
final TextView textDescription = container.findViewById(R.id.description);
RadioButton radioButton = (RadioButton) container.findViewById(R.id.compound_button);
UiUtilities.setupCompoundButton(radioButton, nightMode, PROFILE_DEPENDENT);
AndroidUiHelper.updateVisibility(secondaryIcon, true);
AndroidUiHelper.updateVisibility(progressBar, downloadThread.isDownloading((IndexItem) indexItem));
@ -272,17 +270,17 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
}
if (indexItem.isDownloaded()) {
AndroidUiHelper.updateVisibility(compoundButton, true);
AndroidUiHelper.updateVisibility(radioButton, true);
AndroidUiHelper.updateVisibility(secondaryIcon, false);
} else {
AndroidUiHelper.updateVisibility(compoundButton, false);
AndroidUiHelper.updateVisibility(radioButton, false);
AndroidUiHelper.updateVisibility(secondaryIcon, true);
}
boolean selected = settings.VOICE_PROVIDER.getModeValue(applicationMode).contains(indexItem.getBasename());
final BottomSheetItemWithCompoundButton[] voiceDownloadedItem = new BottomSheetItemWithCompoundButton[1];
voiceDownloadedItem[0] = (BottomSheetItemWithCompoundButton) new BottomSheetItemWithCompoundButton.Builder()
.setCompoundButtonColorId(activeColorResId)
.setChecked(settings.VOICE_PROVIDER.getModeValue(applicationMode).contains(indexItem.getBasename()))
.setChecked(selected)
.setDescription(description)
.setIconHidden(true)
.setTitle(title)
@ -294,7 +292,7 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
boolean checked = !voiceDownloadedItem[0].isChecked();
voiceDownloadedItem[0].setChecked(checked);
settings.VOICE_PROVIDER.setModeValue(applicationMode, indexItem.getBasename());
updateItems();
dismiss();
} else {
if (downloadThread.isDownloading((IndexItem) indexItem)) {
downloadThread.cancelDownload(indexItem);
@ -311,7 +309,7 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
}
}
})
.setCustomView(view)
.setCustomView(container)
.create();
items.add(voiceDownloadedItem[0]);
}
@ -330,8 +328,6 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
}
public List<DownloadItem> getVoiceList(String type) {
List<DownloadItem> suggestedVoice = new ArrayList<>();
if (!downloadThread.getIndexes().isDownloadedFromInternet && settings.isInternetConnectionAvailable()) {
downloadThread.runReloadIndexFiles();
}
@ -340,22 +336,14 @@ public class VoiceLanguageBottomSheetFragment extends MenuBottomSheetDialogFragm
&& !downloadThread.getIndexes().isDownloadedFromInternet
&& !downloadThread.getIndexes().downloadFromInternetFailed;
List<DownloadItem> suggestedVoice = new ArrayList<>();
if (!downloadIndexes) {
suggestedVoice.addAll(getVoiceItems(type));
suggestedVoice.addAll(app.getDownloadThread().getIndexes().getDownloadItemsForGroup(type));
}
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 {
TTS,
RECORDED