blob: 0ab644ab2b8a24d57fd25274027428ee4eaf675b [file] [log] [blame]
Jason Monk3d5f5512014-07-25 11:17:28 -04001/*
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 */
16package com.android.systemui.qs;
17
18import android.app.AlertDialog;
19import android.content.Context;
20import android.content.DialogInterface;
21import android.os.Handler;
22import android.os.Looper;
23import android.os.Message;
24import android.util.Log;
Jason Monk3d5f5512014-07-25 11:17:28 -040025import android.view.LayoutInflater;
26import android.view.View;
27import android.view.View.OnClickListener;
Jason Monk3d5f5512014-07-25 11:17:28 -040028import android.widget.ImageView;
29import android.widget.TextView;
30
Jorim Jaggie17c4b42014-08-26 17:27:31 +020031import com.android.systemui.FontSizeUtils;
Jason Monk3d5f5512014-07-25 11:17:28 -040032import com.android.systemui.R;
33import com.android.systemui.statusbar.phone.QSTileHost;
34import com.android.systemui.statusbar.phone.SystemUIDialog;
35import com.android.systemui.statusbar.policy.SecurityController;
Jason Monk3d5f5512014-07-25 11:17:28 -040036
37public class QSFooter implements OnClickListener, DialogInterface.OnClickListener {
38 protected static final String TAG = "QSFooter";
39 protected static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
40
41 private final View mRootView;
42 private final TextView mFooterText;
43 private final ImageView mFooterIcon;
44 private final Context mContext;
45 private final Callback mCallback = new Callback();
46
47 private SecurityController mSecurityController;
48 private AlertDialog mDialog;
49 private QSTileHost mHost;
50 private Handler mHandler;
Selim Cinek24ac55e2014-08-27 12:51:45 +020051 private final Handler mMainHandler;
Jason Monk3d5f5512014-07-25 11:17:28 -040052
Jason Monkb1c2f3b2014-08-20 10:17:00 -040053 private boolean mIsVisible;
54 private boolean mIsIconVisible;
55 private int mFooterTextId;
56
Jason Monk3d5f5512014-07-25 11:17:28 -040057 public QSFooter(QSPanel qsPanel, Context context) {
58 mRootView = LayoutInflater.from(context)
59 .inflate(R.layout.quick_settings_footer, qsPanel, false);
60 mRootView.setOnClickListener(this);
61 mFooterText = (TextView) mRootView.findViewById(R.id.footer_text);
62 mFooterIcon = (ImageView) mRootView.findViewById(R.id.footer_icon);
63 mContext = context;
Selim Cinek24ac55e2014-08-27 12:51:45 +020064 mMainHandler = new Handler();
Jason Monk3d5f5512014-07-25 11:17:28 -040065 }
66
67 public void setHost(QSTileHost host) {
68 mHost = host;
69 mSecurityController = host.getSecurityController();
70 mHandler = new H(host.getLooper());
71 }
72
73 public void setListening(boolean listening) {
74 if (listening) {
75 mSecurityController.addCallback(mCallback);
76 } else {
77 mSecurityController.removeCallback(mCallback);
78 }
79 }
80
Jorim Jaggie17c4b42014-08-26 17:27:31 +020081 public void onConfigurationChanged() {
82 FontSizeUtils.updateFontSize(mFooterText, R.dimen.qs_tile_text_size);
83 }
84
Jason Monk3d5f5512014-07-25 11:17:28 -040085 public View getView() {
86 return mRootView;
87 }
88
89 public boolean hasFooter() {
90 return mRootView.getVisibility() != View.GONE;
91 }
92
93 @Override
94 public void onClick(View v) {
95 mHandler.sendEmptyMessage(H.CLICK);
96 }
97
98 private void handleClick() {
99 mHost.collapsePanels();
100 // TODO: Delay dialog creation until after panels are collapsed.
101 createDialog();
102 }
103
104 public void refreshState() {
105 mHandler.sendEmptyMessage(H.REFRESH_STATE);
106 }
107
108 private void handleRefreshState() {
109 if (mSecurityController.hasDeviceOwner()) {
Jason Monkb1c2f3b2014-08-20 10:17:00 -0400110 mFooterTextId = R.string.device_owned_footer;
111 mIsVisible = true;
112 mIsIconVisible = false;
Selim Cinek24ac55e2014-08-27 12:51:45 +0200113 } else if (mSecurityController.hasProfileOwner()) {
114 mFooterTextId = R.string.profile_owned_footer;
115 mIsVisible = true;
116 mIsIconVisible = false;
Jason Monk3d5f5512014-07-25 11:17:28 -0400117 } else if (mSecurityController.isVpnEnabled()) {
Jason Monkb1c2f3b2014-08-20 10:17:00 -0400118 mFooterTextId = R.string.vpn_footer;
119 mIsVisible = true;
120 mIsIconVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -0400121 } else {
Jason Monkb1c2f3b2014-08-20 10:17:00 -0400122 mIsVisible = false;
Selim Cinek24ac55e2014-08-27 12:51:45 +0200123 mIsIconVisible = false;
Jason Monk3d5f5512014-07-25 11:17:28 -0400124 }
Selim Cinek24ac55e2014-08-27 12:51:45 +0200125 mMainHandler.post(mUpdateDisplayState);
Jason Monk3d5f5512014-07-25 11:17:28 -0400126 }
127
128 @Override
129 public void onClick(DialogInterface dialog, int which) {
130 if (which == DialogInterface.BUTTON_NEGATIVE) {
Jeff Davidson05542602014-08-11 14:07:27 -0700131 mSecurityController.disconnectFromVpn();
Jason Monk3d5f5512014-07-25 11:17:28 -0400132 }
133 }
134
135 private void createDialog() {
136 mDialog = new SystemUIDialog(mContext);
137 mDialog.setTitle(getTitle());
138 mDialog.setMessage(getMessage());
139 mDialog.setButton(DialogInterface.BUTTON_POSITIVE, getPositiveButton(), this);
140 if (mSecurityController.isVpnEnabled()) {
141 mDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getNegativeButton(), this);
142 }
143 mDialog.show();
144 }
145
146 private String getNegativeButton() {
147 if (mSecurityController.isLegacyVpn()) {
148 return mContext.getString(R.string.disconnect_vpn);
149 } else {
Jeff Davidson05542602014-08-11 14:07:27 -0700150 return mContext.getString(R.string.disable_vpn);
Jason Monk3d5f5512014-07-25 11:17:28 -0400151 }
152 }
153
154 private String getPositiveButton() {
155 return mContext.getString(R.string.quick_settings_done);
156 }
157
158 private String getMessage() {
159 if (mSecurityController.hasDeviceOwner()) {
Selim Cinek24ac55e2014-08-27 12:51:45 +0200160 if (mSecurityController.hasProfileOwner()) {
161 if (mSecurityController.isVpnEnabled()) {
162 if (mSecurityController.isLegacyVpn()) {
163 return mContext.getString(
164 R.string.monitoring_description_legacy_vpn_device_and_profile_owned,
165 mSecurityController.getDeviceOwnerName(),
166 mSecurityController.getProfileOwnerName(),
167 mSecurityController.getLegacyVpnName());
168 } else {
169 return mContext.getString(
170 R.string.monitoring_description_vpn_device_and_profile_owned,
171 mSecurityController.getDeviceOwnerName(),
172 mSecurityController.getProfileOwnerName(),
173 mSecurityController.getVpnApp());
174 }
175 } else {
176 return mContext.getString(
177 R.string.monitoring_description_device_and_profile_owned,
178 mSecurityController.getDeviceOwnerName(),
179 mSecurityController.getProfileOwnerName());
180 }
181 } else {
182 if (mSecurityController.isVpnEnabled()) {
183 if (mSecurityController.isLegacyVpn()) {
184 return mContext.getString(
185 R.string.monitoring_description_legacy_vpn_device_owned,
186 mSecurityController.getDeviceOwnerName(),
187 mSecurityController.getLegacyVpnName());
188 } else {
189 return mContext.getString(R.string.monitoring_description_vpn_device_owned,
190 mSecurityController.getDeviceOwnerName(),
191 mSecurityController.getVpnApp());
192 }
193 } else {
194 return mContext.getString(R.string.monitoring_description_device_owned,
195 mSecurityController.getDeviceOwnerName());
196 }
197 }
198 } else if (mSecurityController.hasProfileOwner()) {
Jason Monk3d5f5512014-07-25 11:17:28 -0400199 if (mSecurityController.isVpnEnabled()) {
200 if (mSecurityController.isLegacyVpn()) {
201 return mContext.getString(
Selim Cinek24ac55e2014-08-27 12:51:45 +0200202 R.string.monitoring_description_legacy_vpn_profile_owned,
203 mSecurityController.getProfileOwnerName(),
Jason Monk3d5f5512014-07-25 11:17:28 -0400204 mSecurityController.getLegacyVpnName());
205 } else {
Selim Cinek24ac55e2014-08-27 12:51:45 +0200206 return mContext.getString(
207 R.string.monitoring_description_vpn_profile_owned,
208 mSecurityController.getProfileOwnerName(),
Jason Monk3d5f5512014-07-25 11:17:28 -0400209 mSecurityController.getVpnApp());
210 }
211 } else {
Selim Cinek24ac55e2014-08-27 12:51:45 +0200212 return mContext.getString(
213 R.string.monitoring_description_profile_owned,
214 mSecurityController.getProfileOwnerName());
Jason Monk3d5f5512014-07-25 11:17:28 -0400215 }
216 } else {
217 if (mSecurityController.isLegacyVpn()) {
218 return mContext.getString(R.string.monitoring_description_legacy_vpn,
219 mSecurityController.getLegacyVpnName());
220
221 } else {
222 return mContext.getString(R.string.monitoring_description_vpn,
223 mSecurityController.getVpnApp());
224 }
225 }
226 }
227
228 private int getTitle() {
229 if (mSecurityController.hasDeviceOwner()) {
230 return R.string.monitoring_title_device_owned;
231 }
Selim Cinek24ac55e2014-08-27 12:51:45 +0200232 if (mSecurityController.hasProfileOwner()) {
233 return R.string.monitoring_title_profile_owned;
234 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400235 return R.string.monitoring_title;
236 }
237
Jason Monkb1c2f3b2014-08-20 10:17:00 -0400238 private final Runnable mUpdateDisplayState = new Runnable() {
239 @Override
240 public void run() {
241 if (mFooterTextId != 0) {
242 mFooterText.setText(mFooterTextId);
243 }
244 mRootView.setVisibility(mIsVisible ? View.VISIBLE : View.GONE);
245 mFooterIcon.setVisibility(mIsIconVisible ? View.VISIBLE : View.INVISIBLE);
246 }
247 };
248
Selim Cinek24ac55e2014-08-27 12:51:45 +0200249 private class Callback implements SecurityController.SecurityControllerCallback {
Jason Monk3d5f5512014-07-25 11:17:28 -0400250 @Override
Selim Cinek24ac55e2014-08-27 12:51:45 +0200251 public void onStateChanged() {
Jason Monk3d5f5512014-07-25 11:17:28 -0400252 refreshState();
253 }
254 }
255
256 private class H extends Handler {
257 private static final int CLICK = 0;
258 private static final int REFRESH_STATE = 1;
259
260 private H(Looper looper) {
261 super(looper);
262 }
263
264 @Override
265 public void handleMessage(Message msg) {
266 String name = null;
267 try {
268 if (msg.what == REFRESH_STATE) {
269 name = "handleRefreshState";
270 handleRefreshState();
271 } else if (msg.what == CLICK) {
272 name = "handleClick";
273 handleClick();
274 }
275 } catch (Throwable t) {
276 final String error = "Error in " + name;
277 Log.w(TAG, error, t);
278 mHost.warn(error, t);
279 }
280 }
281 }
282
283}