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.manageapplications; 18 19 import android.content.pm.ApplicationInfo; 20 import android.content.pm.PackageManager; 21 import android.graphics.drawable.Drawable; 22 import android.util.Log; 23 import android.view.LayoutInflater; 24 import android.view.View; 25 import android.view.ViewGroup; 26 import android.widget.ImageView; 27 import android.widget.Switch; 28 import android.widget.TextView; 29 30 import androidx.annotation.StringRes; 31 import androidx.annotation.VisibleForTesting; 32 import androidx.recyclerview.widget.RecyclerView; 33 34 import com.android.settings.R; 35 import com.android.settingslib.applications.ApplicationsState; 36 import com.android.settingslib.applications.ApplicationsState.AppEntry; 37 38 public class ApplicationViewHolder extends RecyclerView.ViewHolder { 39 40 private final TextView mAppName; 41 private final ImageView mAppIcon; 42 43 @VisibleForTesting 44 final TextView mSummary; 45 @VisibleForTesting 46 final TextView mDisabled; 47 @VisibleForTesting 48 final ViewGroup mWidgetContainer; 49 @VisibleForTesting 50 final Switch mSwitch; 51 ApplicationViewHolder(View itemView)52 ApplicationViewHolder(View itemView) { 53 super(itemView); 54 mAppName = itemView.findViewById(android.R.id.title); 55 mAppIcon = itemView.findViewById(android.R.id.icon); 56 mSummary = itemView.findViewById(android.R.id.summary); 57 mDisabled = itemView.findViewById(R.id.appendix); 58 mSwitch = itemView.findViewById(R.id.switchWidget); 59 mWidgetContainer = itemView.findViewById(android.R.id.widget_frame); 60 } 61 newView(ViewGroup parent)62 static View newView(ViewGroup parent) { 63 return newView(parent, false /* twoTarget */); 64 } 65 newView(ViewGroup parent, boolean twoTarget)66 static View newView(ViewGroup parent, boolean twoTarget) { 67 ViewGroup view = (ViewGroup) LayoutInflater.from(parent.getContext()) 68 .inflate(R.layout.preference_app, parent, false); 69 final ViewGroup widgetFrame = view.findViewById(android.R.id.widget_frame); 70 if (twoTarget) { 71 if (widgetFrame != null) { 72 LayoutInflater.from(parent.getContext()) 73 .inflate(R.layout.preference_widget_master_switch, widgetFrame, true); 74 75 View divider = LayoutInflater.from(parent.getContext()).inflate( 76 R.layout.preference_two_target_divider, view, false); 77 // second to last, before widget frame 78 view.addView(divider, view.getChildCount() - 1); 79 } 80 } else if (widgetFrame != null) { 81 widgetFrame.setVisibility(View.GONE); 82 } 83 return view; 84 } 85 setSummary(CharSequence summary)86 void setSummary(CharSequence summary) { 87 mSummary.setText(summary); 88 } 89 setSummary(@tringRes int summary)90 void setSummary(@StringRes int summary) { 91 mSummary.setText(summary); 92 } 93 setEnabled(boolean isEnabled)94 void setEnabled(boolean isEnabled) { 95 itemView.setEnabled(isEnabled); 96 } 97 setTitle(CharSequence title)98 void setTitle(CharSequence title) { 99 if (title == null) { 100 return; 101 } 102 mAppName.setText(title); 103 } 104 setIcon(int drawableRes)105 void setIcon(int drawableRes) { 106 mAppIcon.setImageResource(drawableRes); 107 } 108 setIcon(Drawable icon)109 void setIcon(Drawable icon) { 110 if (icon == null) { 111 return; 112 } 113 mAppIcon.setImageDrawable(icon); 114 } 115 updateDisableView(ApplicationInfo info)116 void updateDisableView(ApplicationInfo info) { 117 if ((info.flags & ApplicationInfo.FLAG_INSTALLED) == 0) { 118 mDisabled.setVisibility(View.VISIBLE); 119 mDisabled.setText(R.string.not_installed); 120 } else if (!info.enabled || info.enabledSetting 121 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) { 122 mDisabled.setVisibility(View.VISIBLE); 123 mDisabled.setText(R.string.disabled); 124 } else { 125 mDisabled.setVisibility(View.GONE); 126 } 127 } 128 updateSizeText(AppEntry entry, CharSequence invalidSizeStr, int whichSize)129 void updateSizeText(AppEntry entry, CharSequence invalidSizeStr, int whichSize) { 130 if (ManageApplications.DEBUG) { 131 Log.d(ManageApplications.TAG, "updateSizeText of " 132 + entry.label + " " + entry + ": " + entry.sizeStr); 133 } 134 if (entry.sizeStr != null) { 135 switch (whichSize) { 136 case ManageApplications.SIZE_INTERNAL: 137 setSummary(entry.internalSizeStr); 138 break; 139 case ManageApplications.SIZE_EXTERNAL: 140 setSummary(entry.externalSizeStr); 141 break; 142 default: 143 setSummary(entry.sizeStr); 144 break; 145 } 146 } else if (entry.size == ApplicationsState.SIZE_INVALID) { 147 setSummary(invalidSizeStr); 148 } 149 } 150 updateSwitch(View.OnClickListener listener, boolean enabled, boolean checked)151 void updateSwitch(View.OnClickListener listener, boolean enabled, boolean checked) { 152 if (mSwitch != null && mWidgetContainer != null) { 153 mWidgetContainer.setOnClickListener(listener); 154 mSwitch.setChecked(checked); 155 mSwitch.setEnabled(enabled); 156 } 157 } 158 } 159