• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.applications.specialaccess.notificationaccess;
18 
19 import android.companion.ICompanionDeviceManager;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.pm.PackageInfo;
23 import android.content.pm.PackageManager;
24 import android.util.IconDrawableFactory;
25 import android.view.View;
26 
27 import androidx.lifecycle.LifecycleObserver;
28 import androidx.preference.PreferenceScreen;
29 
30 import com.android.settings.R;
31 import com.android.settings.core.BasePreferenceController;
32 import com.android.settings.core.PreferenceControllerMixin;
33 import com.android.settings.dashboard.DashboardFragment;
34 import com.android.settings.notification.NotificationBackend;
35 import com.android.settings.widget.EntityHeaderController;
36 import com.android.settingslib.applications.AppUtils;
37 import com.android.settingslib.bluetooth.LocalBluetoothManager;
38 import com.android.settingslib.widget.LayoutPreference;
39 
40 public class HeaderPreferenceController extends BasePreferenceController
41         implements PreferenceControllerMixin, LifecycleObserver {
42 
43     private DashboardFragment mFragment;
44     private EntityHeaderController mHeaderController;
45     private PackageInfo mPackageInfo;
46     private PackageManager mPm;
47     private CharSequence mServiceName;
48     private ICompanionDeviceManager mCdm;
49     private LocalBluetoothManager mBm;
50     private ComponentName mCn;
51     private int mUserId;
52 
HeaderPreferenceController(Context context, String key)53     public HeaderPreferenceController(Context context, String key) {
54         super(context, key);
55     }
56 
setFragment(DashboardFragment fragment)57     public HeaderPreferenceController setFragment(DashboardFragment fragment) {
58         mFragment = fragment;
59         return this;
60     }
61 
setPackageInfo(PackageInfo packageInfo)62     public HeaderPreferenceController setPackageInfo(PackageInfo packageInfo) {
63         mPackageInfo = packageInfo;
64         return this;
65     }
66 
setPm(PackageManager pm)67     public HeaderPreferenceController setPm(PackageManager pm) {
68         mPm = pm;
69         return this;
70     }
71 
setServiceName(CharSequence serviceName)72     public HeaderPreferenceController setServiceName(CharSequence serviceName) {
73         mServiceName = serviceName;
74         return this;
75     }
76 
setCdm(ICompanionDeviceManager cdm)77     public HeaderPreferenceController setCdm(ICompanionDeviceManager cdm) {
78         mCdm = cdm;
79         return this;
80     }
81 
setBluetoothManager(LocalBluetoothManager bm)82     public HeaderPreferenceController setBluetoothManager(LocalBluetoothManager bm) {
83         mBm = bm;
84         return this;
85     }
86 
setCn(ComponentName cn)87     public HeaderPreferenceController setCn(ComponentName cn) {
88         mCn = cn;
89         return this;
90     }
91 
setUserId(int userId)92     public HeaderPreferenceController setUserId(int userId) {
93         mUserId = userId;
94         return this;
95     }
96 
97     @Override
getAvailabilityStatus()98     public int getAvailabilityStatus() {
99         return AVAILABLE;
100     }
101 
102     @Override
displayPreference(PreferenceScreen screen)103     public void displayPreference(PreferenceScreen screen) {
104         super.displayPreference(screen);
105         if (mFragment == null) {
106             return;
107         }
108         LayoutPreference pref = screen.findPreference(getPreferenceKey());
109         mHeaderController = EntityHeaderController.newInstance(
110                 mFragment.getActivity(), mFragment, pref.findViewById(R.id.entity_header));
111         pref = mHeaderController
112                 .setRecyclerView(mFragment.getListView(), mFragment.getSettingsLifecycle())
113                 .setIcon(IconDrawableFactory.newInstance(mFragment.getActivity())
114                         .getBadgedIcon(mPackageInfo.applicationInfo))
115                 .setLabel(mPackageInfo.applicationInfo.loadLabel(mPm))
116                 .setSummary(mServiceName)
117                 .setSecondSummary(new NotificationBackend().getDeviceList(
118                         mCdm, mBm, mCn.getPackageName(), mUserId))
119                 .setIsInstantApp(AppUtils.isInstant(mPackageInfo.applicationInfo))
120                 .setPackageName(mPackageInfo.packageName)
121                 .setUid(mPackageInfo.applicationInfo.uid)
122                 .setHasAppInfoLink(true)
123                 .setButtonActions(EntityHeaderController.ActionType.ACTION_NONE,
124                         EntityHeaderController.ActionType.ACTION_NONE)
125                 .done(mFragment.getActivity(), mContext);
126         pref.findViewById(R.id.entity_header).setVisibility(View.VISIBLE);
127     }
128 }
129