Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 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 | package com.android.keychain; |
| 18 | |
| 19 | import android.app.ListActivity; |
| 20 | import android.content.Intent; |
| 21 | import android.os.Bundle; |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 22 | import android.os.RemoteException; |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 23 | import android.security.Credentials; |
Brian Carlstrom | f5b50a4 | 2011-06-09 16:05:09 -0700 | [diff] [blame^] | 24 | import android.security.IKeyChainAliasCallback; |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 25 | import android.security.KeyChain; |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 26 | import android.security.KeyStore; |
| 27 | import android.view.View; |
| 28 | import android.widget.AdapterView.OnItemClickListener; |
| 29 | import android.widget.AdapterView; |
| 30 | import android.widget.ArrayAdapter; |
| 31 | import android.widget.ListView; |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 32 | |
| 33 | public class KeyChainActivity extends ListActivity { |
| 34 | |
| 35 | private static final String TAG = "KeyChainActivity"; |
| 36 | |
| 37 | private static String KEY_STATE = "state"; |
| 38 | |
| 39 | private static final int REQUEST_UNLOCK = 1; |
| 40 | |
| 41 | private static enum State { INITIAL, UNLOCK_REQUESTED }; |
| 42 | |
| 43 | private State mState; |
| 44 | |
| 45 | private KeyStore mKeyStore = KeyStore.getInstance(); |
| 46 | |
| 47 | private boolean isKeyStoreUnlocked() { |
Brian Carlstrom | e3b3390 | 2011-05-31 01:06:20 -0700 | [diff] [blame] | 48 | return mKeyStore.state() == KeyStore.State.UNLOCKED; |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Brian Carlstrom | c5b990f | 2011-06-02 16:26:12 -0700 | [diff] [blame] | 51 | private boolean isKeyStoreEmpty() { |
| 52 | String[] aliases = mKeyStore.saw(Credentials.USER_PRIVATE_KEY); |
| 53 | return (aliases == null || aliases.length == 0); |
| 54 | } |
| 55 | |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 56 | @Override public void onCreate(Bundle savedState) { |
| 57 | super.onCreate(savedState); |
| 58 | if (savedState == null) { |
| 59 | mState = State.INITIAL; |
| 60 | } else { |
| 61 | mState = (State) savedState.getSerializable(KEY_STATE); |
| 62 | if (mState == null) { |
| 63 | mState = State.INITIAL; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | @Override public void onResume() { |
| 69 | super.onResume(); |
| 70 | |
| 71 | // see if KeyStore has been unlocked, if not start activity to do so |
| 72 | switch (mState) { |
| 73 | case INITIAL: |
Brian Carlstrom | c5b990f | 2011-06-02 16:26:12 -0700 | [diff] [blame] | 74 | if (isKeyStoreEmpty()) { |
| 75 | finish(null); |
| 76 | return; |
| 77 | } |
| 78 | |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 79 | if (!isKeyStoreUnlocked()) { |
| 80 | mState = State.UNLOCK_REQUESTED; |
| 81 | this.startActivityForResult(new Intent(Credentials.UNLOCK_ACTION), |
| 82 | REQUEST_UNLOCK); |
| 83 | // Note that Credentials.unlock will start an |
| 84 | // Activity and we will be paused but then resumed |
| 85 | // when the unlock Activity completes and our |
| 86 | // onActivityResult is called with REQUEST_UNLOCK |
| 87 | return; |
| 88 | } |
| 89 | showAliasList(); |
| 90 | return; |
| 91 | case UNLOCK_REQUESTED: |
| 92 | // we've already asked, but have not heard back, probably just rotated. |
| 93 | // wait to hear back via onActivityResult |
| 94 | return; |
| 95 | default: |
| 96 | throw new AssertionError(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | private void showAliasList() { |
| 101 | |
| 102 | String[] aliases = mKeyStore.saw(Credentials.USER_PRIVATE_KEY); |
Brian Carlstrom | dd74b9e | 2011-05-17 17:42:38 -0700 | [diff] [blame] | 103 | if (aliases == null || aliases.length == 0) { |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 104 | finish(null); |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | |
| 108 | final ArrayAdapter<String> adapter |
| 109 | = new ArrayAdapter<String>(this, |
| 110 | android.R.layout.simple_list_item_1, |
| 111 | aliases); |
| 112 | setListAdapter(adapter); |
| 113 | |
| 114 | ListView lv = getListView(); |
| 115 | lv.setTextFilterEnabled(true); |
| 116 | lv.setOnItemClickListener(new OnItemClickListener() { |
| 117 | @Override public void onItemClick(AdapterView<?> parent, |
| 118 | View view, |
| 119 | int position, |
| 120 | long id) { |
| 121 | String alias = adapter.getItem(position); |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 122 | finish(alias); |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 123 | } |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 128 | switch (requestCode) { |
| 129 | case REQUEST_UNLOCK: |
| 130 | if (isKeyStoreUnlocked()) { |
| 131 | showAliasList(); |
| 132 | } else { |
| 133 | // user must have canceled unlock, give up |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 134 | finish(null); |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 135 | } |
| 136 | return; |
| 137 | default: |
| 138 | throw new AssertionError(); |
| 139 | } |
| 140 | } |
| 141 | |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 142 | private void finish(String alias) { |
| 143 | if (alias == null) { |
| 144 | setResult(RESULT_CANCELED); |
| 145 | } else { |
| 146 | Intent result = new Intent(); |
| 147 | result.putExtra(Intent.EXTRA_TEXT, alias); |
| 148 | setResult(RESULT_OK, result); |
| 149 | } |
Brian Carlstrom | f5b50a4 | 2011-06-09 16:05:09 -0700 | [diff] [blame^] | 150 | IKeyChainAliasCallback keyChainAliasResponse |
| 151 | = IKeyChainAliasCallback.Stub.asInterface( |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 152 | getIntent().getIBinderExtra(KeyChain.EXTRA_RESPONSE)); |
| 153 | if (keyChainAliasResponse != null) { |
| 154 | try { |
| 155 | keyChainAliasResponse.alias(alias); |
Brian Carlstrom | 2a85883 | 2011-05-26 09:30:26 -0700 | [diff] [blame] | 156 | } catch (Exception ignored) { |
| 157 | // don't just catch RemoteException, caller could |
| 158 | // throw back a RuntimeException across processes |
| 159 | // which we should protect against. |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | finish(); |
| 163 | } |
| 164 | |
Brian Carlstrom | 9e606df | 2011-06-07 12:03:08 -0700 | [diff] [blame] | 165 | @Override public void onBackPressed() { |
| 166 | finish(null); |
| 167 | } |
| 168 | |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 169 | @Override protected void onSaveInstanceState(Bundle savedState) { |
| 170 | super.onSaveInstanceState(savedState); |
| 171 | if (mState != State.INITIAL) { |
| 172 | savedState.putSerializable(KEY_STATE, mState); |
| 173 | } |
| 174 | } |
| 175 | } |