blob: fe84d81354dd95c934f7bf2ed94d85dfc7f771b4 [file] [log] [blame]
Selim Cinek024ca592014-09-01 15:11:28 +02001/*
2 * Copyright (C) 2014 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;
18
Julia Reynoldsa07af882015-12-17 08:32:48 -050019import android.app.INotificationManager;
20import android.app.Notification;
Selim Cinek024ca592014-09-01 15:11:28 +020021import android.content.Context;
Julia Reynolds21c30542016-01-15 15:00:02 -050022import android.content.pm.PackageInfo;
23import android.content.pm.PackageManager;
Selim Cinek024ca592014-09-01 15:11:28 +020024import android.graphics.Canvas;
25import android.graphics.drawable.Drawable;
Julia Reynoldsa07af882015-12-17 08:32:48 -050026import android.os.RemoteException;
27import android.os.ServiceManager;
28import android.service.notification.NotificationListenerService;
29import android.service.notification.StatusBarNotification;
Selim Cinek024ca592014-09-01 15:11:28 +020030import android.util.AttributeSet;
Julia Reynoldsa07af882015-12-17 08:32:48 -050031import android.view.View;
Julia Reynolds7727ad22016-01-22 14:38:46 -050032import android.widget.ImageView;
Julia Reynoldsead00aa2015-12-07 08:23:48 -050033import android.widget.LinearLayout;
Julia Reynoldsa07af882015-12-17 08:32:48 -050034import android.widget.RadioButton;
35import android.widget.SeekBar;
36import android.widget.TextView;
Julia Reynoldsead00aa2015-12-07 08:23:48 -050037
Julia Reynoldsb1a235f2016-02-09 12:57:02 -050038import com.android.internal.logging.MetricsLogger;
39import com.android.internal.logging.MetricsProto.MetricsEvent;
Julia Reynolds21c30542016-01-15 15:00:02 -050040import com.android.settingslib.Utils;
Selim Cinek024ca592014-09-01 15:11:28 +020041import com.android.systemui.R;
42
43/**
44 * The guts of a notification revealed when performing a long press.
45 */
Julia Reynoldsead00aa2015-12-07 08:23:48 -050046public class NotificationGuts extends LinearLayout {
Selim Cinek024ca592014-09-01 15:11:28 +020047
48 private Drawable mBackground;
49 private int mClipTopAmount;
50 private int mActualHeight;
Selim Cinekd84a5932015-12-15 11:45:36 -080051 private boolean mExposed;
Julia Reynolds0714f622016-01-07 13:24:09 -050052 private SeekBar mSeekBar;
Julia Reynolds0714f622016-01-07 13:24:09 -050053 private INotificationManager mINotificationManager;
Julia Reynoldsb1a235f2016-02-09 12:57:02 -050054 private int mStartingImportance;
Selim Cinek024ca592014-09-01 15:11:28 +020055
56 public NotificationGuts(Context context, AttributeSet attrs) {
57 super(context, attrs);
58 setWillNotDraw(false);
59 }
60
61 @Override
62 protected void onDraw(Canvas canvas) {
63 draw(canvas, mBackground);
64 }
65
66 private void draw(Canvas canvas, Drawable drawable) {
67 if (drawable != null) {
68 drawable.setBounds(0, mClipTopAmount, getWidth(), mActualHeight);
69 drawable.draw(canvas);
70 }
71 }
72
73 @Override
74 protected void onFinishInflate() {
75 super.onFinishInflate();
76 mBackground = mContext.getDrawable(R.drawable.notification_guts_bg);
77 if (mBackground != null) {
78 mBackground.setCallback(this);
79 }
80 }
81
82 @Override
83 protected boolean verifyDrawable(Drawable who) {
84 return super.verifyDrawable(who) || who == mBackground;
85 }
86
87 @Override
88 protected void drawableStateChanged() {
89 drawableStateChanged(mBackground);
90 }
91
92 private void drawableStateChanged(Drawable d) {
93 if (d != null && d.isStateful()) {
94 d.setState(getDrawableState());
95 }
96 }
97
98 @Override
99 public void drawableHotspotChanged(float x, float y) {
100 if (mBackground != null) {
101 mBackground.setHotspot(x, y);
102 }
103 }
104
Julia Reynoldsa07af882015-12-17 08:32:48 -0500105 void bindImportance(final StatusBarNotification sbn, final ExpandableNotificationRow row,
106 final int importance) {
Julia Reynoldsb1a235f2016-02-09 12:57:02 -0500107 mStartingImportance = importance;
Julia Reynolds0714f622016-01-07 13:24:09 -0500108 mINotificationManager = INotificationManager.Stub.asInterface(
Julia Reynoldsa07af882015-12-17 08:32:48 -0500109 ServiceManager.getService(Context.NOTIFICATION_SERVICE));
Julia Reynoldsa07af882015-12-17 08:32:48 -0500110
Julia Reynoldsef37f282016-02-12 09:11:27 -0500111 final TextView importanceSummary = ((TextView) row.findViewById(R.id.summary));
112 final TextView importanceTitle = ((TextView) row.findViewById(R.id.title));
Julia Reynolds0714f622016-01-07 13:24:09 -0500113 mSeekBar = (SeekBar) row.findViewById(R.id.seekbar);
Julia Reynolds21c30542016-01-15 15:00:02 -0500114 boolean systemApp = false;
115 try {
116 final PackageManager pm = BaseStatusBar.getPackageManagerForUser(
117 getContext(), sbn.getUser().getIdentifier());
118 final PackageInfo info =
119 pm.getPackageInfo(sbn.getPackageName(), PackageManager.GET_SIGNATURES);
120 systemApp = Utils.isSystemPackage(pm, info);
121 } catch (PackageManager.NameNotFoundException e) {
122 // unlikely.
123 }
Julia Reynolds7727ad22016-01-22 14:38:46 -0500124 if (systemApp) {
125 ((ImageView) row.findViewById(R.id.low_importance)).getDrawable().setTint(
126 mContext.getColor(R.color.notification_guts_disabled_icon_tint));
127 }
Julia Reynolds21c30542016-01-15 15:00:02 -0500128 final int minProgress = systemApp ?
129 NotificationListenerService.Ranking.IMPORTANCE_LOW
130 : NotificationListenerService.Ranking.IMPORTANCE_NONE;
131 mSeekBar.setMax(NotificationListenerService.Ranking.IMPORTANCE_MAX);
Julia Reynolds0714f622016-01-07 13:24:09 -0500132 mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
Julia Reynoldsa07af882015-12-17 08:32:48 -0500133 @Override
134 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Julia Reynolds21c30542016-01-15 15:00:02 -0500135 if (progress < minProgress) {
136 seekBar.setProgress(minProgress);
137 progress = minProgress;
138 }
Julia Reynoldsa07af882015-12-17 08:32:48 -0500139 updateTitleAndSummary(progress);
140 if (fromUser) {
Julia Reynoldsb1a235f2016-02-09 12:57:02 -0500141 MetricsLogger.action(mContext, MetricsEvent.ACTION_MODIFY_IMPORTANCE_SLIDER);
Julia Reynoldsa07af882015-12-17 08:32:48 -0500142 }
143 }
144
145 @Override
146 public void onStartTrackingTouch(SeekBar seekBar) {
147 // no-op
148 }
149
150 @Override
151 public void onStopTrackingTouch(SeekBar seekBar) {
152 // no-op
153 }
154
155 private void updateTitleAndSummary(int progress) {
156 switch (progress) {
157 case NotificationListenerService.Ranking.IMPORTANCE_NONE:
Julia Reynoldsef37f282016-02-12 09:11:27 -0500158 importanceSummary.setText(mContext.getString(
Julia Reynoldsa07af882015-12-17 08:32:48 -0500159 R.string.notification_importance_blocked));
Julia Reynoldsef37f282016-02-12 09:11:27 -0500160 importanceTitle.setText(mContext.getString(R.string.blocked_importance));
Julia Reynoldsa07af882015-12-17 08:32:48 -0500161 break;
162 case NotificationListenerService.Ranking.IMPORTANCE_LOW:
Julia Reynoldsef37f282016-02-12 09:11:27 -0500163 importanceSummary.setText(mContext.getString(
Julia Reynoldsa07af882015-12-17 08:32:48 -0500164 R.string.notification_importance_low));
Julia Reynoldsef37f282016-02-12 09:11:27 -0500165 importanceTitle.setText(mContext.getString(R.string.low_importance));
Julia Reynoldsa07af882015-12-17 08:32:48 -0500166 break;
167 case NotificationListenerService.Ranking.IMPORTANCE_DEFAULT:
Julia Reynoldsef37f282016-02-12 09:11:27 -0500168 importanceSummary.setText(mContext.getString(
Julia Reynoldsa07af882015-12-17 08:32:48 -0500169 R.string.notification_importance_default));
Julia Reynoldsef37f282016-02-12 09:11:27 -0500170 importanceTitle.setText(mContext.getString(R.string.default_importance));
Julia Reynoldsa07af882015-12-17 08:32:48 -0500171 break;
172 case NotificationListenerService.Ranking.IMPORTANCE_HIGH:
Julia Reynoldsef37f282016-02-12 09:11:27 -0500173 importanceSummary.setText(mContext.getString(
Julia Reynoldsa07af882015-12-17 08:32:48 -0500174 R.string.notification_importance_high));
Julia Reynoldsef37f282016-02-12 09:11:27 -0500175 importanceTitle.setText(mContext.getString(R.string.high_importance));
Julia Reynoldsa07af882015-12-17 08:32:48 -0500176 break;
177 case NotificationListenerService.Ranking.IMPORTANCE_MAX:
Julia Reynoldsef37f282016-02-12 09:11:27 -0500178 importanceSummary.setText(mContext.getString(
Julia Reynoldsa07af882015-12-17 08:32:48 -0500179 R.string.notification_importance_max));
Julia Reynoldsef37f282016-02-12 09:11:27 -0500180 importanceTitle.setText(mContext.getString(R.string.max_importance));
Julia Reynoldsa07af882015-12-17 08:32:48 -0500181 break;
182 }
183 }
184 });
Julia Reynolds0714f622016-01-07 13:24:09 -0500185 mSeekBar.setProgress(importance);
186 }
187
188 void saveImportance(final StatusBarNotification sbn) {
189 int progress = mSeekBar.getProgress();
Julia Reynoldsb1a235f2016-02-09 12:57:02 -0500190 MetricsLogger.action(mContext, MetricsEvent.ACTION_SAVE_IMPORTANCE,
191 progress - mStartingImportance);
Julia Reynolds0714f622016-01-07 13:24:09 -0500192 try {
Julia Reynoldsef37f282016-02-12 09:11:27 -0500193 mINotificationManager.setImportance(sbn.getPackageName(), sbn.getUid(), progress);
Julia Reynolds0714f622016-01-07 13:24:09 -0500194 } catch (RemoteException e) {
195 // :(
196 }
Julia Reynoldsa07af882015-12-17 08:32:48 -0500197 }
198
Selim Cinek024ca592014-09-01 15:11:28 +0200199 public void setActualHeight(int actualHeight) {
200 mActualHeight = actualHeight;
201 invalidate();
202 }
203
204 public int getActualHeight() {
205 return mActualHeight;
206 }
207
208 public void setClipTopAmount(int clipTopAmount) {
209 mClipTopAmount = clipTopAmount;
210 invalidate();
211 }
212
213 @Override
214 public boolean hasOverlappingRendering() {
215
216 // Prevents this view from creating a layer when alpha is animating.
217 return false;
218 }
Selim Cinekd84a5932015-12-15 11:45:36 -0800219
220 public void setExposed(boolean exposed) {
221 mExposed = exposed;
222 }
223
224 public boolean areGutsExposed() {
225 return mExposed;
226 }
Selim Cinek024ca592014-09-01 15:11:28 +0200227}