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.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.lifecycle.LifecycleObserver;
29 import androidx.lifecycle.OnLifecycleEvent;
30 import androidx.preference.Preference;
31 
32 import com.android.settings.R;
33 import com.android.settings.core.PreferenceControllerMixin;
34 import com.android.settings.dashboard.DashboardFragment;
35 import com.android.settings.widget.EntityHeaderController;
36 import com.android.settingslib.core.lifecycle.Lifecycle;
37 import com.android.settingslib.widget.LayoutPreference;
38 
39 public class HeaderPreferenceController extends NotificationPreferenceController
40         implements PreferenceControllerMixin, LifecycleObserver {
41 
42     private final DashboardFragment mFragment;
43     private EntityHeaderController mHeaderController;
44     private boolean mStarted = false;
45 
HeaderPreferenceController(Context context, DashboardFragment fragment)46     public HeaderPreferenceController(Context context, DashboardFragment 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
isIncludedInFilter()62     boolean isIncludedInFilter() {
63         return true;
64     }
65 
66     @Override
updateState(Preference preference)67     public void updateState(Preference preference) {
68         if (mAppRow != null && mFragment != null) {
69 
70             Activity activity = null;
71             if (mStarted) {
72                 // don't call done on an activity if it hasn't started yet
73                 activity = mFragment.getActivity();
74             }
75 
76             if (activity == null) {
77                 return;
78             }
79 
80             LayoutPreference pref = (LayoutPreference) preference;
81             mHeaderController = EntityHeaderController.newInstance(
82                     activity, mFragment, pref.findViewById(R.id.entity_header));
83             pref = mHeaderController.setIcon(mAppRow.icon)
84                     .setLabel(getLabel())
85                     .setSummary(getSummary())
86                     .setSecondSummary(getSecondSummary())
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             pref.findViewById(R.id.entity_header).setVisibility(View.VISIBLE);
94             pref.findViewById(R.id.entity_header).setBackground(null);
95         }
96     }
97 
getLabel()98     public CharSequence getLabel() {
99         if (mChannel != null && !isDefaultChannel()) {
100             return mChannel.getName();
101         } else {
102             return mAppRow.label;
103         }
104     }
105 
106     @OnLifecycleEvent(Lifecycle.Event.ON_START)
onStart()107     public void onStart() {
108         mStarted = true;
109     }
110 
111     @Override
getSummary()112     public CharSequence getSummary() {
113         if (mChannel != null) {
114             if (mChannelGroup != null
115                     && !TextUtils.isEmpty(mChannelGroup.getName())) {
116                 final SpannableStringBuilder summary = new SpannableStringBuilder();
117                 BidiFormatter bidi = BidiFormatter.getInstance();
118                 summary.append(bidi.unicodeWrap(mAppRow.label));
119                 summary.append(bidi.unicodeWrap(mContext.getText(
120                         R.string.notification_header_divider_symbol_with_spaces)));
121                 summary.append(bidi.unicodeWrap(mChannelGroup.getName().toString()));
122                 return summary.toString();
123             } else {
124                 return mAppRow.label.toString();
125             }
126         }
127         return "";
128     }
129 
getSecondSummary()130     public CharSequence getSecondSummary() {
131         return mChannel == null ? null : mChannel.getDescription();
132     }
133 }
134