Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 17 | package com.android.systemui.statusbar; |
| 18 | |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 19 | import static android.app.NotificationManager.IMPORTANCE_NONE; |
| 20 | |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 21 | import android.app.INotificationManager; |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 22 | import android.app.Notification; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 23 | import android.app.NotificationChannel; |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 24 | import android.app.NotificationChannelGroup; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 26 | import android.content.Intent; |
| 27 | import android.content.pm.ActivityInfo; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 28 | import android.content.pm.ApplicationInfo; |
| 29 | import android.content.pm.PackageInfo; |
| 30 | import android.content.pm.PackageManager; |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 31 | import android.content.pm.ResolveInfo; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 32 | import android.graphics.drawable.Drawable; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 33 | import android.os.RemoteException; |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 34 | import android.service.notification.StatusBarNotification; |
| 35 | import android.text.TextUtils; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 36 | import android.util.AttributeSet; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 37 | import android.view.View; |
Geoffrey Pitsch | d94e788 | 2017-04-06 09:52:11 -0400 | [diff] [blame] | 38 | import android.view.accessibility.AccessibilityEvent; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 39 | import android.widget.ImageView; |
| 40 | import android.widget.LinearLayout; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 41 | import android.widget.Switch; |
| 42 | import android.widget.TextView; |
| 43 | |
| 44 | import com.android.internal.logging.MetricsLogger; |
| 45 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
| 46 | import com.android.settingslib.Utils; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 47 | import com.android.systemui.R; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 48 | |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 49 | import java.lang.IllegalArgumentException; |
| 50 | import java.util.List; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 51 | import java.util.Set; |
| 52 | |
| 53 | /** |
| 54 | * The guts of a notification revealed when performing a long press. |
| 55 | */ |
Mady Mellor | 95d743c | 2017-01-10 12:05:27 -0800 | [diff] [blame] | 56 | public class NotificationInfo extends LinearLayout implements NotificationGuts.GutsContent { |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 57 | private static final String TAG = "InfoGuts"; |
| 58 | |
| 59 | private INotificationManager mINotificationManager; |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 60 | private String mPkg; |
Geoffrey Pitsch | d94e788 | 2017-04-06 09:52:11 -0400 | [diff] [blame] | 61 | private String mAppName; |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 62 | private int mAppUid; |
| 63 | private List<NotificationChannel> mNotificationChannels; |
| 64 | private NotificationChannel mSingleNotificationChannel; |
Geoffrey Pitsch | 1fbf66e | 2017-05-09 11:32:27 -0400 | [diff] [blame] | 65 | private boolean mIsSingleDefaultChannel; |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 66 | private StatusBarNotification mSbn; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 67 | private int mStartingUserImportance; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 68 | |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 69 | private TextView mNumChannelsView; |
| 70 | private View mChannelDisabledView; |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 71 | private TextView mSettingsLinkView; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 72 | private Switch mChannelEnabledSwitch; |
Geoffrey Pitsch | 5278d3d | 2017-03-29 13:39:10 -0400 | [diff] [blame] | 73 | private CheckSaveListener mCheckSaveListener; |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 74 | private OnAppSettingsClickListener mAppSettingsClickListener; |
| 75 | private PackageManager mPm; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 76 | |
Mady Mellor | 95d743c | 2017-01-10 12:05:27 -0800 | [diff] [blame] | 77 | private NotificationGuts mGutsContainer; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 78 | |
| 79 | public NotificationInfo(Context context, AttributeSet attrs) { |
| 80 | super(context, attrs); |
| 81 | } |
| 82 | |
Geoffrey Pitsch | 5278d3d | 2017-03-29 13:39:10 -0400 | [diff] [blame] | 83 | // Specify a CheckSaveListener to override when/if the user's changes are committed. |
| 84 | public interface CheckSaveListener { |
| 85 | // Invoked when importance has changed and the NotificationInfo wants to try to save it. |
| 86 | // Listener should run saveImportance unless the change should be canceled. |
| 87 | void checkSave(Runnable saveImportance); |
| 88 | } |
| 89 | |
Jason Monk | 2a6ea9c | 2017-01-26 11:14:51 -0500 | [diff] [blame] | 90 | public interface OnSettingsClickListener { |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 91 | void onClick(View v, NotificationChannel channel, int appUid); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 94 | public interface OnAppSettingsClickListener { |
| 95 | void onClick(View v, Intent intent); |
| 96 | } |
| 97 | |
Jason Monk | 2a6ea9c | 2017-01-26 11:14:51 -0500 | [diff] [blame] | 98 | public void bindNotification(final PackageManager pm, |
| 99 | final INotificationManager iNotificationManager, |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 100 | final String pkg, |
| 101 | final List<NotificationChannel> notificationChannels, |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 102 | int startingUserImportance, |
| 103 | final StatusBarNotification sbn, |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 104 | OnSettingsClickListener onSettingsClick, |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 105 | OnAppSettingsClickListener onAppSettingsClick, |
Geoffrey Pitsch | 5278d3d | 2017-03-29 13:39:10 -0400 | [diff] [blame] | 106 | OnClickListener onDoneClick, |
| 107 | CheckSaveListener checkSaveListener, |
| 108 | final Set<String> nonBlockablePkgs) |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 109 | throws RemoteException { |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 110 | mINotificationManager = iNotificationManager; |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 111 | mPkg = pkg; |
| 112 | mNotificationChannels = notificationChannels; |
Geoffrey Pitsch | 5278d3d | 2017-03-29 13:39:10 -0400 | [diff] [blame] | 113 | mCheckSaveListener = checkSaveListener; |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 114 | mSbn = sbn; |
| 115 | mPm = pm; |
| 116 | mAppSettingsClickListener = onAppSettingsClick; |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 117 | mStartingUserImportance = startingUserImportance; |
Geoffrey Pitsch | d94e788 | 2017-04-06 09:52:11 -0400 | [diff] [blame] | 118 | mAppName = mPkg; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 119 | Drawable pkgicon = null; |
Julia Reynolds | 5a31193 | 2017-03-01 16:33:44 -0500 | [diff] [blame] | 120 | CharSequence channelNameText = ""; |
| 121 | ApplicationInfo info = null; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 122 | try { |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 123 | info = pm.getApplicationInfo(mPkg, |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 124 | PackageManager.MATCH_UNINSTALLED_PACKAGES |
| 125 | | PackageManager.MATCH_DISABLED_COMPONENTS |
| 126 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE |
| 127 | | PackageManager.MATCH_DIRECT_BOOT_AWARE); |
| 128 | if (info != null) { |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 129 | mAppUid = info.uid; |
Geoffrey Pitsch | d94e788 | 2017-04-06 09:52:11 -0400 | [diff] [blame] | 130 | mAppName = String.valueOf(pm.getApplicationLabel(info)); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 131 | pkgicon = pm.getApplicationIcon(info); |
| 132 | } |
| 133 | } catch (PackageManager.NameNotFoundException e) { |
| 134 | // app is gone, just show package name and generic icon |
| 135 | pkgicon = pm.getDefaultActivityIcon(); |
| 136 | } |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 137 | ((ImageView) findViewById(R.id.pkgicon)).setImageDrawable(pkgicon); |
| 138 | |
Geoffrey Pitsch | d034d29 | 2017-05-12 11:59:20 -0400 | [diff] [blame] | 139 | int numTotalChannels = iNotificationManager.getNumNotificationChannelsForPackage( |
| 140 | pkg, mAppUid, false /* includeDeleted */); |
| 141 | if (mNotificationChannels.isEmpty()) { |
| 142 | throw new IllegalArgumentException("bindNotification requires at least one channel"); |
| 143 | } else { |
| 144 | if (mNotificationChannels.size() == 1) { |
| 145 | mSingleNotificationChannel = mNotificationChannels.get(0); |
| 146 | // Special behavior for the Default channel if no other channels have been defined. |
| 147 | mIsSingleDefaultChannel = |
| 148 | (mSingleNotificationChannel.getId() |
| 149 | .equals(NotificationChannel.DEFAULT_CHANNEL_ID) && |
| 150 | numTotalChannels <= 1); |
| 151 | } else { |
| 152 | mSingleNotificationChannel = null; |
| 153 | mIsSingleDefaultChannel = false; |
| 154 | } |
| 155 | } |
| 156 | |
Geoffrey Pitsch | b43486a | 2017-06-01 13:45:59 -0400 | [diff] [blame] | 157 | boolean nonBlockable = false; |
| 158 | try { |
| 159 | final PackageInfo pkgInfo = pm.getPackageInfo(pkg, PackageManager.GET_SIGNATURES); |
| 160 | if (Utils.isSystemPackage(getResources(), pm, pkgInfo)) { |
| 161 | final int numChannels = mNotificationChannels.size(); |
| 162 | for (int i = 0; i < numChannels; i++) { |
| 163 | final NotificationChannel notificationChannel = mNotificationChannels.get(i); |
| 164 | // If any of the system channels is not blockable, the bundle is nonblockable |
| 165 | if (!notificationChannel.isBlockableSystem()) { |
| 166 | nonBlockable = true; |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } catch (PackageManager.NameNotFoundException e) { |
| 172 | // unlikely. |
| 173 | } |
| 174 | if (nonBlockablePkgs != null) { |
| 175 | nonBlockable |= nonBlockablePkgs.contains(pkg); |
| 176 | } |
| 177 | |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 178 | String channelsDescText; |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 179 | mNumChannelsView = findViewById(R.id.num_channels_desc); |
Geoffrey Pitsch | b43486a | 2017-06-01 13:45:59 -0400 | [diff] [blame] | 180 | if (nonBlockable) { |
| 181 | channelsDescText = mContext.getString(R.string.notification_unblockable_desc); |
| 182 | } else if (mIsSingleDefaultChannel) { |
Geoffrey Pitsch | ee8c81e | 2017-03-23 11:38:56 -0400 | [diff] [blame] | 183 | channelsDescText = mContext.getString(R.string.notification_default_channel_desc); |
| 184 | } else { |
| 185 | switch (mNotificationChannels.size()) { |
| 186 | case 1: |
| 187 | channelsDescText = String.format(mContext.getResources().getQuantityString( |
Geoffrey Pitsch | 1fbf66e | 2017-05-09 11:32:27 -0400 | [diff] [blame] | 188 | R.plurals.notification_num_channels_desc, numTotalChannels), |
| 189 | numTotalChannels); |
Geoffrey Pitsch | ee8c81e | 2017-03-23 11:38:56 -0400 | [diff] [blame] | 190 | break; |
| 191 | case 2: |
| 192 | channelsDescText = mContext.getString( |
| 193 | R.string.notification_channels_list_desc_2, |
| 194 | mNotificationChannels.get(0).getName(), |
| 195 | mNotificationChannels.get(1).getName()); |
| 196 | break; |
| 197 | default: |
| 198 | final int numOthers = mNotificationChannels.size() - 2; |
| 199 | channelsDescText = String.format( |
| 200 | mContext.getResources().getQuantityString( |
| 201 | R.plurals.notification_channels_list_desc_2_and_others, |
| 202 | numOthers), |
| 203 | mNotificationChannels.get(0).getName(), |
| 204 | mNotificationChannels.get(1).getName(), |
| 205 | numOthers); |
| 206 | } |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 207 | } |
| 208 | mNumChannelsView.setText(channelsDescText); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 209 | |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 210 | if (mSingleNotificationChannel == null) { |
| 211 | // Multiple channels don't use a channel name for the title. |
| 212 | channelNameText = mContext.getString(R.string.notification_num_channels, |
| 213 | mNotificationChannels.size()); |
Geoffrey Pitsch | b43486a | 2017-06-01 13:45:59 -0400 | [diff] [blame] | 214 | } else if (mIsSingleDefaultChannel || nonBlockable) { |
| 215 | // If this is the default channel or the app is unblockable, |
| 216 | // don't use our channel-specific text. |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 217 | channelNameText = mContext.getString(R.string.notification_header_default_channel); |
| 218 | } else { |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 219 | channelNameText = mSingleNotificationChannel.getName(); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 220 | } |
Geoffrey Pitsch | d94e788 | 2017-04-06 09:52:11 -0400 | [diff] [blame] | 221 | ((TextView) findViewById(R.id.pkgname)).setText(mAppName); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 222 | ((TextView) findViewById(R.id.channel_name)).setText(channelNameText); |
| 223 | |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 224 | // Set group information if this channel has an associated group. |
| 225 | CharSequence groupName = null; |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 226 | if (mSingleNotificationChannel != null && mSingleNotificationChannel.getGroup() != null) { |
| 227 | final NotificationChannelGroup notificationChannelGroup = |
| 228 | iNotificationManager.getNotificationChannelGroupForPackage( |
| 229 | mSingleNotificationChannel.getGroup(), pkg, mAppUid); |
| 230 | if (notificationChannelGroup != null) { |
| 231 | groupName = notificationChannelGroup.getName(); |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 232 | } |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 233 | } |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 234 | TextView groupNameView = ((TextView) findViewById(R.id.group_name)); |
| 235 | TextView groupDividerView = ((TextView) findViewById(R.id.pkg_group_divider)); |
| 236 | if (groupName != null) { |
| 237 | groupNameView.setText(groupName); |
| 238 | groupNameView.setVisibility(View.VISIBLE); |
| 239 | groupDividerView.setVisibility(View.VISIBLE); |
| 240 | } else { |
| 241 | groupNameView.setVisibility(View.GONE); |
| 242 | groupDividerView.setVisibility(View.GONE); |
| 243 | } |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 244 | |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 245 | bindButtons(nonBlockable); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 246 | |
| 247 | // Top-level importance group |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 248 | mChannelDisabledView = findViewById(R.id.channel_disabled); |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 249 | updateSecondaryText(); |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 250 | |
| 251 | // Settings button. |
| 252 | final TextView settingsButton = (TextView) findViewById(R.id.more_settings); |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 253 | if (mAppUid >= 0 && onSettingsClick != null) { |
Geoffrey Pitsch | 029a3fa | 2017-03-30 13:01:57 -0400 | [diff] [blame] | 254 | settingsButton.setVisibility(View.VISIBLE); |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 255 | final int appUidF = mAppUid; |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 256 | settingsButton.setOnClickListener( |
| 257 | (View view) -> { |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 258 | onSettingsClick.onClick(view, mSingleNotificationChannel, appUidF); |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 259 | }); |
Geoffrey Pitsch | b43486a | 2017-06-01 13:45:59 -0400 | [diff] [blame] | 260 | if (numTotalChannels <= 1 || nonBlockable) { |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 261 | settingsButton.setText(R.string.notification_more_settings); |
Geoffrey Pitsch | b43486a | 2017-06-01 13:45:59 -0400 | [diff] [blame] | 262 | } else { |
| 263 | settingsButton.setText(R.string.notification_all_categories); |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 264 | } |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 265 | } else { |
| 266 | settingsButton.setVisibility(View.GONE); |
| 267 | } |
| 268 | |
| 269 | // Done button. |
| 270 | final TextView doneButton = (TextView) findViewById(R.id.done); |
| 271 | doneButton.setText(R.string.notification_done); |
| 272 | doneButton.setOnClickListener(onDoneClick); |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 273 | |
| 274 | // Optional settings link |
| 275 | updateAppSettingsLink(); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Geoffrey Pitsch | 5278d3d | 2017-03-29 13:39:10 -0400 | [diff] [blame] | 278 | private boolean hasImportanceChanged() { |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 279 | return mSingleNotificationChannel != null && |
Geoffrey Pitsch | 8c8dbde | 2017-04-20 11:44:33 -0400 | [diff] [blame] | 280 | mChannelEnabledSwitch != null && |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 281 | mStartingUserImportance != getSelectedImportance(); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 284 | private void saveImportance() { |
Geoffrey Pitsch | 8c8dbde | 2017-04-20 11:44:33 -0400 | [diff] [blame] | 285 | if (!hasImportanceChanged()) { |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 286 | return; |
| 287 | } |
Geoffrey Pitsch | 8c8dbde | 2017-04-20 11:44:33 -0400 | [diff] [blame] | 288 | final int selectedImportance = getSelectedImportance(); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 289 | MetricsLogger.action(mContext, MetricsEvent.ACTION_SAVE_IMPORTANCE, |
| 290 | selectedImportance - mStartingUserImportance); |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 291 | mSingleNotificationChannel.setImportance(selectedImportance); |
Julia Reynolds | 8ceb579 | 2017-04-11 11:32:44 -0400 | [diff] [blame] | 292 | mSingleNotificationChannel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 293 | try { |
| 294 | mINotificationManager.updateNotificationChannelForPackage( |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 295 | mPkg, mAppUid, mSingleNotificationChannel); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 296 | } catch (RemoteException e) { |
| 297 | // :( |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | private int getSelectedImportance() { |
| 302 | if (!mChannelEnabledSwitch.isChecked()) { |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 303 | return IMPORTANCE_NONE; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 304 | } else { |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 305 | return mStartingUserImportance; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 309 | private void bindButtons(final boolean nonBlockable) { |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 310 | // Enabled Switch |
| 311 | mChannelEnabledSwitch = (Switch) findViewById(R.id.channel_enabled_switch); |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 312 | mChannelEnabledSwitch.setChecked( |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 313 | mStartingUserImportance != IMPORTANCE_NONE); |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 314 | final boolean visible = !nonBlockable && mSingleNotificationChannel != null; |
| 315 | mChannelEnabledSwitch.setVisibility(visible ? View.VISIBLE : View.INVISIBLE); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 316 | |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 317 | // Callback when checked. |
| 318 | mChannelEnabledSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> { |
Mady Mellor | 95d743c | 2017-01-10 12:05:27 -0800 | [diff] [blame] | 319 | if (mGutsContainer != null) { |
| 320 | mGutsContainer.resetFalsingCheck(); |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 321 | } |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 322 | updateSecondaryText(); |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 323 | updateAppSettingsLink(); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 324 | }); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Geoffrey Pitsch | d94e788 | 2017-04-06 09:52:11 -0400 | [diff] [blame] | 327 | @Override |
| 328 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 329 | super.onInitializeAccessibilityEvent(event); |
| 330 | if (mGutsContainer != null && |
| 331 | event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { |
| 332 | if (mGutsContainer.isExposed()) { |
| 333 | event.getText().add(mContext.getString( |
| 334 | R.string.notification_channel_controls_opened_accessibility, mAppName)); |
| 335 | } else { |
| 336 | event.getText().add(mContext.getString( |
| 337 | R.string.notification_channel_controls_closed_accessibility, mAppName)); |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 342 | private void updateSecondaryText() { |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 343 | final boolean disabled = mSingleNotificationChannel != null && |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 344 | getSelectedImportance() == IMPORTANCE_NONE; |
Geoffrey Pitsch | ee8c81e | 2017-03-23 11:38:56 -0400 | [diff] [blame] | 345 | if (disabled) { |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 346 | mChannelDisabledView.setVisibility(View.VISIBLE); |
| 347 | mNumChannelsView.setVisibility(View.GONE); |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 348 | } else { |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 349 | mChannelDisabledView.setVisibility(View.GONE); |
Geoffrey Pitsch | 1fbf66e | 2017-05-09 11:32:27 -0400 | [diff] [blame] | 350 | mNumChannelsView.setVisibility(mIsSingleDefaultChannel ? View.INVISIBLE : View.VISIBLE); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 354 | private void updateAppSettingsLink() { |
| 355 | mSettingsLinkView = findViewById(R.id.app_settings); |
| 356 | Intent settingsIntent = getAppSettingsIntent(mPm, mPkg, mSingleNotificationChannel, |
| 357 | mSbn.getId(), mSbn.getTag()); |
| 358 | if (settingsIntent != null && getSelectedImportance() != IMPORTANCE_NONE |
| 359 | && !TextUtils.isEmpty(mSbn.getNotification().getSettingsText())) { |
| 360 | mSettingsLinkView.setVisibility(View.VISIBLE); |
| 361 | mSettingsLinkView.setText(mContext.getString(R.string.notification_app_settings, |
| 362 | mSbn.getNotification().getSettingsText())); |
| 363 | mSettingsLinkView.setOnClickListener((View view) -> { |
| 364 | mAppSettingsClickListener.onClick(view, settingsIntent); |
| 365 | }); |
| 366 | } else { |
| 367 | mSettingsLinkView.setVisibility(View.GONE); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | private Intent getAppSettingsIntent(PackageManager pm, String packageName, |
| 372 | NotificationChannel channel, int id, String tag) { |
| 373 | Intent intent = new Intent(Intent.ACTION_MAIN) |
| 374 | .addCategory(Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCES) |
| 375 | .setPackage(packageName); |
| 376 | final List<ResolveInfo> resolveInfos = pm.queryIntentActivities( |
| 377 | intent, |
| 378 | PackageManager.MATCH_DEFAULT_ONLY |
| 379 | ); |
| 380 | if (resolveInfos == null || resolveInfos.size() == 0 || resolveInfos.get(0) == null) { |
| 381 | return null; |
| 382 | } |
| 383 | final ActivityInfo activityInfo = resolveInfos.get(0).activityInfo; |
| 384 | intent.setClassName(activityInfo.packageName, activityInfo.name); |
| 385 | if (channel != null) { |
| 386 | intent.putExtra(Notification.EXTRA_CHANNEL_ID, channel.getId()); |
| 387 | } |
| 388 | intent.putExtra(Notification.EXTRA_NOTIFICATION_ID, id); |
| 389 | intent.putExtra(Notification.EXTRA_NOTIFICATION_TAG, tag); |
| 390 | return intent; |
| 391 | } |
| 392 | |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 393 | @Override |
Mady Mellor | 95d743c | 2017-01-10 12:05:27 -0800 | [diff] [blame] | 394 | public void setGutsParent(NotificationGuts guts) { |
| 395 | mGutsContainer = guts; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | @Override |
Mady Mellor | 434180c | 2017-02-13 11:29:42 -0800 | [diff] [blame] | 399 | public boolean willBeRemoved() { |
Geoffrey Pitsch | 8c8dbde | 2017-04-20 11:44:33 -0400 | [diff] [blame] | 400 | return mChannelEnabledSwitch != null && !mChannelEnabledSwitch.isChecked(); |
Mady Mellor | 434180c | 2017-02-13 11:29:42 -0800 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | @Override |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 404 | public View getContentView() { |
| 405 | return this; |
| 406 | } |
| 407 | |
| 408 | @Override |
Mady Mellor | c2dbe49 | 2017-03-30 13:22:03 -0700 | [diff] [blame] | 409 | public boolean handleCloseControls(boolean save, boolean force) { |
Geoffrey Pitsch | 5278d3d | 2017-03-29 13:39:10 -0400 | [diff] [blame] | 410 | if (save && hasImportanceChanged()) { |
| 411 | if (mCheckSaveListener != null) { |
| 412 | mCheckSaveListener.checkSave(() -> { saveImportance(); }); |
| 413 | } else { |
| 414 | saveImportance(); |
| 415 | } |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 416 | } |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 417 | return false; |
| 418 | } |
Mady Mellor | e09fb70 | 2017-03-30 13:23:29 -0700 | [diff] [blame] | 419 | |
| 420 | @Override |
| 421 | public int getActualHeight() { |
| 422 | return getHeight(); |
| 423 | } |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 424 | } |