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