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
17package com.android.settings.dashboard;
18
19import android.app.Fragment;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.content.res.Resources;
25import android.os.Bundle;
26import android.os.Handler;
27import android.os.Message;
28import android.text.TextUtils;
29import android.util.Log;
30import android.view.LayoutInflater;
31import android.view.View;
32import android.view.ViewGroup;
33import android.widget.ImageView;
34import android.widget.TextView;
35import com.android.settings.R;
36import com.android.settings.SettingsActivity;
37
38import java.util.List;
39
40public class DashboardSummary extends Fragment {
41    private static final String LOG_TAG = "DashboardSummary";
42
43    private LayoutInflater mLayoutInflater;
44    private ViewGroup mDashboard;
45
46    private static final int MSG_REBUILD_UI = 1;
47    private Handler mHandler = new Handler() {
48        @Override
49        public void handleMessage(Message msg) {
50            switch (msg.what) {
51                case MSG_REBUILD_UI: {
52                    final Context context = getActivity();
53                    rebuildUI(context);
54                } break;
55            }
56        }
57    };
58
59    private class HomePackageReceiver extends BroadcastReceiver {
60        @Override
61        public void onReceive(Context context, Intent intent) {
62            rebuildUI(context);
63        }
64    }
65    private HomePackageReceiver mHomePackageReceiver = new HomePackageReceiver();
66
67    @Override
68    public void onResume() {
69        super.onResume();
70
71        sendRebuildUI();
72
73        final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
74        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
75        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
76        filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
77        filter.addDataScheme("package");
78        getActivity().registerReceiver(mHomePackageReceiver, filter);
79    }
80
81    @Override
82    public void onPause() {
83        super.onPause();
84
85        getActivity().unregisterReceiver(mHomePackageReceiver);
86    }
87
88    @Override
89    public View onCreateView(LayoutInflater inflater, ViewGroup container,
90                             Bundle savedInstanceState) {
91
92        mLayoutInflater = inflater;
93
94        final View rootView = inflater.inflate(R.layout.dashboard, container, false);
95        mDashboard = (ViewGroup) rootView.findViewById(R.id.dashboard_container);
96
97        return rootView;
98    }
99
100    private void rebuildUI(Context context) {
101        if (!isAdded()) {
102            Log.w(LOG_TAG, "Cannot build the DashboardSummary UI yet as the Fragment is not added");
103            return;
104        }
105
106        long start = System.currentTimeMillis();
107        final Resources res = getResources();
108
109        mDashboard.removeAllViews();
110
111        List<DashboardCategory> categories =
112                ((SettingsActivity) context).getDashboardCategories(true);
113
114        final int count = categories.size();
115
116        for (int n = 0; n < count; n++) {
117            DashboardCategory category = categories.get(n);
118
119            View categoryView = mLayoutInflater.inflate(R.layout.dashboard_category, mDashboard,
120                    false);
121
122            TextView categoryLabel = (TextView) categoryView.findViewById(R.id.category_title);
123            categoryLabel.setText(category.getTitle(res));
124
125            ViewGroup categoryContent =
126                    (ViewGroup) categoryView.findViewById(R.id.category_content);
127
128            final int tilesCount = category.getTilesCount();
129            for (int i = 0; i < tilesCount; i++) {
130                DashboardTile tile = category.getTile(i);
131
132                DashboardTileView tileView = new DashboardTileView(context);
133                updateTileView(context, res, tile, tileView.getImageView(),
134                        tileView.getTitleTextView(), tileView.getStatusTextView());
135
136                tileView.setTile(tile);
137
138                categoryContent.addView(tileView);
139            }
140
141            // Add the category
142            mDashboard.addView(categoryView);
143        }
144        long delta = System.currentTimeMillis() - start;
145        Log.d(LOG_TAG, "rebuildUI took: " + delta + " ms");
146    }
147
148    private void updateTileView(Context context, Resources res, DashboardTile tile,
149            ImageView tileIcon, TextView tileTextView, TextView statusTextView) {
150
151        if (tile.iconRes > 0) {
152            tileIcon.setImageResource(tile.iconRes);
153        } else {
154            tileIcon.setImageDrawable(null);
155            tileIcon.setBackground(null);
156        }
157
158        tileTextView.setText(tile.getTitle(res));
159
160        CharSequence summary = tile.getSummary(res);
161        if (!TextUtils.isEmpty(summary)) {
162            statusTextView.setVisibility(View.VISIBLE);
163            statusTextView.setText(summary);
164        } else {
165            statusTextView.setVisibility(View.GONE);
166        }
167    }
168
169    private void sendRebuildUI() {
170        if (!mHandler.hasMessages(MSG_REBUILD_UI)) {
171            mHandler.sendEmptyMessage(MSG_REBUILD_UI);
172        }
173    }
174}
175