CertificateSelector.java revision 994343b14bfba216969a9d9e86b53312686f7832
1/* 2 * Copyright (C) 2011 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 18package com.android.email.view; 19 20import android.content.Context; 21import android.os.Parcel; 22import android.os.Parcelable; 23import android.security.KeyChain; 24import android.util.AttributeSet; 25import android.view.View; 26import android.view.View.OnClickListener; 27import android.widget.Button; 28import android.widget.LinearLayout; 29import android.widget.TextView; 30 31import com.android.email.R; 32import com.android.email.activity.UiUtilities; 33 34/** 35 * A simple view that can be used to select a certificate from the system {@link KeyChain}. 36 * 37 * Host activities must register themselves view {@link #setHostActivity} for this selector to work. 38 */ 39public class CertificateSelector extends LinearLayout implements OnClickListener { 40 41 /** Button to select or remove the certificate. */ 42 private Button mSelectButton; 43 private TextView mAliasText; 44 45 /** The host activity. */ 46 private HostCallback mHost; 47 48 public interface HostCallback { 49 void onCertificateRequested(); 50 } 51 52 public CertificateSelector(Context context) { 53 super(context); 54 } 55 public CertificateSelector(Context context, AttributeSet attrs) { 56 super(context, attrs); 57 } 58 public CertificateSelector(Context context, AttributeSet attrs, int defStyle) { 59 super(context, attrs, defStyle); 60 } 61 62 public void setHostActivity(HostCallback host) { 63 mHost = host; 64 } 65 66 public void setDelegate(String uri) { 67 } 68 69 @Override 70 protected void onFinishInflate() { 71 super.onFinishInflate(); 72 73 mAliasText = UiUtilities.getView(this, R.id.certificate_alias); 74 mSelectButton = UiUtilities.getView(this, R.id.select_button); 75 mSelectButton.setOnClickListener(this); 76 setCertificate(null); 77 } 78 79 public void setCertificate(String alias) { 80 mAliasText.setText(alias); 81 mAliasText.setVisibility((alias == null) ? View.GONE : View.VISIBLE); 82 mSelectButton.setText(getResources().getString( 83 (alias == null) 84 ? R.string.account_setup_exchange_use_certificate 85 : R.string.account_setup_exchange_remove_certificate)); 86 } 87 88 public boolean hasCertificate() { 89 return mAliasText.getVisibility() == View.VISIBLE; 90 } 91 92 /** 93 * Gets the alias for the currently selected certificate, or null if one is not selected. 94 */ 95 public String getCertificate() { 96 return hasCertificate() ? mAliasText.getText().toString() : null; 97 } 98 99 100 @Override 101 public void onClick(View target) { 102 if (target == mSelectButton && mHost != null) { 103 if (hasCertificate()) { 104 // Handle the click on the button when it says "Remove" 105 setCertificate(null); 106 } else { 107 mHost.onCertificateRequested(); 108 } 109 } 110 } 111 112 @Override 113 protected void onRestoreInstanceState(Parcelable parcel) { 114 SavedState savedState = (SavedState) parcel; 115 super.onRestoreInstanceState(savedState.getSuperState()); 116 setCertificate(savedState.mValue); 117 } 118 119 @Override 120 protected Parcelable onSaveInstanceState() { 121 return new SavedState(super.onSaveInstanceState(), getCertificate()); 122 } 123 124 public static class SavedState extends BaseSavedState { 125 final String mValue; 126 127 SavedState(Parcelable superState, String value) { 128 super(superState); 129 mValue = value; 130 } 131 132 @Override 133 public void writeToParcel(Parcel out, int flags) { 134 super.writeToParcel(out, flags); 135 out.writeString(mValue); 136 } 137 138 @SuppressWarnings("hiding") 139 public static final Parcelable.Creator<SavedState> CREATOR 140 = new Parcelable.Creator<SavedState>() { 141 public SavedState createFromParcel(Parcel in) { 142 return new SavedState(in); 143 } 144 145 public SavedState[] newArray(int size) { 146 return new SavedState[size]; 147 } 148 }; 149 150 private SavedState(Parcel in) { 151 super(in); 152 mValue = in.readString(); 153 } 154 } 155} 156