1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.display; 18 19 import android.content.Context; 20 import android.content.res.Configuration; 21 import android.os.Bundle; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.view.accessibility.AccessibilityEvent; 26 import android.widget.SeekBar; 27 import android.widget.SeekBar.OnSeekBarChangeListener; 28 import android.widget.TextView; 29 30 import androidx.viewpager.widget.ViewPager; 31 import androidx.viewpager.widget.ViewPager.OnPageChangeListener; 32 33 import com.android.settings.R; 34 import com.android.settings.SettingsPreferenceFragment; 35 import com.android.settings.widget.DotsPageIndicator; 36 import com.android.settings.widget.LabeledSeekBar; 37 38 /** 39 * Preference fragment shows a preview and a seek bar to adjust a specific settings. 40 */ 41 public abstract class PreviewSeekBarPreferenceFragment extends SettingsPreferenceFragment { 42 43 /** List of entries corresponding the settings being set. */ 44 protected String[] mEntries; 45 46 /** Index of the entry corresponding to initial value of the settings. */ 47 protected int mInitialIndex; 48 49 /** Index of the entry corresponding to current value of the settings. */ 50 protected int mCurrentIndex; 51 52 private ViewPager mPreviewPager; 53 private PreviewPagerAdapter mPreviewPagerAdapter; 54 private DotsPageIndicator mPageIndicator; 55 56 private TextView mLabel; 57 private LabeledSeekBar mSeekBar; 58 private View mLarger; 59 private View mSmaller; 60 61 private class onPreviewSeekBarChangeListener implements OnSeekBarChangeListener { 62 private boolean mSeekByTouch; 63 64 @Override onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)65 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 66 setPreviewLayer(progress, false); 67 if (!mSeekByTouch) { 68 commit(); 69 } 70 } 71 72 @Override onStartTrackingTouch(SeekBar seekBar)73 public void onStartTrackingTouch(SeekBar seekBar) { 74 mSeekByTouch = true; 75 } 76 77 @Override onStopTrackingTouch(SeekBar seekBar)78 public void onStopTrackingTouch(SeekBar seekBar) { 79 if (mPreviewPagerAdapter.isAnimating()) { 80 mPreviewPagerAdapter.setAnimationEndAction(() -> commit()); 81 } else { 82 commit(); 83 } 84 mSeekByTouch = false; 85 } 86 } 87 88 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)89 public View onCreateView(LayoutInflater inflater, ViewGroup container, 90 Bundle savedInstanceState) { 91 final View root = super.onCreateView(inflater, container, savedInstanceState); 92 final ViewGroup listContainer = root.findViewById(android.R.id.list_container); 93 listContainer.removeAllViews(); 94 95 final View content = inflater.inflate(getActivityLayoutResId(), listContainer, false); 96 listContainer.addView(content); 97 98 mLabel = content.findViewById(R.id.current_label); 99 100 // The maximum SeekBar value always needs to be non-zero. If there's 101 // only one available value, we'll handle this by disabling the 102 // seek bar. 103 final int max = Math.max(1, mEntries.length - 1); 104 105 mSeekBar = content.findViewById(R.id.seek_bar); 106 mSeekBar.setLabels(mEntries); 107 mSeekBar.setMax(max); 108 109 mSmaller = content.findViewById(R.id.smaller); 110 mSmaller.setOnClickListener(v -> { 111 final int progress = mSeekBar.getProgress(); 112 if (progress > 0) { 113 mSeekBar.setProgress(progress - 1, true); 114 } 115 }); 116 117 mLarger = content.findViewById(R.id.larger); 118 mLarger.setOnClickListener(v -> { 119 final int progress = mSeekBar.getProgress(); 120 if (progress < mSeekBar.getMax()) { 121 mSeekBar.setProgress(progress + 1, true); 122 } 123 }); 124 125 if (mEntries.length == 1) { 126 // The larger and smaller buttons will be disabled when we call 127 // setPreviewLayer() later in this method. 128 mSeekBar.setEnabled(false); 129 } 130 131 final Context context = getContext(); 132 final Configuration origConfig = context.getResources().getConfiguration(); 133 final boolean isLayoutRtl = origConfig.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; 134 Configuration[] configurations = new Configuration[mEntries.length]; 135 for (int i = 0; i < mEntries.length; ++i) { 136 configurations[i] = createConfig(origConfig, i); 137 } 138 139 final int[] previews = getPreviewSampleResIds(); 140 mPreviewPager = content.findViewById(R.id.preview_pager); 141 mPreviewPagerAdapter = new PreviewPagerAdapter(context, isLayoutRtl, 142 previews, configurations); 143 mPreviewPager.setAdapter(mPreviewPagerAdapter); 144 mPreviewPager.setCurrentItem(isLayoutRtl ? previews.length - 1 : 0); 145 mPreviewPager.addOnPageChangeListener(mPreviewPageChangeListener); 146 147 mPageIndicator = content.findViewById(R.id.page_indicator); 148 if (previews.length > 1) { 149 mPageIndicator.setViewPager(mPreviewPager); 150 mPageIndicator.setVisibility(View.VISIBLE); 151 mPageIndicator.setOnPageChangeListener(mPageIndicatorPageChangeListener); 152 } else { 153 mPageIndicator.setVisibility(View.GONE); 154 } 155 156 setPreviewLayer(mInitialIndex, false); 157 return root; 158 } 159 160 @Override onStart()161 public void onStart() { 162 super.onStart(); 163 // Set SeekBar listener here to avoid onProgressChanged() is called 164 // during onRestoreInstanceState(). 165 mSeekBar.setProgress(mCurrentIndex); 166 mSeekBar.setOnSeekBarChangeListener(new onPreviewSeekBarChangeListener()); 167 } 168 169 @Override onStop()170 public void onStop() { 171 super.onStop(); 172 mSeekBar.setOnSeekBarChangeListener(null); 173 } 174 175 /** Resource id of the layout for this preference fragment. */ getActivityLayoutResId()176 protected abstract int getActivityLayoutResId(); 177 178 /** Resource id of the layout that defines the contents inside preview screen. */ getPreviewSampleResIds()179 protected abstract int[] getPreviewSampleResIds(); 180 181 /** 182 * Creates new configuration based on the current position of the SeekBar. 183 */ createConfig(Configuration origConfig, int index)184 protected abstract Configuration createConfig(Configuration origConfig, int index); 185 186 /** 187 * Persists the selected value and sends a configuration change. 188 */ commit()189 protected abstract void commit(); 190 setPreviewLayer(int index, boolean animate)191 private void setPreviewLayer(int index, boolean animate) { 192 mLabel.setText(mEntries[index]); 193 mSmaller.setEnabled(index > 0); 194 mLarger.setEnabled(index < mEntries.length - 1); 195 setPagerIndicatorContentDescription(mPreviewPager.getCurrentItem()); 196 mPreviewPagerAdapter.setPreviewLayer(index, mCurrentIndex, 197 mPreviewPager.getCurrentItem(), animate); 198 199 mCurrentIndex = index; 200 } 201 202 private void setPagerIndicatorContentDescription(int position) { 203 mPageIndicator.setContentDescription( 204 getString(R.string.preview_page_indicator_content_description, 205 position + 1, getPreviewSampleResIds().length)); 206 } 207 208 private OnPageChangeListener mPreviewPageChangeListener = new OnPageChangeListener() { 209 @Override 210 public void onPageScrollStateChanged(int state) { 211 // Do nothing. 212 } 213 214 @Override 215 public void onPageScrolled(int position, float positionOffset, 216 int positionOffsetPixels) { 217 // Do nothing. 218 } 219 220 @Override 221 public void onPageSelected(int position) { 222 mPreviewPager.sendAccessibilityEvent(AccessibilityEvent.TYPE_ANNOUNCEMENT); 223 } 224 }; 225 226 private OnPageChangeListener mPageIndicatorPageChangeListener = new OnPageChangeListener() { 227 @Override 228 public void onPageScrollStateChanged(int state) { 229 // Do nothing. 230 } 231 232 @Override 233 public void onPageScrolled(int position, float positionOffset, 234 int positionOffsetPixels) { 235 // Do nothing. 236 } 237 238 @Override 239 public void onPageSelected(int position) { 240 setPagerIndicatorContentDescription(position); 241 } 242 }; 243 } 244