source: dispositivos_moviles/TibisayMovil/src/ve/gob/cenditel/tibisaymovil/CertificateRepositoryActivity.java @ 42e7061

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

Corrección de error en el repositorio de certificados 'Sin acceso al repositorio'

  • Property mode set to 100644
File size: 12.0 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.util.Log;
15import android.view.View;
16import android.view.ViewGroup;
17import android.view.View.OnClickListener;
18import android.widget.AdapterView;
19import android.widget.LinearLayout;
20import android.widget.ListView;
21import android.widget.TextView;
22import android.widget.Toast;
23import android.widget.AdapterView.OnItemClickListener;
24import android.widget.LinearLayout.LayoutParams;
25
26public class CertificateRepositoryActivity extends Activity implements OnClickListener, OnItemClickListener {
27       
28       
29
30    public DirectKeyChain keyChain;
31    public KeyChainView viewHolder;
32        protected int requestCode;
33        protected int resultCode;
34        private int numChecks;
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               
48               
49               
50               
51        }
52       
53        //prepara el llavero
54        private void prepareKeyChain() {
55               
56                this.keyChain = DirectKeyChain.getInstance();
57                this.keyChain.setListView(this.viewHolder.aliases);
58                this.keyChain.setMasterActivity((Activity) this);
59                this.keyChain.setButtonsKeyStoreStatus(false);
60                this.keyChain.choosePrivateKeyAlias();
61               
62//        try {
63//              if (this.viewHolder.aliases.getAdapter()!=null)
64//                      this.viewHolder.aliases.setAdapter(new DirectCertificateAdapter(this.keyChain, CertificateRepositoryActivity.this));
65//        } catch (KeystoreException e) {
66//            Toast.makeText(CertificateRepositoryActivity.this, R.string.error_unknown, Toast.LENGTH_LONG).show();
67//            //throw new RuntimeException(e);
68//        }     
69        }       
70       
71       
72        //On click listener for button1
73    final OnClickListener mGlobal_OnClickListener = new OnClickListener() {
74        public void onClick(final View v) {
75               
76                switch(v.getId()) {
77                        case R.id.cert_chooser_install_button:
78                                Toast.makeText(CertificateRepositoryActivity.this, "install", Toast.LENGTH_SHORT).show();
79                                Intent intent = new Intent(CertificateRepositoryActivity.this.getBaseContext(), PKCS12FilePickerActivity.class);
80                                startActivity(intent);
81                                break;
82                               
83                }
84               
85               
86                           
87        }
88    };
89
90    @Override
91    protected void onActivityResult(int requestCode, int resultCode,
92            Intent data) {
93       
94        if (requestCode == ActivityResult.IMPORT_CERTIFICATE && resultCode == Activity.RESULT_OK) {
95            this.launchPasswordDialog(data);
96        }
97       if (resultCode == RESULT_OK) {
98           
99           //startActivity(new Intent(Intent.ACTION_VIEW, data));
100           Log.d("CertificateRepositoryActivity: ", "onActivityResult -> RESULT_OK");
101           
102        }
103    }
104
105       
106
107        @Override
108    public void onClick(View view) {
109
110        switch (view.getId()) {
111
112        case R.id.cert_chooser_install_button:
113                if(this.keyChain.isButtonsKeyStoreStatus()) {
114                        Intent intent = new Intent(this, PKCS12FilePickerActivity.class);           
115                        ((Activity) this).startActivityForResult(intent, ActivityResult.IMPORT_CERTIFICATE);
116                } else {
117                Toast.makeText(CertificateRepositoryActivity.this, R.string.no_access_to_keystore, Toast.LENGTH_LONG).show();
118                }
119            break;
120
121        case R.id.button_delete:
122                if (this.numChecks>0)
123                        deleteSelectedCertificates();
124            break;
125           
126        case R.id.button_open:
127                if(this.keyChain.isButtonsKeyStoreStatus()) {
128                //Crear nuevo almacen.
129                        this.keyChain.createKeystoreAndChoosePrivateKeyAlias();
130                } else {
131                Toast.makeText(CertificateRepositoryActivity.this, R.string.no_access_to_keystore, Toast.LENGTH_LONG).show();
132                }
133                break;
134               
135        case R.id.change_password:
136                if(this.keyChain.isButtonsKeyStoreStatus()) {
137                        this.changePassword();
138                } else {
139                Toast.makeText(CertificateRepositoryActivity.this, R.string.no_access_to_keystore, Toast.LENGTH_LONG).show();
140                }
141            break;
142        }
143    }
144       
145
146    public void changePassword() {
147       
148        final PasswordConfirmationDialog dialog =
149                new PasswordConfirmationDialog((Activity) this, R.string.keystore_new_password);
150        dialog.setAcceptListener(new OnClickListener() {
151
152            @Override
153            public void onClick(View v) {
154                String newPassword = dialog.getPassword();
155                dialog.dismiss();
156                try {
157                    CertificateRepositoryActivity.this.keyChain.saveKeystore(newPassword);
158                    Toast.makeText(CertificateRepositoryActivity.this, R.string.password_change_success, Toast.LENGTH_LONG).show();
159                } catch (OutOfSpaceError e) {
160                    Toast.makeText(CertificateRepositoryActivity.this, R.string.error_no_space, Toast.LENGTH_LONG).show();
161                } catch (KeystoreException e) {
162                        Toast.makeText(CertificateRepositoryActivity.this, R.string.error_unknown, Toast.LENGTH_LONG).show();
163                    //throw new RuntimeException(e);
164                }
165            }
166        });
167        dialog.show();
168    }
169   
170        @Override
171    public void onItemClick(AdapterView<?> parent, View view, int item, long id) {
172        this.updateCheckedNumber();
173        this.updateButtons();
174    }
175
176        private void updateButtons() {
177                this.viewHolder.delete.setEnabled(this.numChecks>0);
178                //this.viewHolder.accept.setEnabled(this.numChecks==1);
179                Drawable icon = this.viewHolder.delete.getCompoundDrawables()[1];
180                if (this.numChecks>0)
181                        icon.setAlpha(255);
182                else
183                        icon.setAlpha(127);
184               
185        }
186
187    private void updateCheckedNumber() {
188                SparseBooleanArray checkedItems = this.viewHolder.aliases.getCheckedItemPositions();
189                int count = 0;
190                for (int i = 0; i < checkedItems.size(); i++) {
191                                if (checkedItems.valueAt(i))
192                                        count++;
193                }
194                this.numChecks = count;         
195        }
196   
197
198   
199        private void deleteSelectedCertificates() {
200
201                SparseBooleanArray checkedItems = this.viewHolder.aliases.getCheckedItemPositions();
202               
203                for (int i = 0; i < checkedItems.size(); i++) {
204                        if(checkedItems.valueAt(i) == true){
205                               
206                                try {
207                                        int position = checkedItems.keyAt(i);
208                                        this.keyChain.deleteCertificate(
209                                                        (String) this.viewHolder.aliases.getItemAtPosition(position));
210                                } catch (KeystoreException e) {
211                                        e.printStackTrace();
212                                }
213                        }
214                }
215       
216                //Refresh adapter. NotifyDataSetChange didnt work.
217                try {
218                        CertificateRepositoryActivity.this.viewHolder.aliases.setAdapter(
219                                new DirectCertificateAdapter(CertificateRepositoryActivity.this.keyChain, this));
220                } catch (KeystoreException e) {
221                        e.printStackTrace();
222                }
223                this.updateCheckedNumber();
224                this.updateButtons();
225               
226                try {
227                        this.keyChain.saveKeystore();
228                } catch (KeystoreException e) {
229                } catch (OutOfSpaceError e) {
230                }
231               
232        }
233
234    private void launchPasswordDialog(final Intent data) {
235       
236        final PasswordDialog dialog = new PasswordDialog(this, R.string.certificate_password, false);
237        dialog.setAcceptListener(new OnClickListener() {
238
239            @Override
240            public void onClick(View v) {
241
242                try {
243                        DirectKeyChain direct = DirectKeyChain.getInstance();
244                    int imported = direct.importCertificate(data.getData(), dialog.getPassword());
245                    String text = CertificateRepositoryActivity.this.getResources().getQuantityString(R.plurals.number_imported_certificates, imported, imported);
246                    Toast.makeText(CertificateRepositoryActivity.this, text, Toast.LENGTH_LONG).show();
247                    direct.saveKeystore();
248                    direct.refreshAdapter();
249                   
250                } catch (IncorrectPasswordException e) {
251                    Toast toast = Toast.makeText(CertificateRepositoryActivity.this, R.string.incorrect_password, Toast.LENGTH_LONG);
252                    toast.show();
253                } catch (Exception e) {
254                    Toast toast = Toast.makeText(CertificateRepositoryActivity.this, R.string.error_reading_p12, Toast.LENGTH_LONG);
255                    toast.show();
256                }
257               
258             
259               
260                dialog.dismiss();
261                CertificateRepositoryActivity.this.requestCode=Integer.MIN_VALUE;
262                CertificateRepositoryActivity.this.resultCode=Integer.MIN_VALUE;
263            }
264        });
265               
266       
267       dialog.setOnDismissListener(new OnDismissListener(){
268
269                        @Override
270                        public void onDismiss(DialogInterface arg0) {
271                                CertificateRepositoryActivity.this.requestCode=Integer.MIN_VALUE;
272                                CertificateRepositoryActivity.this.resultCode=Integer.MIN_VALUE;                       
273                }});
274       
275       
276       
277       
278        dialog.setOnCancelListener(new OnCancelListener(){
279
280                        @Override
281                        public void onCancel(DialogInterface dialog) {
282                                CertificateRepositoryActivity.this.requestCode=Integer.MIN_VALUE;
283                                CertificateRepositoryActivity.this.resultCode=Integer.MIN_VALUE;                               
284                        }});
285     
286 
287       
288       
289        dialog.show();
290       
291    }
292       
293        /*********************** Otras clases ********************/
294    private class KeyChainView {
295        public ViewGroup parent;
296        public View generalView;
297        public ListView aliases;
298        public TextView install;
299       // public Button accept;
300       // public Button cancel;
301        public TextView delete;
302                private Context context;
303                private TextView open;
304                private TextView change;
305                private LinearLayout tempLayout;
306
307        public KeyChainView(Context context, ViewGroup parent) {
308                this.context=context;
309                this.parent=parent; 
310               
311                this.tempLayout = new LinearLayout(context); 
312                tempLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
313               
314                LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
315                this.generalView = inflater.inflate(R.layout.certificate_chooser, tempLayout, true);
316               
317                TextView description1 = (TextView) this.generalView.findViewById(R.id.cert_description);
318                description1.setTypeface(Typeface.createFromAsset(this.context.getAssets(), "fonts/Roboto-Medium.ttf"), Typeface.BOLD);
319
320            this.aliases = (ListView) this.generalView.findViewById(R.id.cert_chooser);
321            this.aliases.setOnItemClickListener(CertificateRepositoryActivity.this);
322
323            this.install = (TextView) this.generalView.findViewById(R.id.cert_chooser_install_button);
324            this.install.setOnClickListener(CertificateRepositoryActivity.this);
325           
326            this.delete = (TextView) this.generalView.findViewById(R.id.button_delete);
327            this.delete.setOnClickListener(CertificateRepositoryActivity.this);   
328           
329            this.open = (TextView) this.generalView.findViewById(R.id.button_open);
330            this.open.setOnClickListener(CertificateRepositoryActivity.this); 
331           
332            this.change = (TextView) this.generalView.findViewById(R.id.change_password);
333            this.change.setOnClickListener(CertificateRepositoryActivity.this); 
334           
335            /*              this.accept = (Button) this.generalView.findViewById(R.id.button_accept);
336            this.cancel = (Button) this.generalView.findViewById(R.id.button_cancel);
337            this.accept.setVisibility(View.GONE);
338            this.cancel.setVisibility(View.GONE);*/
339           
340           
341            this.parent.addView(tempLayout);
342           
343         
344        }
345    }   
346}
Note: See TracBrowser for help on using the repository browser.