Guest User

Untitled

a guest
Jan 19th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.57 KB | None | 0 0
  1. public void startRestore(View view)
  2. {
  3. int EXTERNAL_WRITE_PERMISSION = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
  4.  
  5. if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M)
  6. {
  7. if(EXTERNAL_WRITE_PERMISSION != PackageManager.PERMISSION_GRANTED)
  8. {
  9. if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE))
  10. {
  11. Snackbar.make(mLayout, "Write permission is required",
  12. Snackbar.LENGTH_INDEFINITE).setAction("OK", new View.OnClickListener() {
  13. @Override
  14. public void onClick(View view) {
  15. // Request the permission
  16. ActivityCompat.requestPermissions(BackupActivity.this,
  17. new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
  18. PERMISSION_REQUEST_STORAGE);
  19. }
  20. }).show();
  21.  
  22. } else {
  23. ActivityCompat.requestPermissions(this,
  24. new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
  25. PERMISSION_REQUEST_STORAGE);
  26. }
  27. }
  28. }
  29. if (backupExists())
  30. {
  31. Log.d("RESTORE: ", "Started restore");
  32.  
  33. final String driveFileID = sharedPreferences.getString("dbBackupDriveFileID", "");
  34. final DriveFile driveFile = DriveId.decodeFromString(driveFileID).asDriveFile();
  35.  
  36. Log.d("RESTORE_FileID: ", driveFileID);
  37.  
  38.  
  39. final Task<DriveContents> openFileTask = mDriveResourceClient.openFile(driveFile, DriveFile.MODE_READ_ONLY);
  40.  
  41. openFileTask.continueWithTask(new Continuation<DriveContents, Task<Void>>()
  42. {
  43. @Override
  44. public Task<Void> then(@NonNull Task<DriveContents> task) throws Exception
  45. {
  46. Log.d("RESTORE: ", "open File task");
  47.  
  48. DriveContents driveContents = task.getResult();
  49. //TODO download file an add to database
  50.  
  51. InputStream inputStream = driveContents.getInputStream();
  52.  
  53. byte[] buf = new byte[8192];
  54.  
  55. int c = 0;
  56.  
  57.  
  58.  
  59. String baseDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
  60. String fileName = DatabaseHelper.DATABASE_NAME;
  61.  
  62. Log.d("RESTORE: ", baseDir + "/" +fileName);
  63.  
  64.  
  65. File f = new File(baseDir+File.pathSeparator+fileName);
  66. if(f.canWrite())
  67. {
  68. Log.d("RESTORE: ", "File writable");
  69.  
  70. OutputStream outputStream = new FileOutputStream(f);
  71.  
  72. while ((c = inputStream.read(buf, 0, buf.length)) > 0)
  73. {
  74. outputStream.write(buf, 0, c);
  75. outputStream.flush();
  76. }
  77. outputStream.close();
  78. }
  79. else
  80. {
  81. Log.d("RESTORE: ", "File not writable");
  82. }
  83.  
  84. return mDriveResourceClient.discardContents(driveContents);
  85. }
  86. })
  87. .addOnFailureListener(new OnFailureListener()
  88. {
  89. @Override
  90. public void onFailure(@NonNull Exception e)
  91. {
  92.  
  93. }
  94. });
  95.  
  96.  
  97. }
  98. else
  99. {
  100. Toast.makeText(this, "Backup does not exists", Toast.LENGTH_SHORT).show();
  101. }
  102.  
  103. }
  104.  
  105. public void startBackup(View view)
  106. {
  107. final ProgressDialog progressDialog = new ProgressDialog(this);
  108.  
  109. final File currentDB = this.getDatabasePath(DatabaseHelper.DATABASE_NAME);
  110.  
  111. Log.d("DATABASE: ", currentDB.getAbsolutePath());
  112. Log.d("DATABASE: ", currentDB.getName());
  113.  
  114. progressDialog.setMessage("Backing Up!!!!");
  115. progressDialog.show();
  116.  
  117. final Task<DriveFolder> appFolderTask = mDriveResourceClient.getAppFolder();
  118. final Task<DriveContents> createContentsTask = mDriveResourceClient.createContents();
  119.  
  120. Tasks.whenAll(appFolderTask, createContentsTask)
  121. .continueWithTask(new Continuation<Void, Task<DriveFile>>()
  122. {
  123. @Override
  124. public Task<DriveFile> then(@NonNull Task<Void> task) throws Exception
  125. {
  126. DriveFolder parent = appFolderTask.getResult();
  127. DriveContents contents = createContentsTask.getResult();
  128.  
  129. InputStream inputStream = null;
  130.  
  131. try
  132. {
  133. inputStream = new FileInputStream(currentDB);
  134. }
  135. catch (FileNotFoundException e)
  136. {
  137. e.printStackTrace();
  138. }
  139.  
  140. OutputStream outputStream = contents.getOutputStream();
  141. int c = 0;
  142. byte[] buf = new byte[8192];
  143. if (inputStream != null)
  144. {
  145.  
  146. while ((c = inputStream.read(buf, 0, buf.length)) > 0)
  147. {
  148. outputStream.write(buf, 0, c);
  149. outputStream.flush();
  150. }
  151. outputStream.close();
  152. }
  153. else
  154. {
  155. Toast.makeText(BackupActivity.this, "Some error occurred", Toast.LENGTH_SHORT).show();
  156. }
  157.  
  158. MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
  159. .setMimeType("application/x-sqlite3")
  160. .setTitle(currentDB.getName())
  161. .build();
  162.  
  163.  
  164. return mDriveResourceClient.createFile(parent, changeSet, contents);
  165. }
  166. })
  167. .addOnSuccessListener(this, new OnSuccessListener<DriveFile>() {
  168. @Override
  169. public void onSuccess(DriveFile driveFile)
  170. {
  171. progressDialog.dismiss();
  172.  
  173. String driveFileID = driveFile.getDriveId().encodeToString();
  174.  
  175. String dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date());
  176.  
  177. SharedPreferences.Editor editor = sharedPreferences.edit();
  178. editor.putString("dbBackupDriveFileID", driveFileID);
  179. editor.putString("lastDbBackupTime", dateTime);
  180. editor.apply();
  181.  
  182. Log.d("DRIVE_FILE", driveFileID);
  183.  
  184. String d = getString(R.string.last_backup) + dateTime;
  185.  
  186. textView.setText(d);
  187.  
  188. Toast.makeText(BackupActivity.this, "Backup Successful. File "+driveFile.getDriveId()
  189. .encodeToString(), Toast.LENGTH_LONG).show();
  190.  
  191. }
  192. })
  193. .addOnFailureListener(this, new OnFailureListener() {
  194. @Override
  195. public void onFailure(@NonNull Exception e)
  196. {
  197. progressDialog.dismiss();
  198. Log.e("DRIVE ", "Unable to create file", e);
  199. Toast.makeText(BackupActivity.this, "Unable to backup", Toast.LENGTH_SHORT).show();
  200. }
  201. });
  202. }
Add Comment
Please, Sign In to add comment