summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/dashboard/DashboardSummary.java
blob: 73dcd17804511224c367b7874bcb98b9b601aaa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.dashboard;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.OnAccountsUpdateListener;
import android.app.Fragment;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.accounts.AuthenticatorHelper;
import com.android.settings.accounts.ManageAccountsSettings;

import java.util.List;

public class DashboardSummary extends Fragment implements OnAccountsUpdateListener {
    private static final String LOG_TAG = "DashboardSummary";

    private LayoutInflater mLayoutInflater;
    private ViewGroup mContainer;
    private ViewGroup mDashboard;
    private AuthenticatorHelper mAuthHelper;
    private boolean mAccountListenerAdded;

    private static final int MSG_BUILD_CATEGORIES = 1;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_BUILD_CATEGORIES: {
                    final Context context = getActivity();
                    rebuildUI(context);
                } break;
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        final Context context = getActivity();

        mLayoutInflater = inflater;
        mContainer = container;

        final View rootView = inflater.inflate(R.layout.dashboard, container, false);
        mDashboard = (ViewGroup) rootView.findViewById(R.id.dashboard_container);

        mAuthHelper = ((SettingsActivity) context).getAuthenticatorHelper();

        return rootView;
    }

    private void rebuildUI(Context context) {
        long start = System.currentTimeMillis();
        final Resources res = getResources();

        mDashboard.removeAllViews();

        List<DashboardCategory> categories =
                ((SettingsActivity) context).getDashboardCategories();

        final int count = categories.size();

        for (int n = 0; n < count; n++) {
            DashboardCategory category = categories.get(n);

            View categoryView = mLayoutInflater.inflate(R.layout.dashboard_category, mContainer,
                    false);

            TextView categoryLabel = (TextView) categoryView.findViewById(R.id.category_title);
            categoryLabel.setText(category.getTitle(res));

            ViewGroup categoryContent =
                    (ViewGroup) categoryView.findViewById(R.id.category_content);

            final int tilesCount = category.getTilesCount();
            for (int i = 0; i < tilesCount; i++) {
                DashboardTile tile = category.getTile(i);

                DashboardTileView tileView = new DashboardTileView(context);
                updateTileView(context, res, tile, tileView.getImageView(),
                        tileView.getTitleTextView(), tileView.getStatusTextView());

                tileView.setTile(tile);

                categoryContent.addView(tileView);
            }

            // Add the category
            mDashboard.addView(categoryView);
        }
        long delta = System.currentTimeMillis() - start;
        Log.d(LOG_TAG, "rebuildUI took: " + delta + " ms");
    }

    @Override
    public void onResume() {
        super.onResume();

        if (!mAccountListenerAdded) {
            AccountManager.get(getActivity()).addOnAccountsUpdatedListener(this, null, false);
            mAccountListenerAdded = true;
        }

        rebuildCategories();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (mAccountListenerAdded) {
            AccountManager.get(getActivity()).removeOnAccountsUpdatedListener(this);
            mAccountListenerAdded = false;
        }
    }

    private void updateTileView(Context context, Resources res, DashboardTile tile,
            ImageView tileIcon, TextView tileTextView, TextView statusTextView) {

        if (tile.extras != null
                && tile.extras.containsKey(ManageAccountsSettings.KEY_ACCOUNT_TYPE)) {
            String accType = tile.extras.getString(ManageAccountsSettings.KEY_ACCOUNT_TYPE);
            Drawable drawable = mAuthHelper.getDrawableForType(context, accType);
            tileIcon.setImageDrawable(drawable);
        } else {
            if (tile.iconRes > 0) {
                tileIcon.setImageResource(tile.iconRes);
            } else {
                tileIcon.setImageDrawable(null);
            }
        }
        if (tileIcon != null) {
            if (tile.iconRes > 0) {
            } else {
                tileIcon.setBackground(null);
            }
        }
        tileTextView.setText(tile.getTitle(res));

        CharSequence summary = tile.getSummary(res);
        if (!TextUtils.isEmpty(summary)) {
            statusTextView.setVisibility(View.VISIBLE);
            statusTextView.setText(summary);
        } else {
            statusTextView.setVisibility(View.GONE);
        }
    }

    private void rebuildCategories() {
        if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
            mHandler.sendEmptyMessage(MSG_BUILD_CATEGORIES);
        }
    }

    @Override
    public void onAccountsUpdated(Account[] accounts) {
        final SettingsActivity sa = (SettingsActivity) getActivity();
        sa.setNeedToRebuildCategories(true);
        rebuildCategories();
    }
}