1 /*
2  * Copyright (C) 2015 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;
18 
19 import android.app.Activity;
20 import android.app.Fragment;
21 import android.content.Intent;
22 import android.content.res.ColorStateList;
23 import android.graphics.drawable.Drawable;
24 import android.os.Bundle;
25 import android.view.View;
26 import android.view.View.OnClickListener;
27 import android.view.ViewGroup;
28 import android.widget.ImageView;
29 import android.widget.TextView;
30 
31 import com.android.settings.applications.AppInfoBase;
32 import com.android.settings.applications.InstalledAppDetails;
33 
34 public class AppHeader {
35 
36     public static final String EXTRA_HIDE_INFO_BUTTON = "hideInfoButton";
37     // constant value that can be used to check return code from sub activity.
38     private static final int INSTALLED_APP_DETAILS = 1;
39 
createAppHeader(SettingsPreferenceFragment fragment, Drawable icon, CharSequence label, String pkgName, int uid)40     public static void createAppHeader(SettingsPreferenceFragment fragment, Drawable icon,
41             CharSequence label, String pkgName, int uid) {
42         createAppHeader(fragment, icon, label, pkgName, uid, 0, null);
43     }
44 
createAppHeader(SettingsPreferenceFragment fragment, Drawable icon, CharSequence label, String pkgName, int uid, Intent externalSettings)45     public static void createAppHeader(SettingsPreferenceFragment fragment, Drawable icon,
46             CharSequence label, String pkgName, int uid, Intent externalSettings) {
47         createAppHeader(fragment, icon, label, pkgName, uid, 0, externalSettings);
48     }
49 
createAppHeader(Activity activity, Drawable icon, CharSequence label, String pkgName, int uid, ViewGroup pinnedHeader)50     public static void createAppHeader(Activity activity, Drawable icon, CharSequence label,
51             String pkgName, int uid, ViewGroup pinnedHeader) {
52         final View bar = activity.getLayoutInflater().inflate(R.layout.app_header,
53                 pinnedHeader, false);
54         setupHeaderView(activity, icon, label, pkgName, uid, false, 0, bar, null);
55         pinnedHeader.addView(bar);
56     }
57 
createAppHeader(SettingsPreferenceFragment fragment, Drawable icon, CharSequence label, String pkgName, int uid, int tintColorRes)58     public static void createAppHeader(SettingsPreferenceFragment fragment, Drawable icon,
59             CharSequence label, String pkgName, int uid, int tintColorRes) {
60         createAppHeader(fragment, icon, label, pkgName, uid, tintColorRes, null);
61     }
62 
createAppHeader(SettingsPreferenceFragment fragment, Drawable icon, CharSequence label, String pkgName, int uid, int tintColorRes, Intent externalSettings)63     public static void createAppHeader(SettingsPreferenceFragment fragment, Drawable icon,
64             CharSequence label, String pkgName, int uid, int tintColorRes,
65             Intent externalSettings) {
66         View bar = fragment.setPinnedHeaderView(R.layout.app_header);
67         setupHeaderView(fragment.getActivity(), icon, label, pkgName, uid, includeAppInfo(fragment),
68                 tintColorRes, bar, externalSettings);
69     }
70 
setupHeaderView(final Activity activity, Drawable icon, CharSequence label, final String pkgName, final int uid, final boolean includeAppInfo, int tintColorRes, View bar, final Intent externalSettings)71     public static View setupHeaderView(final Activity activity, Drawable icon, CharSequence label,
72             final String pkgName, final int uid, final boolean includeAppInfo, int tintColorRes,
73             View bar, final Intent externalSettings) {
74         final ImageView appIcon = (ImageView) bar.findViewById(R.id.app_icon);
75         appIcon.setImageDrawable(icon);
76         if (tintColorRes != 0) {
77             appIcon.setImageTintList(ColorStateList.valueOf(activity.getColor(tintColorRes)));
78         }
79 
80         final TextView appName = (TextView) bar.findViewById(R.id.app_name);
81         appName.setText(label);
82 
83         if (pkgName != null && !pkgName.equals(Utils.OS_PKG)) {
84             bar.setClickable(true);
85             bar.setOnClickListener(new OnClickListener() {
86                 @Override
87                 public void onClick(View v) {
88                     if (includeAppInfo) {
89                         AppInfoBase.startAppInfoFragment(InstalledAppDetails.class,
90                                 R.string.application_info_label, pkgName, uid, activity,
91                                 INSTALLED_APP_DETAILS);
92                     } else {
93                         activity.finish();
94                     }
95                 }
96             });
97             if (externalSettings != null) {
98                 final View appSettings = bar.findViewById(R.id.app_settings);
99                 appSettings.setVisibility(View.VISIBLE);
100                 appSettings.setOnClickListener(new OnClickListener() {
101                     @Override
102                     public void onClick(View v) {
103                         activity.startActivity(externalSettings);
104                     }
105                 });
106             }
107         }
108         return bar;
109     }
110 
includeAppInfo(final Fragment fragment)111     public static boolean includeAppInfo(final Fragment fragment) {
112         Bundle args = fragment.getArguments();
113         boolean showInfo = true;
114         if (args != null && args.getBoolean(EXTRA_HIDE_INFO_BUTTON, false)) {
115             showInfo = false;
116         }
117         Intent intent = fragment.getActivity().getIntent();
118         if (intent != null && intent.getBooleanExtra(EXTRA_HIDE_INFO_BUTTON, false)) {
119             showInfo = false;
120         }
121         return showInfo;
122     }
123 }
124