• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.systemui.statusbar.policy;
18 
19 import android.animation.Animator;
20 import android.animation.AnimatorListenerAdapter;
21 import android.animation.LayoutTransition;
22 import android.animation.ObjectAnimator;
23 import android.animation.ValueAnimator;
24 import android.content.Context;
25 import android.content.res.Configuration;
26 import android.content.res.TypedArray;
27 import android.graphics.Rect;
28 import android.graphics.drawable.Drawable;
29 import android.util.AttributeSet;
30 import android.util.Log;
31 import android.util.Slog;
32 import android.view.MotionEvent;
33 import android.view.View;
34 import android.view.View.OnClickListener;
35 import android.view.ViewConfiguration;
36 import android.view.ViewGroup;
37 import android.widget.LinearLayout;
38 import android.widget.RemoteViews;
39 
40 import com.android.systemui.R;
41 import com.android.systemui.SwipeHelper;
42 import com.android.systemui.statusbar.BaseStatusBar;
43 
44 import java.util.HashMap;
45 
46 public 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 
IntruderAlertView(Context context, AttributeSet attrs)60     public IntruderAlertView(Context context, AttributeSet attrs) {
61         this(context, attrs, 0);
62     }
63 
IntruderAlertView(Context context, AttributeSet attrs, int defStyle)64     public IntruderAlertView(Context context, AttributeSet attrs, int defStyle) {
65         super(context, attrs, defStyle);
66 
67         setOrientation(LinearLayout.VERTICAL);
68     }
69 
70     @Override
onAttachedToWindow()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 
setBar(BaseStatusBar bar)83     public void setBar(BaseStatusBar bar) {
84         mBar = bar;
85     }
86 
87     @Override
onInterceptTouchEvent(MotionEvent ev)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
onTouchEvent(MotionEvent ev)95     public boolean onTouchEvent(MotionEvent ev) {
96         return mSwipeHelper.onTouchEvent(ev) ||
97             super.onTouchEvent(ev);
98     }
99 
canChildBeDismissed(View v)100     public boolean canChildBeDismissed(View v) {
101         return true;
102     }
103 
onChildDismissed(View v)104     public void onChildDismissed(View v) {
105         Slog.v(TAG, "User swiped intruder to dismiss");
106         mBar.dismissIntruder();
107     }
108 
onBeginDrag(View v)109     public void onBeginDrag(View v) {
110     }
111 
onDragCancelled(View v)112     public void onDragCancelled(View v) {
113         mContentHolder.setAlpha(1f); // sometimes this isn't quite reset
114     }
115 
getChildAtPosition(MotionEvent ev)116     public View getChildAtPosition(MotionEvent ev) {
117         return mContentHolder;
118     }
119 
getChildContentView(View v)120     public View getChildContentView(View v) {
121         return v;
122     }
123 
124     @Override
onConfigurationChanged(Configuration newConfig)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
onDraw(android.graphics.Canvas c)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 
applyIntruderContent(RemoteViews intruderView, OnClickListener listener)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 }