1/*
2 * Copyright (C) 2012 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 */
16
17package com.android.systemui.statusbar.phone;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.util.Log;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewParent;
25import android.widget.FrameLayout;
26
27/**
28 *
29 */
30class QuickSettingsTileView extends FrameLayout {
31    private static final String TAG = "QuickSettingsTileView";
32
33    private int mContentLayoutId;
34    private int mColSpan;
35    private boolean mPrepared;
36    private OnPrepareListener mOnPrepareListener;
37
38    public QuickSettingsTileView(Context context, AttributeSet attrs) {
39        super(context, attrs);
40
41        mContentLayoutId = -1;
42        mColSpan = 1;
43    }
44
45    void setColumnSpan(int span) {
46        mColSpan = span;
47    }
48
49    int getColumnSpan() {
50        return mColSpan;
51    }
52
53    void setContent(int layoutId, LayoutInflater inflater) {
54        mContentLayoutId = layoutId;
55        inflater.inflate(layoutId, this);
56    }
57
58    void reinflateContent(LayoutInflater inflater) {
59        if (mContentLayoutId != -1) {
60            removeAllViews();
61            setContent(mContentLayoutId, inflater);
62        } else {
63            Log.e(TAG, "Not reinflating content: No layoutId set");
64        }
65    }
66
67    @Override
68    public void setVisibility(int vis) {
69        if (QuickSettings.DEBUG_GONE_TILES) {
70            if (vis == View.GONE) {
71                vis = View.VISIBLE;
72                setAlpha(0.25f);
73                setEnabled(false);
74            } else {
75                setAlpha(1f);
76                setEnabled(true);
77            }
78        }
79        super.setVisibility(vis);
80    }
81
82    public void setOnPrepareListener(OnPrepareListener listener) {
83        if (mOnPrepareListener != listener) {
84            mOnPrepareListener = listener;
85            mPrepared = false;
86            post(new Runnable() {
87                @Override
88                public void run() {
89                    updatePreparedState();
90                }
91            });
92        }
93    }
94
95    @Override
96    protected void onVisibilityChanged(View changedView, int visibility) {
97        super.onVisibilityChanged(changedView, visibility);
98        updatePreparedState();
99    }
100
101    @Override
102    protected void onAttachedToWindow() {
103        super.onAttachedToWindow();
104        updatePreparedState();
105    }
106
107    @Override
108    protected void onDetachedFromWindow() {
109        super.onDetachedFromWindow();
110        updatePreparedState();
111    }
112
113    private void updatePreparedState() {
114        if (mOnPrepareListener != null) {
115            if (isParentVisible()) {
116                if (!mPrepared) {
117                    mPrepared = true;
118                    mOnPrepareListener.onPrepare();
119                }
120            } else if (mPrepared) {
121                mPrepared = false;
122                mOnPrepareListener.onUnprepare();
123            }
124        }
125    }
126
127    private boolean isParentVisible() {
128        if (!isAttachedToWindow()) {
129            return false;
130        }
131        for (ViewParent current = getParent(); current instanceof View;
132                current = current.getParent()) {
133            View view = (View)current;
134            if (view.getVisibility() != VISIBLE) {
135                return false;
136            }
137        }
138        return true;
139    }
140
141    /**
142     * Called when the view's parent becomes visible or invisible to provide
143     * an opportunity for the client to provide new content.
144     */
145    public interface OnPrepareListener {
146        void onPrepare();
147        void onUnprepare();
148    }
149}