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