From 4826e0a8c89e43401a3188d7a4d51cdbfd64c37a Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 22 Jun 2015 23:44:03 +0200 Subject: [PATCH] move select file logic into EncryptDecryptOverviewFragment --- .../keychain/ui/DecryptActivity.java | 55 ++----- .../ui/DecryptFilesInputFragment.java | 145 ------------------ .../ui/EncryptDecryptOverviewFragment.java | 45 ++++-- .../keychain/util/FileHelper.java | 8 +- .../encrypt_decrypt_overview_fragment.xml | 30 +--- OpenKeychain/src/main/res/values/strings.xml | 7 +- 6 files changed, 63 insertions(+), 227 deletions(-) delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFilesInputFragment.java diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptActivity.java index d14c8555c..4375be740 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptActivity.java @@ -17,6 +17,7 @@ package org.sufficientlysecure.keychain.ui; + import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; @@ -33,7 +34,6 @@ import android.support.v4.app.FragmentTransaction; import android.view.View; import android.widget.Toast; -import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.intents.OpenKeychainIntents; import org.sufficientlysecure.keychain.provider.TemporaryStorageProvider; @@ -43,13 +43,7 @@ import org.sufficientlysecure.keychain.ui.base.BaseActivity; public class DecryptActivity extends BaseActivity { /* Intents */ - public static final String ACTION_DECRYPT_DATA = OpenKeychainIntents.DECRYPT_DATA; - // TODO handle this intent - // public static final String ACTION_DECRYPT_TEXT = OpenKeychainIntents.DECRYPT_TEXT; - public static final String ACTION_DECRYPT_FROM_CLIPBOARD = Constants.INTENT_PREFIX + "DECRYPT_DATA_CLIPBOARD"; - - // intern - public static final String ACTION_DECRYPT_DATA_OPEN = Constants.INTENT_PREFIX + "DECRYPT_DATA_OPEN"; + public static final String ACTION_DECRYPT_FROM_CLIPBOARD = "DECRYPT_DATA_CLIPBOARD"; @Override public void onCreate(Bundle savedInstanceState) { @@ -86,6 +80,13 @@ public class DecryptActivity extends BaseActivity { String action = intent.getAction(); + if (action == null) { + Toast.makeText(this, "Error: No action specified!", Toast.LENGTH_LONG).show(); + setResult(Activity.RESULT_CANCELED); + finish(); + return; + } + try { switch (action) { @@ -93,8 +94,6 @@ public class DecryptActivity extends BaseActivity { // When sending to Keychain Decrypt via share menu // Binary via content provider (could also be files) // override uri to get stream from send - action = ACTION_DECRYPT_DATA; - if (intent.hasExtra(Intent.EXTRA_STREAM)) { uris.add(intent.getParcelableExtra(Intent.EXTRA_STREAM)); } else if (intent.hasExtra(Intent.EXTRA_TEXT)) { @@ -107,8 +106,6 @@ public class DecryptActivity extends BaseActivity { } case Intent.ACTION_SEND_MULTIPLE: { - action = ACTION_DECRYPT_DATA; - if (intent.hasExtra(Intent.EXTRA_STREAM)) { uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); } else if (intent.hasExtra(Intent.EXTRA_TEXT)) { @@ -122,8 +119,6 @@ public class DecryptActivity extends BaseActivity { } case ACTION_DECRYPT_FROM_CLIPBOARD: { - action = ACTION_DECRYPT_DATA; - ClipboardManager clipMan = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = clipMan.getPrimaryClip(); @@ -148,11 +143,9 @@ public class DecryptActivity extends BaseActivity { break; } + // for everything else, just work on the intent data + case OpenKeychainIntents.DECRYPT_DATA: case Intent.ACTION_VIEW: - // Android's Action when opening file associated to Keychain (see AndroidManifest.xml) - action = ACTION_DECRYPT_DATA; - - // fallthrough default: uris.add(intent.getData()); @@ -165,19 +158,15 @@ public class DecryptActivity extends BaseActivity { return; } - if (ACTION_DECRYPT_DATA.equals(action)) { - // Definitely need a data uri with the decrypt_data intent - if (uris.isEmpty()) { - Toast.makeText(this, "No data to decrypt!", Toast.LENGTH_LONG).show(); - setResult(Activity.RESULT_CANCELED); - finish(); - } - displayListFragment(uris); + // Definitely need a data uri with the decrypt_data intent + if (uris.isEmpty()) { + Toast.makeText(this, "No data to decrypt!", Toast.LENGTH_LONG).show(); + setResult(Activity.RESULT_CANCELED); + finish(); return; } - boolean showOpenDialog = ACTION_DECRYPT_DATA_OPEN.equals(action); - displayInputFragment(showOpenDialog); + displayListFragment(uris); } @@ -189,16 +178,6 @@ public class DecryptActivity extends BaseActivity { return tempFile; } - public void displayInputFragment(boolean showOpenDialog) { - DecryptFilesInputFragment frag = DecryptFilesInputFragment.newInstance(showOpenDialog); - - // Add the fragment to the 'fragment_container' FrameLayout - // NOTE: We use commitAllowingStateLoss() to prevent weird crashes! - getSupportFragmentManager().beginTransaction() - .replace(R.id.decrypt_files_fragment_container, frag) - .commit(); - } - public void displayListFragment(ArrayList inputUris) { DecryptListFragment frag = DecryptListFragment.newInstance(inputUris); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFilesInputFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFilesInputFragment.java deleted file mode 100644 index 93066ee32..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFilesInputFragment.java +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Copyright (C) 2014 Dominik Schürmann - * Copyright (C) 2015 Vincent Breitmoser - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.sufficientlysecure.keychain.ui; - -import java.util.ArrayList; - -import android.app.Activity; -import android.content.Intent; -import android.net.Uri; -import android.os.Build; -import android.os.Bundle; -import android.support.v4.app.Fragment; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; - -import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.ui.util.Notify; -import org.sufficientlysecure.keychain.util.FileHelper; - -public class DecryptFilesInputFragment extends Fragment { - public static final String ARG_URI = "uri"; - public static final String ARG_OPEN_DIRECTLY = "open_directly"; - - private static final int REQUEST_CODE_INPUT = 0x00007003; - - private TextView mFilename; - private View mDecryptButton; - - private Uri mInputUri = null; - - public static DecryptFilesInputFragment newInstance(boolean openDirectly) { - DecryptFilesInputFragment frag = new DecryptFilesInputFragment(); - - Bundle args = new Bundle(); - args.putBoolean(ARG_OPEN_DIRECTLY, openDirectly); - - frag.setArguments(args); - - return frag; - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.decrypt_files_input_fragment, container, false); - - mFilename = (TextView) view.findViewById(R.id.decrypt_files_filename); - mDecryptButton = view.findViewById(R.id.decrypt_files_action_decrypt); - view.findViewById(R.id.decrypt_files_browse).setOnClickListener(new View.OnClickListener() { - public void onClick(View v) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - FileHelper.openDocument(DecryptFilesInputFragment.this, "*/*", REQUEST_CODE_INPUT); - } else { - FileHelper.openFile(DecryptFilesInputFragment.this, mInputUri, "*/*", - REQUEST_CODE_INPUT); - } - } - }); - mDecryptButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - decryptAction(); - } - }); - - return view; - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - - outState.putParcelable(ARG_URI, mInputUri); - } - - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - - Bundle state = savedInstanceState != null ? savedInstanceState : getArguments(); - setInputUri(state.getParcelable(ARG_URI)); - - // should only come from args - if (state.getBoolean(ARG_OPEN_DIRECTLY, false)) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - FileHelper.openDocument(DecryptFilesInputFragment.this, "*/*", REQUEST_CODE_INPUT); - } else { - FileHelper.openFile(DecryptFilesInputFragment.this, mInputUri, "*/*", REQUEST_CODE_INPUT); - } - } - } - - private void setInputUri(Uri inputUri) { - if (inputUri == null) { - mInputUri = null; - mFilename.setText(""); - return; - } - - mInputUri = inputUri; - mFilename.setText(FileHelper.getFilename(getActivity(), mInputUri)); - } - - private void decryptAction() { - if (mInputUri == null) { - Notify.create(getActivity(), R.string.no_file_selected, Notify.Style.ERROR).show(); - return; - } - - DecryptActivity activity = (DecryptActivity) getActivity(); - - ArrayList uris = new ArrayList<>(); - uris.add(mInputUri); - activity.displayListFragment(uris); - } - - @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode != REQUEST_CODE_INPUT) { - return; - } - - if (resultCode == Activity.RESULT_OK && data != null) { - setInputUri(data.getData()); - } - } - -} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptDecryptOverviewFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptDecryptOverviewFragment.java index 6e5589940..566ad1d67 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptDecryptOverviewFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptDecryptOverviewFragment.java @@ -18,8 +18,15 @@ package org.sufficientlysecure.keychain.ui; + +import java.util.ArrayList; +import java.util.regex.Matcher; + +import android.app.Activity; import android.content.Intent; +import android.net.Uri; import android.os.AsyncTask; +import android.os.Build; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; @@ -29,16 +36,17 @@ import android.view.ViewGroup; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.ClipboardReflection; -import org.sufficientlysecure.keychain.operations.results.OperationResult; import org.sufficientlysecure.keychain.pgp.PgpHelper; +import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.util.SubtleAttentionSeeker; - -import java.util.regex.Matcher; +import org.sufficientlysecure.keychain.util.FileHelper; public class EncryptDecryptOverviewFragment extends Fragment { View mClipboardIcon; + private static final int REQUEST_CODE_INPUT = 0x00007003; + @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); @@ -74,9 +82,11 @@ public class EncryptDecryptOverviewFragment extends Fragment { mDecryptFile.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Intent filesDecrypt = new Intent(getActivity(), DecryptActivity.class); - filesDecrypt.setAction(DecryptActivity.ACTION_DECRYPT_DATA_OPEN); - startActivity(filesDecrypt); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + FileHelper.openDocument(EncryptDecryptOverviewFragment.this, "*/*", REQUEST_CODE_INPUT); + } else { + FileHelper.openFile(EncryptDecryptOverviewFragment.this, null, "*/*", REQUEST_CODE_INPUT); + } } }); @@ -135,12 +145,23 @@ public class EncryptDecryptOverviewFragment extends Fragment { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { - // if a result has been returned, display a notify - if (data != null && data.hasExtra(OperationResult.EXTRA_RESULT)) { - OperationResult result = data.getParcelableExtra(OperationResult.EXTRA_RESULT); - result.createNotify(getActivity()).show(); - } else { - super.onActivityResult(requestCode, resultCode, data); + if (requestCode != REQUEST_CODE_INPUT) { + return; + } + + if (resultCode == Activity.RESULT_OK && data != null) { + Uri uri = data.getData(); + if (uri == null) { + Notify.create(getActivity(), R.string.no_file_selected, Notify.Style.ERROR).show(this); + return; + } + + Intent intent = new Intent(getActivity(), DecryptActivity.class); + intent.setAction(Intent.ACTION_VIEW); + intent.setData(uri); + startActivity(intent); + } } + } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelper.java index 558ab9a85..e223217ef 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelper.java @@ -53,7 +53,6 @@ public class FileHelper { /** * Checks if external storage is mounted if file is located on external storage * - * @param file * @return true if storage is mounted */ public static boolean isStorageMounted(String file) { @@ -70,7 +69,6 @@ public class FileHelper { * Opens the preferred installed file manager on Android and shows a toast if no manager is * installed. * - * @param fragment * @param last default selected Uri, not supported by all file managers * @param mimeType can be text/plain for example * @param requestCode requestCode used to identify the result coming back from file manager to @@ -145,7 +143,6 @@ public class FileHelper { /** * Opens the storage browser on Android 4.4 or later for opening a file * - * @param fragment * @param mimeType can be text/plain for example * @param multiple allow file chooser to return multiple files * @param requestCode used to identify the result coming back from storage browser onActivityResult() in your @@ -163,7 +160,6 @@ public class FileHelper { /** * Opens the storage browser on Android 4.4 or later for saving a file * - * @param fragment * @param mimeType can be text/plain for example * @param suggestedName a filename desirable for the file to be saved * @param requestCode used to identify the result coming back from storage browser onActivityResult() in your @@ -271,7 +267,7 @@ public class FileHelper { } - public static interface FileDialogCallback { - public void onFileSelected(File file, boolean checked); + public interface FileDialogCallback { + void onFileSelected(File file, boolean checked); } } diff --git a/OpenKeychain/src/main/res/layout/encrypt_decrypt_overview_fragment.xml b/OpenKeychain/src/main/res/layout/encrypt_decrypt_overview_fragment.xml index bd640d9af..394f14a34 100644 --- a/OpenKeychain/src/main/res/layout/encrypt_decrypt_overview_fragment.xml +++ b/OpenKeychain/src/main/res/layout/encrypt_decrypt_overview_fragment.xml @@ -1,5 +1,6 @@ + android:paddingRight="0dp" + tools:ignore="UseCompoundDrawables"> - - - - - - - + android:layout_gravity="center_vertical" + android:text="@string/btn_decrypt_clipboard" /> "Keyserver" "Fingerprint" "Encrypt" - "Decrypt" + "Decrypt / Verify" "Current expiry" "New expiry" @@ -80,9 +80,8 @@ "Add file(s)" "Share decrypted text" "Copy decrypted text" - "Decrypt text from clipboard" - "and verify signatures" - "Decrypt files" + "Read from clipboard" + "Select input file" "Encrypt files" "Encrypt text" "Add additional email address"