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.applications.appinfo;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.pm.PackageInfo;
22 
23 import androidx.preference.PreferenceScreen;
24 
25 import com.android.settings.R;
26 import com.android.settings.core.BasePreferenceController;
27 import com.android.settings.widget.EntityHeaderController;
28 import com.android.settingslib.applications.AppUtils;
29 import com.android.settingslib.applications.ApplicationsState.AppEntry;
30 import com.android.settingslib.core.lifecycle.Lifecycle;
31 import com.android.settingslib.core.lifecycle.LifecycleObserver;
32 import com.android.settingslib.widget.LayoutPreference;
33 
34 public class AppHeaderViewPreferenceController extends BasePreferenceController
35         implements AppInfoDashboardFragment.Callback, LifecycleObserver {
36 
37     private static final String KEY_HEADER = "header_view";
38 
39     private LayoutPreference mHeader;
40     private final AppInfoDashboardFragment mParent;
41     private final String mPackageName;
42     private final Lifecycle mLifecycle;
43 
44     private EntityHeaderController mEntityHeaderController;
45 
AppHeaderViewPreferenceController(Context context, AppInfoDashboardFragment parent, String packageName, Lifecycle lifecycle)46     public AppHeaderViewPreferenceController(Context context, AppInfoDashboardFragment parent,
47             String packageName, Lifecycle lifecycle) {
48         super(context, KEY_HEADER);
49         mParent = parent;
50         mPackageName = packageName;
51         mLifecycle = lifecycle;
52         if (mLifecycle != null) {
53             mLifecycle.addObserver(this);
54         }
55     }
56 
57     @Override
getAvailabilityStatus()58     public int getAvailabilityStatus() {
59         return AVAILABLE;
60     }
61 
62     @Override
displayPreference(PreferenceScreen screen)63     public void displayPreference(PreferenceScreen screen) {
64         super.displayPreference(screen);
65         mHeader = screen.findPreference(KEY_HEADER);
66         final Activity activity = mParent.getActivity();
67         mEntityHeaderController = EntityHeaderController
68                 .newInstance(activity, mParent, mHeader.findViewById(R.id.entity_header))
69                 .setRecyclerView(mParent.getListView(), mLifecycle)
70                 .setPackageName(mPackageName)
71                 .setButtonActions(EntityHeaderController.ActionType.ACTION_NONE,
72                         EntityHeaderController.ActionType.ACTION_NONE)
73                 .bindHeaderButtons();
74     }
75 
76     @Override
refreshUi()77     public void refreshUi() {
78         setAppLabelAndIcon(mParent.getPackageInfo(), mParent.getAppEntry());
79     }
80 
81     // Utility method to set application label and icon.
setAppLabelAndIcon(PackageInfo pkgInfo, AppEntry appEntry)82     private void setAppLabelAndIcon(PackageInfo pkgInfo, AppEntry appEntry) {
83         final Activity activity = mParent.getActivity();
84         final boolean isInstantApp = AppUtils.isInstant(pkgInfo.applicationInfo);
85         mEntityHeaderController
86                 .setLabel(appEntry)
87                 .setIcon(appEntry)
88                 .setIsInstantApp(isInstantApp)
89                 .done(activity, false /* rebindActions */);
90     }
91 }
92