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