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; |
| 25 | import android.view.ContextThemeWrapper; |
| 26 | import android.view.LayoutInflater; |
| 27 | import android.view.View; |
| 28 | import android.view.View.OnClickListener; |
| 29 | import android.view.WindowManager; |
| 30 | import android.widget.ImageView; |
| 31 | import android.widget.TextView; |
| 32 | |
| 33 | import com.android.systemui.R; |
| 34 | import com.android.systemui.statusbar.phone.QSTileHost; |
| 35 | import com.android.systemui.statusbar.phone.SystemUIDialog; |
| 36 | import com.android.systemui.statusbar.policy.SecurityController; |
| 37 | import com.android.systemui.statusbar.policy.SecurityController.VpnCallback; |
| 38 | |
| 39 | public class QSFooter implements OnClickListener, DialogInterface.OnClickListener { |
| 40 | protected static final String TAG = "QSFooter"; |
| 41 | protected static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 42 | |
| 43 | private final View mRootView; |
| 44 | private final TextView mFooterText; |
| 45 | private final ImageView mFooterIcon; |
| 46 | private final Context mContext; |
| 47 | private final Callback mCallback = new Callback(); |
| 48 | |
| 49 | private SecurityController mSecurityController; |
| 50 | private AlertDialog mDialog; |
| 51 | private QSTileHost mHost; |
| 52 | private Handler mHandler; |
| 53 | |
Jason Monk | b1c2f3b | 2014-08-20 10:17:00 -0400 | [diff] [blame] | 54 | private boolean mIsVisible; |
| 55 | private boolean mIsIconVisible; |
| 56 | private int mFooterTextId; |
| 57 | |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 58 | public QSFooter(QSPanel qsPanel, Context context) { |
| 59 | mRootView = LayoutInflater.from(context) |
| 60 | .inflate(R.layout.quick_settings_footer, qsPanel, false); |
| 61 | mRootView.setOnClickListener(this); |
| 62 | mFooterText = (TextView) mRootView.findViewById(R.id.footer_text); |
| 63 | mFooterIcon = (ImageView) mRootView.findViewById(R.id.footer_icon); |
| 64 | mContext = context; |
| 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 | |
| 81 | public View getView() { |
| 82 | return mRootView; |
| 83 | } |
| 84 | |
| 85 | public boolean hasFooter() { |
| 86 | return mRootView.getVisibility() != View.GONE; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void onClick(View v) { |
| 91 | mHandler.sendEmptyMessage(H.CLICK); |
| 92 | } |
| 93 | |
| 94 | private void handleClick() { |
| 95 | mHost.collapsePanels(); |
| 96 | // TODO: Delay dialog creation until after panels are collapsed. |
| 97 | createDialog(); |
| 98 | } |
| 99 | |
| 100 | public void refreshState() { |
| 101 | mHandler.sendEmptyMessage(H.REFRESH_STATE); |
| 102 | } |
| 103 | |
| 104 | private void handleRefreshState() { |
| 105 | if (mSecurityController.hasDeviceOwner()) { |
Jason Monk | b1c2f3b | 2014-08-20 10:17:00 -0400 | [diff] [blame] | 106 | mFooterTextId = R.string.device_owned_footer; |
| 107 | mIsVisible = true; |
| 108 | mIsIconVisible = false; |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 109 | } else if (mSecurityController.isVpnEnabled()) { |
Jason Monk | b1c2f3b | 2014-08-20 10:17:00 -0400 | [diff] [blame] | 110 | mFooterTextId = R.string.vpn_footer; |
| 111 | mIsVisible = true; |
| 112 | mIsIconVisible = true; |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 113 | } else { |
Jason Monk | b1c2f3b | 2014-08-20 10:17:00 -0400 | [diff] [blame] | 114 | mIsVisible = false; |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 115 | } |
Jason Monk | b1c2f3b | 2014-08-20 10:17:00 -0400 | [diff] [blame] | 116 | mRootView.post(mUpdateDisplayState); |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void onClick(DialogInterface dialog, int which) { |
| 121 | if (which == DialogInterface.BUTTON_NEGATIVE) { |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame^] | 122 | mSecurityController.disconnectFromVpn(); |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | private void createDialog() { |
| 127 | mDialog = new SystemUIDialog(mContext); |
| 128 | mDialog.setTitle(getTitle()); |
| 129 | mDialog.setMessage(getMessage()); |
| 130 | mDialog.setButton(DialogInterface.BUTTON_POSITIVE, getPositiveButton(), this); |
| 131 | if (mSecurityController.isVpnEnabled()) { |
| 132 | mDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getNegativeButton(), this); |
| 133 | } |
| 134 | mDialog.show(); |
| 135 | } |
| 136 | |
| 137 | private String getNegativeButton() { |
| 138 | if (mSecurityController.isLegacyVpn()) { |
| 139 | return mContext.getString(R.string.disconnect_vpn); |
| 140 | } else { |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame^] | 141 | return mContext.getString(R.string.disable_vpn); |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | private String getPositiveButton() { |
| 146 | return mContext.getString(R.string.quick_settings_done); |
| 147 | } |
| 148 | |
| 149 | private String getMessage() { |
| 150 | if (mSecurityController.hasDeviceOwner()) { |
| 151 | if (mSecurityController.isVpnEnabled()) { |
| 152 | if (mSecurityController.isLegacyVpn()) { |
| 153 | return mContext.getString( |
| 154 | R.string.monitoring_description_legacy_vpn_device_owned, |
| 155 | mSecurityController.getDeviceOwnerName(), |
| 156 | mSecurityController.getLegacyVpnName()); |
| 157 | } else { |
| 158 | return mContext.getString(R.string.monitoring_description_vpn_device_owned, |
| 159 | mSecurityController.getDeviceOwnerName(), |
| 160 | mSecurityController.getVpnApp()); |
| 161 | } |
| 162 | } else { |
| 163 | return mContext.getString(R.string.monitoring_description_device_owned, |
| 164 | mSecurityController.getDeviceOwnerName()); |
| 165 | } |
| 166 | } else { |
| 167 | if (mSecurityController.isLegacyVpn()) { |
| 168 | return mContext.getString(R.string.monitoring_description_legacy_vpn, |
| 169 | mSecurityController.getLegacyVpnName()); |
| 170 | |
| 171 | } else { |
| 172 | return mContext.getString(R.string.monitoring_description_vpn, |
| 173 | mSecurityController.getVpnApp()); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | private int getTitle() { |
| 179 | if (mSecurityController.hasDeviceOwner()) { |
| 180 | return R.string.monitoring_title_device_owned; |
| 181 | } |
| 182 | return R.string.monitoring_title; |
| 183 | } |
| 184 | |
Jason Monk | b1c2f3b | 2014-08-20 10:17:00 -0400 | [diff] [blame] | 185 | private final Runnable mUpdateDisplayState = new Runnable() { |
| 186 | @Override |
| 187 | public void run() { |
| 188 | if (mFooterTextId != 0) { |
| 189 | mFooterText.setText(mFooterTextId); |
| 190 | } |
| 191 | mRootView.setVisibility(mIsVisible ? View.VISIBLE : View.GONE); |
| 192 | mFooterIcon.setVisibility(mIsIconVisible ? View.VISIBLE : View.INVISIBLE); |
| 193 | } |
| 194 | }; |
| 195 | |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 196 | private class Callback implements VpnCallback { |
| 197 | @Override |
| 198 | public void onVpnStateChanged() { |
| 199 | refreshState(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | private class H extends Handler { |
| 204 | private static final int CLICK = 0; |
| 205 | private static final int REFRESH_STATE = 1; |
| 206 | |
| 207 | private H(Looper looper) { |
| 208 | super(looper); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public void handleMessage(Message msg) { |
| 213 | String name = null; |
| 214 | try { |
| 215 | if (msg.what == REFRESH_STATE) { |
| 216 | name = "handleRefreshState"; |
| 217 | handleRefreshState(); |
| 218 | } else if (msg.what == CLICK) { |
| 219 | name = "handleClick"; |
| 220 | handleClick(); |
| 221 | } |
| 222 | } catch (Throwable t) { |
| 223 | final String error = "Error in " + name; |
| 224 | Log.w(TAG, error, t); |
| 225 | mHost.warn(error, t); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | } |