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 17 package com.android.settings.notification; 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 HeaderPreferenceController extends NotificationPreferenceController 41 implements PreferenceControllerMixin, LifecycleObserver { 42 43 private final DashboardFragment mFragment; 44 private EntityHeaderController mHeaderController; 45 private boolean mStarted = false; 46 HeaderPreferenceController(Context context, DashboardFragment fragment)47 public HeaderPreferenceController(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 updateState(Preference preference)63 public void updateState(Preference preference) { 64 if (mAppRow != null && mFragment != null) { 65 66 Activity activity = null; 67 if (mStarted) { 68 // don't call done on an activity if it hasn't started yet 69 activity = mFragment.getActivity(); 70 } 71 72 if (activity == null) { 73 return; 74 } 75 76 LayoutPreference pref = (LayoutPreference) preference; 77 mHeaderController = EntityHeaderController.newInstance( 78 activity, mFragment, pref.findViewById(R.id.entity_header)); 79 pref = mHeaderController.setIcon(mAppRow.icon) 80 .setLabel(getLabel()) 81 .setSummary(getSummary()) 82 .setPackageName(mAppRow.pkg) 83 .setUid(mAppRow.uid) 84 .setButtonActions(EntityHeaderController.ActionType.ACTION_NOTIF_PREFERENCE, 85 EntityHeaderController.ActionType.ACTION_NONE) 86 .setHasAppInfoLink(true) 87 .setRecyclerView(mFragment.getListView(), mFragment.getSettingsLifecycle()) 88 .done(activity, mContext); 89 pref.findViewById(R.id.entity_header).setVisibility(View.VISIBLE); 90 } 91 } 92 93 @Override getSummary()94 public CharSequence getSummary() { 95 if (mChannel != null && !isDefaultChannel()) { 96 if (mChannelGroup != null 97 && !TextUtils.isEmpty(mChannelGroup.getName())) { 98 final SpannableStringBuilder summary = new SpannableStringBuilder(); 99 BidiFormatter bidi = BidiFormatter.getInstance(); 100 summary.append(bidi.unicodeWrap(mAppRow.label.toString())); 101 summary.append(bidi.unicodeWrap(mContext.getText( 102 R.string.notification_header_divider_symbol_with_spaces))); 103 summary.append(bidi.unicodeWrap(mChannelGroup.getName().toString())); 104 return summary.toString(); 105 } else { 106 return mAppRow.label.toString(); 107 } 108 } else if (mChannelGroup != null) { 109 return mAppRow.label.toString(); 110 } else { 111 return ""; 112 } 113 } 114 115 @OnLifecycleEvent(Lifecycle.Event.ON_START) onStart()116 public void onStart() { 117 mStarted = true; 118 if (mHeaderController != null) { 119 mHeaderController.styleActionBar(mFragment.getActivity()); 120 } 121 } 122 123 @VisibleForTesting getLabel()124 CharSequence getLabel() { 125 return (mChannel != null && !isDefaultChannel()) ? mChannel.getName() 126 : mChannelGroup != null 127 ? mChannelGroup.getName() 128 : mAppRow.label; 129 } 130 } 131