Skip to content

Commit

Permalink
Completed Drive integration - able to merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Javinator9889 committed Apr 27, 2018
1 parent 141f38d commit f77ef50
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 381 deletions.
@@ -1,4 +1,4 @@
package javinator9889.securepass.backup.drive;
package javinator9889.securepass;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down
Expand Up @@ -5,6 +5,7 @@
import android.support.annotation.NonNull;
import android.text.InputType;
import android.util.Log;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.google.android.gms.drive.DriveContents;
Expand All @@ -17,8 +18,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.StreamCorruptedException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand All @@ -33,27 +34,14 @@
*/
public class RetrieveContentWithDownloadProgress implements IDriveDownloadOperations {
private static final String TAG = "RetrieveWithProgress";

private Context driveContext;
private Activity mainActivity;
private DriveResourceClient resourceClient;
private MaterialDialog mProgressBar;
private ExecutorService mExecutorService;
private byte[] iv;

/*@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mProgressBar = new MaterialDialog.Builder(this)
.title(R.string.retrieving_data)
.content(R.string.wait)
.cancelable(false)
.progress(false, 100)
.build();
mExecutorService = Executors.newSingleThreadExecutor();
launchActivities();
}*/

public RetrieveContentWithDownloadProgress(@NonNull Context driveContext,
@NonNull Activity mainActivity,
@NonNull DriveResourceClient resourceClient) {
Expand All @@ -70,35 +58,6 @@ public RetrieveContentWithDownloadProgress(@NonNull Context driveContext,
mExecutorService = Executors.newSingleThreadExecutor();
}

/*private void launchActivities() {
pickIvFile()
.addOnSuccessListener(this,
driveId -> retrieveIvVector(driveId.asDriveFile()))
.addOnFailureListener(this, e -> {
Log.e(TAG, "Fail when recovering IV vector. Message: " + e.getMessage());
finish();
});
try {
wait(TimeUnit.SECONDS.toMillis(10));
pickClassFile()
.addOnSuccessListener(this,
driveId -> retrieveContents(driveId.asDriveFile()))
.addOnFailureListener(this, e -> {
Log.e(TAG, DRIVE.GOOGLE_FILE_NO_SELECTED, e);
finish();
});
} catch (InterruptedException e) {
Log.e(TAG, "Interrupted while waiting. Message: " + e.getMessage());
finish();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mExecutorService.shutdown();
}*/

@Override
public void retrieveIvVector(DriveFile file) {
Task<DriveContents> openFileTask = resourceClient
Expand All @@ -108,8 +67,6 @@ public void retrieveIvVector(DriveFile file) {
DriveContents contents = task.getResult();
try (InputStream stream = contents.getInputStream()) {
this.iv = ByteStreams.toByteArray(stream);
System.out.println(Arrays.toString(iv));
//notify();
return resourceClient.discardContents(contents);
}
})
Expand All @@ -121,19 +78,6 @@ public void retrieveIvVector(DriveFile file) {

@Override
public void retrieveContents(DriveFile file) {
/*Task<DriveContents> openFileTask = resourceClient.openFile(file, DriveFile.MODE_READ_ONLY);
openFileTask
.continueWith(task -> {
DriveContents contents = task.getResult();
try (InputStream obtainedFile = contents.getInputStream()) {
IOManager io = IOManager.newInstance(driveContext);
io.writeDownloadedClass(obtainedFile);
//InputStream clonedStream = Cloner.clone(obtainedFile);
//completeDownload(clonedStream);
}
completeDownload();
return resourceClient.discardContents(contents);
});*/
mProgressBar.show();
resourceClient.openFile(file, DriveFile.MODE_READ_ONLY, new OpenFileCallback() {
@Override
Expand All @@ -148,40 +92,14 @@ public void onContents(@NonNull DriveContents driveContents) {
mProgressBar.setProgress(100);
mProgressBar.dismiss();
try {
try (InputStream obtainedFile = driveContents.getInputStream()){
try (InputStream obtainedFile = driveContents.getInputStream()) {
IOManager io = IOManager.newInstance(driveContext);
io.writeDownloadedClass(obtainedFile);
completeDownload();
}
} catch (IOException e) {
Log.e(TAG, "IOException when retrieving contents", e);
}
//return resourceClient.discardContents(driveContents);
/*final StringBuilder passwordBuilder = new StringBuilder();
new MaterialDialog.Builder(
driveContext)
.title(R.string.put_pass)
.content(R.string.pass_need)
.inputType(InputType.TYPE_TEXT_VARIATION_PASSWORD)
.input(null, null, false,
(dialog, input) -> passwordBuilder.append(input))
.onAny((dialog, which) -> {
String password = passwordBuilder.toString();
try (InputStream fileInput = driveContents.getInputStream()) {
System.out.println(fileInput);
FileCipher decrypt = FileCipher.newInstance(password, iv);
ClassContainer restoredData = (ClassContainer) decrypt
.decrypt(fileInput);
System.out.println(restoredData.toString());
//restoredData.storeDataInDB();
resourceClient.discardContents(driveContents);
} catch (IOException | NoSuchAlgorithmException | InvalidKeyException
| NoSuchPaddingException |
InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
})
.show();*/
}

@Override
Expand All @@ -190,7 +108,6 @@ public void onError(@NonNull Exception e) {
mainActivity.finish();
}
});
//resourceClient.openFile(file, DriveFile.MODE_READ_ONLY, openCallback);*/
}

private void completeDownload() {
Expand All @@ -207,21 +124,24 @@ private void completeDownload() {
String password = Hashing.sha256()
.hashString(passwordBuilder.toString(), StandardCharsets.UTF_8)
.toString();
System.out.println(password);
try {
FileCipher fileDecrypt = FileCipher.newInstance(password,
iv);
ClassContainer restoredData =
(ClassContainer) fileDecrypt.decrypt(obtainedStream);
Log.d(TAG, restoredData.toString());
restoredData.storeDataInDB(); // must implement
obtainedStream.close();
io.deleteDownloadedClass();
} catch (IOException e) {
Log.e(TAG, "IOException captured. Message: " +
e.getMessage(), e);
} catch (StreamCorruptedException e) {
Log.e(TAG, "Password not correct captured");
Toast.makeText(driveContext, "Password is not correct",
Toast.LENGTH_LONG).show();
completeDownload();
} catch (Exception e) {
Log.e(TAG, "Exception when decrypting", e);
Log.e(TAG, "Exception during process of decrypting", e);
}
}))
.cancelable(false)
.build().show();
}

Expand Down

This file was deleted.

Expand Up @@ -6,11 +6,9 @@
import android.util.Log;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveClient;
import com.google.android.gms.drive.DriveFolder;
import com.google.android.gms.drive.DriveId;
import com.google.android.gms.drive.DriveResourceClient;
Expand All @@ -23,7 +21,6 @@
import com.google.android.gms.drive.query.SortableField;
import com.google.android.gms.drive.widget.DataBufferAdapter;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;

import java.util.concurrent.atomic.AtomicInteger;

Expand All @@ -42,10 +39,7 @@ public class GoogleDriveBase implements IDriveBase {

private Context driveContext;
private Activity mainActivity;
//private GoogleSignInClient mGoogleSignInClient;
private DriveClient mDriveClient;
private DriveResourceClient mDriveResourceClient;
private TaskCompletionSource<DriveId> mOpenItemTaskSource;
private DataBufferAdapter<Metadata> resultsAdapter;
private SignIn mSignInClient;

Expand Down Expand Up @@ -86,19 +80,13 @@ public void uploadFile(@NonNull ClassContainer dataToBackup) {
driveContext,
mainActivity,
mDriveResourceClient);
//createFileTask.createFileInAppFolder(dataToBackup);
queryFilesAndDelete(createFileTask, dataToBackup);
}
}

@Override
public void restoreData() {
if (isAbleToSignIn()) {
/*MaterialDialog md = new MaterialDialog.Builder(driveContext)
.progress(true, 0)
.title(R.string.wait)
.build();
md.show();*/
queryFiles();
}
}
Expand Down Expand Up @@ -150,7 +138,6 @@ private void continueWithDownload() {
break;
}
}
//md.dismiss();
try {
retrieveContentClass.retrieveIvVector(ivId.asDriveFile());
retrieveContentClass.retrieveContents(classId.asDriveFile());
Expand Down Expand Up @@ -202,26 +189,11 @@ private void queryFiles() {
});
}

@Override
public void setResult(DriveId id) {
mOpenItemTaskSource.setResult(id);
}

@Override
public void setException(Exception e) {
mOpenItemTaskSource.setException(e);
}

@Override
public void setDriveResourceClient(DriveResourceClient mDriveResourceClient) {
this.mDriveResourceClient = mDriveResourceClient;
}

@Override
public void setDriveClient(DriveClient mDriveClient) {
this.mDriveClient = mDriveClient;
}

public void setLoggedIn(boolean isLoggedIn) {
mSignInClient.setSignedIn(isLoggedIn);
}
Expand Down

0 comments on commit f77ef50

Please sign in to comment.