Modify AppHeaderController to provide UI for notif settings

- The AppHeaderController is no longer a singleton. Each time appheader
  should be updated we will create a new controller. It acts as a
  builder/mutator, user provides customizable inputs, and the controller
  will bind everything to UI when calling done().

Bug: 32442716
Test: RunSettingsRoboTests
Change-Id: Icfc5bcd8bc170a02b57432d864eaddf71db0d5b4
This commit is contained in:
Fan Zhang
2016-10-27 15:50:36 -07:00
parent 6e3b964cf4
commit 1ff25ba7e6
9 changed files with 364 additions and 75 deletions

View File

@@ -16,20 +16,26 @@
package com.android.settings.applications;
import android.annotation.IdRes;
import android.annotation.UserIdInt;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.UserHandle;
import android.support.annotation.IntDef;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.settings.AppHeader;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settingslib.applications.ApplicationsState;
@@ -39,43 +45,126 @@ import java.lang.annotation.RetentionPolicy;
public class AppHeaderController {
@IntDef({ActionType.ACTION_APP_INFO,
@IntDef({ActionType.ACTION_NONE,
ActionType.ACTION_APP_INFO,
ActionType.ACTION_APP_PREFERENCE,
ActionType.ACTION_STORE_DEEP_LINK,
ActionType.ACTION_NOTIF_PREFERENCE})
@Retention(RetentionPolicy.SOURCE)
@interface ActionType {
int ACTION_APP_INFO = 0;
int ACTION_STORE_DEEP_LINK = 1;
int ACTION_APP_PREFERENCE = 2;
int ACTION_NOTIF_PREFERENCE = 3;
public @interface ActionType {
int ACTION_NONE = 0;
int ACTION_APP_INFO = 1;
int ACTION_STORE_DEEP_LINK = 2;
int ACTION_APP_PREFERENCE = 3;
int ACTION_NOTIF_PREFERENCE = 4;
}
private static final String TAG = "AppDetailFeature";
private final Context mContext;
private final Fragment mFragment;
private final View mAppHeader;
public AppHeaderController(Context context) {
private Drawable mIcon;
private CharSequence mLabel;
private CharSequence mSummary;
private String mPackageName;
private Intent mAppNotifPrefIntent;
@UserIdInt
private int mUid = UserHandle.USER_NULL;
@ActionType
private int mLeftAction;
@ActionType
private int mRightAction;
public AppHeaderController(Context context, Fragment fragment, View appHeader) {
mContext = context;
mFragment = fragment;
if (appHeader != null) {
mAppHeader = appHeader;
} else {
mAppHeader = LayoutInflater.from(mContext)
.inflate(R.layout.app_details, null /* root */);
}
}
public void bindAppHeader(View appSnippet, PackageInfo packageInfo,
ApplicationsState.AppEntry appEntry) {
public AppHeaderController setIcon(Drawable icon) {
if (icon != null) {
mIcon = icon.getConstantState().newDrawable(mContext.getResources());
}
return this;
}
public AppHeaderController setIcon(ApplicationsState.AppEntry appEntry) {
if (appEntry.icon != null) {
mIcon = appEntry.icon.getConstantState().newDrawable(mContext.getResources());
}
return this;
}
public AppHeaderController setLabel(CharSequence label) {
mLabel = label;
return this;
}
public AppHeaderController setLabel(ApplicationsState.AppEntry appEntry) {
mLabel = appEntry.label;
return this;
}
public AppHeaderController setSummary(CharSequence summary) {
mSummary = summary;
return this;
}
public AppHeaderController setSummary(PackageInfo packageInfo) {
if (packageInfo != null) {
mSummary = packageInfo.versionName;
}
return this;
}
public AppHeaderController setButtonActions(@ActionType int leftAction,
@ActionType int rightAction) {
mLeftAction = leftAction;
mRightAction = rightAction;
return this;
}
public AppHeaderController setPackageName(String packageName) {
mPackageName = packageName;
return this;
}
public AppHeaderController setUid(int uid) {
mUid = uid;
return this;
}
public AppHeaderController setAppNotifPrefIntent(Intent appNotifPrefIntent) {
mAppNotifPrefIntent = appNotifPrefIntent;
return this;
}
/**
* Binds app header view and data from {@code PackageInfo} and {@code AppEntry}.
*/
public void bindAppHeader(PackageInfo packageInfo, ApplicationsState.AppEntry appEntry) {
final String versionName = packageInfo == null ? null : packageInfo.versionName;
final Resources res = appSnippet.getResources();
final Resources res = mAppHeader.getResources();
// Set Icon
final ImageView iconView = (ImageView) appSnippet.findViewById(android.R.id.icon);
final ImageView iconView = (ImageView) mAppHeader.findViewById(android.R.id.icon);
if (appEntry.icon != null) {
iconView.setImageDrawable(appEntry.icon.getConstantState().newDrawable(res));
}
// Set application name.
final TextView labelView = (TextView) appSnippet.findViewById(android.R.id.title);
final TextView labelView = (TextView) mAppHeader.findViewById(android.R.id.title);
labelView.setText(appEntry.label);
// Version number of application
final TextView appVersion = (TextView) appSnippet.findViewById(android.R.id.summary);
final TextView appVersion = (TextView) mAppHeader.findViewById(android.R.id.summary);
if (!TextUtils.isEmpty(versionName)) {
appVersion.setSelected(true);
@@ -86,60 +175,97 @@ public class AppHeaderController {
}
}
public void bindAppHeaderButtons(Fragment fragment, View appLinkButtons, String packageName,
@ActionType int leftAction, @ActionType int rightAction) {
ImageButton leftButton = (ImageButton) appLinkButtons.findViewById(R.id.left_button);
ImageButton rightButton = (ImageButton) appLinkButtons.findViewById(R.id.right_button);
bindAppDetailButton(fragment, packageName, leftButton, leftAction);
bindAppDetailButton(fragment, packageName, rightButton, rightAction);
/**
* Done mutating appheader, rebinds everything.
*/
public View done() {
return done(true);
}
private void bindAppDetailButton(Fragment fragment, String packageName,
ImageButton button, @ActionType int action) {
/**
* Done mutating appheader, rebinds everything (optionally skip rebinding buttons).
*/
public View done(boolean rebindActions) {
ImageView iconView = (ImageView) mAppHeader.findViewById(android.R.id.icon);
if (iconView != null) {
iconView.setImageDrawable(mIcon);
}
setText(android.R.id.title, mLabel);
setText(android.R.id.summary, mSummary);
if (rebindActions) {
bindAppHeaderButtons();
}
return mAppHeader;
}
/**
* Only binds app header with button actions.
*/
public void bindAppHeaderButtons() {
ImageButton leftButton = (ImageButton) mAppHeader.findViewById(R.id.left_button);
ImageButton rightButton = (ImageButton) mAppHeader.findViewById(R.id.right_button);
bindButton(leftButton, mLeftAction);
bindButton(rightButton, mRightAction);
}
private void bindButton(ImageButton button, @ActionType int action) {
if (button == null) {
return;
}
switch (action) {
case ActionType.ACTION_APP_INFO: {
if (packageName == null || packageName.equals(Utils.OS_PKG)) {
if (mPackageName == null || mPackageName.equals(Utils.OS_PKG)
|| mUid == UserHandle.USER_NULL
|| !AppHeader.includeAppInfo(mFragment)) {
button.setVisibility(View.GONE);
} else {
// TODO
button.setImageResource(com.android.settings.R.drawable.ic_info);
button.setOnClickListener(v -> AppInfoBase.startAppInfoFragment(
InstalledAppDetails.class, R.string.application_info_label,
mPackageName, mUid, mFragment, 0));
button.setVisibility(View.VISIBLE);
}
return;
}
case ActionType.ACTION_STORE_DEEP_LINK: {
final Intent intent = new Intent(Intent.ACTION_SHOW_APP_INFO)
.setPackage(getInstallerPackageName(mContext, packageName));
.setPackage(getInstallerPackageName(mContext, mPackageName));
final Intent result = resolveIntent(intent);
if (result == null) {
button.setVisibility(View.GONE);
} else {
result.putExtra(Intent.EXTRA_PACKAGE_NAME, packageName);
result.putExtra(Intent.EXTRA_PACKAGE_NAME, mPackageName);
button.setImageResource(R.drawable.ic_sim_sd);
button.setOnClickListener(v -> fragment.startActivity(intent));
button.setOnClickListener(v -> mFragment.startActivity(intent));
button.setVisibility(View.VISIBLE);
}
return;
}
case ActionType.ACTION_NOTIF_PREFERENCE: {
// TODO
if (mAppNotifPrefIntent == null) {
button.setVisibility(View.GONE);
} else {
button.setOnClickListener(v -> mFragment.startActivity(mAppNotifPrefIntent));
button.setVisibility(View.VISIBLE);
}
return;
}
case ActionType.ACTION_APP_PREFERENCE: {
final Intent intent = resolveIntent(
new Intent(Intent.ACTION_APPLICATION_PREFERENCES).setPackage(packageName));
new Intent(Intent.ACTION_APPLICATION_PREFERENCES).setPackage(mPackageName));
if (intent == null) {
button.setVisibility(View.GONE);
return;
}
button.setOnClickListener(v -> fragment.startActivity(intent));
button.setOnClickListener(v -> mFragment.startActivity(intent));
button.setVisibility(View.VISIBLE);
return;
}
case ActionType.ACTION_NONE: {
button.setVisibility(View.GONE);
return;
}
}
}
@@ -160,4 +286,12 @@ public class AppHeaderController {
}
return null;
}
}
private void setText(@IdRes int id, CharSequence text) {
TextView textView = (TextView) mAppHeader.findViewById(id);
if (textView != null) {
textView.setText(text);
textView.setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE);
}
}
}