Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 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 | package com.android.systemui.qs; |
| 17 | |
| 18 | import android.app.AlertDialog; |
| 19 | import android.content.Context; |
| 20 | import android.content.DialogInterface; |
| 21 | import android.os.Handler; |
| 22 | import android.os.Looper; |
| 23 | import android.os.Message; |
| 24 | import android.util.Log; |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 25 | import android.view.LayoutInflater; |
| 26 | import android.view.View; |
| 27 | import android.view.View.OnClickListener; |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 28 | import android.widget.ImageView; |
| 29 | import android.widget.TextView; |
| 30 | |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 31 | import com.android.systemui.FontSizeUtils; |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 32 | import com.android.systemui.R; |
| 33 | import com.android.systemui.statusbar.phone.QSTileHost; |
| 34 | import com.android.systemui.statusbar.phone.SystemUIDialog; |
| 35 | import com.android.systemui.statusbar.policy.SecurityController; |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 36 | |
| 37 | public 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 Cinek | 24ac55e | 2014-08-27 12:51:45 +0200 | [diff] [blame] | 51 | private final Handler mMainHandler; |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 52 | |
Jason Monk | b1c2f3b | 2014-08-20 10:17:00 -0400 | [diff] [blame] | 53 | private boolean mIsVisible; |
| 54 | private boolean mIsIconVisible; |
| 55 | private int mFooterTextId; |
| 56 | |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 57 | 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 Cinek | 24ac55e | 2014-08-27 12:51:45 +0200 | [diff] [blame] | 64 | mMainHandler = new Handler(); |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 65 | } |
| 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 Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 81 | public void onConfigurationChanged() { |
| 82 | FontSizeUtils.updateFontSize(mFooterText, R.dimen.qs_tile_text_size); |
| 83 | } |
| 84 | |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 85 | 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 Monk | b1c2f3b | 2014-08-20 10:17:00 -0400 | [diff] [blame] | 110 | mFooterTextId = R.string.device_owned_footer; |
| 111 | mIsVisible = true; |
| 112 | mIsIconVisible = false; |
Selim Cinek | 24ac55e | 2014-08-27 12:51:45 +0200 | [diff] [blame] | 113 | } else if (mSecurityController.hasProfileOwner()) { |
| 114 | mFooterTextId = R.string.profile_owned_footer; |
| 115 | mIsVisible = true; |
| 116 | mIsIconVisible = false; |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 117 | } else if (mSecurityController.isVpnEnabled()) { |
Jason Monk | b1c2f3b | 2014-08-20 10:17:00 -0400 | [diff] [blame] | 118 | mFooterTextId = R.string.vpn_footer; |
| 119 | mIsVisible = true; |
| 120 | mIsIconVisible = true; |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 121 | } else { |
Jason Monk | b1c2f3b | 2014-08-20 10:17:00 -0400 | [diff] [blame] | 122 | mIsVisible = false; |
Selim Cinek | 24ac55e | 2014-08-27 12:51:45 +0200 | [diff] [blame] | 123 | mIsIconVisible = false; |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 124 | } |
Selim Cinek | 24ac55e | 2014-08-27 12:51:45 +0200 | [diff] [blame] | 125 | mMainHandler.post(mUpdateDisplayState); |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void onClick(DialogInterface dialog, int which) { |
| 130 | if (which == DialogInterface.BUTTON_NEGATIVE) { |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 131 | mSecurityController.disconnectFromVpn(); |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 132 | } |
| 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 Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 150 | return mContext.getString(R.string.disable_vpn); |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 151 | } |
| 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 Cinek | 24ac55e | 2014-08-27 12:51:45 +0200 | [diff] [blame] | 160 | 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 Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 199 | if (mSecurityController.isVpnEnabled()) { |
| 200 | if (mSecurityController.isLegacyVpn()) { |
| 201 | return mContext.getString( |
Selim Cinek | 24ac55e | 2014-08-27 12:51:45 +0200 | [diff] [blame] | 202 | R.string.monitoring_description_legacy_vpn_profile_owned, |
| 203 | mSecurityController.getProfileOwnerName(), |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 204 | mSecurityController.getLegacyVpnName()); |
| 205 | } else { |
Selim Cinek | 24ac55e | 2014-08-27 12:51:45 +0200 | [diff] [blame] | 206 | return mContext.getString( |
| 207 | R.string.monitoring_description_vpn_profile_owned, |
| 208 | mSecurityController.getProfileOwnerName(), |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 209 | mSecurityController.getVpnApp()); |
| 210 | } |
| 211 | } else { |
Selim Cinek | 24ac55e | 2014-08-27 12:51:45 +0200 | [diff] [blame] | 212 | return mContext.getString( |
| 213 | R.string.monitoring_description_profile_owned, |
| 214 | mSecurityController.getProfileOwnerName()); |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 215 | } |
| 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 Cinek | 24ac55e | 2014-08-27 12:51:45 +0200 | [diff] [blame] | 232 | if (mSecurityController.hasProfileOwner()) { |
| 233 | return R.string.monitoring_title_profile_owned; |
| 234 | } |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 235 | return R.string.monitoring_title; |
| 236 | } |
| 237 | |
Jason Monk | b1c2f3b | 2014-08-20 10:17:00 -0400 | [diff] [blame] | 238 | 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 Cinek | 24ac55e | 2014-08-27 12:51:45 +0200 | [diff] [blame] | 249 | private class Callback implements SecurityController.SecurityControllerCallback { |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 250 | @Override |
Selim Cinek | 24ac55e | 2014-08-27 12:51:45 +0200 | [diff] [blame] | 251 | public void onStateChanged() { |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 252 | 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 | } |