diff --git a/APP/app/src/main/java/javinator9889/securepass/backup/drive/DataClassForTests.java b/APP/app/src/main/java/javinator9889/securepass/DataClassForTests.java similarity index 97% rename from APP/app/src/main/java/javinator9889/securepass/backup/drive/DataClassForTests.java rename to APP/app/src/main/java/javinator9889/securepass/DataClassForTests.java index c035b75..653aa90 100644 --- a/APP/app/src/main/java/javinator9889/securepass/backup/drive/DataClassForTests.java +++ b/APP/app/src/main/java/javinator9889/securepass/DataClassForTests.java @@ -1,4 +1,4 @@ -package javinator9889.securepass.backup.drive; +package javinator9889.securepass; import java.util.ArrayList; import java.util.HashMap; diff --git a/APP/app/src/main/java/javinator9889/securepass/backup/drive/RetrieveContentWithDownloadProgress.java b/APP/app/src/main/java/javinator9889/securepass/backup/drive/RetrieveContentWithDownloadProgress.java index 7ad6808..4681352 100644 --- a/APP/app/src/main/java/javinator9889/securepass/backup/drive/RetrieveContentWithDownloadProgress.java +++ b/APP/app/src/main/java/javinator9889/securepass/backup/drive/RetrieveContentWithDownloadProgress.java @@ -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; @@ -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; @@ -33,7 +34,7 @@ */ public class RetrieveContentWithDownloadProgress implements IDriveDownloadOperations { private static final String TAG = "RetrieveWithProgress"; - + private Context driveContext; private Activity mainActivity; private DriveResourceClient resourceClient; @@ -41,19 +42,6 @@ public class RetrieveContentWithDownloadProgress implements IDriveDownloadOperat 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) { @@ -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 openFileTask = resourceClient @@ -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); } }) @@ -121,19 +78,6 @@ public void retrieveIvVector(DriveFile file) { @Override public void retrieveContents(DriveFile file) { - /*Task 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 @@ -148,7 +92,7 @@ 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(); @@ -156,32 +100,6 @@ public void onContents(@NonNull DriveContents driveContents) { } 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 @@ -190,7 +108,6 @@ public void onError(@NonNull Exception e) { mainActivity.finish(); } }); - //resourceClient.openFile(file, DriveFile.MODE_READ_ONLY, openCallback);*/ } private void completeDownload() { @@ -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(); } diff --git a/APP/app/src/main/java/javinator9889/securepass/backup/drive/base/GoogleConnection.java b/APP/app/src/main/java/javinator9889/securepass/backup/drive/base/GoogleConnection.java deleted file mode 100644 index b713db2..0000000 --- a/APP/app/src/main/java/javinator9889/securepass/backup/drive/base/GoogleConnection.java +++ /dev/null @@ -1,31 +0,0 @@ -package javinator9889.securepass.backup.drive.base; - -import android.os.Bundle; -import android.support.annotation.Nullable; -import android.support.v7.app.AppCompatActivity; - -import com.google.android.gms.auth.api.signin.GoogleSignIn; -import com.google.android.gms.auth.api.signin.GoogleSignInClient; -import com.google.android.gms.auth.api.signin.GoogleSignInOptions; -import com.google.android.gms.drive.Drive; - -/** - * Created by Javinator9889 on 22/04/2018. - */ -public abstract class GoogleConnection extends AppCompatActivity { - private GoogleSignInClient mSignInClient; - - @Override - protected void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - mSignInClient = buildGoogleSignInClient(); - } - - private GoogleSignInClient buildGoogleSignInClient() { - GoogleSignInOptions signInOptions = new GoogleSignInOptions - .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) - .requestScopes(Drive.SCOPE_FILE, Drive.SCOPE_APPFOLDER) - .build(); - return GoogleSignIn.getClient(this, signInOptions); - } -} diff --git a/APP/app/src/main/java/javinator9889/securepass/backup/drive/base/GoogleDriveBase.java b/APP/app/src/main/java/javinator9889/securepass/backup/drive/base/GoogleDriveBase.java index ac49697..88ae2be 100644 --- a/APP/app/src/main/java/javinator9889/securepass/backup/drive/base/GoogleDriveBase.java +++ b/APP/app/src/main/java/javinator9889/securepass/backup/drive/base/GoogleDriveBase.java @@ -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; @@ -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; @@ -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 mOpenItemTaskSource; private DataBufferAdapter resultsAdapter; private SignIn mSignInClient; @@ -86,7 +80,6 @@ public void uploadFile(@NonNull ClassContainer dataToBackup) { driveContext, mainActivity, mDriveResourceClient); - //createFileTask.createFileInAppFolder(dataToBackup); queryFilesAndDelete(createFileTask, dataToBackup); } } @@ -94,11 +87,6 @@ public void uploadFile(@NonNull ClassContainer dataToBackup) { @Override public void restoreData() { if (isAbleToSignIn()) { - /*MaterialDialog md = new MaterialDialog.Builder(driveContext) - .progress(true, 0) - .title(R.string.wait) - .build(); - md.show();*/ queryFiles(); } } @@ -150,7 +138,6 @@ private void continueWithDownload() { break; } } - //md.dismiss(); try { retrieveContentClass.retrieveIvVector(ivId.asDriveFile()); retrieveContentClass.retrieveContents(classId.asDriveFile()); @@ -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); } diff --git a/APP/app/src/main/java/javinator9889/securepass/backup/drive/base/GoogleDriveBase_old.java b/APP/app/src/main/java/javinator9889/securepass/backup/drive/base/GoogleDriveBase_old.java deleted file mode 100644 index 93f5374..0000000 --- a/APP/app/src/main/java/javinator9889/securepass/backup/drive/base/GoogleDriveBase_old.java +++ /dev/null @@ -1,190 +0,0 @@ -package javinator9889.securepass.backup.drive.base; - -import android.app.Activity; -import android.content.Context; -import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.widget.Toast; - -import com.google.android.gms.auth.api.signin.GoogleSignInClient; -import com.google.android.gms.common.ConnectionResult; -import com.google.android.gms.common.GoogleApiAvailability; -import com.google.android.gms.drive.DriveClient; -import com.google.android.gms.drive.DriveId; -import com.google.android.gms.drive.DriveResourceClient; -import com.google.android.gms.tasks.TaskCompletionSource; - -/** - * Created by Javinator9889 on 07/04/2018. - */ -public abstract class GoogleDriveBase_old extends Activity { - private static final String TAG = "DriveBaseActivity"; - private boolean isSignedIn = false; - - private GoogleSignInClient mGoogleSignInClient; - private DriveClient mDriveClient; - private DriveResourceClient mDriveResourceClient; - private TaskCompletionSource mOpenItemTaskSource; - - @Override - protected void onStart() { - super.onStart(); - //GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this); - - //if (isGooglePlayServicesAvailable(getApplicationContext())) - //signIn(); - /*else - throw new GoogleDriveNotAvailableException(DRIVE.GOOGLE_PLAY_NOT_AVAILABLE);*/ - } - - @Override - protected void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - /*GoogleSignInOptions gso = new GoogleSignInOptions - .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) - .requestEmail() - .build(); - mGoogleSignInClient = GoogleSignIn.getClient(this, gso);*/ - } - - private static boolean isGooglePlayServicesAvailable(Context servicesContext) { - GoogleApiAvailability googleApiAvailabilityForPlayServices = GoogleApiAvailability - .getInstance(); - int status = googleApiAvailabilityForPlayServices - .isGooglePlayServicesAvailable(servicesContext); - return status == ConnectionResult.SUCCESS; - } - - /*@Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); - switch (requestCode) { - case REQUEST_CODE_SIGN_IN: - if (resultCode != RESULT_OK) { - Log.e(TAG, DRIVE.GOOGLE_ACCOUNT_NOT_SIGNED_IN + " | Result code: " - + resultCode); - finish(); - return; - } - Task getAccountTask = - GoogleSignIn.getSignedInAccountFromIntent(data); - if (getAccountTask.isSuccessful()) { - initializeDriveClient(getAccountTask.getResult()); - isSignedIn = true; - } - else { - Log.e(TAG, DRIVE.GOOGLE_ACCOUNT_NOT_SIGNED_IN + " | Task unsuccessful"); - finish(); - } - break; - case DRIVE.REQUEST_CODE_OPEN_ITEM: - if (resultCode == RESULT_OK) { - DriveId driveId = data.getParcelableExtra( - OpenFileActivityOptions.EXTRA_RESPONSE_DRIVE_ID); - mOpenItemTaskSource.setResult(driveId); - } else - mOpenItemTaskSource.setException(new GoogleDriveUnableToOpenFileException( - "Unable to open file" - )); - break; - } - super.onActivityResult(requestCode, resultCode, data); - }*/ - - /*protected void newSignIn() { - Intent intent = mGoogleSignInClient.getSignInIntent(); - startActivityForResult(intent, DRIVE.REQUEST_CODE_SIGN_IN); - } - - protected void signIn() { - Set requiredScopes = new HashSet<>(DRIVE.HASHSET_INITIAL_CAPACITY); - requiredScopes.add(Drive.SCOPE_FILE); - requiredScopes.add(Drive.SCOPE_APPFOLDER); - GoogleSignInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(this); - if (signInAccount != null && signInAccount.getGrantedScopes().containsAll(requiredScopes)) - initializeDriveClient(signInAccount); - else { - GoogleSignInOptions signInOptions = - new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) - .requestScopes(Drive.SCOPE_FILE) - .requestScopes(Drive.SCOPE_APPFOLDER) - .requestEmail() - .build(); - GoogleSignInClient googleSignInClient = - GoogleSignIn.getClient(this, signInOptions); - startActivityForResult(googleSignInClient.getSignInIntent(), - DRIVE.REQUEST_CODE_SIGN_IN); - } - } - - private void initializeDriveClient(GoogleSignInAccount signInAccount) { - mDriveClient = Drive.getDriveClient(getApplicationContext(), signInAccount); - mDriveResourceClient = Drive.getDriveResourceClient(getApplicationContext(), signInAccount); - onDriveClientReady(); - } - - protected Task pickClassFile() { - List filters = new ArrayList<>(2); - filters.add(Filters.eq(SearchableField.MIME_TYPE, DRIVE.MIME_TYPE)); - filters.add(Filters.eq(SearchableField.TITLE, DRIVE.FILE_TITLE)); - OpenFileActivityOptions openOptions = - new OpenFileActivityOptions.Builder() - .setSelectionFilter(Filters.and(filters)) - .build(); - return pickItem(openOptions); - } - - protected Task pickIvFile() { - List filters = new ArrayList<>(2); - filters.add(Filters.eq(SearchableField.MIME_TYPE, DRIVE.MIME_TYPE)); - filters.add(Filters.eq(SearchableField.TITLE, DRIVE.IV_FILE)); - OpenFileActivityOptions openOptions = - new OpenFileActivityOptions.Builder() - .setSelectionFilter(Filters.and(filters)) - .build(); - return pickItem(openOptions); - } - - protected Task pickFolder() { - OpenFileActivityOptions openOptions = - new OpenFileActivityOptions.Builder() - .setSelectionFilter(Filters.eq(SearchableField.MIME_TYPE, DriveFolder.MIME_TYPE)) - .build(); - return pickItem(openOptions); - } - - protected Task pickItem(OpenFileActivityOptions openOptions) { - mOpenItemTaskSource = new TaskCompletionSource<>(); - getDriveClient() - .newOpenFileActivityIntentSender(openOptions) - .continueWith((Continuation) task -> { - startIntentSenderForResult(task.getResult(), - DRIVE.REQUEST_CODE_OPEN_ITEM, - DRIVE.FILL_IN_INTENT, - DRIVE.FLAGS_MASK, - DRIVE.FLAGS_VALUES, - DRIVE.EXTRA_FLAGS); - return null; - }); - return mOpenItemTaskSource.getTask(); - }*/ - - protected void showMessage(@NonNull String message) { - Toast.makeText(this, message, Toast.LENGTH_LONG).show(); - } - - protected abstract void onDriveClientReady(); - - protected DriveClient getDriveClient() { - return mDriveClient; - } - - protected DriveResourceClient getDriveResourceClient() { - return mDriveResourceClient; - } - - protected boolean isSignedIn() { - return isSignedIn; - } -} diff --git a/APP/app/src/main/java/javinator9889/securepass/backup/drive/base/IDriveBase.java b/APP/app/src/main/java/javinator9889/securepass/backup/drive/base/IDriveBase.java index cbd29de..4abdf22 100644 --- a/APP/app/src/main/java/javinator9889/securepass/backup/drive/base/IDriveBase.java +++ b/APP/app/src/main/java/javinator9889/securepass/backup/drive/base/IDriveBase.java @@ -30,8 +30,5 @@ public interface IDriveBase { void uploadFile(@NonNull ClassContainer dataToBackup); void restoreData(); void setDriveResourceClient(DriveResourceClient resourceClient); - void setDriveClient(DriveClient client); - void setResult(DriveId id); - void setException(Exception e); void setLoggedIn(boolean isLoggedIn); } diff --git a/APP/app/src/main/java/javinator9889/securepass/util/Cloner.java b/APP/app/src/main/java/javinator9889/securepass/util/Cloner.java deleted file mode 100644 index a003f00..0000000 --- a/APP/app/src/main/java/javinator9889/securepass/util/Cloner.java +++ /dev/null @@ -1,29 +0,0 @@ -package javinator9889.securepass.util; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.InputStream; - -/** - * Created by Javinator9889 on 24/04/2018. - */ -public class Cloner { - public static InputStream clone(final InputStream inputStream) { - try { - inputStream.mark(0); - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; - int readLength = 0; - while ((readLength = inputStream.read(buffer)) != -1) { - outputStream.write(buffer, 0, readLength); - } -// inputStream.reset(); - outputStream.flush(); - return new ByteArrayInputStream(outputStream.toByteArray()); - } - catch (Exception ex) { - ex.printStackTrace(); - } - return null; - } -} diff --git a/APP/app/src/main/java/javinator9889/securepass/views/fragments/DriveContent.java b/APP/app/src/main/java/javinator9889/securepass/views/fragments/DriveContent.java index 48b7359..a98575e 100644 --- a/APP/app/src/main/java/javinator9889/securepass/views/fragments/DriveContent.java +++ b/APP/app/src/main/java/javinator9889/securepass/views/fragments/DriveContent.java @@ -23,7 +23,7 @@ import javinator9889.securepass.R; import javinator9889.securepass.data.container.ClassContainer; import javinator9889.securepass.errors.GoogleDriveUnableToOpenFileException; -import javinator9889.securepass.backup.drive.DataClassForTests; +import javinator9889.securepass.DataClassForTests; import javinator9889.securepass.backup.drive.base.GoogleDriveBase; import javinator9889.securepass.backup.drive.base.IDriveBase; @@ -94,7 +94,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { .getDriveClient(this, latestSignedInAccount); DriveResourceClient mDriveResourceClient = Drive.getDriveResourceClient(this, latestSignedInAccount); - googleDrive.setDriveClient(mDriveClient); + //googleDrive.setDriveClient(mDriveClient); googleDrive.setDriveResourceClient(mDriveResourceClient); googleDrive.setLoggedIn(true); } /*else @@ -105,14 +105,14 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { //finish(); } break; - case REQUEST_CODE_OPEN_ITEM: + case REQUEST_CODE_OPEN_ITEM: // not necessary if (resultCode == RESULT_OK) { DriveId id = data.getParcelableExtra( OpenFileActivityOptions.EXTRA_RESPONSE_DRIVE_ID); - googleDrive.setResult(id); + //googleDrive.setResult(id); } else - googleDrive.setException(new GoogleDriveUnableToOpenFileException( - "Unable to open file")); + /*googleDrive.setException(new GoogleDriveUnableToOpenFileException( + "Unable to open file"));*/ break; default: Log.e(TAG, "Result for activity no contemplated. RequestCode: " + requestCode +