Add wake time dialog

This commit is contained in:
Vitaliy 2020-05-28 15:55:51 +03:00
parent e570176963
commit b37d968368
8 changed files with 259 additions and 12 deletions

View file

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<net.osmand.plus.widgets.TextViewEx xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:letterSpacing="@dimen/text_button_letter_spacing"
android:minHeight="@dimen/bottom_sheet_list_item_height"
android:paddingStart="@dimen/content_padding"
android:paddingLeft="@dimen/content_padding"
android:paddingTop="@dimen/bottom_sheet_image_text_margin_start"
android:paddingEnd="@dimen/content_padding"
android:paddingRight="@dimen/content_padding"
android:paddingBottom="@dimen/gpx_small_text_margin"
android:textColor="?android:attr/textColorSecondary"
android:textSize="@dimen/default_list_text_size"
app:typeface="@string/font_roboto_regular"
tools:text="@string/auto_zoom_map_descr" />

View file

@ -1,17 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_sheet_list_item_height"
android:background="?attr/selectableItemBackground"
android:gravity="center_vertical"
android:minHeight="@dimen/bottom_sheet_list_item_height"
android:paddingStart="@dimen/content_padding"
android:paddingLeft="@dimen/content_padding"
android:paddingRight="@dimen/content_padding"
android:paddingEnd="@dimen/content_padding"
android:paddingStart="@dimen/content_padding">
android:paddingRight="@dimen/content_padding">
<TextView
<net.osmand.plus.widgets.TextViewEx
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
@ -21,6 +22,7 @@
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="@style/TextAppearance.ListItemTitle"
app:typeface="@string/font_roboto_regular"
tools:text="Some Title" />
<androidx.appcompat.widget.SwitchCompat

View file

@ -11,6 +11,10 @@
Thx - Hardy
-->
<string name="screen_timeout_descr">If the \"%1$s\" option is enabled, the activity time will depend on it.</string>
<string name="keep_screen_off">Keep screen off</string>
<string name="keep_screen_on">Keep screen on</string>
<string name="turn_screen_on_wake_time_descr">Select the screen activity time after waking up, the screen will not turn off if \"%1$s\" is enabled.</string>
<string name="turn_screen_on_proximity_sensor">Proximity sensor</string>
<string name="turn_screen_on_power_button">Power button</string>
<string name="turn_screen_on_power_button_descr">Press on the device power button to turn on screen with OsmAnd on the foreground.</string>

View file

@ -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;

View file

@ -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);
}

View file

@ -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;
}
}
}

View file

@ -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));
}

View file

@ -87,7 +87,7 @@ public class ListPreferenceEx extends DialogPreference {
return -1;
}
private int getValueIndex() {
public int getValueIndex() {
return findIndexOfValue(selectedValue);
}