Home | History | Annotate | Download | only in certinstaller
      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 
     17 package com.android.certinstaller;
     18 
     19 import android.content.Intent;
     20 import android.os.Bundle;
     21 import android.security.Credentials;
     22 import android.widget.Toast;
     23 
     24 import java.io.File;
     25 import java.util.List;
     26 
     27 /**
     28  * The main class for installing certificates to the system keystore. It reacts
     29  * to the public {@link Credentials#INSTALL_ACTION} intent.
     30  */
     31 public class CertInstallerMain extends CertFile implements Runnable {
     32     @Override
     33     protected void onCreate(Bundle savedInstanceState) {
     34         super.onCreate(savedInstanceState);
     35         if (savedInstanceState != null) return;
     36 
     37         new Thread(new Runnable() {
     38             public void run() {
     39                 // don't want to call startActivityForResult() (invoked in
     40                 // installFromFile()) here as it makes the new activity (thus
     41                 // the whole display) get stuck for about 5 seconds
     42                 runOnUiThread(CertInstallerMain.this);
     43             }
     44         }).start();
     45     }
     46 
     47     public void run() {
     48         Intent intent = getIntent();
     49         String action = (intent == null) ? null : intent.getAction();
     50 
     51         if (Credentials.INSTALL_ACTION.equals(action)) {
     52             Bundle bundle = intent.getExtras();
     53 
     54             if ((bundle == null) || bundle.isEmpty()) {
     55                 if (!isSdCardPresent()) {
     56                     Toast.makeText(this, R.string.sdcard_not_present,
     57                             Toast.LENGTH_SHORT).show();
     58                 } else {
     59                     List<File> allFiles = getAllCertFiles();
     60                     if (allFiles.isEmpty()) {
     61                         Toast.makeText(this, R.string.no_cert_file_found,
     62                                 Toast.LENGTH_SHORT).show();
     63                     } else if (allFiles.size() == 1) {
     64                         installFromFile(allFiles.get(0));
     65                         return;
     66                     } else {
     67                         startActivity(new Intent(this, CertFileList.class));
     68                     }
     69                 }
     70             } else {
     71                 Intent newIntent = new Intent(this, CertInstaller.class);
     72                 newIntent.putExtras(intent);
     73                 startActivity(newIntent);
     74             }
     75         }
     76         finish();
     77     }
     78 
     79     @Override
     80     protected void onInstallationDone(boolean success) {
     81         finish();
     82     }
     83 
     84     @Override
     85     protected void onError(int errorId) {
     86         finish();
     87     }
     88 }
     89