blob: 8b94344e6b4ad0652f3a715e4ed31e58aa599f3d [file] [log] [blame]
Brian Carlstrom3e6251d2011-04-11 09:05:06 -07001/*
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
17package com.android.keychain;
18
19import android.app.ListActivity;
20import android.content.Intent;
21import android.os.Bundle;
Brian Carlstrombb04f702011-05-24 21:54:51 -070022import android.os.RemoteException;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070023import android.security.Credentials;
Brian Carlstromf5b50a42011-06-09 16:05:09 -070024import android.security.IKeyChainAliasCallback;
Brian Carlstrombb04f702011-05-24 21:54:51 -070025import android.security.KeyChain;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070026import android.security.KeyStore;
27import android.view.View;
28import android.widget.AdapterView.OnItemClickListener;
29import android.widget.AdapterView;
30import android.widget.ArrayAdapter;
31import android.widget.ListView;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070032
33public 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 Carlstrome3b33902011-05-31 01:06:20 -070048 return mKeyStore.state() == KeyStore.State.UNLOCKED;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070049 }
50
Brian Carlstromc5b990f2011-06-02 16:26:12 -070051 private boolean isKeyStoreEmpty() {
52 String[] aliases = mKeyStore.saw(Credentials.USER_PRIVATE_KEY);
53 return (aliases == null || aliases.length == 0);
54 }
55
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070056 @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 Carlstromc5b990f2011-06-02 16:26:12 -070074 if (isKeyStoreEmpty()) {
75 finish(null);
76 return;
77 }
78
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070079 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 Carlstromdd74b9e2011-05-17 17:42:38 -0700103 if (aliases == null || aliases.length == 0) {
Brian Carlstrombb04f702011-05-24 21:54:51 -0700104 finish(null);
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700105 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 Carlstrombb04f702011-05-24 21:54:51 -0700122 finish(alias);
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700123 }
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 Carlstrombb04f702011-05-24 21:54:51 -0700134 finish(null);
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700135 }
136 return;
137 default:
138 throw new AssertionError();
139 }
140 }
141
Brian Carlstrombb04f702011-05-24 21:54:51 -0700142 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 Carlstromf5b50a42011-06-09 16:05:09 -0700150 IKeyChainAliasCallback keyChainAliasResponse
151 = IKeyChainAliasCallback.Stub.asInterface(
Brian Carlstrombb04f702011-05-24 21:54:51 -0700152 getIntent().getIBinderExtra(KeyChain.EXTRA_RESPONSE));
153 if (keyChainAliasResponse != null) {
154 try {
155 keyChainAliasResponse.alias(alias);
Brian Carlstrom2a858832011-05-26 09:30:26 -0700156 } 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 Carlstrombb04f702011-05-24 21:54:51 -0700160 }
161 }
162 finish();
163 }
164
Brian Carlstrom9e606df2011-06-07 12:03:08 -0700165 @Override public void onBackPressed() {
166 finish(null);
167 }
168
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700169 @Override protected void onSaveInstanceState(Bundle savedState) {
170 super.onSaveInstanceState(savedState);
171 if (mState != State.INITIAL) {
172 savedState.putSerializable(KEY_STATE, mState);
173 }
174 }
175}