• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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
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(mAppRow.icon)
85                     .setLabel(getLabel())
86                     .setSummary(getSummary())
87                     .setSecondSummary(getSecondSummary())
88                     .setPackageName(mAppRow.pkg)
89                     .setUid(mAppRow.uid)
90                     .setButtonActions(EntityHeaderController.ActionType.ACTION_NOTIF_PREFERENCE,
91                             EntityHeaderController.ActionType.ACTION_NONE)
92                     .setHasAppInfoLink(true)
93                     .setRecyclerView(mFragment.getListView(), mFragment.getSettingsLifecycle())
94                     .done(activity, mContext);
95             pref.findViewById(R.id.entity_header).setVisibility(View.VISIBLE);
96             pref.findViewById(R.id.entity_header).setBackground(null);
97         }
98     }
99 
getLabel()100     public CharSequence getLabel() {
101         if (mChannel != null && !isDefaultChannel()) {
102             return mChannel.getName();
103         } else {
104             return mAppRow.label;
105         }
106     }
107 
108     @OnLifecycleEvent(Lifecycle.Event.ON_START)
onStart()109     public void onStart() {
110         mStarted = true;
111     }
112 
113     @Override
getSummary()114     public CharSequence getSummary() {
115         if (mChannel != null) {
116             if (mChannelGroup != null
117                     && !TextUtils.isEmpty(mChannelGroup.getName())) {
118                 final SpannableStringBuilder summary = new SpannableStringBuilder();
119                 BidiFormatter bidi = BidiFormatter.getInstance();
120                 summary.append(bidi.unicodeWrap(mAppRow.label));
121                 summary.append(bidi.unicodeWrap(mContext.getText(
122                         R.string.notification_header_divider_symbol_with_spaces)));
123                 summary.append(bidi.unicodeWrap(mChannelGroup.getName().toString()));
124                 return summary.toString();
125             } else {
126                 return mAppRow.label.toString();
127             }
128         }
129         return "";
130     }
131 
getSecondSummary()132     public CharSequence getSecondSummary() {
133         return mChannel == null ? null : mChannel.getDescription();
134     }
135 }
136