OSDN Git Service

Revert "Use the keystore service instead of the direct file acess."
authorChung-yih Wang <cywang@google.com>
Thu, 11 Jun 2009 10:13:49 +0000 (18:13 +0800)
committerChung-yih Wang <cywang@google.com>
Thu, 11 Jun 2009 10:13:49 +0000 (18:13 +0800)
This reverts commit 289705f3311932a45adef1710358f5e302115013.

src/com/android/settings/wifi/AccessPointDialog.java
src/com/android/settings/wifi/Keystore.java [new file with mode: 0644]

index 572ca85..d8da3d0 100644 (file)
@@ -22,7 +22,6 @@ import android.app.AlertDialog;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.res.Resources;
-import android.security.Keystore;
 import android.net.wifi.WifiInfo;
 import android.net.wifi.WifiManager;
 import android.os.Bundle;
@@ -80,8 +79,7 @@ public class AccessPointDialog extends AlertDialog implements DialogInterface.On
             AccessPointState.WEP_PASSWORD_AUTO, AccessPointState.WEP_PASSWORD_ASCII,
             AccessPointState.WEP_PASSWORD_HEX
     };
-    private static final String NOT_APPLICABLE = "N/A";
-
+    
     // Button positions, default to impossible values
     private int mConnectButtonPos = Integer.MAX_VALUE; 
     private int mForgetButtonPos = Integer.MAX_VALUE;
@@ -132,13 +130,11 @@ public class AccessPointDialog extends AlertDialog implements DialogInterface.On
     private TextView mSecurityText;
     private Spinner mSecuritySpinner;
     private Spinner mWepTypeSpinner;
-    private Keystore mKeystore;
     
     public AccessPointDialog(Context context, WifiLayer wifiLayer) {
         super(context);
 
         mWifiLayer = wifiLayer;
-        mKeystore = Keystore.getInstance();
     }
 
     @Override
@@ -329,34 +325,19 @@ public class AccessPointDialog extends AlertDialog implements DialogInterface.On
             setEnterpriseFields(view);
             mEapSpinner.setSelection(getSelectionIndex(
                     R.array.wifi_eap_entries, mState.getEap()));
+            Keystore ks = Keystore.getInstance();
             mClientCertSpinner.setSelection(getSelectionIndex(
-                    getAllCertificateKeys(), mState.getEnterpriseField(
+                    ks.getAllCertificateKeys(), mState.getEnterpriseField(
                     AccessPointState.CLIENT_CERT)));
             mCaCertSpinner.setSelection(getSelectionIndex(
-                    getAllCertificateKeys(), mState.getEnterpriseField(
+                    ks.getAllCertificateKeys(), mState.getEnterpriseField(
                     AccessPointState.CA_CERT)));
             mPrivateKeySpinner.setSelection(getSelectionIndex(
-                    getAllUserkeyKeys(), mState.getEnterpriseField(
+                    ks.getAllUserkeyKeys(), mState.getEnterpriseField(
                     AccessPointState.PRIVATE_KEY)));
         }
     }
 
-    private String[] getAllCertificateKeys() {
-        return appendEmptyInSelection(mKeystore.getAllCertificateKeys());
-    }
-
-    private String[] getAllUserkeyKeys() {
-        return appendEmptyInSelection(mKeystore.getAllUserkeyKeys());
-    }
-
-    private String[] appendEmptyInSelection(String[] keys) {
-      if (keys.length == 0) return keys;
-      String[] selections = new String[keys.length + 1];
-      System.arraycopy(keys, 0, selections, 0, keys.length);
-      selections[keys.length] = NOT_APPLICABLE;
-      return selections;
-    }
-
     private void setEnterpriseFields(View view) {
         mIdentityText = (TextView) view.findViewById(R.id.identity_text);
         mIdentityEdit = (EditText) view.findViewById(R.id.identity_edit);
@@ -384,24 +365,26 @@ public class AccessPointDialog extends AlertDialog implements DialogInterface.On
         mPhase2Spinner.setPromptId(R.string.please_select_phase2);
         setSpinnerAdapter(mPhase2Spinner, R.array.wifi_phase2_entries);
 
+        Keystore ks = Keystore.getInstance();
+
         mClientCertSpinner =
                 (Spinner) view.findViewById(R.id.client_certificate_spinner);
         mClientCertSpinner.setOnItemSelectedListener(this);
         mClientCertSpinner.setPromptId(
                 R.string.please_select_client_certificate);
-        setSpinnerAdapter(mClientCertSpinner, getAllCertificateKeys());
+        setSpinnerAdapter(mClientCertSpinner, ks.getAllCertificateKeys());
 
         mCaCertSpinner =
                 (Spinner) view.findViewById(R.id.ca_certificate_spinner);
         mCaCertSpinner.setOnItemSelectedListener(this);
         mCaCertSpinner.setPromptId(R.string.please_select_ca_certificate);
-        setSpinnerAdapter(mCaCertSpinner, getAllCertificateKeys());
+        setSpinnerAdapter(mCaCertSpinner, ks.getAllCertificateKeys());
 
         mPrivateKeySpinner =
                 (Spinner) view.findViewById(R.id.private_key_spinner);
         mPrivateKeySpinner.setOnItemSelectedListener(this);
         mPrivateKeySpinner.setPromptId(R.string.please_select_private_key);
-        setSpinnerAdapter(mPrivateKeySpinner, getAllUserkeyKeys());
+        setSpinnerAdapter(mPrivateKeySpinner, ks.getAllUserkeyKeys());
 
         mEnterpriseTextFields = new EditText[] {
             mIdentityEdit, mAnonymousIdentityEdit, mPrivateKeyPasswdEdit
@@ -656,6 +639,7 @@ public class AccessPointDialog extends AlertDialog implements DialogInterface.On
 
     private void updateEnterpriseFields(int securityType) {
             int i;
+            Keystore ks = Keystore.getInstance();
             for (i = AccessPointState.IDENTITY ;
                 i < AccessPointState.MAX_ENTRPRISE_FIELD ; i++) {
                 String value;
@@ -664,21 +648,16 @@ public class AccessPointDialog extends AlertDialog implements DialogInterface.On
                 } else {
                     Spinner spinner =  mEnterpriseSpinnerFields[i -
                         AccessPointState.CLIENT_CERT];
-                    int index = spinner.getSelectedItemPosition();
-                    if (index == (spinner.getCount() - 1)) {
-                        value = "";
+
+                    if (i != AccessPointState.PRIVATE_KEY) {
+                        value = ks.getCertificate(ks.getAllCertificateKeys()
+                            [spinner.getSelectedItemPosition()]);
                     } else {
-                        if (i != AccessPointState.PRIVATE_KEY) {
-                            value = mKeystore.getCertificate(
-                                    getAllCertificateKeys()[index]);
-                        } else {
-                            value = mKeystore.getUserkey(
-                                    getAllUserkeyKeys()[index]);
-                        }
+                        value = ks.getUserkey(ks.getAllUserkeyKeys()
+                            [spinner.getSelectedItemPosition()]);
                     }
                 }
-                if (!TextUtils.isEmpty(value) ||
-                        (i == AccessPointState.PRIVATE_KEY_PASSWD)) {
+                if (!TextUtils.isEmpty(value)) {
                     mState.setEnterpriseField(i, value);
                 }
             }
diff --git a/src/com/android/settings/wifi/Keystore.java b/src/com/android/settings/wifi/Keystore.java
new file mode 100644 (file)
index 0000000..68af868
--- /dev/null
@@ -0,0 +1,81 @@
+package com.android.settings.wifi;
+
+import android.util.Log;
+
+import java.io.File;
+
+/**
+ */
+public abstract class Keystore {
+    public static final String TAG = "Keystore";
+
+    private static final String PACKAGE_PREFIX =
+        Keystore.class.getPackage().getName() + ".";
+
+    public static final String ACTION_KEYSTORE_CERTIFICATES =
+        PACKAGE_PREFIX + "CERTIFICATES";
+    public static final String ACTION_KEYSTORE_USERKEYS =
+        PACKAGE_PREFIX + "USERKEYS";
+
+    /**
+     */
+    public static Keystore getInstance() {
+        return new FileKeystore();
+    }
+
+    /**
+     */
+    public abstract String getUserkey(String key);
+
+    /**
+     */
+    public abstract String getCertificate(String key);
+
+    /**
+     */
+    public abstract String[] getAllCertificateKeys();
+
+    /**
+     */
+    public abstract String[] getAllUserkeyKeys();
+
+    private static class FileKeystore extends Keystore {
+        private static final String PATH = "/data/misc/keystore/";
+        private static final String USERKEY_PATH = PATH + "userkeys/";
+        private static final String CERT_PATH = PATH + "certs/";
+
+        @Override
+        public String getUserkey(String key) {
+            String path = USERKEY_PATH + key;
+            return (new File(path).exists() ? path : null);
+        }
+
+        @Override
+        public String getCertificate(String key) {
+            String path = CERT_PATH + key;
+            return (new File(path).exists() ? path : null);
+        }
+
+        @Override
+        public String[] getAllCertificateKeys() {
+            File dir = new File(CERT_PATH);
+            if (dir.exists()) {
+                return dir.list();
+            } else {
+                Log.v(TAG, "-------- cert directory does not exist!");
+                return null;
+            }
+        }
+
+        @Override
+        public String[] getAllUserkeyKeys() {
+            File dir = new File(USERKEY_PATH);
+            if (dir.exists()) {
+                return dir.list();
+            } else {
+                Log.v(TAG, "-------- userkey directory does not exist!");
+                return null;
+            }
+        }
+    }
+}