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

View File

@@ -22,10 +22,12 @@ package com.keepassdroid.activities;
import android.app.Activity; import android.app.Activity;
import android.app.Dialog; import android.app.Dialog;
import android.app.SearchManager; import android.app.SearchManager;
import android.app.assist.AssistStructure;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@@ -41,6 +43,7 @@ import android.widget.ImageView;
import com.keepassdroid.adapters.NodeAdapter; import com.keepassdroid.adapters.NodeAdapter;
import com.keepassdroid.app.App; import com.keepassdroid.app.App;
import com.keepassdroid.autofill.AutofillHelper;
import com.keepassdroid.database.Database; import com.keepassdroid.database.Database;
import com.keepassdroid.database.PwEntry; import com.keepassdroid.database.PwEntry;
import com.keepassdroid.database.PwGroup; import com.keepassdroid.database.PwGroup;
@@ -70,6 +73,8 @@ public class GroupActivity extends ListNodesActivity
protected EditGroupDialogAction editGroupDialogAction = EditGroupDialogAction.NONE; protected EditGroupDialogAction editGroupDialogAction = EditGroupDialogAction.NONE;
private ListNodesWithAddButtonView rootView; private ListNodesWithAddButtonView rootView;
private AutofillHelper autofillHelper;
private enum EditGroupDialogAction { private enum EditGroupDialogAction {
CREATION, UPDATE, NONE CREATION, UPDATE, NONE
} }
@@ -77,15 +82,27 @@ public class GroupActivity extends ListNodesActivity
private static final String TAG = "Group Activity:"; private static final String TAG = "Group Activity:";
public static void launch(Activity act) { 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); Intent intent = new Intent(act, GroupActivity.class);
if ( group != null ) { if ( group != null ) {
intent.putExtra(KEY_ENTRY, group.getId()); 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 @Override
@@ -128,6 +145,9 @@ public class GroupActivity extends ListNodesActivity
setGroupTitle(); setGroupTitle();
setGroupIcon(); setGroupIcon();
autofillHelper = new AutofillHelper();
autofillHelper.retrieveAssistStructure(getIntent());
Log.w(TAG, "Finished creating tree"); Log.w(TAG, "Finished creating tree");
if (isRoot) { if (isRoot) {
@@ -164,6 +184,30 @@ public class GroupActivity extends ListNodesActivity
return (RecyclerView) findViewById(R.id.nodes_list); 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 @Override
protected void addOptionsToAdapter(NodeAdapter nodeAdapter) { protected void addOptionsToAdapter(NodeAdapter nodeAdapter) {
super.addOptionsToAdapter(nodeAdapter); super.addOptionsToAdapter(nodeAdapter);
@@ -224,6 +268,12 @@ public class GroupActivity extends ListNodesActivity
rootView.showButton(); rootView.showButton();
} }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
AutofillHelper.onActivityResult(this, requestCode, resultCode, data);
}
@Override @Override
public void onSortSelected(SortNodeEnum sortNodeEnum, boolean ascending, boolean groupsBefore, boolean recycleBinBottom) { public void onSortSelected(SortNodeEnum sortNodeEnum, boolean ascending, boolean groupsBefore, boolean recycleBinBottom) {
super.onSortSelected(sortNodeEnum, ascending, groupsBefore, 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/>. * 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.PendingIntent;
import android.app.assist.AssistStructure;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentSender; import android.content.IntentSender;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi; import android.support.annotation.RequiresApi;
import com.keepassdroid.fileselect.FileSelectActivity; import com.keepassdroid.fileselect.FileSelectActivity;
@@ -33,17 +35,35 @@ import com.kunzisoft.keepass.KeePass;
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
public class AutoFillAuthActivity extends KeePass { 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) { public static IntentSender getAuthIntentSenderForResponse(Context context) {
final Intent intent = new Intent(context, AutoFillAuthActivity.class); final Intent intent = new Intent(context, AutoFillAuthActivity.class);
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT) return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
.getIntentSender(); .getIntentSender();
} }
@Override
protected void startFileSelectActivity() { protected void startFileSelectActivity() {
// Pass extra for Autofill (EXTRA_ASSIST_STRUCTURE) // Pass extra for Autofill (EXTRA_ASSIST_STRUCTURE)
Bundle extras = null; autofillHelper.retrieveAssistStructure(getIntent());
if (getIntent() != null && getIntent().getExtras() != null) AssistStructure assistStructure = autofillHelper.getAssistStructure();
extras = getIntent().getExtras(); if (assistStructure != null) {
FileSelectActivity.launch(this, extras); 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.support.annotation.RequiresApi;
import android.util.Log; import android.util.Log;
import android.view.autofill.AutofillId; import android.view.autofill.AutofillId;
import android.view.autofill.AutofillManager;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import com.keepassdroid.autofill.dataSource.SharedPrefsAutofillRepository; import com.keepassdroid.autofill.dataSource.SharedPrefsAutofillRepository;
@@ -41,7 +42,31 @@ import java.util.Set;
public class AutofillHelper { 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 * 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 * Method to hit when right key is selected
*/ */
@RequiresApi(api = Build.VERSION_CODES.O) @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 // TODO Connect this method in each item in GroupActivity
Intent mReplyIntent = null; Intent mReplyIntent = null;
Intent intent = activity.getIntent(); 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. * client View.
*/ */
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)

View File

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

View File

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

View File

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