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