2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.dashboard;
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;
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;
46 import java.util.List;
48 public class DashboardSummary extends InstrumentedFragment {
49 private static final String LOG_TAG = "DashboardSummary";
51 private LayoutInflater mLayoutInflater;
52 private ViewGroup mDashboard;
54 private static final int MSG_REBUILD_UI = 1;
55 private Handler mHandler = new Handler() {
57 public void handleMessage(Message msg) {
59 case MSG_REBUILD_UI: {
60 final Context context = getActivity();
67 private class HomePackageReceiver extends BroadcastReceiver {
69 public void onReceive(Context context, Intent intent) {
73 private HomePackageReceiver mHomePackageReceiver = new HomePackageReceiver();
76 protected int getMetricsCategory() {
77 return MetricsLogger.DASHBOARD_SUMMARY;
81 public void onCreate(Bundle savedInstanceState) {
82 super.onCreate(savedInstanceState);
84 setHasOptionsMenu(true);
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());
95 public void onResume() {
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);
109 public void onPause() {
112 getActivity().unregisterReceiver(mHomePackageReceiver);
116 public View onCreateView(LayoutInflater inflater, ViewGroup container,
117 Bundle savedInstanceState) {
119 mLayoutInflater = inflater;
121 final View rootView = inflater.inflate(R.layout.dashboard, container, false);
122 mDashboard = (ViewGroup) rootView.findViewById(R.id.dashboard_container);
127 private void rebuildUI(Context context) {
129 Log.w(LOG_TAG, "Cannot build the DashboardSummary UI yet as the Fragment is not added");
133 long start = System.currentTimeMillis();
134 final Resources res = getResources();
136 mDashboard.removeAllViews();
138 List<DashboardCategory> categories =
139 ((SettingsActivity) context).getDashboardCategories(true);
141 final int count = categories.size();
143 for (int n = 0; n < count; n++) {
144 DashboardCategory category = categories.get(n);
146 View categoryView = mLayoutInflater.inflate(R.layout.dashboard_category, mDashboard,
149 TextView categoryLabel = (TextView) categoryView.findViewById(R.id.category_title);
150 categoryLabel.setText(category.getTitle(res));
152 ViewGroup categoryContent =
153 (ViewGroup) categoryView.findViewById(R.id.category_content);
155 final int tilesCount = category.getTilesCount();
156 for (int i = 0; i < tilesCount; i++) {
157 DashboardTile tile = category.getTile(i);
159 DashboardTileView tileView = new DashboardTileView(context);
160 updateTileView(context, res, tile, tileView.getImageView(),
161 tileView.getTitleTextView(), tileView.getStatusTextView());
163 tileView.setTile(tile);
165 categoryContent.addView(tileView);
169 mDashboard.addView(categoryView);
171 long delta = System.currentTimeMillis() - start;
172 Log.d(LOG_TAG, "rebuildUI took: " + delta + " ms");
175 private void updateTileView(Context context, Resources res, DashboardTile tile,
176 ImageView tileIcon, TextView tileTextView, TextView statusTextView) {
178 if (!TextUtils.isEmpty(tile.iconPkg)) {
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,
187 drawable.setTint(tintColor.data);
189 tileIcon.setImageDrawable(drawable);
190 } catch (NameNotFoundException | Resources.NotFoundException e) {
191 tileIcon.setImageDrawable(null);
192 tileIcon.setBackground(null);
194 } else if (tile.iconRes > 0) {
195 tileIcon.setImageResource(tile.iconRes);
197 tileIcon.setImageDrawable(null);
198 tileIcon.setBackground(null);
201 tileTextView.setText(tile.getTitle(res));
203 CharSequence summary = tile.getSummary(res);
204 if (!TextUtils.isEmpty(summary)) {
205 statusTextView.setVisibility(View.VISIBLE);
206 statusTextView.setText(summary);
208 statusTextView.setVisibility(View.GONE);
212 private void sendRebuildUI() {
213 if (!mHandler.hasMessages(MSG_REBUILD_UI)) {
214 mHandler.sendEmptyMessage(MSG_REBUILD_UI);