Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. public class PasswordKeeperActivity extends Activity implements
  2. AdapterView.OnItemSelectedListener {
  3.  
  4. // initialise
  5. EditText username, password, note;
  6. Button save, reset;
  7. public String savedata = Environment.getExternalStorageDirectory().toString();
  8.  
  9. String[] countryNames={"Google", "Yahoo", "Facebook", "Twitter", "Instagram", "BBM", "Skype", "Other"};
  10. int flags[] = {R.drawable.google, R.drawable.yahoo, R.drawable.facebook, R.drawable.twitter, R.drawable.instagram, R.drawable.bbm, R.drawable.skype, R.drawable.other};
  11.  
  12. private static final int REQUEST_EXTERNAL_STORAGE = 1;
  13. private static String[] PERMISSIONS_STORAGE = {
  14. Manifest.permission.READ_EXTERNAL_STORAGE,
  15. Manifest.permission.WRITE_EXTERNAL_STORAGE
  16. };
  17. // for inflating the menu
  18. public boolean onCreateOptionsMenu(Menu menu) {
  19. MenuInflater inflater = getMenuInflater();
  20. inflater.inflate(R.menu.menu, menu);
  21. return true;
  22. }
  23.  
  24. // on selection of the menu
  25. @Override
  26. public boolean onOptionsItemSelected(MenuItem item) {
  27. // Handle item selection
  28. switch (item.getItemId()) {
  29. case R.id.view_passwords:
  30. Intent intent = new Intent(this, PasswordView.class);
  31. startActivity(intent);
  32. return true;
  33.  
  34. default:
  35. return super.onOptionsItemSelected(item);
  36. }
  37. }
  38.  
  39. public void onItemSelected(AdapterView<?> parent, View view,
  40. int pos, long id) {
  41. // An item was selected. You can retrieve the selected item using
  42. // parent.getItemAtPosition(pos)
  43.  
  44. }
  45.  
  46. public void onNothingSelected(AdapterView<?> parent) {
  47. // Another interface callback
  48. }
  49.  
  50. public void onCreate(Bundle savedInstanceState) {
  51. super.onCreate(savedInstanceState);
  52. setContentView(R.layout.activity_home);
  53.  
  54. initialise();
  55.  
  56. //Getting the instance of Spinner and applying OnItemSelectedListener on
  57. it
  58. Spinner spin = (Spinner) findViewById(R.id.planets_spinner);
  59. spin.setOnItemSelectedListener(this);
  60.  
  61. CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(),
  62. flags, countryNames);
  63. spin.setAdapter(customAdapter);
  64.  
  65. //to set the site Edit Text to get the focus
  66.  
  67.  
  68.  
  69.  
  70. // save the data to the textfile
  71. save.setOnClickListener(new OnClickListener() {
  72. public void onClick(View v) {
  73.  
  74. // creates hidden directory if not existing
  75. File dir = new File(getCacheDir(), "/sk/");
  76. if (!dir.exists()) {
  77. dir.mkdirs();
  78. }
  79.  
  80. // saving data part
  81. String sFileName = getCacheDir() + "/sk/logp.csv";
  82. try {
  83. FileWriter writer = new FileWriter(sFileName, true);
  84.  
  85. String countryNames, sUser, sPass, sAdd;
  86.  
  87. countryNames =
  88. sUser = username.getText().toString();
  89. sPass = password.getText().toString();
  90. sAdd = note.getText().toString();
  91.  
  92.  
  93.  
  94. if ((sUser.equals("")) && (sPass.equals("")) &&
  95. (sAdd.equals(""))) {
  96. Toast.makeText(getBaseContext(), "Please Enter At least
  97. one Field",
  98. Toast.LENGTH_SHORT).show();
  99.  
  100. } else {
  101. if (sUser.equals(""))
  102. sUser = "null";
  103. if (sPass.equals(""))
  104. sPass = "null";
  105. if (sAdd.equals(""))
  106. sAdd = "null";
  107.  
  108.  
  109. // encrypting the passwords before saving
  110. SimpleCrypto mcrypt = new SimpleCrypto();
  111. sPass = SimpleCrypto.bytesToHex( mcrypt.encrypt(sPass) );
  112. //sPass = SimpleCrypto.encrypt("fugly", sPass);
  113.  
  114.  
  115.  
  116.  
  117. writer.append(sUser);
  118. writer.append(',');
  119.  
  120. writer.append(sPass);
  121. writer.append(',');
  122. writer.append(sAdd);
  123.  
  124. writer.append('n');
  125.  
  126. // generate whatever data you want
  127.  
  128. writer.flush();
  129. writer.close();
  130.  
  131. Toast.makeText(getBaseContext(), "Password Saved!",
  132. Toast.LENGTH_SHORT).show();
  133.  
  134. Intent intent = new Intent(PasswordKeeperActivity.this,
  135. PasswordView.class);
  136. String[] myStrings = new String[] {"Google", "Yahoo",
  137. "Facebook", "Twitter", "Instagram", "BBM", "Skype", "Other"};
  138. int logo[] = new int[] {R.drawable.google,
  139. R.drawable.yahoo, R.drawable.facebook, R.drawable.twitter,
  140. R.drawable.instagram,
  141. R.drawable.bbm, R.drawable.skype, R.drawable.other};
  142. intent.putExtra("strings", myStrings);
  143. intent.putExtra("logos", logo);
  144. startActivity(intent);
  145. }
  146.  
  147. } catch (Exception e) {
  148. Toast.makeText(getBaseContext(), e.getMessage(),
  149. Toast.LENGTH_SHORT).show();
  150. }
  151.  
  152. }
  153. });
  154.  
  155. // Reset
  156. reset.setOnClickListener(new OnClickListener() {
  157. public void onClick(View v) {
  158.  
  159. countryNames.equals("Google");
  160. note.setText("");
  161. username.setText("");
  162. password.setText("");
  163. Toast.makeText(getBaseContext(), "Field(s) Cleared!",
  164. Toast.LENGTH_SHORT).show();
  165. }
  166. });
  167.  
  168. }
  169.  
  170.  
  171.  
  172. public void initialise() {
  173.  
  174.  
  175. username = (EditText) findViewById(R.id.input_name);
  176. password = (EditText) findViewById(R.id.input_email);
  177. note = (EditText) findViewById(R.id.input_password);
  178.  
  179. save = (Button) findViewById(R.id.buttonSave);
  180. reset = (Button) findViewById(R.id.ButtonReset);
  181. }
  182.  
  183. /**
  184. * Checks if the app has permission to write to device storage
  185. *
  186. * If the app does not has permission then the user will be prompted to
  187. grant permissions
  188. *
  189. * @param activity
  190. */
  191. public static void verifyStoragePermissions(Activity activity) {
  192. // Check if we have write permission
  193. int permission = ActivityCompat.checkSelfPermission(activity,
  194. Manifest.permission.WRITE_EXTERNAL_STORAGE);
  195.  
  196. if (permission != PackageManager.PERMISSION_GRANTED) {
  197. // We don't have permission so prompt the user
  198. ActivityCompat.requestPermissions(
  199. activity,
  200. PERMISSIONS_STORAGE,
  201. REQUEST_EXTERNAL_STORAGE
  202. );
  203. }
  204. }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement