[09c6eb7] | 1 | package ve.gob.cenditel.tibisaymovil; |
---|
| 2 | |
---|
| 3 | import android.app.Activity; |
---|
| 4 | import android.content.Context; |
---|
[288126d] | 5 | import android.content.DialogInterface; |
---|
[09c6eb7] | 6 | import android.content.Intent; |
---|
[288126d] | 7 | import android.content.DialogInterface.OnCancelListener; |
---|
| 8 | import android.content.DialogInterface.OnDismissListener; |
---|
| 9 | import android.graphics.Typeface; |
---|
| 10 | import android.graphics.drawable.Drawable; |
---|
[09c6eb7] | 11 | import android.os.Bundle; |
---|
[288126d] | 12 | import android.util.SparseBooleanArray; |
---|
| 13 | import android.view.LayoutInflater; |
---|
[c95e4a6] | 14 | import android.util.Log; |
---|
[09c6eb7] | 15 | import android.view.View; |
---|
[288126d] | 16 | import android.view.ViewGroup; |
---|
[09c6eb7] | 17 | import android.view.View.OnClickListener; |
---|
| 18 | import android.widget.AdapterView; |
---|
[288126d] | 19 | import android.widget.LinearLayout; |
---|
| 20 | import android.widget.ListView; |
---|
| 21 | import android.widget.TextView; |
---|
[09c6eb7] | 22 | import android.widget.Toast; |
---|
| 23 | import android.widget.AdapterView.OnItemClickListener; |
---|
[288126d] | 24 | import android.widget.LinearLayout.LayoutParams; |
---|
[09c6eb7] | 25 | |
---|
| 26 | public class CertificateRepositoryActivity extends Activity implements OnClickListener, OnItemClickListener { |
---|
| 27 | |
---|
| 28 | |
---|
[288126d] | 29 | |
---|
| 30 | public DirectKeyChain keyChain; |
---|
| 31 | public KeyChainView viewHolder; |
---|
| 32 | protected int requestCode; |
---|
| 33 | protected int resultCode; |
---|
| 34 | private int numChecks; |
---|
| 35 | |
---|
| 36 | |
---|
[09c6eb7] | 37 | @Override |
---|
| 38 | public void onCreate(Bundle savedInstanceState) { |
---|
| 39 | |
---|
| 40 | super.onCreate(savedInstanceState); |
---|
[288126d] | 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); |
---|
[09c6eb7] | 47 | |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | } |
---|
| 52 | |
---|
[288126d] | 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); |
---|
[6141013] | 59 | this.keyChain.setButtonsKeyStoreStatus(false); |
---|
[288126d] | 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 | |
---|
[09c6eb7] | 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 | |
---|
[c95e4a6] | 90 | @Override |
---|
| 91 | protected void onActivityResult(int requestCode, int resultCode, |
---|
| 92 | Intent data) { |
---|
[3fe55cf] | 93 | |
---|
| 94 | if (requestCode == ActivityResult.IMPORT_CERTIFICATE && resultCode == Activity.RESULT_OK) { |
---|
| 95 | this.launchPasswordDialog(data); |
---|
| 96 | } |
---|
[c95e4a6] | 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 | |
---|
[09c6eb7] | 105 | |
---|
[288126d] | 106 | |
---|
[09c6eb7] | 107 | @Override |
---|
| 108 | public void onClick(View view) { |
---|
| 109 | |
---|
| 110 | switch (view.getId()) { |
---|
| 111 | |
---|
| 112 | case R.id.cert_chooser_install_button: |
---|
[288126d] | 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 | } |
---|
[09c6eb7] | 119 | break; |
---|
| 120 | |
---|
| 121 | case R.id.button_delete: |
---|
[288126d] | 122 | if (this.numChecks>0) |
---|
| 123 | deleteSelectedCertificates(); |
---|
[09c6eb7] | 124 | break; |
---|
| 125 | |
---|
| 126 | case R.id.button_open: |
---|
[288126d] | 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 | } |
---|
[09c6eb7] | 133 | break; |
---|
| 134 | |
---|
| 135 | case R.id.change_password: |
---|
[288126d] | 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 | } |
---|
[09c6eb7] | 141 | break; |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
[288126d] | 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 | |
---|
[09c6eb7] | 170 | @Override |
---|
| 171 | public void onItemClick(AdapterView<?> parent, View view, int item, long id) { |
---|
[288126d] | 172 | this.updateCheckedNumber(); |
---|
| 173 | this.updateButtons(); |
---|
[09c6eb7] | 174 | } |
---|
[288126d] | 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 | } |
---|
[09c6eb7] | 215 | |
---|
[288126d] | 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 | |
---|
[3fe55cf] | 232 | } |
---|
[288126d] | 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 | } |
---|
[09c6eb7] | 292 | |
---|
[288126d] | 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; |
---|
[1081d4d] | 309 | this.parent=parent; |
---|
[288126d] | 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 | } |
---|
[09c6eb7] | 346 | } |
---|