/* Swisscom Safe Connect Copyright (C) 2014 Swisscom This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.swisscom.safeconnect.view; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import com.swisscom.safeconnect.R; /** * Created by vadim on 19.09.14. */ public class ConnectionStatusView extends RelativeLayout { public enum VpnState { CONNECTED, DISCONNECTED, WAITING, UNAVAILABLE } private TextView tvConnStatus; private ImageView imgSwitch; private VpnState state; private PipeProgressBar prBar; public ConnectionStatusView(Context context, AttributeSet attrs) { super(context, attrs); LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); params.setMargins(0, 0, (int) (20*getResources().getDisplayMetrics().density), 0); tvConnStatus = new PipeTextView(context, attrs); tvConnStatus.setLayoutParams(params); if (!isInEditMode()) { tvConnStatus.setTextAppearance(context, R.style.VpnStatusText); } tvConnStatus.setCompoundDrawablePadding(10); params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); params.setMargins((int) (10*getResources().getDisplayMetrics().density), 0, 0, 0); imgSwitch = new ImageView(context); imgSwitch.setImageResource(R.drawable.ic_switch); imgSwitch.setLayoutParams(params); params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); prBar = new PipeProgressBar(context); prBar.setLayoutParams(params); addView(imgSwitch); addView(tvConnStatus); addView(prBar); setConnectionState(VpnState.DISCONNECTED); } public void setConnectionState(VpnState state) { VpnState prev = this.state; this.state = state; prBar.setVisibility(View.GONE); switch (state) { case CONNECTED: tvConnStatus.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_signal_lock, 0, 0, 0); tvConnStatus.setText(R.string.lab_vpn_connected); setBackgroundResource(R.drawable.conn_status_connected); break; case DISCONNECTED: tvConnStatus.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_signal_unlock, 0, 0, 0); tvConnStatus.setText(R.string.lab_vpn_disconnected); setBackgroundResource(R.drawable.conn_status_novpn); break; case WAITING: prBar.setVisibility(View.VISIBLE); prBar.setProgressBar(prev); break; case UNAVAILABLE: tvConnStatus.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_signal_no_signal, 0, 0, 0); tvConnStatus.setText(R.string.lab_vpn_no_network); setBackgroundResource(R.drawable.conn_status_nosignal); break; } } public void setActionVisible(boolean visible) { imgSwitch.setVisibility(visible? View.VISIBLE: View.GONE); } public VpnState getConnectionState() { return state; } }