summaryrefslogtreecommitdiffstats
path: root/packages/SystemUI/src/com/android/systemui/qs/tiles/ScreenTimeoutTile.java
blob: 8e87ac10b2fb6741a4a6011474f03bb84ba1bb2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * Copyright (C) 2015 The CyanogenMod Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.qs.tiles;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.graphics.drawable.AnimatedVectorDrawable;
import android.net.Uri;
import android.provider.Settings;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.android.systemui.R;
import com.android.systemui.qs.QSDetailItemsList;
import com.android.systemui.qs.QSTile;

import java.util.ArrayList;
import java.util.Arrays;

public class ScreenTimeoutTile extends QSTile<ScreenTimeoutTile.TimeoutState> {
    private static final Intent SETTINGS_INTENT = new Intent("android.settings.DISPLAY_SETTINGS");
    private static final String TIMEOUT_ENTRIES_NAME = "screen_timeout_entries";
    private static final String TIMEOUT_VALUES_NAME = "screen_timeout_values";
    private static final String SETTINGS_PACKAGE_NAME = "com.android.settings";
    private String[] mEntries, mValues;
    private boolean mShowingDetail;
    ArrayList<AnimatedVectorDrawable> mAnimationList
            = new ArrayList<AnimatedVectorDrawable>();

    public ScreenTimeoutTile(Host host) {
        super(host);
        populateList();
    }

    private void populateList() {
        try {
            Context context = mContext.createPackageContext(SETTINGS_PACKAGE_NAME, 0);
            Resources mSettingsResources = context.getResources();
            int id = mSettingsResources.getIdentifier(TIMEOUT_ENTRIES_NAME,
                    "array", SETTINGS_PACKAGE_NAME);
            if (id <= 0) {
                return;
            }
            mEntries = mSettingsResources.getStringArray(id);
            id = mSettingsResources.getIdentifier(TIMEOUT_VALUES_NAME,
                    "array", SETTINGS_PACKAGE_NAME);
            if (id <= 0) {
                return;
            }
            mValues = mSettingsResources.getStringArray(id);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private int getScreenTimeout() {
        return Settings.System.getInt(mContext.getContentResolver(),
                Settings.System.SCREEN_OFF_TIMEOUT, 0);
    }

    @Override
    public DetailAdapter getDetailAdapter() {
        return new LocationDetailAdapter();
    }

    private ContentObserver mObserver = new ContentObserver(mHandler) {
        @Override
        public void onChange(boolean selfChange, Uri uri) {
            refreshState();
        }
    };

    @Override
    public void setListening(boolean listening) {
        if (listening) {
            mContext.getContentResolver().registerContentObserver(
                    Settings.System.getUriFor(Settings.System.SCREEN_OFF_TIMEOUT),
                    false, mObserver);
        } else {
            mContext.getContentResolver().unregisterContentObserver(mObserver);
        }
    }

    @Override
    protected TimeoutState newTileState() {
        return new TimeoutState();
    }

    @Override
    protected void handleClick() {
        if (mEntries.length > 0) {
            mShowingDetail = true;
            mAnimationList.clear();
            showDetail(true);
        }
    }

    @Override
    protected void handleLongClick() {
        mHost.startSettingsActivity(SETTINGS_INTENT);
    }

    private String makeTimeoutSummaryString(int timeout) {
        Resources res = mContext.getResources();
        int resId;

        /* ms -> seconds */
        timeout /= 1000;

        if (timeout >= 60 && timeout % 60 == 0) {
            /* seconds -> minutes */
            timeout /= 60;
            if (timeout >= 60 && timeout % 60 == 0) {
                /* minutes -> hours */
                timeout /= 60;
                resId = com.android.internal.R.plurals.duration_hours;
            } else {
                resId = com.android.internal.R.plurals.duration_minutes;
            }
        } else {
            resId = com.android.internal.R.plurals.duration_seconds;
        }

        return res.getQuantityString(resId, timeout, timeout);
    }

    public static final class TimeoutState extends QSTile.State {
        int previousTimeout;
    }

    private enum Bucket {
        SMALL(0, 30000),
        MEDIUM(60000,300000),
        LARGE(600000, 1800000);
        private final int start;
        private final int stop;

        Bucket(int start, int stop) {
            this.start = start;
            this.stop = stop;
        }

        public static Bucket getBucket(int value) {
            for (Bucket item : Bucket.values()) {
                if (value >= item.start && value <= item.stop) {
                    return item;
                }
            }
            return null;
        }

    }
    @Override
    protected void handleUpdateState(final TimeoutState state, Object arg) {
        if (mAnimationList.isEmpty() && mShowingDetail && arg == null) {
            return;
        }

        int newTimeout = getScreenTimeout();

        int drawableId = 0;
        Resources resources = mContext.getResources();
        Bucket nextBucket = Bucket.getBucket(newTimeout);
        Bucket previousBucket = Bucket.getBucket(state.previousTimeout);

        switch (state.previousTimeout) {
            case 0:
            case 15000:
            case 30000:
                // Default
                drawableId = R.drawable.ic_qs_screen_timeout_med_reverse_avd;
                if (nextBucket == Bucket.MEDIUM) {
                    // Medium
                    drawableId = R.drawable.ic_qs_screen_timeout_short_avd;
                } else if (nextBucket == Bucket.LARGE) {
                    // Large
                    drawableId = R.drawable.ic_qs_screen_timeout_short_reverse_avd;
                }
                break;
            case 60000:
            case 120000:
            case 300000:
                // Default
                drawableId = R.drawable.ic_qs_screen_timeout_short_avd;
                if (nextBucket == Bucket.SMALL) {
                    // Small
                    drawableId = R.drawable.ic_qs_screen_timeout_med_reverse_avd;
                } else if (nextBucket == Bucket.LARGE) {
                    // Large
                    drawableId = R.drawable.ic_qs_screen_timeout_med_avd;
                }
                break;
            case 600000:
            case 1800000:
                drawableId = R.drawable.ic_qs_screen_timeout_med_avd;
                if (nextBucket == Bucket.MEDIUM) {
                    // Small
                    drawableId = R.drawable.ic_qs_screen_timeout_long_reverse_avd;
                } else if (nextBucket == Bucket.SMALL) {
                    // Large
                    drawableId = R.drawable.ic_qs_screen_timeout_long_avd;
                }
                break;
        }

        if (state.icon == null || previousBucket != nextBucket) {
            final AnimatedVectorDrawable d = (AnimatedVectorDrawable) resources.getDrawable(drawableId);
            mUiHandler.post(new Runnable() {
                @Override
                public void run() {
                    d.start();
                }
            });
            state.icon = d;
        }

        runNextAnimation(state);
        state.visible = true;
        state.label = makeTimeoutSummaryString(newTimeout);
        state.previousTimeout = newTimeout;
    }

    private void runNextAnimation(final TimeoutState state) {
        if (mAnimationList.isEmpty()) {
            return;
        }
        state.icon = mAnimationList.remove(0);
        mUiHandler.post(new Runnable() {
            @Override
            public void run() {
                ((AnimatedVectorDrawable) state.icon).start();
            }
        });
    }

    private class RadioAdapter extends ArrayAdapter<String> {

        public RadioAdapter(Context context, int resource, String[] objects) {
            super(context, resource, objects);
        }

        public RadioAdapter(Context context, int resource,
                            int textViewResourceId, String[] objects) {
            super(context, resource, textViewResourceId, objects);
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            view = super.getView(position, view, parent);

            view.setMinimumHeight(mContext.getResources() .getDimensionPixelSize(
                    R.dimen.qs_detail_item_height));

            return view;
        }

    }
    private class LocationDetailAdapter implements DetailAdapter, AdapterView.OnItemClickListener {
        private QSDetailItemsList mItems;

        @Override
        public int getTitle() {
            return R.string.quick_settings_screen_timeout_detail_title;
        }

        @Override
        public Boolean getToggleState() {
            return null;
        }

        @Override
        public Intent getSettingsIntent() {
            return SETTINGS_INTENT;
        }

        @Override
        public void setToggleState(boolean state) {
            // noop
        }

        @Override
        public View createDetailView(Context context, View convertView, ViewGroup parent) {
            mItems = QSDetailItemsList.convertOrInflate(context, convertView, parent);
            ListView listView = mItems.getListView();
            listView.setOnItemClickListener(this);
            listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            listView.setDivider(null);
            RadioAdapter adapter = new RadioAdapter(context,
                    android.R.layout.simple_list_item_single_choice, mEntries);
            int indexOfSelection = Arrays.asList(mValues).indexOf(String.valueOf(getScreenTimeout()));
            mItems.setAdapter(adapter);
            listView.setItemChecked(indexOfSelection, true);
            mItems.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
                @Override
                public void onViewAttachedToWindow(View v) {
                }

                @Override
                public void onViewDetachedFromWindow(View v) {
                    mUiHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mShowingDetail = false;
                            refreshState(true);
                        }
                    }, 100);

                }
            });
            return mItems;
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int selectedTimeout = Integer.valueOf(mValues[position]);
            Settings.System.putInt(mContext.getContentResolver(),
                    Settings.System.SCREEN_OFF_TIMEOUT, selectedTimeout);
        }
    }
}