QSCustomizer.java revision bd6dbb0698eca76c4ee1337ef1a73b67c8a64ae4
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 */
16package com.android.systemui.qs.customize;
17
18import android.content.Context;
19import android.util.AttributeSet;
20import android.util.TypedValue;
21import android.view.ContextThemeWrapper;
22import android.view.Menu;
23import android.view.MenuItem;
24import android.view.View;
25import android.widget.LinearLayout;
26import android.widget.Toolbar;
27import android.widget.Toolbar.OnMenuItemClickListener;
28
29import com.android.systemui.R;
30import com.android.systemui.SystemUIApplication;
31import com.android.systemui.qs.QSTile.Host.Callback;
32import com.android.systemui.statusbar.phone.PhoneStatusBar;
33import com.android.systemui.statusbar.phone.QSTileHost;
34import com.android.systemui.tuner.QSPagingSwitch;
35
36import java.util.ArrayList;
37
38/**
39 * Allows full-screen customization of QS, through show() and hide().
40 *
41 * This adds itself to the status bar window, so it can appear on top of quick settings and
42 * *someday* do fancy animations to get into/out of it.
43 */
44public class QSCustomizer extends LinearLayout implements OnMenuItemClickListener, Callback {
45
46    private static final int MENU_SAVE = Menu.FIRST;
47    private static final int MENU_RESET = Menu.FIRST + 1;
48
49    private PhoneStatusBar mPhoneStatusBar;
50
51    private Toolbar mToolbar;
52    private CustomQSPanel mQsPanel;
53
54    private boolean isShown;
55    private CustomQSTileHost mHost;
56
57    public QSCustomizer(Context context, AttributeSet attrs) {
58        super(new ContextThemeWrapper(context, android.R.style.Theme_Material), attrs);
59        mPhoneStatusBar = ((SystemUIApplication) mContext.getApplicationContext())
60                .getComponent(PhoneStatusBar.class);
61    }
62
63    public void setHost(QSTileHost host) {
64        mHost = new CustomQSTileHost(mContext, host);
65        mHost.setCallback(this);
66        mQsPanel.setTiles(mHost.getTiles());
67        mQsPanel.setHost(mHost);
68        mHost.setSavedTiles();
69    }
70
71    @Override
72    protected void onFinishInflate() {
73        super.onFinishInflate();
74        mToolbar = (Toolbar) findViewById(com.android.internal.R.id.action_bar);
75        TypedValue value = new TypedValue();
76        mContext.getTheme().resolveAttribute(android.R.attr.homeAsUpIndicator, value, true);
77        mToolbar.setNavigationIcon(
78                getResources().getDrawable(value.resourceId, mContext.getTheme()));
79        mToolbar.setNavigationOnClickListener(new OnClickListener() {
80            @Override
81            public void onClick(View v) {
82                // TODO: Is this all we want...?
83                hide();
84            }
85        });
86        mToolbar.setOnMenuItemClickListener(this);
87        mToolbar.getMenu().add(Menu.NONE, MENU_SAVE, 0, mContext.getString(R.string.save))
88                .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
89        mToolbar.getMenu().add(Menu.NONE, MENU_RESET, 0,
90                mContext.getString(com.android.internal.R.string.reset));
91
92        mQsPanel = (CustomQSPanel) findViewById(R.id.quick_settings_panel);
93    }
94
95    public void show() {
96        isShown = true;
97        mHost.setSavedTiles();
98        // TODO: Fancy shmancy reveal.
99        mPhoneStatusBar.getStatusBarWindow().addView(this);
100    }
101
102    public void hide() {
103        isShown = false;
104        // TODO: Similarly awesome or better hide.
105        mPhoneStatusBar.getStatusBarWindow().removeView(this);
106    }
107
108    public boolean isCustomizing() {
109        return isShown;
110    }
111
112    private void reset() {
113        ArrayList<String> tiles = new ArrayList<>();
114        for (String tile : QSPagingSwitch.QS_PAGE_TILES.split(",")) {
115            tiles.add(tile);
116        }
117        mHost.setTiles(tiles);
118    }
119
120    private void save() {
121        mHost.saveCurrentTiles();
122        hide();
123    }
124
125    @Override
126    public boolean onMenuItemClick(MenuItem item) {
127        switch (item.getItemId()) {
128            case MENU_SAVE:
129                save();
130                break;
131            case MENU_RESET:
132                reset();
133                break;
134        }
135        return true;
136    }
137
138    @Override
139    public void onTilesChanged() {
140        mQsPanel.setTiles(mHost.getTiles());
141    }
142}