blob: 552a73ada804b61d510ddca6429c5aedf9aa2e9f [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 Carlstrombb04f702011-05-24 21:54:51 -070024import android.security.IKeyChainAliasResponse;
25import 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() {
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 Carlstromdd74b9e2011-05-17 17:42:38 -070093 if (aliases == null || aliases.length == 0) {
Brian Carlstrombb04f702011-05-24 21:54:51 -070094 finish(null);
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070095 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 Carlstrombb04f702011-05-24 21:54:51 -0700112 finish(alias);
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700113 }
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 Carlstrombb04f702011-05-24 21:54:51 -0700124 finish(null);
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700125 }
126 return;
127 default:
128 throw new AssertionError();
129 }
130 }
131
Brian Carlstrombb04f702011-05-24 21:54:51 -0700132 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 Carlstrom3e6251d2011-04-11 09:05:06 -0700152 @Override protected void onSaveInstanceState(Bundle savedState) {
153 super.onSaveInstanceState(savedState);
154 if (mState != State.INITIAL) {
155 savedState.putSerializable(KEY_STATE, mState);
156 }
157 }
158}