OSDN Git Service

Merge tag 'android-6.0.1_r74' into HEAD
[android-x86/packages-apps-Settings.git] / src / com / android / settings / dashboard / DashboardTileView.java
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
17 package com.android.settings.dashboard;
18
19 import android.app.Activity;
20 import android.content.Context;
21 import android.util.AttributeSet;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.widget.FrameLayout;
25 import android.widget.ImageView;
26 import android.widget.Switch;
27 import android.widget.TextView;
28
29 import com.android.settings.ProfileSelectDialog;
30 import com.android.settings.R;
31 import com.android.settings.Utils;
32
33 import java.lang.reflect.Constructor;
34 import java.lang.reflect.InvocationTargetException;
35
36 public class DashboardTileView extends FrameLayout implements View.OnClickListener {
37
38     private static final int DEFAULT_COL_SPAN = 1;
39
40     private ImageView mImageView;
41     private TextView mTitleTextView;
42     private TextView mStatusTextView;
43     private View mDivider;
44     private Switch mSwitch;
45     private GenericSwitchToggle mSwitchToggle;
46
47     private int mColSpan = DEFAULT_COL_SPAN;
48
49     private DashboardTile mTile;
50
51     public DashboardTileView(Context context) {
52         this(context, null);
53     }
54
55     public DashboardTileView(Context context, AttributeSet attrs) {
56         super(context, attrs);
57
58         final View view = LayoutInflater.from(context).inflate(R.layout.dashboard_tile, this);
59
60         mImageView = (ImageView) view.findViewById(R.id.icon);
61         mTitleTextView = (TextView) view.findViewById(R.id.title);
62         mStatusTextView = (TextView) view.findViewById(R.id.status);
63         mDivider = view.findViewById(R.id.tile_divider);
64         mSwitch = (Switch) view.findViewById(R.id.dashboard_switch);
65
66         setOnClickListener(this);
67         setBackgroundResource(R.drawable.dashboard_tile_background);
68         setFocusable(true);
69     }
70
71     public TextView getTitleTextView() {
72         return mTitleTextView;
73     }
74
75     public TextView getStatusTextView() {
76         return mStatusTextView;
77     }
78
79     public ImageView getImageView() {
80         return mImageView;
81     }
82
83     @Override
84     protected void onAttachedToWindow() {
85         super.onAttachedToWindow();
86         if (mSwitchToggle != null) {
87             mSwitchToggle.resume(getContext());
88         }
89     }
90
91     @Override
92     protected void onDetachedFromWindow() {
93         super.onDetachedFromWindow();
94         if (mSwitchToggle != null) {
95             mSwitchToggle.pause();
96         }
97     }
98
99     public void setTile(DashboardTile tile) {
100         mTile = tile;
101
102         if (mTile.switchControl != null) {
103             try {
104                 Class<?> clazz = getClass().getClassLoader().loadClass(mTile.switchControl);
105                 Constructor<?> constructor = clazz.getConstructor(Context.class, Switch.class);
106                 GenericSwitchToggle sw = (GenericSwitchToggle) constructor.newInstance(
107                         getContext(), mSwitch);
108                 mSwitchToggle = sw;
109                 mSwitchToggle.resume(getContext());
110             } catch (ClassNotFoundException
111                     | NoSuchMethodException
112                     | InvocationTargetException
113                     | InstantiationException
114                     | IllegalAccessException e) {
115                 e.printStackTrace();
116             }
117         }
118     }
119
120     public void setDividerVisibility(boolean visible) {
121         mDivider.setVisibility(visible ? View.VISIBLE : View.GONE);
122     }
123
124     void setColumnSpan(int span) {
125         mColSpan = span;
126     }
127
128     int getColumnSpan() {
129         return mColSpan;
130     }
131
132     @Override
133     public void onClick(View v) {
134         if (mTile.fragment != null) {
135             Utils.startWithFragment(getContext(), mTile.fragment, mTile.fragmentArguments, null, 0,
136                     mTile.titleRes, mTile.getTitle(getResources()));
137         } else if (mTile.intent != null) {
138             int numUserHandles = mTile.userHandle.size();
139             if (numUserHandles > 1) {
140                 ProfileSelectDialog.show(((Activity) getContext()).getFragmentManager(), mTile);
141             } else if (numUserHandles == 1) {
142                 getContext().startActivityAsUser(mTile.intent, mTile.userHandle.get(0));
143             } else {
144                 getContext().startActivity(mTile.intent);
145             }
146         }
147     }
148
149     public Switch getSwitchView() {
150         return mSwitch;
151     }
152
153     public void setEnabledTile(boolean enabled) {
154         mImageView.setAlpha(enabled ? 1f : Utils.DISABLED_ALPHA);
155         mTitleTextView.setEnabled(enabled);
156         mStatusTextView.setEnabled(enabled);
157         mSwitch.setEnabled(enabled);
158         mSwitch.setClickable(enabled);
159         setFocusable(enabled);
160         setEnabled(enabled);
161     }
162 }