1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.notification.zen; 18 19 import static android.app.NotificationManager.Policy.CONVERSATION_SENDERS_ANYONE; 20 import static android.app.NotificationManager.Policy.CONVERSATION_SENDERS_IMPORTANT; 21 import static android.app.NotificationManager.Policy.CONVERSATION_SENDERS_NONE; 22 23 import android.content.Context; 24 import android.content.pm.ParceledListSlice; 25 import android.graphics.drawable.Drawable; 26 import android.os.AsyncTask; 27 import android.service.notification.ConversationChannelWrapper; 28 import android.view.View; 29 import android.view.ViewGroup; 30 import android.widget.FrameLayout; 31 import android.widget.ImageView; 32 33 import androidx.preference.Preference; 34 import androidx.preference.PreferenceScreen; 35 36 import com.android.settings.R; 37 import com.android.settings.notification.NotificationBackend; 38 import com.android.settingslib.core.lifecycle.Lifecycle; 39 import com.android.settingslib.widget.LayoutPreference; 40 41 import java.util.ArrayList; 42 import java.util.List; 43 44 /** 45 * Updates the DND Settings conversations image resource based on the conversations channels. 46 */ 47 public class ZenModeConversationsImagePreferenceController 48 extends AbstractZenModePreferenceController { 49 private static final int MAX_CONVERSATIONS_SHOWN = 5; 50 private final int mIconSizePx; 51 private final int mIconOffsetPx; 52 private final ArrayList<Drawable> mConversationDrawables = new ArrayList<>(); 53 private final NotificationBackend mNotificationBackend; 54 55 private ViewGroup mViewGroup; 56 private LayoutPreference mPreference; 57 ZenModeConversationsImagePreferenceController(Context context, String key, Lifecycle lifecycle, NotificationBackend notificationBackend)58 public ZenModeConversationsImagePreferenceController(Context context, String key, 59 Lifecycle lifecycle, NotificationBackend notificationBackend) { 60 super(context, key, lifecycle); 61 mNotificationBackend = notificationBackend; 62 mIconSizePx = 63 mContext.getResources().getDimensionPixelSize(R.dimen.zen_conversations_icon_size); 64 mIconOffsetPx = mContext.getResources() 65 .getDimensionPixelSize(R.dimen.zen_conversations_icon_offset); 66 } 67 68 @Override displayPreference(PreferenceScreen screen)69 public void displayPreference(PreferenceScreen screen) { 70 super.displayPreference(screen); 71 mPreference = (LayoutPreference) screen.findPreference(KEY); 72 mViewGroup = 73 (ViewGroup) mPreference.findViewById(R.id.zen_mode_settings_senders_overlay_view); 74 loadConversations(); 75 } 76 77 @Override isAvailable()78 public boolean isAvailable() { 79 return true; 80 } 81 82 @Override getPreferenceKey()83 public String getPreferenceKey() { 84 return KEY; 85 } 86 87 @Override updateState(Preference preference)88 public void updateState(Preference preference) { 89 loadConversations(); 90 91 mViewGroup.removeAllViews(); 92 final int conversationSenders = mBackend.getPriorityConversationSenders(); 93 if (conversationSenders == CONVERSATION_SENDERS_ANYONE) { 94 mViewGroup.setContentDescription( 95 mContext.getResources().getString(R.string.zen_mode_from_all_conversations)); 96 } else if (conversationSenders == CONVERSATION_SENDERS_IMPORTANT) { 97 mViewGroup.setContentDescription( 98 mContext.getResources().getString( 99 R.string.zen_mode_from_important_conversations)); 100 } else { 101 mViewGroup.setContentDescription(null); 102 mViewGroup.setVisibility(View.GONE); 103 return; 104 } 105 106 final int numDrawablesToShow = Math.min(MAX_CONVERSATIONS_SHOWN, 107 mConversationDrawables.size()); 108 for (int i = 0; i < numDrawablesToShow; i++) { 109 ImageView iv = new ImageView(mContext); 110 iv.setImageDrawable(mConversationDrawables.get(i)); 111 iv.setLayoutParams(new ViewGroup.LayoutParams(mIconSizePx, mIconSizePx)); 112 113 FrameLayout fl = new FrameLayout(mContext); 114 fl.addView(iv); 115 fl.setPadding((numDrawablesToShow - i - 1) * mIconOffsetPx, 0, 0, 0); 116 mViewGroup.addView(fl); 117 } 118 119 mViewGroup.setVisibility(numDrawablesToShow > 0 ? View.VISIBLE : View.GONE); 120 } 121 loadConversations()122 private void loadConversations() { 123 // Load conversations 124 new AsyncTask<Void, Void, Void>() { 125 private List<Drawable> mDrawables = new ArrayList<>(); 126 @Override 127 protected Void doInBackground(Void... unused) { 128 mDrawables.clear(); 129 final int conversationSenders = mBackend.getPriorityConversationSenders(); 130 if (conversationSenders == CONVERSATION_SENDERS_NONE) { 131 return null; 132 } 133 ParceledListSlice<ConversationChannelWrapper> conversations = 134 mNotificationBackend.getConversations( 135 conversationSenders == CONVERSATION_SENDERS_IMPORTANT); 136 if (conversations != null) { 137 for (ConversationChannelWrapper conversation : conversations.getList()) { 138 if (!conversation.getNotificationChannel().isDemoted()) { 139 Drawable drawable = mNotificationBackend.getConversationDrawable( 140 mContext, 141 conversation.getShortcutInfo(), 142 conversation.getPkg(), 143 conversation.getUid(), 144 conversation.getNotificationChannel() 145 .isImportantConversation()); 146 if (drawable != null) { 147 mDrawables.add(drawable); 148 } 149 } 150 } 151 } 152 153 return null; 154 } 155 156 @Override 157 protected void onPostExecute(Void unused) { 158 if (mContext == null) { 159 return; 160 } 161 mConversationDrawables.clear(); 162 mConversationDrawables.addAll(mDrawables); 163 updateState(mPreference); 164 } 165 }.execute(); 166 } 167 } 168