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 | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 157 | String channelsDescText; |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 158 | mNumChannelsView = findViewById(R.id.num_channels_desc); |
Geoffrey Pitsch | 1fbf66e | 2017-05-09 11:32:27 -0400 | [diff] [blame] | 159 | if (mIsSingleDefaultChannel) { |
Geoffrey Pitsch | ee8c81e | 2017-03-23 11:38:56 -0400 | [diff] [blame] | 160 | channelsDescText = mContext.getString(R.string.notification_default_channel_desc); |
| 161 | } else { |
| 162 | switch (mNotificationChannels.size()) { |
| 163 | case 1: |
| 164 | channelsDescText = String.format(mContext.getResources().getQuantityString( |
Geoffrey Pitsch | 1fbf66e | 2017-05-09 11:32:27 -0400 | [diff] [blame] | 165 | R.plurals.notification_num_channels_desc, numTotalChannels), |
| 166 | numTotalChannels); |
Geoffrey Pitsch | ee8c81e | 2017-03-23 11:38:56 -0400 | [diff] [blame] | 167 | break; |
| 168 | case 2: |
| 169 | channelsDescText = mContext.getString( |
| 170 | R.string.notification_channels_list_desc_2, |
| 171 | mNotificationChannels.get(0).getName(), |
| 172 | mNotificationChannels.get(1).getName()); |
| 173 | break; |
| 174 | default: |
| 175 | final int numOthers = mNotificationChannels.size() - 2; |
| 176 | channelsDescText = String.format( |
| 177 | mContext.getResources().getQuantityString( |
| 178 | R.plurals.notification_channels_list_desc_2_and_others, |
| 179 | numOthers), |
| 180 | mNotificationChannels.get(0).getName(), |
| 181 | mNotificationChannels.get(1).getName(), |
| 182 | numOthers); |
| 183 | } |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 184 | } |
| 185 | mNumChannelsView.setText(channelsDescText); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 186 | |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 187 | if (mSingleNotificationChannel == null) { |
| 188 | // Multiple channels don't use a channel name for the title. |
| 189 | channelNameText = mContext.getString(R.string.notification_num_channels, |
| 190 | mNotificationChannels.size()); |
Geoffrey Pitsch | 1fbf66e | 2017-05-09 11:32:27 -0400 | [diff] [blame] | 191 | } else if (mIsSingleDefaultChannel) { |
Geoffrey Pitsch | ee8c81e | 2017-03-23 11:38:56 -0400 | [diff] [blame] | 192 | // 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] | 193 | channelNameText = mContext.getString(R.string.notification_header_default_channel); |
| 194 | } else { |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 195 | channelNameText = mSingleNotificationChannel.getName(); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 196 | } |
Geoffrey Pitsch | d94e788 | 2017-04-06 09:52:11 -0400 | [diff] [blame] | 197 | ((TextView) findViewById(R.id.pkgname)).setText(mAppName); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 198 | ((TextView) findViewById(R.id.channel_name)).setText(channelNameText); |
| 199 | |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 200 | // Set group information if this channel has an associated group. |
| 201 | CharSequence groupName = null; |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 202 | if (mSingleNotificationChannel != null && mSingleNotificationChannel.getGroup() != null) { |
| 203 | final NotificationChannelGroup notificationChannelGroup = |
| 204 | iNotificationManager.getNotificationChannelGroupForPackage( |
| 205 | mSingleNotificationChannel.getGroup(), pkg, mAppUid); |
| 206 | if (notificationChannelGroup != null) { |
| 207 | groupName = notificationChannelGroup.getName(); |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 208 | } |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 209 | } |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 210 | TextView groupNameView = ((TextView) findViewById(R.id.group_name)); |
| 211 | TextView groupDividerView = ((TextView) findViewById(R.id.pkg_group_divider)); |
| 212 | if (groupName != null) { |
| 213 | groupNameView.setText(groupName); |
| 214 | groupNameView.setVisibility(View.VISIBLE); |
| 215 | groupDividerView.setVisibility(View.VISIBLE); |
| 216 | } else { |
| 217 | groupNameView.setVisibility(View.GONE); |
| 218 | groupDividerView.setVisibility(View.GONE); |
| 219 | } |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 220 | |
| 221 | boolean nonBlockable = false; |
| 222 | try { |
Julia Reynolds | 5a31193 | 2017-03-01 16:33:44 -0500 | [diff] [blame] | 223 | final PackageInfo pkgInfo = pm.getPackageInfo(pkg, PackageManager.GET_SIGNATURES); |
| 224 | nonBlockable = Utils.isSystemPackage(getResources(), pm, pkgInfo); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 225 | } catch (PackageManager.NameNotFoundException e) { |
| 226 | // unlikely. |
| 227 | } |
| 228 | if (nonBlockablePkgs != null) { |
| 229 | nonBlockable |= nonBlockablePkgs.contains(pkg); |
| 230 | } |
| 231 | |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 232 | bindButtons(nonBlockable); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 233 | |
| 234 | // Top-level importance group |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 235 | mChannelDisabledView = findViewById(R.id.channel_disabled); |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 236 | updateSecondaryText(); |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 237 | |
| 238 | // Settings button. |
| 239 | final TextView settingsButton = (TextView) findViewById(R.id.more_settings); |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 240 | if (mAppUid >= 0 && onSettingsClick != null) { |
Geoffrey Pitsch | 029a3fa | 2017-03-30 13:01:57 -0400 | [diff] [blame] | 241 | settingsButton.setVisibility(View.VISIBLE); |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 242 | final int appUidF = mAppUid; |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 243 | settingsButton.setOnClickListener( |
| 244 | (View view) -> { |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 245 | onSettingsClick.onClick(view, mSingleNotificationChannel, appUidF); |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 246 | }); |
Geoffrey Pitsch | 1fbf66e | 2017-05-09 11:32:27 -0400 | [diff] [blame] | 247 | if (numTotalChannels > 1) { |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 248 | settingsButton.setText(R.string.notification_all_categories); |
| 249 | } else { |
| 250 | settingsButton.setText(R.string.notification_more_settings); |
| 251 | } |
| 252 | |
| 253 | } else { |
| 254 | settingsButton.setVisibility(View.GONE); |
| 255 | } |
| 256 | |
| 257 | // Done button. |
| 258 | final TextView doneButton = (TextView) findViewById(R.id.done); |
| 259 | doneButton.setText(R.string.notification_done); |
| 260 | doneButton.setOnClickListener(onDoneClick); |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 261 | |
| 262 | // Optional settings link |
| 263 | updateAppSettingsLink(); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Geoffrey Pitsch | 5278d3d | 2017-03-29 13:39:10 -0400 | [diff] [blame] | 266 | private boolean hasImportanceChanged() { |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 267 | return mSingleNotificationChannel != null && |
Geoffrey Pitsch | 8c8dbde | 2017-04-20 11:44:33 -0400 | [diff] [blame] | 268 | mChannelEnabledSwitch != null && |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 269 | mStartingUserImportance != getSelectedImportance(); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 272 | private void saveImportance() { |
Geoffrey Pitsch | 8c8dbde | 2017-04-20 11:44:33 -0400 | [diff] [blame] | 273 | if (!hasImportanceChanged()) { |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 274 | return; |
| 275 | } |
Geoffrey Pitsch | 8c8dbde | 2017-04-20 11:44:33 -0400 | [diff] [blame] | 276 | final int selectedImportance = getSelectedImportance(); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 277 | MetricsLogger.action(mContext, MetricsEvent.ACTION_SAVE_IMPORTANCE, |
| 278 | selectedImportance - mStartingUserImportance); |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 279 | mSingleNotificationChannel.setImportance(selectedImportance); |
Julia Reynolds | 8ceb579 | 2017-04-11 11:32:44 -0400 | [diff] [blame] | 280 | mSingleNotificationChannel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 281 | try { |
| 282 | mINotificationManager.updateNotificationChannelForPackage( |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 283 | mPkg, mAppUid, mSingleNotificationChannel); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 284 | } catch (RemoteException e) { |
| 285 | // :( |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | private int getSelectedImportance() { |
| 290 | if (!mChannelEnabledSwitch.isChecked()) { |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 291 | return IMPORTANCE_NONE; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 292 | } else { |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 293 | return mStartingUserImportance; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 297 | private void bindButtons(final boolean nonBlockable) { |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 298 | // Enabled Switch |
| 299 | mChannelEnabledSwitch = (Switch) findViewById(R.id.channel_enabled_switch); |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 300 | mChannelEnabledSwitch.setChecked( |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 301 | mStartingUserImportance != IMPORTANCE_NONE); |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 302 | final boolean visible = !nonBlockable && mSingleNotificationChannel != null; |
| 303 | mChannelEnabledSwitch.setVisibility(visible ? View.VISIBLE : View.INVISIBLE); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 304 | |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 305 | // Callback when checked. |
| 306 | mChannelEnabledSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> { |
Mady Mellor | 95d743c | 2017-01-10 12:05:27 -0800 | [diff] [blame] | 307 | if (mGutsContainer != null) { |
| 308 | mGutsContainer.resetFalsingCheck(); |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 309 | } |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 310 | updateSecondaryText(); |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 311 | updateAppSettingsLink(); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 312 | }); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Geoffrey Pitsch | d94e788 | 2017-04-06 09:52:11 -0400 | [diff] [blame] | 315 | @Override |
| 316 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 317 | super.onInitializeAccessibilityEvent(event); |
| 318 | if (mGutsContainer != null && |
| 319 | event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { |
| 320 | if (mGutsContainer.isExposed()) { |
| 321 | event.getText().add(mContext.getString( |
| 322 | R.string.notification_channel_controls_opened_accessibility, mAppName)); |
| 323 | } else { |
| 324 | event.getText().add(mContext.getString( |
| 325 | R.string.notification_channel_controls_closed_accessibility, mAppName)); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 330 | private void updateSecondaryText() { |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 331 | final boolean disabled = mSingleNotificationChannel != null && |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 332 | getSelectedImportance() == IMPORTANCE_NONE; |
Geoffrey Pitsch | ee8c81e | 2017-03-23 11:38:56 -0400 | [diff] [blame] | 333 | if (disabled) { |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 334 | mChannelDisabledView.setVisibility(View.VISIBLE); |
| 335 | mNumChannelsView.setVisibility(View.GONE); |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 336 | } else { |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 337 | mChannelDisabledView.setVisibility(View.GONE); |
Geoffrey Pitsch | 1fbf66e | 2017-05-09 11:32:27 -0400 | [diff] [blame] | 338 | mNumChannelsView.setVisibility(mIsSingleDefaultChannel ? View.INVISIBLE : View.VISIBLE); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 342 | private void updateAppSettingsLink() { |
| 343 | mSettingsLinkView = findViewById(R.id.app_settings); |
| 344 | Intent settingsIntent = getAppSettingsIntent(mPm, mPkg, mSingleNotificationChannel, |
| 345 | mSbn.getId(), mSbn.getTag()); |
| 346 | if (settingsIntent != null && getSelectedImportance() != IMPORTANCE_NONE |
| 347 | && !TextUtils.isEmpty(mSbn.getNotification().getSettingsText())) { |
| 348 | mSettingsLinkView.setVisibility(View.VISIBLE); |
| 349 | mSettingsLinkView.setText(mContext.getString(R.string.notification_app_settings, |
| 350 | mSbn.getNotification().getSettingsText())); |
| 351 | mSettingsLinkView.setOnClickListener((View view) -> { |
| 352 | mAppSettingsClickListener.onClick(view, settingsIntent); |
| 353 | }); |
| 354 | } else { |
| 355 | mSettingsLinkView.setVisibility(View.GONE); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | private Intent getAppSettingsIntent(PackageManager pm, String packageName, |
| 360 | NotificationChannel channel, int id, String tag) { |
| 361 | Intent intent = new Intent(Intent.ACTION_MAIN) |
| 362 | .addCategory(Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCES) |
| 363 | .setPackage(packageName); |
| 364 | final List<ResolveInfo> resolveInfos = pm.queryIntentActivities( |
| 365 | intent, |
| 366 | PackageManager.MATCH_DEFAULT_ONLY |
| 367 | ); |
| 368 | if (resolveInfos == null || resolveInfos.size() == 0 || resolveInfos.get(0) == null) { |
| 369 | return null; |
| 370 | } |
| 371 | final ActivityInfo activityInfo = resolveInfos.get(0).activityInfo; |
| 372 | intent.setClassName(activityInfo.packageName, activityInfo.name); |
| 373 | if (channel != null) { |
| 374 | intent.putExtra(Notification.EXTRA_CHANNEL_ID, channel.getId()); |
| 375 | } |
| 376 | intent.putExtra(Notification.EXTRA_NOTIFICATION_ID, id); |
| 377 | intent.putExtra(Notification.EXTRA_NOTIFICATION_TAG, tag); |
| 378 | return intent; |
| 379 | } |
| 380 | |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 381 | @Override |
Mady Mellor | 95d743c | 2017-01-10 12:05:27 -0800 | [diff] [blame] | 382 | public void setGutsParent(NotificationGuts guts) { |
| 383 | mGutsContainer = guts; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | @Override |
Mady Mellor | 434180c | 2017-02-13 11:29:42 -0800 | [diff] [blame] | 387 | public boolean willBeRemoved() { |
Geoffrey Pitsch | 8c8dbde | 2017-04-20 11:44:33 -0400 | [diff] [blame] | 388 | return mChannelEnabledSwitch != null && !mChannelEnabledSwitch.isChecked(); |
Mady Mellor | 434180c | 2017-02-13 11:29:42 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | @Override |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 392 | public View getContentView() { |
| 393 | return this; |
| 394 | } |
| 395 | |
| 396 | @Override |
Mady Mellor | c2dbe49 | 2017-03-30 13:22:03 -0700 | [diff] [blame] | 397 | public boolean handleCloseControls(boolean save, boolean force) { |
Geoffrey Pitsch | 5278d3d | 2017-03-29 13:39:10 -0400 | [diff] [blame] | 398 | if (save && hasImportanceChanged()) { |
| 399 | if (mCheckSaveListener != null) { |
| 400 | mCheckSaveListener.checkSave(() -> { saveImportance(); }); |
| 401 | } else { |
| 402 | saveImportance(); |
| 403 | } |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 404 | } |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 405 | return false; |
| 406 | } |
Mady Mellor | e09fb70 | 2017-03-30 13:23:29 -0700 | [diff] [blame] | 407 | |
| 408 | @Override |
| 409 | public int getActualHeight() { |
| 410 | return getHeight(); |
| 411 | } |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 412 | } |