Ignore:
Timestamp:
Oct 22, 2013, 3:38:40 PM (11 years ago)
Author:
Jose Ruiz <joseruiz@…>
Branches:
master
Children:
f907928
Parents:
95d372e
Message:

Manejo de repositorio de certificados y firma con pkcs7

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TibisayMovil/src/ve/gob/cenditel/tibisaymovil/CertificateRepositoryActivity.java

    r09c6eb7 r288126d  
    11package ve.gob.cenditel.tibisaymovil;
    2 
    3 
    4 
    5 
    62
    73import android.app.Activity;
    84import android.content.Context;
     5import android.content.DialogInterface;
    96import android.content.Intent;
     7import android.content.DialogInterface.OnCancelListener;
     8import android.content.DialogInterface.OnDismissListener;
     9import android.graphics.Typeface;
     10import android.graphics.drawable.Drawable;
    1011import android.os.Bundle;
     12import android.util.SparseBooleanArray;
     13import android.view.LayoutInflater;
    1114import android.view.View;
     15import android.view.ViewGroup;
    1216import android.view.View.OnClickListener;
    1317import android.widget.AdapterView;
    14 import android.widget.Button;
     18import android.widget.LinearLayout;
     19import android.widget.ListView;
     20import android.widget.TextView;
    1521import android.widget.Toast;
    1622import android.widget.AdapterView.OnItemClickListener;
     23import android.widget.LinearLayout.LayoutParams;
    1724
    1825public class CertificateRepositoryActivity extends Activity implements OnClickListener, OnItemClickListener {
    1926       
    2027       
    21        
     28
     29    public DirectKeyChain keyChain;
     30    public KeyChainView viewHolder;
     31        protected int requestCode;
     32        protected int resultCode;
     33        private int numChecks;
     34        private boolean buttonsEnabled;
     35
     36
    2237        @Override
    2338        public void onCreate(Bundle savedInstanceState) {
    2439
    2540                super.onCreate(savedInstanceState);
    26                 this.setContentView(R.layout.certificate_chooser);
    27                
    28                
    29                 findViewById(R.id.cert_chooser_install_button).setOnClickListener(mGlobal_OnClickListener);
     41                this.setContentView(R.layout.certificate_zone);         
     42
     43        LinearLayout certZone = (LinearLayout) findViewById(R.id.certificate_zone);
     44                this.viewHolder = new KeyChainView(this, certZone);
     45                this.prepareKeyChain();
     46                this.viewHolder.delete.setEnabled(this.numChecks>0);
     47                this.buttonsEnabled = false;
     48               
    3049               
    3150               
     
    3352        }
    3453       
    35         //On click listener for button1
    36     final OnClickListener mGlobal_OnClickListener = new OnClickListener() {
    37         public void onClick(final View v) {
    38                
    39                 switch(v.getId()) {
    40                         case R.id.cert_chooser_install_button:
    41                                 Toast.makeText(CertificateRepositoryActivity.this, "install", Toast.LENGTH_SHORT).show();
    42                                 Intent intent = new Intent(CertificateRepositoryActivity.this.getBaseContext(), PKCS12FilePickerActivity.class);
    43                                 startActivity(intent);
    44                                 break;
    45                                
    46                 }
    47                
    48                
    49                            
    50         }
    51     };
    52 
    53 
    54        
     54        //prepara el llavero
     55        private void prepareKeyChain() {
     56               
     57                this.keyChain = DirectKeyChain.getInstance();
     58                this.keyChain.setListView(this.viewHolder.aliases);
     59                this.keyChain.setMasterActivity((Activity) this);
     60                this.keyChain.setButtonsKeyStoreStatus(this.buttonsEnabled);
     61                this.keyChain.choosePrivateKeyAlias();
     62               
     63//        try {
     64//              if (this.viewHolder.aliases.getAdapter()!=null)
     65//                      this.viewHolder.aliases.setAdapter(new DirectCertificateAdapter(this.keyChain, CertificateRepositoryActivity.this));
     66//        } catch (KeystoreException e) {
     67//            Toast.makeText(CertificateRepositoryActivity.this, R.string.error_unknown, Toast.LENGTH_LONG).show();
     68//            //throw new RuntimeException(e);
     69//        }     
     70        }       
     71       
     72       
     73
     74       
     75
    5576        @Override
    5677    public void onClick(View view) {
    5778
    58                 Context context = getApplicationContext();
    59                 //CharSequence text = "Click!";
    60                 int duration = Toast.LENGTH_SHORT;
    61                
    62                 Toast.makeText(context, "**onClick()**", duration).show();
    63                
    64                
    6579        switch (view.getId()) {
    6680
    6781        case R.id.cert_chooser_install_button:
    68             Intent intent = new Intent(context, PKCS12FilePickerActivity.class);
    69             Toast.makeText(context, "cert_chooser_install_button", duration).show();
    70            
     82                if(this.keyChain.isButtonsKeyStoreStatus()) {
     83                        Intent intent = new Intent(this, PKCS12FilePickerActivity.class);           
     84                        ((Activity) this).startActivityForResult(intent, ActivityResult.IMPORT_CERTIFICATE);
     85                } else {
     86                Toast.makeText(CertificateRepositoryActivity.this, R.string.no_access_to_keystore, Toast.LENGTH_LONG).show();
     87                }
    7188            break;
    7289
    7390        case R.id.button_delete:
    74                 Toast.makeText(context, "button_delete", duration).show();
     91                if (this.numChecks>0)
     92                        deleteSelectedCertificates();
    7593            break;
    7694           
    7795        case R.id.button_open:
    78                 Toast.makeText(context, "button_open", duration).show();
     96                if(this.keyChain.isButtonsKeyStoreStatus()) {
     97                //Crear nuevo almacen.
     98                        this.keyChain.createKeystoreAndChoosePrivateKeyAlias();
     99                } else {
     100                Toast.makeText(CertificateRepositoryActivity.this, R.string.no_access_to_keystore, Toast.LENGTH_LONG).show();
     101                }
    79102                break;
    80103               
    81104        case R.id.change_password:
    82                 Toast.makeText(context, "change_password", duration).show();
     105                if(this.keyChain.isButtonsKeyStoreStatus()) {
     106                        this.changePassword();
     107                } else {
     108                Toast.makeText(CertificateRepositoryActivity.this, R.string.no_access_to_keystore, Toast.LENGTH_LONG).show();
     109                }
    83110            break;
    84111        }
    85112    }
    86113       
     114
     115    public void changePassword() {
     116       
     117        final PasswordConfirmationDialog dialog =
     118                new PasswordConfirmationDialog((Activity) this, R.string.keystore_new_password);
     119        dialog.setAcceptListener(new OnClickListener() {
     120
     121            @Override
     122            public void onClick(View v) {
     123                String newPassword = dialog.getPassword();
     124                dialog.dismiss();
     125                try {
     126                    CertificateRepositoryActivity.this.keyChain.saveKeystore(newPassword);
     127                    Toast.makeText(CertificateRepositoryActivity.this, R.string.password_change_success, Toast.LENGTH_LONG).show();
     128                } catch (OutOfSpaceError e) {
     129                    Toast.makeText(CertificateRepositoryActivity.this, R.string.error_no_space, Toast.LENGTH_LONG).show();
     130                } catch (KeystoreException e) {
     131                        Toast.makeText(CertificateRepositoryActivity.this, R.string.error_unknown, Toast.LENGTH_LONG).show();
     132                    //throw new RuntimeException(e);
     133                }
     134            }
     135        });
     136        dialog.show();
     137    }
     138   
    87139        @Override
    88140    public void onItemClick(AdapterView<?> parent, View view, int item, long id) {
    89                 Context context = getApplicationContext();
    90                 //CharSequence text = "Click!";
    91                 int duration = Toast.LENGTH_SHORT;
    92                 Toast.makeText(context, "**onItemClick()**", duration).show();
     141        this.updateCheckedNumber();
     142        this.updateButtons();
     143    }
     144
     145        private void updateButtons() {
     146                this.viewHolder.delete.setEnabled(this.numChecks>0);
     147                //this.viewHolder.accept.setEnabled(this.numChecks==1);
     148                Drawable icon = this.viewHolder.delete.getCompoundDrawables()[1];
     149                if (this.numChecks>0)
     150                        icon.setAlpha(255);
     151                else
     152                        icon.setAlpha(127);
     153               
     154        }
     155
     156    private void updateCheckedNumber() {
     157                SparseBooleanArray checkedItems = this.viewHolder.aliases.getCheckedItemPositions();
     158                int count = 0;
     159                for (int i = 0; i < checkedItems.size(); i++) {
     160                                if (checkedItems.valueAt(i))
     161                                        count++;
     162                }
     163                this.numChecks = count;         
     164        }
     165   
     166
     167   
     168        private void deleteSelectedCertificates() {
     169
     170                SparseBooleanArray checkedItems = this.viewHolder.aliases.getCheckedItemPositions();
     171               
     172                for (int i = 0; i < checkedItems.size(); i++) {
     173                        if(checkedItems.valueAt(i) == true){
     174                               
     175                                try {
     176                                        int position = checkedItems.keyAt(i);
     177                                        this.keyChain.deleteCertificate(
     178                                                        (String) this.viewHolder.aliases.getItemAtPosition(position));
     179                                } catch (KeystoreException e) {
     180                                        e.printStackTrace();
     181                                }
     182                        }
     183                }
     184       
     185                //Refresh adapter. NotifyDataSetChange didnt work.
     186                try {
     187                        CertificateRepositoryActivity.this.viewHolder.aliases.setAdapter(
     188                                new DirectCertificateAdapter(CertificateRepositoryActivity.this.keyChain, this));
     189                } catch (KeystoreException e) {
     190                        e.printStackTrace();
     191                }
     192                this.updateCheckedNumber();
     193                this.updateButtons();
     194               
     195                try {
     196                        this.keyChain.saveKeystore();
     197                } catch (KeystoreException e) {
     198                } catch (OutOfSpaceError e) {
     199                }
     200               
     201        }       
     202
     203   
     204    @Override
     205    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
     206               
     207        if (requestCode == ActivityResult.IMPORT_CERTIFICATE && resultCode == Activity.RESULT_OK) {
     208            this.launchPasswordDialog(data);
     209        }
     210    }
     211   
     212
     213
     214    private void launchPasswordDialog(final Intent data) {
     215       
     216        final PasswordDialog dialog = new PasswordDialog(this, R.string.certificate_password, false);
     217        dialog.setAcceptListener(new OnClickListener() {
     218
     219            @Override
     220            public void onClick(View v) {
     221
     222                try {
     223                        DirectKeyChain direct = DirectKeyChain.getInstance();
     224                    int imported = direct.importCertificate(data.getData(), dialog.getPassword());
     225                    String text = CertificateRepositoryActivity.this.getResources().getQuantityString(R.plurals.number_imported_certificates, imported, imported);
     226                    Toast.makeText(CertificateRepositoryActivity.this, text, Toast.LENGTH_LONG).show();
     227                    direct.saveKeystore();
     228                    direct.refreshAdapter();
     229                   
     230                } catch (IncorrectPasswordException e) {
     231                    Toast toast = Toast.makeText(CertificateRepositoryActivity.this, R.string.incorrect_password, Toast.LENGTH_LONG);
     232                    toast.show();
     233                } catch (Exception e) {
     234                    Toast toast = Toast.makeText(CertificateRepositoryActivity.this, R.string.error_reading_p12, Toast.LENGTH_LONG);
     235                    toast.show();
     236                }
     237               
     238             
     239               
     240                dialog.dismiss();
     241                CertificateRepositoryActivity.this.requestCode=Integer.MIN_VALUE;
     242                CertificateRepositoryActivity.this.resultCode=Integer.MIN_VALUE;
     243            }
     244        });
     245               
     246       
     247       dialog.setOnDismissListener(new OnDismissListener(){
     248
     249                        @Override
     250                        public void onDismiss(DialogInterface arg0) {
     251                                CertificateRepositoryActivity.this.requestCode=Integer.MIN_VALUE;
     252                                CertificateRepositoryActivity.this.resultCode=Integer.MIN_VALUE;                       
     253                }});
     254       
     255       
     256       
     257       
     258        dialog.setOnCancelListener(new OnCancelListener(){
     259
     260                        @Override
     261                        public void onCancel(DialogInterface dialog) {
     262                                CertificateRepositoryActivity.this.requestCode=Integer.MIN_VALUE;
     263                                CertificateRepositoryActivity.this.resultCode=Integer.MIN_VALUE;                               
     264                        }});
     265     
     266 
    93267       
    94     }
    95        
    96        
     268       
     269        dialog.show();
     270       
     271    }
     272       
     273        /*********************** Otras clases ********************/
     274    private class KeyChainView {
     275        public ViewGroup parent;
     276        public View generalView;
     277        public ListView aliases;
     278        public TextView install;
     279       // public Button accept;
     280       // public Button cancel;
     281        public TextView delete;
     282                private Context context;
     283                private TextView open;
     284                private TextView change;
     285                private LinearLayout tempLayout;
     286
     287        public KeyChainView(Context context, ViewGroup parent) {
     288                this.context=context;
     289                this.parent=parent;
     290               
     291                this.tempLayout = new LinearLayout(context);
     292                tempLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
     293               
     294                LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     295                this.generalView = inflater.inflate(R.layout.certificate_chooser, tempLayout, true);
     296               
     297                TextView description1 = (TextView) this.generalView.findViewById(R.id.cert_description);
     298                description1.setTypeface(Typeface.createFromAsset(this.context.getAssets(), "fonts/Roboto-Medium.ttf"), Typeface.BOLD);
     299
     300            this.aliases = (ListView) this.generalView.findViewById(R.id.cert_chooser);
     301            this.aliases.setOnItemClickListener(CertificateRepositoryActivity.this);
     302
     303            this.install = (TextView) this.generalView.findViewById(R.id.cert_chooser_install_button);
     304            this.install.setOnClickListener(CertificateRepositoryActivity.this);
     305           
     306            this.delete = (TextView) this.generalView.findViewById(R.id.button_delete);
     307            this.delete.setOnClickListener(CertificateRepositoryActivity.this);   
     308           
     309            this.open = (TextView) this.generalView.findViewById(R.id.button_open);
     310            this.open.setOnClickListener(CertificateRepositoryActivity.this); 
     311           
     312            this.change = (TextView) this.generalView.findViewById(R.id.change_password);
     313            this.change.setOnClickListener(CertificateRepositoryActivity.this); 
     314           
     315            /*              this.accept = (Button) this.generalView.findViewById(R.id.button_accept);
     316            this.cancel = (Button) this.generalView.findViewById(R.id.button_cancel);
     317            this.accept.setVisibility(View.GONE);
     318            this.cancel.setVisibility(View.GONE);*/
     319           
     320           
     321            this.parent.addView(tempLayout);
     322           
     323         
     324        }
     325    }   
    97326}
Note: See TracChangeset for help on using the changeset viewer.