diff options
author | Ricardo Cerqueira <cyanogenmod@cerqueira.org> | 2013-12-06 02:23:37 +0000 |
---|---|---|
committer | Ricardo Cerqueira <cyanogenmod@cerqueira.org> | 2013-12-06 02:23:37 +0000 |
commit | f15e8a506441c2bc541656ca541cfc320f6308c4 (patch) | |
tree | 69fefcb9ab493ea2579d8f83510ec2bda83b1d20 | |
parent | 649e0efa79fdf9e923f8a699c55533d07c65d75a (diff) | |
parent | 88064ddf52b7137e47176906f30a508d72fd3491 (diff) | |
download | android_packages_apps_CertInstaller-f15e8a506441c2bc541656ca541cfc320f6308c4.tar.gz android_packages_apps_CertInstaller-f15e8a506441c2bc541656ca541cfc320f6308c4.tar.bz2 android_packages_apps_CertInstaller-f15e8a506441c2bc541656ca541cfc320f6308c4.zip |
Merge tag 'android-4.4.1_r1' into HEAD
Android 4.4.1 Release 1
-rw-r--r-- | AndroidManifest.xml | 10 | ||||
-rw-r--r-- | res/values-ar/strings.xml | 16 | ||||
-rw-r--r-- | res/values-fa/strings.xml | 18 | ||||
-rw-r--r-- | res/values-hi/strings.xml | 2 | ||||
-rw-r--r-- | res/values-iw/strings.xml | 16 | ||||
-rw-r--r-- | src/com/android/certinstaller/CertFile.java | 206 | ||||
-rw-r--r-- | src/com/android/certinstaller/CertFileList.java | 198 | ||||
-rw-r--r-- | src/com/android/certinstaller/CertInstallerMain.java | 161 |
8 files changed, 104 insertions, 523 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml index b2880d6..0d754b1 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -3,8 +3,6 @@ <original-package android:name="com.android.certinstaller" /> - <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> - <permission android:name="com.android.certinstaller.INSTALL_AS_USER" android:protectionLevel="signature" /> @@ -22,7 +20,10 @@ <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="application/x-x509-ca-cert" /> <data android:mimeType="application/x-x509-user-cert" /> + <data android:mimeType="application/x-x509-server-cert" /> <data android:mimeType="application/x-pkcs12" /> + <data android:mimeType="application/application/x-pem-file" /> + <data android:mimeType="application/pkix-cert" /> </intent-filter> </activity> @@ -40,10 +41,5 @@ android:configChanges="orientation|keyboardHidden" android:exported="false"> </activity> - - <activity android:name=".CertFileList" - android:configChanges="orientation|keyboardHidden" - android:exported="false"> - </activity> </application> </manifest> diff --git a/res/values-ar/strings.xml b/res/values-ar/strings.xml index de1637b..5b134b6 100644 --- a/res/values-ar/strings.xml +++ b/res/values-ar/strings.xml @@ -20,16 +20,16 @@ <string name="pick_file_title" msgid="4481949485108233297">"اختيار شهادة"</string> <string name="pkcs12_password_dialog_title" msgid="5997624645207427161">"استخراج الشهادة"</string> <string name="extracting_pkcs12" msgid="1881267738821799771">"جارٍ الاستخراج..."</string> - <string name="pkcs12_file_password_dialog_title" msgid="8102721384767269510">"استخراج من %s"</string> + <string name="pkcs12_file_password_dialog_title" msgid="8102721384767269510">"استخراج من %s"</string> <string name="name_credential_dialog_title" msgid="277729846491554437">"اسم الشهادة"</string> <string name="credential_name" msgid="1974979771369744378">"اسم الشهادة:"</string> <string name="credential_password" msgid="3520176519550993326">"إدخال كلمة المرور لاستخراج الشهادات."</string> <string name="credential_info" msgid="2436768402308470397">"تحتوي الحزمة على:"</string> - <string name="p12_description" msgid="4128352087331630024">"الشهادات في مخزن مفاتيح PKCS12."</string> + <string name="p12_description" msgid="4128352087331630024">"الشهادات في مخزن مفاتيح PKCS12."</string> <string name="one_userkey" msgid="3500697912370644881">"مفتاح مستخدم واحد"</string> <string name="one_usercrt" msgid="7690798336332403106">"شهادة مستخدم واحد"</string> - <string name="one_cacrt" msgid="2667950425420663146">"شهادة CA واحدة"</string> - <string name="n_cacrts" msgid="2141498640685639208">"%d شهادات CA"</string> + <string name="one_cacrt" msgid="2667950425420663146">"شهادة CA واحدة"</string> + <string name="n_cacrts" msgid="2141498640685639208">"%d شهادات CA"</string> <string name="password_error" msgid="2042471639556516356">"أدخل كلمة المرور الصحيحة."</string> <string name="password_empty_error" msgid="591713406761723025">"أدخل كلمة المرور."</string> <string name="name_empty_error" msgid="3808800768660110354">"اكتب اسمًا."</string> @@ -43,10 +43,10 @@ <string name="cert_too_large_error" msgid="8715414972725646285">"تعذر التثبيت نظرًا لأن حجم الشهادة أكبر من اللازم."</string> <string name="cert_missing_error" msgid="7040527352278728220">"تعذر التثبيت نظرًا لتعذر تحديد مكان ملف الشهادة."</string> <string name="cert_read_error" msgid="7353463360652419796">"تعذر التثبيت نظرًا لتعذر قراءة ملف الشهادة."</string> - <string name="no_cert_file_found" product="nosdcard" msgid="3555332946357131929">"لم يتم العثور على ملف شهادة في وحدة تخزين USB."</string> - <string name="no_cert_file_found" product="default" msgid="1942077544887243999">"لم يتم العثور على ملف شهادة في بطاقة SD."</string> - <string name="sdcard_not_present" product="nosdcard" msgid="9197143240492611196">"وحدة تخزين USB غير متاحة."</string> - <string name="sdcard_not_present" product="default" msgid="3866952732541932947">"بطاقة SD غير موجودة."</string> + <string name="no_cert_file_found" product="nosdcard" msgid="3555332946357131929">"لم يتم العثور على ملف شهادة في وحدة تخزين USB."</string> + <string name="no_cert_file_found" product="default" msgid="1942077544887243999">"لم يتم العثور على ملف شهادة في بطاقة SD."</string> + <string name="sdcard_not_present" product="nosdcard" msgid="9197143240492611196">"وحدة تخزين USB غير متاحة."</string> + <string name="sdcard_not_present" product="default" msgid="3866952732541932947">"بطاقة SD غير موجودة."</string> <string name="only_primary_user_allowed" msgid="4005084935023047716">"يُسمح لمالك هذا الجهاز فقط تثبيت الشهادات."</string> <string name="credential_usage_label" msgid="1513892208033156805">"استخدام بيانات الاعتماد:"</string> <string-array name="credential_usage"> diff --git a/res/values-fa/strings.xml b/res/values-fa/strings.xml index e7cf998..ab0a0cf 100644 --- a/res/values-fa/strings.xml +++ b/res/values-fa/strings.xml @@ -20,16 +20,16 @@ <string name="pick_file_title" msgid="4481949485108233297">"انتخاب یک گواهینامه"</string> <string name="pkcs12_password_dialog_title" msgid="5997624645207427161">"استخراج گواهی"</string> <string name="extracting_pkcs12" msgid="1881267738821799771">"در حال استخراج..."</string> - <string name="pkcs12_file_password_dialog_title" msgid="8102721384767269510">"اجرا از %s"</string> + <string name="pkcs12_file_password_dialog_title" msgid="8102721384767269510">"اجرا از %s"</string> <string name="name_credential_dialog_title" msgid="277729846491554437">"نامگذاری گواهی"</string> <string name="credential_name" msgid="1974979771369744378">"نام گواهی:"</string> <string name="credential_password" msgid="3520176519550993326">"گذرواژه را برای استخراج گواهینامهها وارد کنید."</string> <string name="credential_info" msgid="2436768402308470397">"این بسته شامل این موارد است:"</string> - <string name="p12_description" msgid="4128352087331630024">"گواهیها در حافظه کلید PKCS12."</string> + <string name="p12_description" msgid="4128352087331630024">"گواهیها در حافظه کلید PKCS12."</string> <string name="one_userkey" msgid="3500697912370644881">"یک کلید کاربری"</string> <string name="one_usercrt" msgid="7690798336332403106">"یک گواهی کاربری"</string> - <string name="one_cacrt" msgid="2667950425420663146">"در گواهی CA"</string> - <string name="n_cacrts" msgid="2141498640685639208">"%d گواهی CA"</string> + <string name="one_cacrt" msgid="2667950425420663146">"در گواهی CA"</string> + <string name="n_cacrts" msgid="2141498640685639208">"%d گواهی CA"</string> <string name="password_error" msgid="2042471639556516356">"گذرواژه صحیح را تایپ کنید."</string> <string name="password_empty_error" msgid="591713406761723025">"گذرواژه را تایپ کنید."</string> <string name="name_empty_error" msgid="3808800768660110354">"نامی را تایپ کنید."</string> @@ -43,14 +43,14 @@ <string name="cert_too_large_error" msgid="8715414972725646285">"نصب نشد زیرا اندازه گواهینامه خیلی بزرگ است."</string> <string name="cert_missing_error" msgid="7040527352278728220">"نصب نشد زیرا فایل گواهینامه یافت نشد."</string> <string name="cert_read_error" msgid="7353463360652419796">"نصب نشد زیرا فایل گواهینامه خوانده نشد."</string> - <string name="no_cert_file_found" product="nosdcard" msgid="3555332946357131929">"فایل مجوزی در حافظهٔ USB یافت نشد."</string> - <string name="no_cert_file_found" product="default" msgid="1942077544887243999">"هیچ فایل گواهی در کارت SD یافت نشد."</string> - <string name="sdcard_not_present" product="nosdcard" msgid="9197143240492611196">"حافظهٔ USB موجود نیست."</string> - <string name="sdcard_not_present" product="default" msgid="3866952732541932947">"کارت SD موجود نیست."</string> + <string name="no_cert_file_found" product="nosdcard" msgid="3555332946357131929">"فایل مجوزی در حافظهٔ USB یافت نشد."</string> + <string name="no_cert_file_found" product="default" msgid="1942077544887243999">"هیچ فایل گواهی در کارت SD یافت نشد."</string> + <string name="sdcard_not_present" product="nosdcard" msgid="9197143240492611196">"حافظهٔ USB موجود نیست."</string> + <string name="sdcard_not_present" product="default" msgid="3866952732541932947">"کارت SD موجود نیست."</string> <string name="only_primary_user_allowed" msgid="4005084935023047716">"فقط مالک این دستگاه میتواند گواهیها را نصب نماید."</string> <string name="credential_usage_label" msgid="1513892208033156805">"استفاده از اطلاعات کاربری:"</string> <string-array name="credential_usage"> - <item msgid="375085478373011304">"VPN و برنامهها"</item> + <item msgid="375085478373011304">"VPN و برنامهها"</item> <item msgid="692322974985472861">"Wi-Fi"</item> </string-array> </resources> diff --git a/res/values-hi/strings.xml b/res/values-hi/strings.xml index 0eee7c0..5a80c08 100644 --- a/res/values-hi/strings.xml +++ b/res/values-hi/strings.xml @@ -50,7 +50,7 @@ <string name="only_primary_user_allowed" msgid="4005084935023047716">"केवल इस उपकरण का स्वामी ही प्रमाणपत्रों को इंस्टॉल कर सकता है."</string> <string name="credential_usage_label" msgid="1513892208033156805">"क्रेडेंशियल उपयोग:"</string> <string-array name="credential_usage"> - <item msgid="375085478373011304">"VPN और एप्स"</item> + <item msgid="375085478373011304">"VPN और ऐप्स"</item> <item msgid="692322974985472861">"Wi-Fi"</item> </string-array> </resources> diff --git a/res/values-iw/strings.xml b/res/values-iw/strings.xml index bc86fe5..fe00054 100644 --- a/res/values-iw/strings.xml +++ b/res/values-iw/strings.xml @@ -20,16 +20,16 @@ <string name="pick_file_title" msgid="4481949485108233297">"בחר אישור"</string> <string name="pkcs12_password_dialog_title" msgid="5997624645207427161">"חילוץ אישור"</string> <string name="extracting_pkcs12" msgid="1881267738821799771">"מחלץ..."</string> - <string name="pkcs12_file_password_dialog_title" msgid="8102721384767269510">"חלץ מתוך %s"</string> + <string name="pkcs12_file_password_dialog_title" msgid="8102721384767269510">"חלץ מתוך %s"</string> <string name="name_credential_dialog_title" msgid="277729846491554437">"תן שם לאישור"</string> <string name="credential_name" msgid="1974979771369744378">"שם אישור:"</string> <string name="credential_password" msgid="3520176519550993326">"הזן את הסיסמה כדי לחלץ את האישורים."</string> <string name="credential_info" msgid="2436768402308470397">"החבילה מכילה:"</string> - <string name="p12_description" msgid="4128352087331630024">"אישורים במאגר המפתחות PKCS12."</string> + <string name="p12_description" msgid="4128352087331630024">"אישורים במאגר המפתחות PKCS12."</string> <string name="one_userkey" msgid="3500697912370644881">"מקש משתמש אחד"</string> <string name="one_usercrt" msgid="7690798336332403106">"אישור למשתמש אחד"</string> <string name="one_cacrt" msgid="2667950425420663146">"אישור אחד מרשות אישורים"</string> - <string name="n_cacrts" msgid="2141498640685639208">"אישורים של רשות האישורים %d"</string> + <string name="n_cacrts" msgid="2141498640685639208">"אישורים של רשות האישורים %d"</string> <string name="password_error" msgid="2042471639556516356">"הקלד את הסיסמה הנכונה."</string> <string name="password_empty_error" msgid="591713406761723025">"הקלד את הסיסמה."</string> <string name="name_empty_error" msgid="3808800768660110354">"הקלד שם."</string> @@ -43,14 +43,14 @@ <string name="cert_too_large_error" msgid="8715414972725646285">"לא ניתן להתקין מפני שהאישור גדול מדי."</string> <string name="cert_missing_error" msgid="7040527352278728220">"לא ניתן להתקין מפני שקובץ האישור לא נמצא."</string> <string name="cert_read_error" msgid="7353463360652419796">"לא ניתן להתקין מפני שקובץ האישור לא נמצא."</string> - <string name="no_cert_file_found" product="nosdcard" msgid="3555332946357131929">"לא נמצא קובץ אישור באמצעי אחסון מסוג USB."</string> - <string name="no_cert_file_found" product="default" msgid="1942077544887243999">"לא נמצא קובץ אישור בכרטיס ה-SD."</string> - <string name="sdcard_not_present" product="nosdcard" msgid="9197143240492611196">"אחסון USB לא זמין."</string> - <string name="sdcard_not_present" product="default" msgid="3866952732541932947">"אין כרטיס SD."</string> + <string name="no_cert_file_found" product="nosdcard" msgid="3555332946357131929">"לא נמצא קובץ אישור באמצעי אחסון מסוג USB."</string> + <string name="no_cert_file_found" product="default" msgid="1942077544887243999">"לא נמצא קובץ אישור בכרטיס ה-SD."</string> + <string name="sdcard_not_present" product="nosdcard" msgid="9197143240492611196">"אחסון USB לא זמין."</string> + <string name="sdcard_not_present" product="default" msgid="3866952732541932947">"אין כרטיס SD."</string> <string name="only_primary_user_allowed" msgid="4005084935023047716">"רק הבעלים של מכשיר זה יכול להתקין אישורים."</string> <string name="credential_usage_label" msgid="1513892208033156805">"השימוש בפרטי הכניסה:"</string> <string-array name="credential_usage"> - <item msgid="375085478373011304">"VPN ואפליקציות"</item> + <item msgid="375085478373011304">"VPN ואפליקציות"</item> <item msgid="692322974985472861">"Wi-Fi"</item> </string-array> </resources> diff --git a/src/com/android/certinstaller/CertFile.java b/src/com/android/certinstaller/CertFile.java deleted file mode 100644 index 5b4bfbf..0000000 --- a/src/com/android/certinstaller/CertFile.java +++ /dev/null @@ -1,206 +0,0 @@ -/* - * Copyright (C) 2009 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.certinstaller; - -import android.content.Intent; -import android.os.Bundle; -import android.os.Environment; -import android.preference.PreferenceActivity; -import android.security.Credentials; -import android.security.KeyChain; -import android.util.Log; -import android.widget.Toast; - -import java.io.File; -import java.io.FileFilter; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * Base class that deals with certificate files on the SD card. - */ -public class CertFile extends PreferenceActivity implements FileFilter { - static final int CERT_READ_ERROR = R.string.cert_read_error; - static final int CERT_TOO_LARGE_ERROR = R.string.cert_too_large_error; - static final int CERT_FILE_MISSING_ERROR = R.string.cert_missing_error; - - static final String DOWNLOAD_DIR = "download"; - - private static final String TAG = "CertFile"; - - private static final String CERT_FILE_KEY = "cf"; - private static final int MAX_FILE_SIZE = 1000000; - protected static final int REQUEST_INSTALL_CODE = 1; - - private File mCertFile; - - @Override - protected void onSaveInstanceState(Bundle outStates) { - super.onSaveInstanceState(outStates); - if (mCertFile != null) { - outStates.putString(CERT_FILE_KEY, mCertFile.getAbsolutePath()); - } - } - - @Override - protected void onRestoreInstanceState(Bundle savedStates) { - super.onRestoreInstanceState(savedStates); - String path = savedStates.getString(CERT_FILE_KEY); - if (path != null) { - mCertFile = new File(path); - } - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == REQUEST_INSTALL_CODE) { - boolean success = (resultCode == RESULT_OK - && (mCertFile == null || Util.deleteFile(mCertFile))); - onInstallationDone(success); - mCertFile = null; - } else { - Log.w(TAG, "unknown request code: " + requestCode); - } - } - - /** - * Called when installation is done. - * - * @param success true if installation is done successfully - */ - protected void onInstallationDone(boolean success) { - if (success) { - setResult(RESULT_OK); - } - } - - /** - * Called when an error occurs when reading a certificate file. - * - * @param errorId one of {@link #CERT_READ_ERROR}, - * {@link #CERT_TOO_LARGE_ERROR} and {@link #CERT_FILE_MISSING_ERROR} - */ - protected void onError(int errorId) { - } - - /** - * Returns a list of certificate files found on the SD card. - */ - protected List<File> getAllCertFiles() { - List<File> allFiles = new ArrayList<File>(); - File root = Environment.getExternalStorageDirectory(); - - File download = new File(root, DOWNLOAD_DIR); - if (download != null) { - File[] files = download.listFiles(this); - if (files != null) { - Collections.addAll(allFiles, files); - } - } - - File[] files = root.listFiles(this); - if (files != null) { - Collections.addAll(allFiles, files); - } - - return allFiles; - } - - /** - * Invokes {@link CertInstaller} to install the certificate(s) in the file. - * - * @param file the certificate file - */ - protected void installFromFile(File file) { - Log.d(TAG, "install cert from " + file); - - String fileName = file.getName(); - Bundle bundle = getIntent().getExtras(); - - final String name; - final int installAsUid; - if (bundle == null) { - name = fileName; - installAsUid = -1; - } else { - name = bundle.getString(KeyChain.EXTRA_NAME, fileName); - installAsUid = bundle.getInt(Credentials.EXTRA_INSTALL_AS_UID, -1); - } - - if (file.exists()) { - if (file.length() < MAX_FILE_SIZE) { - byte[] data = Util.readFile(file); - if (data == null) { - toastError(CERT_READ_ERROR); - onError(CERT_READ_ERROR); - return; - } - mCertFile = file; - install(fileName, name, installAsUid, data); - } else { - Log.w(TAG, "cert file is too large: " + file.length()); - toastError(CERT_TOO_LARGE_ERROR); - onError(CERT_TOO_LARGE_ERROR); - } - } else { - Log.w(TAG, "cert file does not exist"); - toastError(CERT_FILE_MISSING_ERROR); - onError(CERT_FILE_MISSING_ERROR); - } - } - - @Override public boolean accept(File file) { - if (!file.isDirectory()) { - return isFileAcceptable(file.getPath()); - } else { - return false; - } - } - - protected boolean isFileAcceptable(String path) { - return (path.endsWith(Credentials.EXTENSION_CRT) || - path.endsWith(Credentials.EXTENSION_P12) || - path.endsWith(Credentials.EXTENSION_CER) || - path.endsWith(Credentials.EXTENSION_PFX)); - } - - protected boolean isSdCardPresent() { - return Environment.getExternalStorageState().equals( - Environment.MEDIA_MOUNTED); - } - - private void install(String fileName, String name, int uid, byte[] value) { - Intent intent = new Intent(this, CertInstaller.class); - intent.putExtra(KeyChain.EXTRA_NAME, name); - intent.putExtra(Credentials.EXTRA_INSTALL_AS_UID, uid); - if (fileName.endsWith(Credentials.EXTENSION_PFX) - || fileName.endsWith(Credentials.EXTENSION_P12)) { - intent.putExtra(KeyChain.EXTRA_PKCS12, value); - } else if (fileName.endsWith(Credentials.EXTENSION_CER) - || fileName.endsWith(Credentials.EXTENSION_CRT)) { - intent.putExtra(KeyChain.EXTRA_CERTIFICATE, value); - } else { - throw new AssertionError(fileName); - } - startActivityForResult(intent, REQUEST_INSTALL_CODE); - } - - private void toastError(int msgId) { - Toast.makeText(this, msgId, Toast.LENGTH_LONG).show(); - } -} diff --git a/src/com/android/certinstaller/CertFileList.java b/src/com/android/certinstaller/CertFileList.java deleted file mode 100644 index 1d32c26..0000000 --- a/src/com/android/certinstaller/CertFileList.java +++ /dev/null @@ -1,198 +0,0 @@ -/* - * Copyright (C) 2009 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.certinstaller; - -import android.os.Bundle; -import android.os.Environment; -import android.os.FileObserver; -import android.preference.Preference; -import android.preference.PreferenceScreen; -import android.security.Credentials; -import android.util.Log; -import android.widget.Toast; - -import java.io.File; -import java.io.IOException; -import java.util.List; - -/** - * Lists certificate files in the SD card. User may click one to install it - * to the system keystore. - */ -public class CertFileList extends CertFile - implements Preference.OnPreferenceClickListener { - private static final String TAG = "CertFileList"; - - private static final String DOWNLOAD_DIR = "download"; - - private SdCardMonitor mSdCardMonitor; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - addPreferencesFromResource(R.xml.pick_file_pref); - createFileList(); - startSdCardMonitor(); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - stopSdCardMonitor(); - } - - @Override - protected void onInstallationDone(boolean fileDeleted) { - super.onInstallationDone(fileDeleted); - if (!fileDeleted) { - if (isSdCardPresent()) { - setAllFilesEnabled(true); - } else { - Toast.makeText(this, R.string.sdcard_not_present, - Toast.LENGTH_SHORT).show(); - finish(); - } - } - } - - @Override - protected void onError(int errorId) { - if (errorId == CERT_FILE_MISSING_ERROR) { - createFileList(); - } - } - - private void setAllFilesEnabled(boolean enabled) { - PreferenceScreen root = getPreferenceScreen(); - for (int i = 0, n = root.getPreferenceCount(); i < n; i++) { - root.getPreference(i).setEnabled(enabled); - } - } - - public boolean onPreferenceClick(Preference pref) { - File file = new File(Environment.getExternalStorageDirectory(), - pref.getTitle().toString()); - if (file.isDirectory()) { - Log.w(TAG, "impossible to pick a directory! " + file); - } else { - setAllFilesEnabled(false); - installFromFile(file); - } - return true; - } - - private void createFileList() { - if (isFinishing()) { - Log.d(TAG, "finishing, exit createFileList()"); - return; - } - if (!isSdCardPresent()) { - Toast.makeText(this, R.string.sdcard_not_present, - Toast.LENGTH_SHORT).show(); - finish(); - return; - } - - try { - PreferenceScreen root = getPreferenceScreen(); - root.removeAll(); - - List<File> allFiles = getAllCertFiles(); - if (allFiles.isEmpty()) { - Toast.makeText(this, R.string.no_cert_file_found, - Toast.LENGTH_SHORT).show(); - finish(); - return; - } else { - int prefixEnd = Environment.getExternalStorageDirectory() - .getCanonicalPath().length() + 1; - for (File file : allFiles) { - Preference pref = new Preference(this); - pref.setTitle(file.getCanonicalPath().substring(prefixEnd)); - root.addPreference(pref); - pref.setOnPreferenceClickListener(this); - } - } - } catch (IOException e) { - // should not occur - Log.w(TAG, "createFileList(): " + e); - throw new RuntimeException(e); - } - } - - private void startSdCardMonitor() { - if (mSdCardMonitor == null) { - mSdCardMonitor = new SdCardMonitor(); - } - mSdCardMonitor.startWatching(); - } - - private void stopSdCardMonitor() { - if (mSdCardMonitor != null) { - mSdCardMonitor.stopWatching(); - } - } - - private class SdCardMonitor { - FileObserver mRootMonitor; - FileObserver mDownloadMonitor; - - SdCardMonitor() { - File root = Environment.getExternalStorageDirectory(); - mRootMonitor = new FileObserver(root.getPath()) { - @Override - public void onEvent(int evt, String path) { - commonHandler(evt, path); - } - }; - - File download = new File(root, DOWNLOAD_DIR); - mDownloadMonitor = new FileObserver(download.getPath()) { - @Override - public void onEvent(int evt, String path) { - commonHandler(evt, path); - } - }; - } - - private void commonHandler(int evt, String path) { - switch (evt) { - case FileObserver.CREATE: - case FileObserver.DELETE: - if (isFileAcceptable(path)) { - runOnUiThread(new Runnable() { - public void run() { - createFileList(); - } - }); - } - break; - } - }; - - void startWatching() { - mRootMonitor.startWatching(); - mDownloadMonitor.startWatching(); - } - - void stopWatching() { - mRootMonitor.stopWatching(); - mDownloadMonitor.stopWatching(); - } - } -} diff --git a/src/com/android/certinstaller/CertInstallerMain.java b/src/com/android/certinstaller/CertInstallerMain.java index 9b10c07..c83e99f 100644 --- a/src/com/android/certinstaller/CertInstallerMain.java +++ b/src/com/android/certinstaller/CertInstallerMain.java @@ -19,46 +19,46 @@ package com.android.certinstaller; import android.content.Intent; import android.net.Uri; import android.os.Bundle; +import android.preference.PreferenceActivity; +import android.provider.DocumentsContract; import android.security.Credentials; import android.security.KeyChain; import android.util.Log; import android.widget.Toast; -import java.io.ByteArrayOutputStream; -import java.io.File; +import libcore.io.IoUtils; +import libcore.io.Streams; + import java.io.IOException; import java.io.InputStream; -import java.util.List; - -import libcore.io.IoUtils; /** * The main class for installing certificates to the system keystore. It reacts * to the public {@link Credentials#INSTALL_ACTION} intent. */ -public class CertInstallerMain extends CertFile implements Runnable { +public class CertInstallerMain extends PreferenceActivity { + private static final String TAG = "CertInstaller"; + + private static final int REQUEST_INSTALL = 1; + private static final int REQUEST_OPEN_DOCUMENT = 2; + + private static final String[] ACCEPT_MIME_TYPES = { + "application/x-pkcs12", + "application/x-x509-ca-cert", + "application/x-x509-user-cert", + "application/x-x509-server-cert", + "application/x-pem-file", + "application/pkix-cert" + }; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if (savedInstanceState != null) { - return; - } - new Thread(new Runnable() { - @Override - public void run() { - // don't want to call startActivityForResult() (invoked in - // installFromFile()) here as it makes the new activity (thus - // the whole display) get stuck for about 5 seconds - runOnUiThread(CertInstallerMain.this); - } - }).start(); - } + setResult(RESULT_CANCELED); - @Override - public void run() { - Intent intent = getIntent(); - String action = (intent == null) ? null : intent.getAction(); + final Intent intent = getIntent(); + final String action = intent.getAction(); if (Credentials.INSTALL_ACTION.equals(action) || Credentials.INSTALL_AS_USER_ACTION.equals(action)) { @@ -74,7 +74,7 @@ public class CertInstallerMain extends CertFile implements Runnable { bundle.remove(Credentials.EXTRA_INSTALL_AS_UID); } - // If bundle is empty of any actual credentials, install from external storage. + // If bundle is empty of any actual credentials, ask user to open. // Otherwise, pass extras to CertInstaller to install those credentials. // Either way, we use KeyChain.EXTRA_NAME as the default name if available. if (bundle == null @@ -82,82 +82,71 @@ public class CertInstallerMain extends CertFile implements Runnable { || (bundle.size() == 1 && (bundle.containsKey(KeyChain.EXTRA_NAME) || bundle.containsKey(Credentials.EXTRA_INSTALL_AS_UID)))) { - if (!isSdCardPresent()) { - Toast.makeText(this, R.string.sdcard_not_present, - Toast.LENGTH_SHORT).show(); - } else { - List<File> allFiles = getAllCertFiles(); - if (allFiles.isEmpty()) { - Toast.makeText(this, R.string.no_cert_file_found, - Toast.LENGTH_SHORT).show(); - } else if (allFiles.size() == 1) { - installFromFile(allFiles.get(0)); - return; - } else { - Intent newIntent = new Intent(this, CertFileList.class); - newIntent.putExtras(intent); - startActivityForResult(newIntent, REQUEST_INSTALL_CODE); - return; - } - } + final Intent openIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT); + openIntent.setType("*/*"); + openIntent.putExtra(Intent.EXTRA_MIME_TYPES, ACCEPT_MIME_TYPES); + openIntent.putExtra(DocumentsContract.EXTRA_SHOW_ADVANCED, true); + startActivityForResult(openIntent, REQUEST_OPEN_DOCUMENT); } else { - Intent newIntent = new Intent(this, CertInstaller.class); - newIntent.putExtras(intent); - startActivityForResult(newIntent, REQUEST_INSTALL_CODE); - return; + final Intent installIntent = new Intent(this, CertInstaller.class); + installIntent.putExtras(intent); + startActivityForResult(installIntent, REQUEST_INSTALL); } } else if (Intent.ACTION_VIEW.equals(action)) { - Uri data = intent.getData(); - String type = intent.getType(); - if ((data != null) && (type != null)) { - byte[] payload = null; - InputStream is = null; - try { - is = getContentResolver().openInputStream(data); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; - int read = 0; - while ((read = is.read(buffer)) > 0) { - out.write(buffer, 0, read); - } - out.flush(); - payload = out.toByteArray(); - } catch (IOException ignored) { - // Not much we can do - it will be logged below as an error. - } finally { - IoUtils.closeQuietly(is); - } - if (payload == null) { - Log.e("CertInstaller", "Unable to read stream for for certificate"); - } else { - installByType(type, payload); - } - } + startInstallActivity(intent.getType(), intent.getData()); } - finish(); } - private void installByType(String type, byte[] value) { + private void startInstallActivity(String mimeType, Uri uri) { + if (mimeType == null) { + mimeType = getContentResolver().getType(uri); + } + + InputStream in = null; + try { + in = getContentResolver().openInputStream(uri); + + final byte[] raw = Streams.readFully(in); + startInstallActivity(mimeType, raw); + + } catch (IOException e) { + Log.e(TAG, "Failed to read certificate: " + e); + Toast.makeText(this, R.string.cert_read_error, Toast.LENGTH_LONG).show(); + } finally { + IoUtils.closeQuietly(in); + } + } + + private void startInstallActivity(String mimeType, byte[] value) { Intent intent = new Intent(this, CertInstaller.class); - if ("application/x-pkcs12".equals(type)) { + if ("application/x-pkcs12".equals(mimeType)) { intent.putExtra(KeyChain.EXTRA_PKCS12, value); - } else if ("application/x-x509-ca-cert".equals(type) - || "application/x-x509-user-cert".equals(type)) { + } else if ("application/x-x509-ca-cert".equals(mimeType) + || "application/x-x509-user-cert".equals(mimeType) + || "application/x-x509-server-cert".equals(mimeType) + || "application/x-pem-file".equals(mimeType) + || "application/pkix-cert".equals(mimeType)) { intent.putExtra(KeyChain.EXTRA_CERTIFICATE, value); } else { - throw new AssertionError("Unknown type: " + type); + throw new IllegalArgumentException("Unknown MIME type: " + mimeType); } - startActivityForResult(intent, REQUEST_INSTALL_CODE); - } - @Override - protected void onInstallationDone(boolean success) { - super.onInstallationDone(success); - finish(); + startActivityForResult(intent, REQUEST_INSTALL); } @Override - protected void onError(int errorId) { - finish(); + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == REQUEST_OPEN_DOCUMENT) { + if (resultCode == RESULT_OK) { + startInstallActivity(null, data.getData()); + } else { + finish(); + } + } else if (requestCode == REQUEST_INSTALL) { + setResult(resultCode); + finish(); + } else { + Log.w(TAG, "unknown request code: " + requestCode); + } } } |