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