QSCustomizer.java revision 04fd24966ae5518528fe5301f054c1cf27ac497f
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.animation.Animator;
19import android.animation.Animator.AnimatorListener;
20import android.animation.AnimatorListenerAdapter;
21import android.content.Context;
22import android.support.v7.widget.DefaultItemAnimator;
23import android.support.v7.widget.GridLayoutManager;
24import android.support.v7.widget.RecyclerView;
25import android.util.AttributeSet;
26import android.util.TypedValue;
27import android.view.ContextThemeWrapper;
28import android.view.Menu;
29import android.view.MenuItem;
30import android.view.View;
31import android.widget.LinearLayout;
32import android.widget.Toolbar;
33import android.widget.Toolbar.OnMenuItemClickListener;
34import com.android.systemui.R;
35import com.android.systemui.qs.QSDetailClipper;
36import com.android.systemui.qs.QSTile;
37import com.android.systemui.statusbar.phone.PhoneStatusBar;
38import com.android.systemui.statusbar.phone.QSTileHost;
39
40import java.util.ArrayList;
41import java.util.List;
42
43/**
44 * Allows full-screen customization of QS, through show() and hide().
45 *
46 * This adds itself to the status bar window, so it can appear on top of quick settings and
47 * *someday* do fancy animations to get into/out of it.
48 */
49public class QSCustomizer extends LinearLayout implements OnMenuItemClickListener {
50
51    private static final int MENU_RESET = Menu.FIRST;
52
53    private final QSDetailClipper mClipper;
54
55    private PhoneStatusBar mPhoneStatusBar;
56
57    private boolean isShown;
58    private QSTileHost mHost;
59    private RecyclerView mRecyclerView;
60    private TileAdapter mTileAdapter;
61    private Toolbar mToolbar;
62
63    public QSCustomizer(Context context, AttributeSet attrs) {
64        super(new ContextThemeWrapper(context, android.R.style.Theme_Material), attrs);
65        mClipper = new QSDetailClipper(this);
66    }
67
68    public void setHost(QSTileHost host) {
69        mHost = host;
70        mPhoneStatusBar = host.getPhoneStatusBar();
71    }
72
73    @Override
74    protected void onFinishInflate() {
75        super.onFinishInflate();
76        mToolbar = (Toolbar) findViewById(com.android.internal.R.id.action_bar);
77        TypedValue value = new TypedValue();
78        mContext.getTheme().resolveAttribute(android.R.attr.homeAsUpIndicator, value, true);
79        mToolbar.setNavigationIcon(
80                getResources().getDrawable(value.resourceId, mContext.getTheme()));
81        mToolbar.setNavigationOnClickListener(new OnClickListener() {
82            @Override
83            public void onClick(View v) {
84                hide((int) v.getX() + v.getWidth() / 2, (int) v.getY() + v.getHeight() / 2);
85            }
86        });
87        mToolbar.setOnMenuItemClickListener(this);
88        mToolbar.getMenu().add(Menu.NONE, MENU_RESET, 0,
89                mContext.getString(com.android.internal.R.string.reset));
90
91        mRecyclerView = (RecyclerView) findViewById(android.R.id.list);
92        mTileAdapter = new TileAdapter(getContext());
93        mRecyclerView.setAdapter(mTileAdapter);
94        mTileAdapter.getItemTouchHelper().attachToRecyclerView(mRecyclerView);
95        GridLayoutManager layout = new GridLayoutManager(getContext(), 3);
96        layout.setSpanSizeLookup(mTileAdapter.getSizeLookup());
97        mRecyclerView.setLayoutManager(layout);
98        mRecyclerView.addItemDecoration(mTileAdapter.getItemDecoration());
99        DefaultItemAnimator animator = new DefaultItemAnimator();
100        animator.setMoveDuration(TileAdapter.MOVE_DURATION);
101        mRecyclerView.setItemAnimator(animator);
102    }
103
104    public void show(int x, int y) {
105        if (!isShown) {
106            isShown = true;
107            mPhoneStatusBar.getStatusBarWindow().addView(this);
108            setTileSpecs();
109            mClipper.animateCircularClip(x, y, true, null);
110            new TileQueryHelper(mContext, mHost).setListener(mTileAdapter);
111        }
112    }
113
114    public void hide(int x, int y) {
115        if (isShown) {
116            isShown = false;
117            save();
118            mClipper.animateCircularClip(x, y, false, mCollapseAnimationListener);
119        }
120    }
121
122    public boolean isCustomizing() {
123        return isShown;
124    }
125
126    @Override
127    public boolean onMenuItemClick(MenuItem item) {
128        switch (item.getItemId()) {
129            case MENU_RESET:
130                reset();
131                break;
132        }
133        return false;
134    }
135
136    private void reset() {
137        ArrayList<String> tiles = new ArrayList<>();
138        String defTiles = mContext.getString(R.string.quick_settings_tiles_default);
139        for (String tile : defTiles.split(",")) {
140            tiles.add(tile);
141        }
142        mTileAdapter.setTileSpecs(tiles);
143    }
144
145    private void setTileSpecs() {
146        List<String> specs = new ArrayList<>();
147        for (QSTile tile : mHost.getTiles()) {
148            specs.add(tile.getTileSpec());
149        }
150        mTileAdapter.setTileSpecs(specs);
151    }
152
153    private void save() {
154        mTileAdapter.saveSpecs(mHost);
155    }
156
157    private final AnimatorListener mCollapseAnimationListener = new AnimatorListenerAdapter() {
158        @Override
159        public void onAnimationEnd(Animator animation) {
160            if (!isShown) {
161                mPhoneStatusBar.getStatusBarWindow().removeView(QSCustomizer.this);
162            }
163        }
164
165        @Override
166        public void onAnimationCancel(Animator animation) {
167            if (!isShown) {
168                mPhoneStatusBar.getStatusBarWindow().removeView(QSCustomizer.this);
169            }
170        }
171    };
172}
173