From b37d96836853f06c17455705a2c2d0be8b31e95e Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 28 May 2020 15:55:51 +0300 Subject: [PATCH] Add wake time dialog --- OsmAnd/res/layout/bottom_sheet_item_descr.xml | 19 ++ .../bottom_sheet_item_with_switch_no_icon.xml | 8 +- OsmAnd/res/values/strings.xml | 4 + .../simpleitems/TitleItem.java | 4 +- .../BooleanPreferenceBottomSheet.java | 2 +- .../bottomsheets/WakeTimeBottomSheet.java | 206 ++++++++++++++++++ .../fragments/TurnScreenOnFragment.java | 26 ++- .../preferences/ListPreferenceEx.java | 2 +- 8 files changed, 259 insertions(+), 12 deletions(-) create mode 100644 OsmAnd/res/layout/bottom_sheet_item_descr.xml create mode 100644 OsmAnd/src/net/osmand/plus/settings/bottomsheets/WakeTimeBottomSheet.java diff --git a/OsmAnd/res/layout/bottom_sheet_item_descr.xml b/OsmAnd/res/layout/bottom_sheet_item_descr.xml new file mode 100644 index 0000000000..33ab71b1ec --- /dev/null +++ b/OsmAnd/res/layout/bottom_sheet_item_descr.xml @@ -0,0 +1,19 @@ + + \ No newline at end of file diff --git a/OsmAnd/res/layout/bottom_sheet_item_with_switch_no_icon.xml b/OsmAnd/res/layout/bottom_sheet_item_with_switch_no_icon.xml index d90d4fd44d..148e20cb46 100644 --- a/OsmAnd/res/layout/bottom_sheet_item_with_switch_no_icon.xml +++ b/OsmAnd/res/layout/bottom_sheet_item_with_switch_no_icon.xml @@ -1,17 +1,18 @@ + android:paddingRight="@dimen/content_padding"> - + If the \"%1$s\" option is enabled, the activity time will depend on it. + Keep screen off + Keep screen on + Select the screen activity time after waking up, the screen will not turn off if \"%1$s\" is enabled. Proximity sensor Power button Press on the device power button to turn on screen with OsmAnd on the foreground. diff --git a/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/simpleitems/TitleItem.java b/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/simpleitems/TitleItem.java index 966f02ef03..fb94762851 100644 --- a/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/simpleitems/TitleItem.java +++ b/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/simpleitems/TitleItem.java @@ -10,12 +10,12 @@ import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; public class TitleItem extends SimpleBottomSheetItem { - public TitleItem(String title) { + public TitleItem(CharSequence title) { this.title = title; this.layoutId = R.layout.bottom_sheet_item_title; } - public TitleItem(String title, @ColorRes int titleColorId) { + public TitleItem(CharSequence title, @ColorRes int titleColorId) { this.title = title; this.layoutId = R.layout.bottom_sheet_item_title; this.titleColorId = titleColorId; diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BooleanPreferenceBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BooleanPreferenceBottomSheet.java index 88df995e44..c988dbdf68 100644 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BooleanPreferenceBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BooleanPreferenceBottomSheet.java @@ -111,7 +111,7 @@ public class BooleanPreferenceBottomSheet extends BasePreferenceBottomSheet { if (description != null) { BaseBottomSheetItem preferenceDescription = new BottomSheetItemWithDescription.Builder() .setDescription(description) - .setLayoutId(R.layout.bottom_sheet_item_preference_descr) + .setLayoutId(R.layout.bottom_sheet_item_descr) .create(); items.add(preferenceDescription); } diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/WakeTimeBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/WakeTimeBottomSheet.java new file mode 100644 index 0000000000..5cf26a5060 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/WakeTimeBottomSheet.java @@ -0,0 +1,206 @@ +package net.osmand.plus.settings.bottomsheets; + +import android.content.Context; +import android.os.Bundle; +import android.view.View; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; + +import com.google.android.material.slider.Slider; + +import net.osmand.plus.R; +import net.osmand.plus.UiUtilities; +import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; +import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton; +import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription; +import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerItem; +import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerSpaceItem; +import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; +import net.osmand.plus.helpers.AndroidUiHelper; +import net.osmand.plus.settings.backend.ApplicationMode; +import net.osmand.plus.settings.fragments.ApplyQueryType; +import net.osmand.plus.settings.fragments.OnPreferenceChanged; +import net.osmand.plus.settings.preferences.ListPreferenceEx; + +public class WakeTimeBottomSheet extends BasePreferenceBottomSheet { + + public static final String TAG = WakeTimeBottomSheet.class.getSimpleName(); + + private static final String SELECTED_ENTRY_INDEX_KEY = "selected_entry_index_key"; + private static final String KEEP_SCREEN_ON_ENABLED = "keep_screen_on_enabled"; + + private ListPreferenceEx listPreference; + + private View sliderView; + + private int selectedEntryIndex = 1; + private boolean keepScreenOnEnabled; + + @Override + public void createMenuItems(Bundle savedInstanceState) { + Context ctx = getContext(); + listPreference = getListPreference(); + if (ctx == null || listPreference == null) { + return; + } + if (savedInstanceState != null) { + selectedEntryIndex = savedInstanceState.getInt(SELECTED_ENTRY_INDEX_KEY); + keepScreenOnEnabled = savedInstanceState.getBoolean(KEEP_SCREEN_ON_ENABLED); + } else { + int savedValIndex = listPreference.getValueIndex(); + keepScreenOnEnabled = savedValIndex == 0; + selectedEntryIndex = savedValIndex != 0 ? savedValIndex : 1; + } + + items.add(new TitleItem(listPreference.getDialogTitle())); + + BaseBottomSheetItem preferenceDescription = new BottomSheetItemWithDescription.Builder() + .setDescription(listPreference.getDescription()) + .setLayoutId(R.layout.bottom_sheet_item_descr) + .create(); + items.add(preferenceDescription); + + final String on = getString(R.string.keep_screen_on); + final String off = getString(R.string.keep_screen_off); + final BottomSheetItemWithCompoundButton[] preferenceBtn = new BottomSheetItemWithCompoundButton[1]; + preferenceBtn[0] = (BottomSheetItemWithCompoundButton) new BottomSheetItemWithCompoundButton.Builder() + .setChecked(keepScreenOnEnabled) + .setTitle(keepScreenOnEnabled ? on : off) + .setLayoutId(R.layout.bottom_sheet_item_with_switch_no_icon) + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + keepScreenOnEnabled = !keepScreenOnEnabled; + preferenceBtn[0].setTitle(keepScreenOnEnabled ? on : off); + preferenceBtn[0].setChecked(keepScreenOnEnabled); + AndroidUiHelper.updateVisibility(sliderView, !keepScreenOnEnabled); + setupHeightAndBackground(getView()); + } + }) + .create(); + items.add(preferenceBtn[0]); + + DividerItem dividerItem = new DividerItem(ctx); + int topMargin = ctx.getResources().getDimensionPixelSize(R.dimen.context_menu_subtitle_margin); + int startMargin = ctx.getResources().getDimensionPixelSize(R.dimen.content_padding); + dividerItem.setMargins(startMargin, topMargin, 0, 0); + items.add(dividerItem); + items.add(new DividerSpaceItem(ctx, ctx.getResources().getDimensionPixelSize(R.dimen.content_padding_small))); + + sliderView = UiUtilities.getInflater(ctx, nightMode).inflate(R.layout.bottom_sheet_item_slider_with_two_text, null); + AndroidUiHelper.updateVisibility(sliderView, !keepScreenOnEnabled); + + Context themedCtx = UiUtilities.getThemedContext(ctx, nightMode); + + TextView tvSliderTitle = sliderView.findViewById(android.R.id.title); + tvSliderTitle.setText(getString(R.string.shared_string_time)); + + final TextView tvSliderSummary = sliderView.findViewById(android.R.id.summary); + tvSliderSummary.setText(listPreference.getEntries()[selectedEntryIndex]); + + Slider slider = sliderView.findViewById(R.id.slider); + slider.setValue(selectedEntryIndex); + slider.setStepSize(1); + slider.setValueFrom(1); + slider.setValueTo(listPreference.getEntryValues().length - 1); + slider.addOnChangeListener(new Slider.OnChangeListener() { + @Override + public void onValueChange(@NonNull Slider slider, float value, boolean fromUser) { + if (fromUser) { + selectedEntryIndex = (int) value; + tvSliderSummary.setText(listPreference.getEntries()[selectedEntryIndex]); + } + } + }); + + int appModeColorId = getAppMode().getIconColorInfo().getColor(nightMode); + int appModeColor = ContextCompat.getColor(themedCtx, appModeColorId); + UiUtilities.setupSlider(slider, nightMode, appModeColor, true); + + items.add(new BaseBottomSheetItem.Builder() + .setCustomView(sliderView) + .create()); + + BaseBottomSheetItem timeoutDescription = new BottomSheetItemWithDescription.Builder() + .setDescription(getString(R.string.screen_timeout_descr, getString(R.string.system_screen_timeout))) + .setLayoutId(R.layout.bottom_sheet_item_descr) + .create(); + items.add(timeoutDescription); + } + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + for (BaseBottomSheetItem item : items) { + if (item instanceof BottomSheetItemWithCompoundButton) { + BottomSheetItemWithCompoundButton itemWithCompoundButton = (BottomSheetItemWithCompoundButton) item; + itemWithCompoundButton.getCompoundButton().setSaveEnabled(false); + } + } + } + + @Override + protected void onRightBottomButtonClick() { + if (keepScreenOnEnabled) { + selectedEntryIndex = 0; + } + Object[] entryValues = listPreference.getEntryValues(); + if (entryValues != null && selectedEntryIndex >= 0) { + Object value = entryValues[selectedEntryIndex]; + if (listPreference.callChangeListener(value)) { + listPreference.setValue(value); + } + Fragment target = getTargetFragment(); + if (target instanceof OnPreferenceChanged) { + ((OnPreferenceChanged) target).onPreferenceChanged(listPreference.getKey()); + } + } + dismiss(); + } + + @Override + protected int getDismissButtonTextId() { + return R.string.shared_string_cancel; + } + + @Override + protected int getRightBottomButtonTextId() { + return R.string.shared_string_apply; + } + + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putInt(SELECTED_ENTRY_INDEX_KEY, selectedEntryIndex); + outState.putBoolean(KEEP_SCREEN_ON_ENABLED, keepScreenOnEnabled); + } + + private ListPreferenceEx getListPreference() { + return (ListPreferenceEx) getPreference(); + } + + public static boolean showInstance(@NonNull FragmentManager fragmentManager, String prefId, Fragment target, boolean usedOnMap, + @Nullable ApplicationMode appMode, ApplyQueryType applyQueryType, + boolean profileDependent) { + try { + Bundle args = new Bundle(); + args.putString(PREFERENCE_ID, prefId); + + WakeTimeBottomSheet fragment = new WakeTimeBottomSheet(); + fragment.setArguments(args); + fragment.setUsedOnMap(usedOnMap); + fragment.setAppMode(appMode); + fragment.setApplyQueryType(applyQueryType); + fragment.setTargetFragment(target, 0); + fragment.setProfileDependent(profileDependent); + fragment.show(fragmentManager, TAG); + return true; + } catch (RuntimeException e) { + return false; + } + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/TurnScreenOnFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/TurnScreenOnFragment.java index ed7fb98719..96e462c32e 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/TurnScreenOnFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/TurnScreenOnFragment.java @@ -1,13 +1,16 @@ package net.osmand.plus.settings.fragments; import android.widget.ImageView; +import android.widget.TextView; import androidx.fragment.app.FragmentManager; import androidx.preference.Preference; import androidx.preference.PreferenceViewHolder; import net.osmand.plus.R; +import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.bottomsheets.ScreenTimeoutBottomSheet; +import net.osmand.plus.settings.bottomsheets.WakeTimeBottomSheet; import net.osmand.plus.settings.preferences.ListPreferenceEx; import net.osmand.plus.settings.preferences.SwitchPreferenceEx; @@ -27,22 +30,34 @@ public class TurnScreenOnFragment extends BaseSettingsFragment implements OnPref @Override protected void onBindPreferenceViewHolder(Preference preference, PreferenceViewHolder holder) { super.onBindPreferenceViewHolder(preference, holder); - if (settings.TURN_SCREEN_ON_TIME_INT.getId().equals(preference.getKey()) && preference instanceof ListPreferenceEx) { + String prefId = preference.getKey(); + if (settings.TURN_SCREEN_ON_TIME_INT.getId().equals(prefId) && preference instanceof ListPreferenceEx) { Object currentValue = ((ListPreferenceEx) preference).getValue(); ImageView imageView = (ImageView) holder.findViewById(android.R.id.icon); if (imageView != null && currentValue instanceof Integer) { - boolean enabled = preference.isEnabled() && (Integer) currentValue > 0; + boolean enabled = preference.isEnabled() && (Integer) currentValue != 0; imageView.setEnabled(enabled); } + } else if ("turn_screen_on_info".equals(prefId) || "turn_screen_on_options_info".equals(prefId)) { + TextView titleView = (TextView) holder.findViewById(android.R.id.title); + if (titleView != null) { + titleView.setTextColor(getDisabledTextColor()); + } } } @Override public void onDisplayPreferenceDialog(Preference preference) { - if (settings.USE_SYSTEM_SCREEN_TIMEOUT.getId().equals(preference.getKey())) { - FragmentManager fragmentManager = getFragmentManager(); + FragmentManager fragmentManager = getFragmentManager(); + ApplicationMode appMode = getSelectedAppMode(); + String prefId = preference.getKey(); + if (settings.USE_SYSTEM_SCREEN_TIMEOUT.getId().equals(prefId)) { if (fragmentManager != null) { - ScreenTimeoutBottomSheet.showInstance(fragmentManager, preference.getKey(), this, false, getSelectedAppMode(), getApplyQueryType(), isProfileDependent()); + ScreenTimeoutBottomSheet.showInstance(fragmentManager, prefId, this, false, appMode, getApplyQueryType(), isProfileDependent()); + } + } else if (settings.TURN_SCREEN_ON_TIME_INT.getId().equals(prefId)) { + if (fragmentManager != null) { + WakeTimeBottomSheet.showInstance(fragmentManager, prefId, this, false, appMode, getApplyQueryType(), isProfileDependent()); } } else { super.onDisplayPreferenceDialog(preference); @@ -68,6 +83,7 @@ public class TurnScreenOnFragment extends BaseSettingsFragment implements OnPref turnScreenOnTime.setEnabled(!settings.USE_SYSTEM_SCREEN_TIMEOUT.getModeValue(getSelectedAppMode())); turnScreenOnTime.setEntries(entries); turnScreenOnTime.setEntryValues(entryValues); + turnScreenOnTime.setDescription(getString(R.string.turn_screen_on_wake_time_descr, getString(R.string.keep_screen_on))); turnScreenOnTime.setIcon(getPersistentPrefIcon(R.drawable.ic_action_time_span)); } diff --git a/OsmAnd/src/net/osmand/plus/settings/preferences/ListPreferenceEx.java b/OsmAnd/src/net/osmand/plus/settings/preferences/ListPreferenceEx.java index 27e0c5a9c0..0743cd9c8b 100644 --- a/OsmAnd/src/net/osmand/plus/settings/preferences/ListPreferenceEx.java +++ b/OsmAnd/src/net/osmand/plus/settings/preferences/ListPreferenceEx.java @@ -87,7 +87,7 @@ public class ListPreferenceEx extends DialogPreference { return -1; } - private int getValueIndex() { + public int getValueIndex() { return findIndexOfValue(selectedValue); }