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