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