1/*
2 * Copyright (C) 2011 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.policy;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.LayoutTransition;
22import android.animation.ObjectAnimator;
23import android.animation.ValueAnimator;
24import android.content.Context;
25import android.content.res.Configuration;
26import android.content.res.TypedArray;
27import android.graphics.Rect;
28import android.graphics.drawable.Drawable;
29import android.util.AttributeSet;
30import android.util.Log;
31import android.util.Slog;
32import android.view.MotionEvent;
33import android.view.View;
34import android.view.View.OnClickListener;
35import android.view.ViewConfiguration;
36import android.view.ViewGroup;
37import android.widget.LinearLayout;
38import android.widget.RemoteViews;
39
40import com.android.systemui.R;
41import com.android.systemui.SwipeHelper;
42import com.android.systemui.statusbar.BaseStatusBar;
43
44import java.util.HashMap;
45
46public class IntruderAlertView extends LinearLayout implements SwipeHelper.Callback {
47    private static final String TAG = "IntruderAlertView";
48    private static final boolean DEBUG = false;
49
50    Rect mTmpRect = new Rect();
51
52    private SwipeHelper mSwipeHelper;
53
54    BaseStatusBar mBar;
55    private ViewGroup mContentHolder;
56
57    private RemoteViews mIntruderRemoteViews;
58    private OnClickListener mOnClickListener;
59
60    public IntruderAlertView(Context context, AttributeSet attrs) {
61        this(context, attrs, 0);
62    }
63
64    public IntruderAlertView(Context context, AttributeSet attrs, int defStyle) {
65        super(context, attrs, defStyle);
66
67        setOrientation(LinearLayout.VERTICAL);
68    }
69
70    @Override
71    public void onAttachedToWindow() {
72        float densityScale = getResources().getDisplayMetrics().density;
73        float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop();
74        mSwipeHelper = new SwipeHelper(SwipeHelper.X, this, densityScale, pagingTouchSlop);
75
76        mContentHolder = (ViewGroup) findViewById(R.id.contentHolder);
77        if (mIntruderRemoteViews != null) {
78            // whoops, we're on already!
79            applyIntruderContent(mIntruderRemoteViews, mOnClickListener);
80        }
81    }
82
83    public void setBar(BaseStatusBar bar) {
84        mBar = bar;
85    }
86
87    @Override
88    public boolean onInterceptTouchEvent(MotionEvent ev) {
89        if (DEBUG) Log.v(TAG, "onInterceptTouchEvent()");
90        return mSwipeHelper.onInterceptTouchEvent(ev) ||
91            super.onInterceptTouchEvent(ev);
92    }
93
94    @Override
95    public boolean onTouchEvent(MotionEvent ev) {
96        return mSwipeHelper.onTouchEvent(ev) ||
97            super.onTouchEvent(ev);
98    }
99
100    public boolean canChildBeDismissed(View v) {
101        return true;
102    }
103
104    public void onChildDismissed(View v) {
105        Slog.v(TAG, "User swiped intruder to dismiss");
106        mBar.dismissIntruder();
107    }
108
109    public void onBeginDrag(View v) {
110    }
111
112    public void onDragCancelled(View v) {
113        mContentHolder.setAlpha(1f); // sometimes this isn't quite reset
114    }
115
116    public View getChildAtPosition(MotionEvent ev) {
117        return mContentHolder;
118    }
119
120    public View getChildContentView(View v) {
121        return v;
122    }
123
124    @Override
125    protected void onConfigurationChanged(Configuration newConfig) {
126        super.onConfigurationChanged(newConfig);
127        float densityScale = getResources().getDisplayMetrics().density;
128        mSwipeHelper.setDensityScale(densityScale);
129        float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop();
130        mSwipeHelper.setPagingTouchSlop(pagingTouchSlop);
131    }
132
133    @Override
134    public void onDraw(android.graphics.Canvas c) {
135        super.onDraw(c);
136        if (DEBUG) {
137            //Slog.d(TAG, "onDraw: canvas height: " + c.getHeight() + "px; measured height: "
138            //        + getMeasuredHeight() + "px");
139            c.save();
140            c.clipRect(6, 6, c.getWidth() - 6, getMeasuredHeight() - 6,
141                    android.graphics.Region.Op.DIFFERENCE);
142            c.drawColor(0xFFcc00cc);
143            c.restore();
144        }
145    }
146
147    public void applyIntruderContent(RemoteViews intruderView, OnClickListener listener) {
148        if (DEBUG) {
149            Slog.v(TAG, "applyIntruderContent: view=" + intruderView + " listener=" + listener);
150        }
151        mIntruderRemoteViews = intruderView;
152        mOnClickListener = listener;
153        if (mContentHolder == null) {
154            // too soon!
155            return;
156        }
157        mContentHolder.setX(0);
158        mContentHolder.setVisibility(View.VISIBLE);
159        mContentHolder.setAlpha(1f);
160        mContentHolder.removeAllViews();
161        final View content = intruderView.apply(getContext(), mContentHolder);
162        if (listener != null) {
163            content.setOnClickListener(listener);
164
165            //content.setBackgroundResource(R.drawable.intruder_row_bg);
166            Drawable bg = getResources().getDrawable(R.drawable.intruder_row_bg);
167            if (bg == null) {
168                Log.e(TAG, String.format("Can't find background drawable id=0x%08x", R.drawable.intruder_row_bg));
169            } else {
170                content.setBackgroundDrawable(bg);
171            }
172        }
173        mContentHolder.addView(content);
174
175    }
176}