Pass data result through activities

This commit is contained in:
J-Jamet
2018-03-06 15:13:17 +01:00
parent 4876623f86
commit 4fc8c74530
7 changed files with 198 additions and 50 deletions

View File

@@ -131,7 +131,7 @@
android:resource="@xml/searchable" />
</activity>
<activity android:name="com.keepassdroid.settings.SettingsActivity" />
<activity android:name="com.keepassdroid.AutoFillAuthActivity"
<activity android:name="com.keepassdroid.autofill.AutoFillAuthActivity"
android:configChanges="orientation|keyboardHidden" />
<service android:name="com.keepassdroid.services.TimeoutService" />

View File

@@ -22,10 +22,12 @@ package com.keepassdroid.activities;
import android.app.Activity;
import android.app.Dialog;
import android.app.SearchManager;
import android.app.assist.AssistStructure;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
@@ -41,6 +43,7 @@ import android.widget.ImageView;
import com.keepassdroid.adapters.NodeAdapter;
import com.keepassdroid.app.App;
import com.keepassdroid.autofill.AutofillHelper;
import com.keepassdroid.database.Database;
import com.keepassdroid.database.PwEntry;
import com.keepassdroid.database.PwGroup;
@@ -70,6 +73,8 @@ public class GroupActivity extends ListNodesActivity
protected EditGroupDialogAction editGroupDialogAction = EditGroupDialogAction.NONE;
private ListNodesWithAddButtonView rootView;
private AutofillHelper autofillHelper;
private enum EditGroupDialogAction {
CREATION, UPDATE, NONE
}
@@ -77,15 +82,27 @@ public class GroupActivity extends ListNodesActivity
private static final String TAG = "Group Activity:";
public static void launch(Activity act) {
launch(act, null);
launch(act, null, null);
}
public static void launch(Activity act, PwGroup group) {
public static void launch(Activity act, AssistStructure assistStructure) {
launch(act, null, assistStructure);
}
public static void launch(Activity act, PwGroup group) {
launch(act, group, null);
}
public static void launch(Activity act, PwGroup group, AssistStructure assistStructure) {
Intent intent = new Intent(act, GroupActivity.class);
if ( group != null ) {
intent.putExtra(KEY_ENTRY, group.getId());
}
act.startActivityForResult(intent,0);
AutofillHelper.addAssistStructureExtraInIntent(intent, assistStructure);
if ( assistStructure != null ) {
act.startActivityForResult(intent, AutofillHelper.AUTOFILL_RESPONSE_REQUEST_CODE);
} else
act.startActivityForResult(intent, 0);
}
@Override
@@ -128,6 +145,9 @@ public class GroupActivity extends ListNodesActivity
setGroupTitle();
setGroupIcon();
autofillHelper = new AutofillHelper();
autofillHelper.retrieveAssistStructure(getIntent());
Log.w(TAG, "Finished creating tree");
if (isRoot) {
@@ -164,6 +184,30 @@ public class GroupActivity extends ListNodesActivity
return (RecyclerView) findViewById(R.id.nodes_list);
}
@Override
public void onNodeClick(PwNode node) {
// Add event when we have Autofill
AssistStructure assistStructure = autofillHelper.getAssistStructure();
if (assistStructure != null) {
mAdapter.registerANodeToUpdate(node);
switch (node.getType()) {
case GROUP:
GroupActivity.launch(this, (PwGroup) node, assistStructure);
break;
case ENTRY:
// TODO Define a DataSet object
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//AutofillHelper.onAutofillResponse(this);
setResult(Activity.RESULT_CANCELED);
finish();
}
break;
}
} else {
super.onNodeClick(node);
}
}
@Override
protected void addOptionsToAdapter(NodeAdapter nodeAdapter) {
super.addOptionsToAdapter(nodeAdapter);
@@ -224,6 +268,12 @@ public class GroupActivity extends ListNodesActivity
rootView.showButton();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
AutofillHelper.onActivityResult(this, requestCode, resultCode, data);
}
@Override
public void onSortSelected(SortNodeEnum sortNodeEnum, boolean ascending, boolean groupsBefore, boolean recycleBinBottom) {
super.onSortSelected(sortNodeEnum, ascending, groupsBefore, recycleBinBottom);

View File

@@ -17,14 +17,16 @@
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.keepassdroid;
package com.keepassdroid.autofill;
import android.app.PendingIntent;
import android.app.assist.AssistStructure;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import com.keepassdroid.fileselect.FileSelectActivity;
@@ -33,17 +35,35 @@ import com.kunzisoft.keepass.KeePass;
@RequiresApi(api = Build.VERSION_CODES.O)
public class AutoFillAuthActivity extends KeePass {
private AutofillHelper autofillHelper;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
autofillHelper = new AutofillHelper();
}
public static IntentSender getAuthIntentSenderForResponse(Context context) {
final Intent intent = new Intent(context, AutoFillAuthActivity.class);
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
.getIntentSender();
}
@Override
protected void startFileSelectActivity() {
// Pass extra for Autofill (EXTRA_ASSIST_STRUCTURE)
Bundle extras = null;
if (getIntent() != null && getIntent().getExtras() != null)
extras = getIntent().getExtras();
FileSelectActivity.launch(this, extras);
autofillHelper.retrieveAssistStructure(getIntent());
AssistStructure assistStructure = autofillHelper.getAssistStructure();
if (assistStructure != null) {
FileSelectActivity.launch(this, assistStructure);
} else {
FileSelectActivity.launch(this);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
AutofillHelper.onActivityResult(this, requestCode, resultCode, data);
}
}

View File

@@ -30,6 +30,7 @@ import android.service.autofill.SaveInfo;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.autofill.AutofillId;
import android.view.autofill.AutofillManager;
import android.widget.RemoteViews;
import com.keepassdroid.autofill.dataSource.SharedPrefsAutofillRepository;
@@ -41,7 +42,31 @@ import java.util.Set;
public class AutofillHelper {
private static final int AUTOFILL_RESPONSE_REQUEST_CODE = 81653;
public static final int AUTOFILL_RESPONSE_REQUEST_CODE = 8165;
private AssistStructure assistStructure;
public void retrieveAssistStructure(Intent intent) {
if (intent != null && intent.getExtras() != null
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
assistStructure = intent.getParcelableExtra(AutofillManager.EXTRA_ASSIST_STRUCTURE);
}
}
/**
* Call retrieveAssistStructure before
*/
public AssistStructure getAssistStructure() {
return assistStructure;
}
public static void addAssistStructureExtraInIntent(Intent intent, AssistStructure assistStructure) {
if (assistStructure != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
intent.putExtra(AutofillManager.EXTRA_ASSIST_STRUCTURE, assistStructure);
}
}
}
/**
* Define if EXTRA_AUTHENTICATION_RESULT is an extra bundle key present in the Intent
@@ -57,7 +82,7 @@ public class AutofillHelper {
* Method to hit when right key is selected
*/
@RequiresApi(api = Build.VERSION_CODES.O)
private static void onAutofillResponse(Activity activity) {
public static void onAutofillResponse(Activity activity) {
// TODO Connect this method in each item in GroupActivity
Intent mReplyIntent = null;
Intent intent = activity.getIntent();
@@ -102,7 +127,7 @@ public class AutofillHelper {
}
/**
* Wraps autofill data in a LoginCredential Dataset object which can then be sent back to the
* Wraps autofill data in a LoginCredential Dataset object which can then be sent back to the
* client View.
*/
@RequiresApi(api = Build.VERSION_CODES.O)

View File

@@ -35,7 +35,6 @@ import android.util.Log;
import android.view.autofill.AutofillId;
import android.widget.RemoteViews;
import com.keepassdroid.AutoFillAuthActivity;
import com.kunzisoft.keepass.R;
import java.util.Arrays;

View File

@@ -21,6 +21,7 @@ package com.keepassdroid.fileselect;
import android.Manifest;
import android.app.Activity;
import android.app.assist.AssistStructure;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
@@ -37,6 +38,7 @@ import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.autofill.AutofillManager;
import android.widget.EditText;
import android.widget.Toast;
@@ -88,6 +90,7 @@ public class FileSelectActivity extends StylishActivity implements
private boolean recentMode = false;
private boolean consultationMode = false;
private AssistStructure assistStructure;
private EditText openFileNameView;
private FileNameView fileNameView;
@@ -101,12 +104,15 @@ public class FileSelectActivity extends StylishActivity implements
launch(activity, null);
}
public static void launch(Activity activity, Bundle extras) {
public static void launch(Activity activity, AssistStructure assistStructure) {
Intent intent = new Intent(activity, FileSelectActivity.class);
if (extras != null)
intent.putExtras(extras);
// only to avoid visible flickering when redirecting
activity.startActivityForResult(intent, 0);
AutofillHelper.addAssistStructureExtraInIntent(intent, assistStructure);
if ( assistStructure != null ) {
activity.startActivityForResult(intent, AutofillHelper.AUTOFILL_RESPONSE_REQUEST_CODE);
} else {
// only to avoid visible flickering when redirecting
activity.startActivityForResult(intent, 0);
}
}
@Override
@@ -141,6 +147,14 @@ public class FileSelectActivity extends StylishActivity implements
RecyclerView mListFiles = (RecyclerView) findViewById(R.id.file_list);
mListFiles.setLayoutManager(new LinearLayoutManager(this));
// To retrieve info for AutoFill
if (getIntent() != null && getIntent().getExtras() != null) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
assistStructure =
getIntent().getParcelableExtra(AutofillManager.EXTRA_ASSIST_STRUCTURE);
}
}
// Open button
View openButton = findViewById(R.id.open_database);
openButton.setOnClickListener(new View.OnClickListener() {
@@ -148,9 +162,13 @@ public class FileSelectActivity extends StylishActivity implements
public void onClick(View v) {
String fileName = openFileNameView.getText().toString();
try {
PasswordActivity.launch(FileSelectActivity.this,
fileName,
getIntent().getExtras());
if (assistStructure != null) {
PasswordActivity.launch(FileSelectActivity.this,
fileName,
assistStructure);
} else {
PasswordActivity.launch(FileSelectActivity.this, fileName);
}
}
catch (ContentFileNotFoundException e) {
Toast.makeText(FileSelectActivity.this,
@@ -204,27 +222,30 @@ public class FileSelectActivity extends StylishActivity implements
File db = new File(path);
if (db.exists()) {
try {
PasswordActivity.launch(FileSelectActivity.this,
path,
getIntent().getExtras());
} catch (Exception e) {
// Ignore exception
}
launchPasswordActivityWithPath(path);
}
}
else {
try {
PasswordActivity.launch(FileSelectActivity.this,
dbUri.toString(),
getIntent().getExtras());
} catch (Exception e) {
// Ignore exception
}
if (dbUri != null)
launchPasswordActivityWithPath(dbUri.toString());
}
}
}
private void launchPasswordActivityWithPath(String path) {
try {
if (assistStructure != null) {
PasswordActivity.launch(FileSelectActivity.this,
path,
assistStructure);
} else {
PasswordActivity.launch(FileSelectActivity.this, path);
}
} catch (Exception e) {
// Ignore exception
}
}
@Override
protected void onResume() {
super.onResume();
@@ -412,8 +433,12 @@ public class FileSelectActivity extends StylishActivity implements
@Override
public void afterOpenFile(String fileName, String keyFile) {
try {
PasswordActivity.launch(FileSelectActivity.this,
fileName, keyFile, getIntent().getExtras());
if (assistStructure != null) {
PasswordActivity.launch(FileSelectActivity.this,
fileName, keyFile, assistStructure);
} else {
PasswordActivity.launch(FileSelectActivity.this, fileName, keyFile);
}
} catch (ContentFileNotFoundException e) {
Toast.makeText(FileSelectActivity.this,
R.string.file_not_found_content, Toast.LENGTH_LONG)
@@ -454,6 +479,8 @@ public class FileSelectActivity extends StylishActivity implements
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
AutofillHelper.onActivityResult(this, requestCode, resultCode, data);
keyFileHelper.onActivityResultCallback(requestCode, resultCode, data,
new KeyFileHelper.KeyFileCallback() {
@Override

View File

@@ -21,6 +21,7 @@ package com.keepassdroid.password;
import android.Manifest;
import android.app.Activity;
import android.app.assist.AssistStructure;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
@@ -53,6 +54,7 @@ import android.widget.Toast;
import com.keepassdroid.activities.GroupActivity;
import com.keepassdroid.activities.LockingActivity;
import com.keepassdroid.app.App;
import com.keepassdroid.autofill.AutofillHelper;
import com.keepassdroid.compat.BackupManagerCompat;
import com.keepassdroid.compat.ClipDataCompat;
import com.keepassdroid.compat.EditorCompat;
@@ -123,24 +125,33 @@ public class PasswordActivity extends LockingActivity
private KeyFileHelper keyFileHelper;
private AutofillHelper autofillHelper;
public static void launch(
Activity act,
String fileName) throws FileNotFoundException {
launch(act, fileName,null);
launch(act, fileName, "", null);
}
public static void launch(
Activity act,
String fileName,
Bundle extras) throws FileNotFoundException {
launch(act, fileName, "", extras);
AssistStructure assistStructure) throws FileNotFoundException {
launch(act, fileName, "", assistStructure);
}
public static void launch(
Activity act,
String fileName,
String keyFile) throws FileNotFoundException {
launch(act, fileName, keyFile, null);
}
public static void launch(
Activity act,
String fileName,
String keyFile,
Bundle extras) throws FileNotFoundException {
AssistStructure assistStructure) throws FileNotFoundException {
if (EmptyUtils.isNullOrEmpty(fileName)) {
throw new FileNotFoundException();
}
@@ -159,10 +170,13 @@ public class PasswordActivity extends LockingActivity
Intent intent = new Intent(act, PasswordActivity.class);
intent.putExtra(UriIntentInitTask.KEY_FILENAME, fileName);
intent.putExtra(UriIntentInitTask.KEY_KEYFILE, keyFile);
if (extras != null)
intent.putExtras(extras);
// only to avoid visible flickering when redirecting
act.startActivityForResult(intent, 0);
AutofillHelper.addAssistStructureExtraInIntent(intent, assistStructure);
if ( assistStructure != null ) {
act.startActivityForResult(intent, AutofillHelper.AUTOFILL_RESPONSE_REQUEST_CODE);
} else {
// only to avoid visible flickering when redirecting
act.startActivityForResult(intent, 0);
}
}
@Override
@@ -172,6 +186,9 @@ public class PasswordActivity extends LockingActivity
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
AutofillHelper.onActivityResult(this, requestCode, resultCode, data);
// TODO Tests
boolean stopResult = false;
if (keyFileHelper != null) {
stopResult = keyFileHelper.onActivityResultCallback(requestCode, resultCode, data,
@@ -262,6 +279,9 @@ public class PasswordActivity extends LockingActivity
fingerPrintAnimatedVector = new FingerPrintAnimatedVector(this,
(ImageView) findViewById(R.id.fingerprint_image));
}
autofillHelper = new AutofillHelper();
autofillHelper.retrieveAssistStructure(getIntent());
}
@@ -787,20 +807,27 @@ public class PasswordActivity extends LockingActivity
public void onClick(
DialogInterface dialog,
int which) {
// TODO GroupActivity.launch(PasswordActivity.this, getIntent().getExtras());
GroupActivity.launch(PasswordActivity.this);
launchGroupActivity();
}
});
} else if (mSuccess) {
// TODO GroupActivity.launch(PasswordActivity.this, getIntent().getExtras());
GroupActivity.launch(PasswordActivity.this);
launchGroupActivity();
} else {
displayMessage(PasswordActivity.this);
}
}
}
private void launchGroupActivity() {
AssistStructure assistStructure = autofillHelper.getAssistStructure();
if (assistStructure != null) {
GroupActivity.launch(PasswordActivity.this, assistStructure);
} else {
GroupActivity.launch(PasswordActivity.this);
}
}
private static class UriIntentInitTask extends AsyncTask<Intent, Void, Integer> {
static final String KEY_FILENAME = "fileName";