source: dispositivos_moviles/TibisayMovil/src/ve/gob/cenditel/tibisaymovil/CertificateRepositoryActivity.java @ 288126d

Last change on this file since 288126d was 288126d, checked in by Jose Ruiz <joseruiz@…>, 11 years ago

Manejo de repositorio de certificados y firma con pkcs7

  • Property mode set to 100644
File size: 11.3 KB
Line 
1package ve.gob.cenditel.tibisaymovil;
2
3import android.app.Activity;
4import android.content.Context;
5import android.content.DialogInterface;
6import android.content.Intent;
7import android.content.DialogInterface.OnCancelListener;
8import android.content.DialogInterface.OnDismissListener;
9import android.graphics.Typeface;
10import android.graphics.drawable.Drawable;
11import android.os.Bundle;
12import android.util.SparseBooleanArray;
13import android.view.LayoutInflater;
14import android.view.View;
15import android.view.ViewGroup;
16import android.view.View.OnClickListener;
17import android.widget.AdapterView;
18import android.widget.LinearLayout;
19import android.widget.ListView;
20import android.widget.TextView;
21import android.widget.Toast;
22import android.widget.AdapterView.OnItemClickListener;
23import android.widget.LinearLayout.LayoutParams;
24
25public class CertificateRepositoryActivity extends Activity implements OnClickListener, OnItemClickListener {
26       
27       
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
37        @Override
38        public void onCreate(Bundle savedInstanceState) {
39
40                super.onCreate(savedInstanceState);
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               
49               
50               
51               
52        }
53       
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
76        @Override
77    public void onClick(View view) {
78
79        switch (view.getId()) {
80
81        case R.id.cert_chooser_install_button:
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                }
88            break;
89
90        case R.id.button_delete:
91                if (this.numChecks>0)
92                        deleteSelectedCertificates();
93            break;
94           
95        case R.id.button_open:
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                }
102                break;
103               
104        case R.id.change_password:
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                }
110            break;
111        }
112    }
113       
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   
139        @Override
140    public void onItemClick(AdapterView<?> parent, View view, int item, long id) {
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 
267       
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    }   
326}
Note: See TracBrowser for help on using the repository browser.