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.app; 18 19 import static com.android.settings.widget.EntityHeaderController.PREF_KEY_APP_HEADER; 20 21 import android.app.Activity; 22 import android.content.Context; 23 import android.text.BidiFormatter; 24 import android.text.SpannableStringBuilder; 25 import android.text.TextUtils; 26 import android.view.View; 27 28 import androidx.annotation.VisibleForTesting; 29 import androidx.lifecycle.LifecycleObserver; 30 import androidx.lifecycle.OnLifecycleEvent; 31 import androidx.preference.Preference; 32 33 import com.android.settings.R; 34 import com.android.settings.core.PreferenceControllerMixin; 35 import com.android.settings.dashboard.DashboardFragment; 36 import com.android.settings.widget.EntityHeaderController; 37 import com.android.settingslib.core.lifecycle.Lifecycle; 38 import com.android.settingslib.widget.LayoutPreference; 39 40 public class ConversationHeaderPreferenceController extends NotificationPreferenceController 41 implements PreferenceControllerMixin, LifecycleObserver { 42 43 private final DashboardFragment mFragment; 44 private EntityHeaderController mHeaderController; 45 private boolean mStarted = false; 46 ConversationHeaderPreferenceController(Context context, DashboardFragment fragment)47 public ConversationHeaderPreferenceController(Context context, DashboardFragment fragment) { 48 super(context, null); 49 mFragment = fragment; 50 } 51 52 @Override getPreferenceKey()53 public String getPreferenceKey() { 54 return PREF_KEY_APP_HEADER; 55 } 56 57 @Override isAvailable()58 public boolean isAvailable() { 59 return mAppRow != null; 60 } 61 62 @Override isIncludedInFilter()63 boolean isIncludedInFilter() { 64 return true; 65 } 66 67 @Override updateState(Preference preference)68 public void updateState(Preference preference) { 69 if (mAppRow != null && mFragment != null) { 70 71 Activity activity = null; 72 if (mStarted) { 73 // don't call done on an activity if it hasn't started yet 74 activity = mFragment.getActivity(); 75 } 76 77 if (activity == null) { 78 return; 79 } 80 81 LayoutPreference pref = (LayoutPreference) preference; 82 mHeaderController = EntityHeaderController.newInstance( 83 activity, mFragment, pref.findViewById(R.id.entity_header)); 84 pref = mHeaderController.setIcon(mConversationDrawable) 85 .setLabel(getLabel()) 86 .setSummary(getSummary()) 87 .setPackageName(mAppRow.pkg) 88 .setUid(mAppRow.uid) 89 .setButtonActions(EntityHeaderController.ActionType.ACTION_NOTIF_PREFERENCE, 90 EntityHeaderController.ActionType.ACTION_NONE) 91 .setHasAppInfoLink(true) 92 .done(mContext); 93 94 pref.findViewById(R.id.entity_header).setVisibility(View.VISIBLE); 95 pref.findViewById(R.id.entity_header).setBackground(null); 96 } 97 } 98 99 @Override getSummary()100 public CharSequence getSummary() { 101 if (mChannel != null && !isDefaultChannel()) { 102 if (mChannelGroup != null 103 && !TextUtils.isEmpty(mChannelGroup.getName())) { 104 final SpannableStringBuilder summary = new SpannableStringBuilder(); 105 BidiFormatter bidi = BidiFormatter.getInstance(); 106 summary.append(bidi.unicodeWrap(mAppRow.label.toString())); 107 summary.append(bidi.unicodeWrap(mContext.getText( 108 R.string.notification_header_divider_symbol_with_spaces))); 109 summary.append(bidi.unicodeWrap(mChannelGroup.getName().toString())); 110 return summary.toString(); 111 } else { 112 return mAppRow.label.toString(); 113 } 114 } else if (mChannelGroup != null) { 115 return mAppRow.label.toString(); 116 } else { 117 return ""; 118 } 119 } 120 121 @OnLifecycleEvent(Lifecycle.Event.ON_START) onStart()122 public void onStart() { 123 mStarted = true; 124 } 125 126 @VisibleForTesting getLabel()127 CharSequence getLabel() { 128 CharSequence label = null; 129 if (mConversationInfo != null) { 130 label = mConversationInfo.getLabel(); 131 } else if (mChannel != null) { 132 label = mChannel.getName(); 133 } 134 return label; 135 } 136 } 137