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 | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame^] | 24 | import android.security.IKeyChainAliasResponse; |
| 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() { |
| 48 | return mKeyStore.test() == KeyStore.NO_ERROR; |
| 49 | } |
| 50 | |
| 51 | @Override public void onCreate(Bundle savedState) { |
| 52 | super.onCreate(savedState); |
| 53 | if (savedState == null) { |
| 54 | mState = State.INITIAL; |
| 55 | } else { |
| 56 | mState = (State) savedState.getSerializable(KEY_STATE); |
| 57 | if (mState == null) { |
| 58 | mState = State.INITIAL; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | @Override public void onResume() { |
| 64 | super.onResume(); |
| 65 | |
| 66 | // see if KeyStore has been unlocked, if not start activity to do so |
| 67 | switch (mState) { |
| 68 | case INITIAL: |
| 69 | if (!isKeyStoreUnlocked()) { |
| 70 | mState = State.UNLOCK_REQUESTED; |
| 71 | this.startActivityForResult(new Intent(Credentials.UNLOCK_ACTION), |
| 72 | REQUEST_UNLOCK); |
| 73 | // Note that Credentials.unlock will start an |
| 74 | // Activity and we will be paused but then resumed |
| 75 | // when the unlock Activity completes and our |
| 76 | // onActivityResult is called with REQUEST_UNLOCK |
| 77 | return; |
| 78 | } |
| 79 | showAliasList(); |
| 80 | return; |
| 81 | case UNLOCK_REQUESTED: |
| 82 | // we've already asked, but have not heard back, probably just rotated. |
| 83 | // wait to hear back via onActivityResult |
| 84 | return; |
| 85 | default: |
| 86 | throw new AssertionError(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private void showAliasList() { |
| 91 | |
| 92 | String[] aliases = mKeyStore.saw(Credentials.USER_PRIVATE_KEY); |
Brian Carlstrom | dd74b9e | 2011-05-17 17:42:38 -0700 | [diff] [blame] | 93 | if (aliases == null || aliases.length == 0) { |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame^] | 94 | finish(null); |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 95 | return; |
| 96 | } |
| 97 | |
| 98 | final ArrayAdapter<String> adapter |
| 99 | = new ArrayAdapter<String>(this, |
| 100 | android.R.layout.simple_list_item_1, |
| 101 | aliases); |
| 102 | setListAdapter(adapter); |
| 103 | |
| 104 | ListView lv = getListView(); |
| 105 | lv.setTextFilterEnabled(true); |
| 106 | lv.setOnItemClickListener(new OnItemClickListener() { |
| 107 | @Override public void onItemClick(AdapterView<?> parent, |
| 108 | View view, |
| 109 | int position, |
| 110 | long id) { |
| 111 | String alias = adapter.getItem(position); |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame^] | 112 | finish(alias); |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 113 | } |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 118 | switch (requestCode) { |
| 119 | case REQUEST_UNLOCK: |
| 120 | if (isKeyStoreUnlocked()) { |
| 121 | showAliasList(); |
| 122 | } else { |
| 123 | // user must have canceled unlock, give up |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame^] | 124 | finish(null); |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 125 | } |
| 126 | return; |
| 127 | default: |
| 128 | throw new AssertionError(); |
| 129 | } |
| 130 | } |
| 131 | |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame^] | 132 | private void finish(String alias) { |
| 133 | if (alias == null) { |
| 134 | setResult(RESULT_CANCELED); |
| 135 | } else { |
| 136 | Intent result = new Intent(); |
| 137 | result.putExtra(Intent.EXTRA_TEXT, alias); |
| 138 | setResult(RESULT_OK, result); |
| 139 | } |
| 140 | IKeyChainAliasResponse keyChainAliasResponse |
| 141 | = IKeyChainAliasResponse.Stub.asInterface( |
| 142 | getIntent().getIBinderExtra(KeyChain.EXTRA_RESPONSE)); |
| 143 | if (keyChainAliasResponse != null) { |
| 144 | try { |
| 145 | keyChainAliasResponse.alias(alias); |
| 146 | } catch (RemoteException ignored) { |
| 147 | } |
| 148 | } |
| 149 | finish(); |
| 150 | } |
| 151 | |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 152 | @Override protected void onSaveInstanceState(Bundle savedState) { |
| 153 | super.onSaveInstanceState(savedState); |
| 154 | if (mState != State.INITIAL) { |
| 155 | savedState.putSerializable(KEY_STATE, mState); |
| 156 | } |
| 157 | } |
| 158 | } |