1 /* 2 * Copyright (C) 2014 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.dashboard; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.content.pm.PackageManager.NameNotFoundException; 24 import android.content.res.Resources; 25 import android.graphics.drawable.Drawable; 26 import android.os.Bundle; 27 import android.os.Handler; 28 import android.os.Message; 29 import android.text.TextUtils; 30 import android.util.Log; 31 import android.util.TypedValue; 32 import android.view.LayoutInflater; 33 import android.view.Menu; 34 import android.view.MenuInflater; 35 import android.view.View; 36 import android.view.ViewGroup; 37 import android.widget.ImageView; 38 import android.widget.TextView; 39 40 import com.android.internal.logging.MetricsLogger; 41 import com.android.settings.HelpUtils; 42 import com.android.settings.InstrumentedFragment; 43 import com.android.settings.R; 44 import com.android.settings.SettingsActivity; 45 46 import java.util.List; 47 48 public class DashboardSummary extends InstrumentedFragment { 49 private static final String LOG_TAG = "DashboardSummary"; 50 51 private LayoutInflater mLayoutInflater; 52 private ViewGroup mDashboard; 53 54 private static final int MSG_REBUILD_UI = 1; 55 private Handler mHandler = new Handler() { 56 @Override 57 public void handleMessage(Message msg) { 58 switch (msg.what) { 59 case MSG_REBUILD_UI: { 60 final Context context = getActivity(); 61 rebuildUI(context); 62 } break; 63 } 64 } 65 }; 66 67 private class HomePackageReceiver extends BroadcastReceiver { 68 @Override 69 public void onReceive(Context context, Intent intent) { 70 rebuildUI(context); 71 } 72 } 73 private HomePackageReceiver mHomePackageReceiver = new HomePackageReceiver(); 74 75 @Override 76 protected int getMetricsCategory() { 77 return MetricsLogger.DASHBOARD_SUMMARY; 78 } 79 80 @Override 81 public void onCreate(Bundle savedInstanceState) { 82 super.onCreate(savedInstanceState); 83 84 setHasOptionsMenu(true); 85 } 86 87 @Override 88 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 89 super.onCreateOptionsMenu(menu, inflater); 90 HelpUtils.prepareHelpMenuItem(getActivity(), menu, R.string.help_uri_dashboard, 91 getClass().getName()); 92 } 93 94 @Override 95 public void onResume() { 96 super.onResume(); 97 98 sendRebuildUI(); 99 100 final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); 101 filter.addAction(Intent.ACTION_PACKAGE_REMOVED); 102 filter.addAction(Intent.ACTION_PACKAGE_CHANGED); 103 filter.addAction(Intent.ACTION_PACKAGE_REPLACED); 104 filter.addDataScheme("package"); 105 getActivity().registerReceiver(mHomePackageReceiver, filter); 106 } 107 108 @Override 109 public void onPause() { 110 super.onPause(); 111 112 getActivity().unregisterReceiver(mHomePackageReceiver); 113 } 114 115 @Override 116 public View onCreateView(LayoutInflater inflater, ViewGroup container, 117 Bundle savedInstanceState) { 118 119 mLayoutInflater = inflater; 120 121 final View rootView = inflater.inflate(R.layout.dashboard, container, false); 122 mDashboard = (ViewGroup) rootView.findViewById(R.id.dashboard_container); 123 124 return rootView; 125 } 126 127 private void rebuildUI(Context context) { 128 if (!isAdded()) { 129 Log.w(LOG_TAG, "Cannot build the DashboardSummary UI yet as the Fragment is not added"); 130 return; 131 } 132 133 long start = System.currentTimeMillis(); 134 final Resources res = getResources(); 135 136 mDashboard.removeAllViews(); 137 138 List<DashboardCategory> categories = 139 ((SettingsActivity) context).getDashboardCategories(true); 140 141 final int count = categories.size(); 142 143 for (int n = 0; n < count; n++) { 144 DashboardCategory category = categories.get(n); 145 146 View categoryView = mLayoutInflater.inflate(R.layout.dashboard_category, mDashboard, 147 false); 148 149 TextView categoryLabel = (TextView) categoryView.findViewById(R.id.category_title); 150 categoryLabel.setText(category.getTitle(res)); 151 152 ViewGroup categoryContent = 153 (ViewGroup) categoryView.findViewById(R.id.category_content); 154 155 final int tilesCount = category.getTilesCount(); 156 for (int i = 0; i < tilesCount; i++) { 157 DashboardTile tile = category.getTile(i); 158 159 DashboardTileView tileView = new DashboardTileView(context); 160 updateTileView(context, res, tile, tileView.getImageView(), 161 tileView.getTitleTextView(), tileView.getStatusTextView()); 162 163 tileView.setTile(tile); 164 165 categoryContent.addView(tileView); 166 } 167 168 // Add the category 169 mDashboard.addView(categoryView); 170 } 171 long delta = System.currentTimeMillis() - start; 172 Log.d(LOG_TAG, "rebuildUI took: " + delta + " ms"); 173 } 174 175 private void updateTileView(Context context, Resources res, DashboardTile tile, 176 ImageView tileIcon, TextView tileTextView, TextView statusTextView) { 177 178 if (!TextUtils.isEmpty(tile.iconPkg)) { 179 try { 180 Drawable drawable = context.getPackageManager() 181 .getResourcesForApplication(tile.iconPkg).getDrawable(tile.iconRes, null); 182 if (!tile.iconPkg.equals(context.getPackageName()) && drawable != null) { 183 // If this drawable is coming from outside Settings, tint it to match the color. 184 TypedValue tintColor = new TypedValue(); 185 context.getTheme().resolveAttribute(com.android.internal.R.attr.colorAccent, 186 tintColor, true); 187 drawable.setTint(tintColor.data); 188 } 189 tileIcon.setImageDrawable(drawable); 190 } catch (NameNotFoundException | Resources.NotFoundException e) { 191 tileIcon.setImageDrawable(null); 192 tileIcon.setBackground(null); 193 } 194 } else if (tile.iconRes > 0) { 195 tileIcon.setImageResource(tile.iconRes); 196 } else { 197 tileIcon.setImageDrawable(null); 198 tileIcon.setBackground(null); 199 } 200 201 tileTextView.setText(tile.getTitle(res)); 202 203 CharSequence summary = tile.getSummary(res); 204 if (!TextUtils.isEmpty(summary)) { 205 statusTextView.setVisibility(View.VISIBLE); 206 statusTextView.setText(summary); 207 } else { 208 statusTextView.setVisibility(View.GONE); 209 } 210 } 211 212 private void sendRebuildUI() { 213 if (!mHandler.hasMessages(MSG_REBUILD_UI)) { 214 mHandler.sendEmptyMessage(MSG_REBUILD_UI); 215 } 216 } 217 } 218