1 | /* |
---|
2 | Tibisay Movil |
---|
3 | |
---|
4 | Copyright (C) 2013 Antonio Araujo (aaraujo@cenditel.gob.ve), Jose Ruiz |
---|
5 | (jruiz@cenditel.gob.ve), Fundacion Centro Nacional de Desarrollo e |
---|
6 | Investigacion en Tecnologias Libres - CENDITEL. |
---|
7 | |
---|
8 | La Fundación CENDITEL concede permiso para usar, copiar, distribuir y/o |
---|
9 | modificar este programa, reconociendo el derecho que la humanidad posee al |
---|
10 | libre acceso al conocimiento, bajo los términos de la licencia de software |
---|
11 | GPL versión 2.0 de la Free Software Foundation. |
---|
12 | |
---|
13 | Este programa se distribuye con la esperanza de que sea util, pero SIN |
---|
14 | NINGUNA GARANTIA; tampoco las implicitas garantias de MERCANTILIDAD o |
---|
15 | ADECUACION A UN PROPOSITO PARTICULAR. |
---|
16 | |
---|
17 | Para mayor información sobre los términos de la licencia ver el archivo |
---|
18 | llamado "gpl-2.0.txt" en ingles. |
---|
19 | */ |
---|
20 | |
---|
21 | |
---|
22 | package ve.gob.cenditel.tibisaymovil; |
---|
23 | |
---|
24 | import android.app.Activity; |
---|
25 | import android.content.Context; |
---|
26 | import android.content.DialogInterface; |
---|
27 | import android.content.Intent; |
---|
28 | import android.content.DialogInterface.OnCancelListener; |
---|
29 | import android.content.DialogInterface.OnDismissListener; |
---|
30 | import android.graphics.Typeface; |
---|
31 | import android.graphics.drawable.Drawable; |
---|
32 | import android.os.Bundle; |
---|
33 | import android.util.SparseBooleanArray; |
---|
34 | import android.view.LayoutInflater; |
---|
35 | import android.util.Log; |
---|
36 | import android.view.View; |
---|
37 | import android.view.ViewGroup; |
---|
38 | import android.view.View.OnClickListener; |
---|
39 | import android.widget.AdapterView; |
---|
40 | import android.widget.LinearLayout; |
---|
41 | import android.widget.ListView; |
---|
42 | import android.widget.TextView; |
---|
43 | import android.widget.Toast; |
---|
44 | import android.widget.AdapterView.OnItemClickListener; |
---|
45 | import android.widget.LinearLayout.LayoutParams; |
---|
46 | |
---|
47 | public class CertificateRepositoryActivity extends Activity implements OnClickListener, OnItemClickListener { |
---|
48 | |
---|
49 | |
---|
50 | |
---|
51 | public DirectKeyChain keyChain; |
---|
52 | public KeyChainView viewHolder; |
---|
53 | protected int requestCode; |
---|
54 | protected int resultCode; |
---|
55 | private int numChecks; |
---|
56 | |
---|
57 | |
---|
58 | @Override |
---|
59 | public void onCreate(Bundle savedInstanceState) { |
---|
60 | |
---|
61 | super.onCreate(savedInstanceState); |
---|
62 | this.setContentView(R.layout.certificate_zone); |
---|
63 | |
---|
64 | LinearLayout certZone = (LinearLayout) findViewById(R.id.certificate_zone); |
---|
65 | this.viewHolder = new KeyChainView(this, certZone); |
---|
66 | this.prepareKeyChain(); |
---|
67 | this.viewHolder.delete.setEnabled(this.numChecks>0); |
---|
68 | |
---|
69 | |
---|
70 | |
---|
71 | |
---|
72 | } |
---|
73 | |
---|
74 | //prepara el llavero |
---|
75 | private void prepareKeyChain() { |
---|
76 | |
---|
77 | this.keyChain = DirectKeyChain.getInstance(); |
---|
78 | this.keyChain.setListView(this.viewHolder.aliases); |
---|
79 | this.keyChain.setMasterActivity((Activity) this); |
---|
80 | this.keyChain.setButtonsKeyStoreStatus(false); |
---|
81 | this.keyChain.choosePrivateKeyAlias(); |
---|
82 | |
---|
83 | // try { |
---|
84 | // if (this.viewHolder.aliases.getAdapter()!=null) |
---|
85 | // this.viewHolder.aliases.setAdapter(new DirectCertificateAdapter(this.keyChain, CertificateRepositoryActivity.this)); |
---|
86 | // } catch (KeystoreException e) { |
---|
87 | // Toast.makeText(CertificateRepositoryActivity.this, R.string.error_unknown, Toast.LENGTH_LONG).show(); |
---|
88 | // //throw new RuntimeException(e); |
---|
89 | // } |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | //On click listener for button1 |
---|
94 | final OnClickListener mGlobal_OnClickListener = new OnClickListener() { |
---|
95 | public void onClick(final View v) { |
---|
96 | |
---|
97 | switch(v.getId()) { |
---|
98 | case R.id.cert_chooser_install_button: |
---|
99 | Toast.makeText(CertificateRepositoryActivity.this, "install", Toast.LENGTH_SHORT).show(); |
---|
100 | Intent intent = new Intent(CertificateRepositoryActivity.this.getBaseContext(), PKCS12FilePickerActivity.class); |
---|
101 | startActivity(intent); |
---|
102 | break; |
---|
103 | |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | } |
---|
109 | }; |
---|
110 | |
---|
111 | @Override |
---|
112 | protected void onActivityResult(int requestCode, int resultCode, |
---|
113 | Intent data) { |
---|
114 | |
---|
115 | if (requestCode == ActivityResult.IMPORT_CERTIFICATE && resultCode == Activity.RESULT_OK) { |
---|
116 | this.launchPasswordDialog(data); |
---|
117 | } |
---|
118 | if (resultCode == RESULT_OK) { |
---|
119 | |
---|
120 | //startActivity(new Intent(Intent.ACTION_VIEW, data)); |
---|
121 | Log.d("CertificateRepositoryActivity: ", "onActivityResult -> RESULT_OK"); |
---|
122 | |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | |
---|
128 | @Override |
---|
129 | public void onClick(View view) { |
---|
130 | |
---|
131 | switch (view.getId()) { |
---|
132 | |
---|
133 | case R.id.cert_chooser_install_button: |
---|
134 | if(this.keyChain.isButtonsKeyStoreStatus()) { |
---|
135 | Intent intent = new Intent(this, PKCS12FilePickerActivity.class); |
---|
136 | ((Activity) this).startActivityForResult(intent, ActivityResult.IMPORT_CERTIFICATE); |
---|
137 | } else { |
---|
138 | Toast.makeText(CertificateRepositoryActivity.this, R.string.no_access_to_keystore, Toast.LENGTH_LONG).show(); |
---|
139 | } |
---|
140 | break; |
---|
141 | |
---|
142 | case R.id.button_delete: |
---|
143 | if (this.numChecks>0) |
---|
144 | deleteSelectedCertificates(); |
---|
145 | break; |
---|
146 | |
---|
147 | case R.id.button_open: |
---|
148 | if(this.keyChain.isButtonsKeyStoreStatus()) { |
---|
149 | //Crear nuevo almacen. |
---|
150 | this.keyChain.createKeystoreAndChoosePrivateKeyAlias(); |
---|
151 | } else { |
---|
152 | Toast.makeText(CertificateRepositoryActivity.this, R.string.no_access_to_keystore, Toast.LENGTH_LONG).show(); |
---|
153 | } |
---|
154 | break; |
---|
155 | |
---|
156 | case R.id.change_password: |
---|
157 | if(this.keyChain.isButtonsKeyStoreStatus()) { |
---|
158 | this.changePassword(); |
---|
159 | } else { |
---|
160 | Toast.makeText(CertificateRepositoryActivity.this, R.string.no_access_to_keystore, Toast.LENGTH_LONG).show(); |
---|
161 | } |
---|
162 | break; |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | public void changePassword() { |
---|
168 | |
---|
169 | final PasswordConfirmationDialog dialog = |
---|
170 | new PasswordConfirmationDialog((Activity) this, R.string.keystore_new_password); |
---|
171 | dialog.setAcceptListener(new OnClickListener() { |
---|
172 | |
---|
173 | @Override |
---|
174 | public void onClick(View v) { |
---|
175 | String newPassword = dialog.getPassword(); |
---|
176 | dialog.dismiss(); |
---|
177 | try { |
---|
178 | CertificateRepositoryActivity.this.keyChain.saveKeystore(newPassword); |
---|
179 | Toast.makeText(CertificateRepositoryActivity.this, R.string.password_change_success, Toast.LENGTH_LONG).show(); |
---|
180 | } catch (OutOfSpaceError e) { |
---|
181 | Toast.makeText(CertificateRepositoryActivity.this, R.string.error_no_space, Toast.LENGTH_LONG).show(); |
---|
182 | } catch (KeystoreException e) { |
---|
183 | Toast.makeText(CertificateRepositoryActivity.this, R.string.error_unknown, Toast.LENGTH_LONG).show(); |
---|
184 | //throw new RuntimeException(e); |
---|
185 | } |
---|
186 | } |
---|
187 | }); |
---|
188 | dialog.show(); |
---|
189 | } |
---|
190 | |
---|
191 | @Override |
---|
192 | public void onItemClick(AdapterView<?> parent, View view, int item, long id) { |
---|
193 | this.updateCheckedNumber(); |
---|
194 | this.updateButtons(); |
---|
195 | } |
---|
196 | |
---|
197 | private void updateButtons() { |
---|
198 | this.viewHolder.delete.setEnabled(this.numChecks>0); |
---|
199 | //this.viewHolder.accept.setEnabled(this.numChecks==1); |
---|
200 | Drawable icon = this.viewHolder.delete.getCompoundDrawables()[1]; |
---|
201 | if (this.numChecks>0) |
---|
202 | icon.setAlpha(255); |
---|
203 | else |
---|
204 | icon.setAlpha(127); |
---|
205 | |
---|
206 | } |
---|
207 | |
---|
208 | private void updateCheckedNumber() { |
---|
209 | SparseBooleanArray checkedItems = this.viewHolder.aliases.getCheckedItemPositions(); |
---|
210 | int count = 0; |
---|
211 | for (int i = 0; i < checkedItems.size(); i++) { |
---|
212 | if (checkedItems.valueAt(i)) |
---|
213 | count++; |
---|
214 | } |
---|
215 | this.numChecks = count; |
---|
216 | } |
---|
217 | |
---|
218 | |
---|
219 | |
---|
220 | private void deleteSelectedCertificates() { |
---|
221 | |
---|
222 | SparseBooleanArray checkedItems = this.viewHolder.aliases.getCheckedItemPositions(); |
---|
223 | |
---|
224 | for (int i = 0; i < checkedItems.size(); i++) { |
---|
225 | if(checkedItems.valueAt(i) == true){ |
---|
226 | |
---|
227 | try { |
---|
228 | int position = checkedItems.keyAt(i); |
---|
229 | this.keyChain.deleteCertificate( |
---|
230 | (String) this.viewHolder.aliases.getItemAtPosition(position)); |
---|
231 | } catch (KeystoreException e) { |
---|
232 | e.printStackTrace(); |
---|
233 | } |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | //Refresh adapter. NotifyDataSetChange didnt work. |
---|
238 | try { |
---|
239 | CertificateRepositoryActivity.this.viewHolder.aliases.setAdapter( |
---|
240 | new DirectCertificateAdapter(CertificateRepositoryActivity.this.keyChain, this)); |
---|
241 | } catch (KeystoreException e) { |
---|
242 | e.printStackTrace(); |
---|
243 | } |
---|
244 | this.updateCheckedNumber(); |
---|
245 | this.updateButtons(); |
---|
246 | |
---|
247 | try { |
---|
248 | this.keyChain.saveKeystore(); |
---|
249 | } catch (KeystoreException e) { |
---|
250 | } catch (OutOfSpaceError e) { |
---|
251 | } |
---|
252 | |
---|
253 | } |
---|
254 | |
---|
255 | private void launchPasswordDialog(final Intent data) { |
---|
256 | |
---|
257 | final PasswordDialog dialog = new PasswordDialog(this, R.string.certificate_password, false); |
---|
258 | dialog.setAcceptListener(new OnClickListener() { |
---|
259 | |
---|
260 | @Override |
---|
261 | public void onClick(View v) { |
---|
262 | |
---|
263 | try { |
---|
264 | DirectKeyChain direct = DirectKeyChain.getInstance(); |
---|
265 | int imported = direct.importCertificate(data.getData(), dialog.getPassword()); |
---|
266 | String text = CertificateRepositoryActivity.this.getResources().getQuantityString(R.plurals.number_imported_certificates, imported, imported); |
---|
267 | Toast.makeText(CertificateRepositoryActivity.this, text, Toast.LENGTH_LONG).show(); |
---|
268 | direct.saveKeystore(); |
---|
269 | direct.refreshAdapter(); |
---|
270 | |
---|
271 | } catch (IncorrectPasswordException e) { |
---|
272 | Toast toast = Toast.makeText(CertificateRepositoryActivity.this, R.string.incorrect_password, Toast.LENGTH_LONG); |
---|
273 | toast.show(); |
---|
274 | } catch (Exception e) { |
---|
275 | Toast toast = Toast.makeText(CertificateRepositoryActivity.this, R.string.error_reading_p12, Toast.LENGTH_LONG); |
---|
276 | toast.show(); |
---|
277 | } |
---|
278 | |
---|
279 | |
---|
280 | |
---|
281 | dialog.dismiss(); |
---|
282 | CertificateRepositoryActivity.this.requestCode=Integer.MIN_VALUE; |
---|
283 | CertificateRepositoryActivity.this.resultCode=Integer.MIN_VALUE; |
---|
284 | } |
---|
285 | }); |
---|
286 | |
---|
287 | |
---|
288 | dialog.setOnDismissListener(new OnDismissListener(){ |
---|
289 | |
---|
290 | @Override |
---|
291 | public void onDismiss(DialogInterface arg0) { |
---|
292 | CertificateRepositoryActivity.this.requestCode=Integer.MIN_VALUE; |
---|
293 | CertificateRepositoryActivity.this.resultCode=Integer.MIN_VALUE; |
---|
294 | }}); |
---|
295 | |
---|
296 | |
---|
297 | |
---|
298 | |
---|
299 | dialog.setOnCancelListener(new OnCancelListener(){ |
---|
300 | |
---|
301 | @Override |
---|
302 | public void onCancel(DialogInterface dialog) { |
---|
303 | CertificateRepositoryActivity.this.requestCode=Integer.MIN_VALUE; |
---|
304 | CertificateRepositoryActivity.this.resultCode=Integer.MIN_VALUE; |
---|
305 | }}); |
---|
306 | |
---|
307 | |
---|
308 | |
---|
309 | |
---|
310 | dialog.show(); |
---|
311 | |
---|
312 | } |
---|
313 | |
---|
314 | /*********************** Otras clases ********************/ |
---|
315 | private class KeyChainView { |
---|
316 | public ViewGroup parent; |
---|
317 | public View generalView; |
---|
318 | public ListView aliases; |
---|
319 | public TextView install; |
---|
320 | // public Button accept; |
---|
321 | // public Button cancel; |
---|
322 | public TextView delete; |
---|
323 | private Context context; |
---|
324 | private TextView open; |
---|
325 | private TextView change; |
---|
326 | private LinearLayout tempLayout; |
---|
327 | |
---|
328 | public KeyChainView(Context context, ViewGroup parent) { |
---|
329 | this.context=context; |
---|
330 | this.parent=parent; |
---|
331 | |
---|
332 | this.tempLayout = new LinearLayout(context); |
---|
333 | tempLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)); |
---|
334 | |
---|
335 | LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
---|
336 | this.generalView = inflater.inflate(R.layout.certificate_chooser, tempLayout, true); |
---|
337 | |
---|
338 | TextView description1 = (TextView) this.generalView.findViewById(R.id.cert_description); |
---|
339 | description1.setTypeface(Typeface.createFromAsset(this.context.getAssets(), "fonts/Roboto-Medium.ttf"), Typeface.BOLD); |
---|
340 | |
---|
341 | this.aliases = (ListView) this.generalView.findViewById(R.id.cert_chooser); |
---|
342 | this.aliases.setOnItemClickListener(CertificateRepositoryActivity.this); |
---|
343 | |
---|
344 | this.install = (TextView) this.generalView.findViewById(R.id.cert_chooser_install_button); |
---|
345 | this.install.setOnClickListener(CertificateRepositoryActivity.this); |
---|
346 | |
---|
347 | this.delete = (TextView) this.generalView.findViewById(R.id.button_delete); |
---|
348 | this.delete.setOnClickListener(CertificateRepositoryActivity.this); |
---|
349 | |
---|
350 | this.open = (TextView) this.generalView.findViewById(R.id.button_open); |
---|
351 | this.open.setOnClickListener(CertificateRepositoryActivity.this); |
---|
352 | |
---|
353 | this.change = (TextView) this.generalView.findViewById(R.id.change_password); |
---|
354 | this.change.setOnClickListener(CertificateRepositoryActivity.this); |
---|
355 | |
---|
356 | /* this.accept = (Button) this.generalView.findViewById(R.id.button_accept); |
---|
357 | this.cancel = (Button) this.generalView.findViewById(R.id.button_cancel); |
---|
358 | this.accept.setVisibility(View.GONE); |
---|
359 | this.cancel.setVisibility(View.GONE);*/ |
---|
360 | |
---|
361 | |
---|
362 | this.parent.addView(tempLayout); |
---|
363 | |
---|
364 | |
---|
365 | } |
---|
366 | } |
---|
367 | } |
---|