source: dispositivos_moviles/TibisayMovil/src/ve/gob/cenditel/tibisaymovil/CertificateRepositoryActivity.java @ 3fe55cf

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

Modificacion de menu de repositorio

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