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.Activity;
20import android.content.Context;
21import android.util.AttributeSet;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.widget.FrameLayout;
25import android.widget.ImageView;
26import android.widget.TextView;
27
28import com.android.settings.ProfileSelectDialog;
29import com.android.settings.R;
30import com.android.settings.Utils;
31
32public class DashboardTileView extends FrameLayout implements View.OnClickListener {
33
34    private static final int DEFAULT_COL_SPAN = 1;
35
36    private ImageView mImageView;
37    private TextView mTitleTextView;
38    private TextView mStatusTextView;
39    private View mDivider;
40
41    private int mColSpan = DEFAULT_COL_SPAN;
42
43    private DashboardTile mTile;
44
45    public DashboardTileView(Context context) {
46        this(context, null);
47    }
48
49    public DashboardTileView(Context context, AttributeSet attrs) {
50        super(context, attrs);
51
52        final View view = LayoutInflater.from(context).inflate(R.layout.dashboard_tile, this);
53
54        mImageView = (ImageView) view.findViewById(R.id.icon);
55        mTitleTextView = (TextView) view.findViewById(R.id.title);
56        mStatusTextView = (TextView) view.findViewById(R.id.status);
57        mDivider = view.findViewById(R.id.tile_divider);
58
59        setOnClickListener(this);
60        setBackgroundResource(R.drawable.dashboard_tile_background);
61        setFocusable(true);
62    }
63
64    public TextView getTitleTextView() {
65        return mTitleTextView;
66    }
67
68    public TextView getStatusTextView() {
69        return mStatusTextView;
70    }
71
72    public ImageView getImageView() {
73        return mImageView;
74    }
75
76    public void setTile(DashboardTile tile) {
77        mTile = tile;
78    }
79
80    public void setDividerVisibility(boolean visible) {
81        mDivider.setVisibility(visible ? View.VISIBLE : View.GONE);
82    }
83
84    void setColumnSpan(int span) {
85        mColSpan = span;
86    }
87
88    int getColumnSpan() {
89        return mColSpan;
90    }
91
92    @Override
93    public void onClick(View v) {
94        if (mTile.fragment != null) {
95            Utils.startWithFragment(getContext(), mTile.fragment, mTile.fragmentArguments, null, 0,
96                    mTile.titleRes, mTile.getTitle(getResources()));
97        } else if (mTile.intent != null) {
98            int numUserHandles = mTile.userHandle.size();
99            if (numUserHandles > 1) {
100                ProfileSelectDialog.show(((Activity) getContext()).getFragmentManager(), mTile);
101            } else if (numUserHandles == 1) {
102                getContext().startActivityAsUser(mTile.intent, mTile.userHandle.get(0));
103            } else {
104                getContext().startActivity(mTile.intent);
105            }
106        }
107    }
108}
109