1263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio/*
2263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio * Copyright (C) 2014 The Android Open Source Project
3263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio *
4263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio * Licensed under the Apache License, Version 2.0 (the "License");
5263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio * you may not use this file except in compliance with the License.
6263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio * You may obtain a copy of the License at
7263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio *
8263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio *      http://www.apache.org/licenses/LICENSE-2.0
9263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio *
10263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio * Unless required by applicable law or agreed to in writing, software
11263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio * distributed under the License is distributed on an "AS IS" BASIS,
12263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio * See the License for the specific language governing permissions and
14263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio * limitations under the License.
15263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio */
16263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
17263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Megliopackage com.android.settings.dashboard;
18263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
19769630c8956fa844545d964166da90cc802fabacFabrice Di Meglioimport android.app.Fragment;
20b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglioimport android.content.BroadcastReceiver;
21769630c8956fa844545d964166da90cc802fabacFabrice Di Meglioimport android.content.Context;
22b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglioimport android.content.Intent;
23b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglioimport android.content.IntentFilter;
24769630c8956fa844545d964166da90cc802fabacFabrice Di Meglioimport android.content.res.Resources;
25263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.os.Bundle;
26769630c8956fa844545d964166da90cc802fabacFabrice Di Meglioimport android.os.Handler;
27769630c8956fa844545d964166da90cc802fabacFabrice Di Meglioimport android.os.Message;
28769630c8956fa844545d964166da90cc802fabacFabrice Di Meglioimport android.text.TextUtils;
29b644f29313c086806d1ba5ad3db8ac36d2bf3c4fFabrice Di Meglioimport android.util.Log;
30263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.view.LayoutInflater;
31263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.view.View;
32263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport android.view.ViewGroup;
33769630c8956fa844545d964166da90cc802fabacFabrice Di Meglioimport android.widget.ImageView;
34769630c8956fa844545d964166da90cc802fabacFabrice Di Meglioimport android.widget.TextView;
35263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglioimport com.android.settings.R;
366f0739a3d9a9eca1591ae6bada1dd4d1d92c0defFabrice Di Meglioimport com.android.settings.SettingsActivity;
37263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
38769630c8956fa844545d964166da90cc802fabacFabrice Di Meglioimport java.util.List;
39769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
40cfc7f9d9600f7b4514ce9608314874bf1023eb21Alexandra Gherghinapublic class DashboardSummary extends Fragment {
41fa7dc240e978b1b77bb5c80a78cd1f8c77648301Fabrice Di Meglio    private static final String LOG_TAG = "DashboardSummary";
42fa7dc240e978b1b77bb5c80a78cd1f8c77648301Fabrice Di Meglio
43769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    private LayoutInflater mLayoutInflater;
44769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    private ViewGroup mDashboard;
45769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
4653d76860a53c1463d182d4f3d28ce8e9f48454f3Fabrice Di Meglio    private static final int MSG_REBUILD_UI = 1;
47769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    private Handler mHandler = new Handler() {
48769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        @Override
49769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        public void handleMessage(Message msg) {
50769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            switch (msg.what) {
5153d76860a53c1463d182d4f3d28ce8e9f48454f3Fabrice Di Meglio                case MSG_REBUILD_UI: {
52769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                    final Context context = getActivity();
53769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                    rebuildUI(context);
54769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                } break;
55769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            }
56769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        }
57769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    };
58769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
59b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio    private class HomePackageReceiver extends BroadcastReceiver {
60b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio        @Override
61b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio        public void onReceive(Context context, Intent intent) {
62b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio            rebuildUI(context);
63b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio        }
64b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio    }
65b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio    private HomePackageReceiver mHomePackageReceiver = new HomePackageReceiver();
66b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio
67b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio    @Override
68b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio    public void onResume() {
69b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio        super.onResume();
70b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio
71b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio        sendRebuildUI();
72b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio
73b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio        final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
74b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
75b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
76b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio        filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
77b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio        filter.addDataScheme("package");
78b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio        getActivity().registerReceiver(mHomePackageReceiver, filter);
79b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio    }
80b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio
81b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio    @Override
82b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio    public void onPause() {
83b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio        super.onPause();
84b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio
85b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio        getActivity().unregisterReceiver(mHomePackageReceiver);
86b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio    }
87b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio
886f0739a3d9a9eca1591ae6bada1dd4d1d92c0defFabrice Di Meglio    @Override
89263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public View onCreateView(LayoutInflater inflater, ViewGroup container,
90263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                             Bundle savedInstanceState) {
91263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
92769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        mLayoutInflater = inflater;
93769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
94769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        final View rootView = inflater.inflate(R.layout.dashboard, container, false);
95769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        mDashboard = (ViewGroup) rootView.findViewById(R.id.dashboard_container);
96769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
97769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        return rootView;
98769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    }
99769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
100769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    private void rebuildUI(Context context) {
10153d76860a53c1463d182d4f3d28ce8e9f48454f3Fabrice Di Meglio        if (!isAdded()) {
10253d76860a53c1463d182d4f3d28ce8e9f48454f3Fabrice Di Meglio            Log.w(LOG_TAG, "Cannot build the DashboardSummary UI yet as the Fragment is not added");
10353d76860a53c1463d182d4f3d28ce8e9f48454f3Fabrice Di Meglio            return;
10453d76860a53c1463d182d4f3d28ce8e9f48454f3Fabrice Di Meglio        }
10553d76860a53c1463d182d4f3d28ce8e9f48454f3Fabrice Di Meglio
106b644f29313c086806d1ba5ad3db8ac36d2bf3c4fFabrice Di Meglio        long start = System.currentTimeMillis();
107769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        final Resources res = getResources();
108769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
109769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        mDashboard.removeAllViews();
110769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
111769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        List<DashboardCategory> categories =
112b0a464fd2cbbe00821d483d28e76a04ac17996eaFabrice Di Meglio                ((SettingsActivity) context).getDashboardCategories(true);
113769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
114769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        final int count = categories.size();
115769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
116769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        for (int n = 0; n < count; n++) {
117769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            DashboardCategory category = categories.get(n);
118769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
11947a25e76409aec1ae29b983779addf39722377f6Fabrice Di Meglio            View categoryView = mLayoutInflater.inflate(R.layout.dashboard_category, mDashboard,
120769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                    false);
121769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
122769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            TextView categoryLabel = (TextView) categoryView.findViewById(R.id.category_title);
123769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            categoryLabel.setText(category.getTitle(res));
124769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
125769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            ViewGroup categoryContent =
126769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                    (ViewGroup) categoryView.findViewById(R.id.category_content);
127263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
128769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            final int tilesCount = category.getTilesCount();
129769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            for (int i = 0; i < tilesCount; i++) {
130769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                DashboardTile tile = category.getTile(i);
131fa7dc240e978b1b77bb5c80a78cd1f8c77648301Fabrice Di Meglio
132769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                DashboardTileView tileView = new DashboardTileView(context);
133769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                updateTileView(context, res, tile, tileView.getImageView(),
134769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                        tileView.getTitleTextView(), tileView.getStatusTextView());
135769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
136769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                tileView.setTile(tile);
137769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
138769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio                categoryContent.addView(tileView);
139769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            }
140769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
141769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            // Add the category
142769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            mDashboard.addView(categoryView);
143769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        }
144b644f29313c086806d1ba5ad3db8ac36d2bf3c4fFabrice Di Meglio        long delta = System.currentTimeMillis() - start;
145b644f29313c086806d1ba5ad3db8ac36d2bf3c4fFabrice Di Meglio        Log.d(LOG_TAG, "rebuildUI took: " + delta + " ms");
146769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    }
147769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
148769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    private void updateTileView(Context context, Resources res, DashboardTile tile,
149769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            ImageView tileIcon, TextView tileTextView, TextView statusTextView) {
150769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
151cfc7f9d9600f7b4514ce9608314874bf1023eb21Alexandra Gherghina        if (tile.iconRes > 0) {
152cfc7f9d9600f7b4514ce9608314874bf1023eb21Alexandra Gherghina            tileIcon.setImageResource(tile.iconRes);
153769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        } else {
154cfc7f9d9600f7b4514ce9608314874bf1023eb21Alexandra Gherghina            tileIcon.setImageDrawable(null);
155cfc7f9d9600f7b4514ce9608314874bf1023eb21Alexandra Gherghina            tileIcon.setBackground(null);
156769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        }
157cfc7f9d9600f7b4514ce9608314874bf1023eb21Alexandra Gherghina
158769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        tileTextView.setText(tile.getTitle(res));
159769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio
160769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        CharSequence summary = tile.getSummary(res);
161769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        if (!TextUtils.isEmpty(summary)) {
162769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            statusTextView.setVisibility(View.VISIBLE);
163769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            statusTextView.setText(summary);
164769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        } else {
165769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio            statusTextView.setVisibility(View.GONE);
166769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        }
167769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio    }
1686f0739a3d9a9eca1591ae6bada1dd4d1d92c0defFabrice Di Meglio
16953d76860a53c1463d182d4f3d28ce8e9f48454f3Fabrice Di Meglio    private void sendRebuildUI() {
17053d76860a53c1463d182d4f3d28ce8e9f48454f3Fabrice Di Meglio        if (!mHandler.hasMessages(MSG_REBUILD_UI)) {
17153d76860a53c1463d182d4f3d28ce8e9f48454f3Fabrice Di Meglio            mHandler.sendEmptyMessage(MSG_REBUILD_UI);
172769630c8956fa844545d964166da90cc802fabacFabrice Di Meglio        }
173263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
174263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio}
175