QSCustomizer.java revision 62b63a02d7ca630e3ad39991ea6550cab57e5d22
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        isShown = true;
94        mPhoneStatusBar.getStatusBarWindow().addView(this);
95        setTileSpecs();
96        mClipper.animateCircularClip(x, y, true, this);
97        new TileQueryHelper(mContext, mHost).setListener(mTileAdapter);
98    }
99
100    public void hide(int x, int y) {
101        isShown = false;
102        mClipper.animateCircularClip(x, y, false, this);
103    }
104
105    public boolean isCustomizing() {
106        return isShown;
107    }
108
109    private void reset() {
110        ArrayList<String> tiles = new ArrayList<>();
111        String defTiles = mContext.getString(R.string.quick_settings_tiles_default);
112        for (String tile : defTiles.split(",")) {
113            tiles.add(tile);
114        }
115        mTileAdapter.setTileSpecs(tiles);
116    }
117
118    private void setTileSpecs() {
119        List<String> specs = new ArrayList<>();
120        for (QSTile tile : mHost.getTiles()) {
121            specs.add(tile.getTileSpec());
122        }
123        mTileAdapter.setTileSpecs(specs);
124    }
125
126    private void save() {
127        mTileAdapter.saveSpecs(mHost);
128        hide((int) mSave.getX() + mSave.getWidth() / 2, (int) mSave.getY() + mSave.getHeight() / 2);
129    }
130
131    @Override
132    public void onClick(View v) {
133        if (v == mClose) {
134            hide((int) mClose.getX() + mClose.getWidth() / 2,
135                    (int) mClose.getY() + mClose.getHeight() / 2);
136        } else if (v == mSave) {
137            save();
138        } else if (v == mReset) {
139            reset();
140        }
141    }
142
143    @Override
144    public void onAnimationEnd(Animator animation) {
145        if (!isShown) {
146            mPhoneStatusBar.getStatusBarWindow().removeView(this);
147        }
148    }
149
150    @Override
151    public void onAnimationCancel(Animator animation) {
152        if (!isShown) {
153            mPhoneStatusBar.getStatusBarWindow().removeView(this);
154        }
155    }
156
157    @Override
158    public void onAnimationStart(Animator animation) {
159        // Don't care.
160    }
161
162    @Override
163    public void onAnimationRepeat(Animator animation) {
164        // Don't care.
165    }
166}
167