CertFileList.java revision 00736f76392c742e9c72c51f158ad7020f22524c
1/*
2 * Copyright (C) 2009 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.certinstaller;
18
19import android.os.Bundle;
20import android.os.Environment;
21import android.os.FileObserver;
22import android.preference.Preference;
23import android.preference.PreferenceScreen;
24import android.util.Log;
25import android.widget.Toast;
26
27import java.io.File;
28import java.io.IOException;
29import java.util.List;
30
31/**
32 * Lists certificate files in the SD card. User may click one to install it
33 * to the system keystore.
34 */
35public class CertFileList extends CertFile
36        implements Preference.OnPreferenceClickListener {
37    private static final String TAG = "CertFileList";
38
39    private static final String DOWNLOAD_DIR = "download";
40
41    private SdCardMonitor mSdCardMonitor;
42
43    @Override
44    protected void onCreate(Bundle savedInstanceState) {
45        super.onCreate(savedInstanceState);
46
47        addPreferencesFromResource(R.xml.pick_file_pref);
48        createFileList();
49        startSdCardMonitor();
50    }
51
52    @Override
53    protected void onDestroy() {
54        super.onDestroy();
55        stopSdCardMonitor();
56    }
57
58    @Override
59    protected void onInstallationDone(boolean fileDeleted) {
60        super.onInstallationDone(fileDeleted);
61        if (!fileDeleted) {
62            if (isSdCardPresent()) {
63                setAllFilesEnabled(true);
64            } else {
65                Toast.makeText(this, R.string.sdcard_not_present,
66                        Toast.LENGTH_SHORT).show();
67                finish();
68            }
69        }
70    }
71
72    @Override
73    protected void onError(int errorId) {
74        if (errorId == CERT_FILE_MISSING_ERROR) {
75            createFileList();
76        }
77    }
78
79    private void setAllFilesEnabled(boolean enabled) {
80        PreferenceScreen root = getPreferenceScreen();
81        for (int i = 0, n = root.getPreferenceCount(); i < n; i++) {
82            root.getPreference(i).setEnabled(enabled);
83        }
84    }
85
86    public boolean onPreferenceClick(Preference pref) {
87        File file = new File(Environment.getExternalStorageDirectory(),
88                pref.getTitle().toString());
89        if (file.isDirectory()) {
90            Log.w(TAG, "impossible to pick a directory! " + file);
91        } else {
92            setAllFilesEnabled(false);
93            installFromFile(file);
94        }
95        return true;
96    }
97
98    private void createFileList() {
99        if (isFinishing()) {
100            Log.d(TAG, "finishing, exit createFileList()");
101            return;
102        }
103        if (!isSdCardPresent()) {
104            Toast.makeText(this, R.string.sdcard_not_present,
105                    Toast.LENGTH_SHORT).show();
106            finish();
107            return;
108        }
109
110        try {
111            PreferenceScreen root = getPreferenceScreen();
112            root.removeAll();
113
114            List<File> allFiles = getAllCertFiles();
115            if (allFiles.isEmpty()) {
116                Toast.makeText(this, R.string.no_cert_file_found,
117                        Toast.LENGTH_SHORT).show();
118            } else {
119                int prefixEnd = Environment.getExternalStorageDirectory()
120                        .getCanonicalPath().length() + 1;
121                for (File file : allFiles) {
122                    Preference pref = new Preference(this);
123                    pref.setTitle(file.getCanonicalPath().substring(prefixEnd));
124                    root.addPreference(pref);
125                    pref.setOnPreferenceClickListener(this);
126                }
127            }
128        } catch (IOException e) {
129            // should not occur
130            Log.w(TAG, "createFileList(): " + e);
131            throw new RuntimeException(e);
132        }
133    }
134
135    private void startSdCardMonitor() {
136        if (mSdCardMonitor == null) {
137            mSdCardMonitor = new SdCardMonitor();
138        }
139        mSdCardMonitor.startWatching();
140    }
141
142    private void stopSdCardMonitor() {
143        if (mSdCardMonitor != null) {
144            mSdCardMonitor.stopWatching();
145        }
146    }
147
148    private class SdCardMonitor {
149        FileObserver mRootMonitor;
150        FileObserver mDownloadMonitor;
151
152        SdCardMonitor() {
153            File root = Environment.getExternalStorageDirectory();
154            mRootMonitor = new FileObserver(root.getPath()) {
155                @Override
156                public void onEvent(int evt, String path) {
157                    commonHandler(evt, path);
158                }
159            };
160
161            File download = new File(root, DOWNLOAD_DIR);
162            mDownloadMonitor = new FileObserver(download.getPath()) {
163                @Override
164                public void onEvent(int evt, String path) {
165                    commonHandler(evt, path);
166                }
167            };
168        }
169
170        private void commonHandler(int evt, String path) {
171            switch (evt) {
172                case FileObserver.CREATE:
173                case FileObserver.DELETE:
174                    if (isFileAcceptable(path)) {
175                        runOnUiThread(new Runnable() {
176                            public void run() {
177                                createFileList();
178                            }
179                        });
180                    }
181                    break;
182            }
183        };
184
185        void startWatching() {
186            mRootMonitor.startWatching();
187            mDownloadMonitor.startWatching();
188        }
189
190        void stopWatching() {
191            mRootMonitor.stopWatching();
192            mDownloadMonitor.stopWatching();
193        }
194    }
195}
196