mirror of
https://github.com/moparisthebest/open-keychain
synced 2025-01-31 07:00:24 -05:00
Fix bug on Android 4.2 with DialogFragments especially with FileDialogFragment
This commit is contained in:
parent
b2dcc657c9
commit
109bc41044
@ -47,7 +47,7 @@
|
||||
|
||||
<uses-sdk
|
||||
android:minSdkVersion="8"
|
||||
android:targetSdkVersion="14" />
|
||||
android:targetSdkVersion="17" />
|
||||
|
||||
<uses-feature
|
||||
android:name="android.hardware.wifi"
|
||||
|
@ -8,5 +8,5 @@
|
||||
# project structure.
|
||||
|
||||
# Project target.
|
||||
target=android-15
|
||||
target=android-17
|
||||
android.library.reference.1=android-libs/ActionBarSherlock
|
||||
|
@ -20,6 +20,7 @@ package org.thialfihar.android.apg.ui;
|
||||
import org.spongycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.thialfihar.android.apg.Constants;
|
||||
import org.thialfihar.android.apg.Id;
|
||||
import org.thialfihar.android.apg.compatibility.Clipboard;
|
||||
import org.thialfihar.android.apg.helper.FileHelper;
|
||||
import org.thialfihar.android.apg.helper.OtherHelper;
|
||||
import org.thialfihar.android.apg.helper.PGPHelper;
|
||||
@ -32,7 +33,6 @@ import org.thialfihar.android.apg.ui.dialog.DeleteFileDialogFragment;
|
||||
import org.thialfihar.android.apg.ui.dialog.FileDialogFragment;
|
||||
import org.thialfihar.android.apg.ui.dialog.LookupUnknownKeyDialogFragment;
|
||||
import org.thialfihar.android.apg.ui.dialog.PassphraseDialogFragment;
|
||||
import org.thialfihar.android.apg.util.Compatibility;
|
||||
import org.thialfihar.android.apg.R;
|
||||
|
||||
import com.actionbarsherlock.app.SherlockFragmentActivity;
|
||||
@ -264,7 +264,7 @@ public class DecryptActivity extends SherlockFragmentActivity {
|
||||
if (mSource.getCurrentView().getId() == R.id.sourceMessage
|
||||
&& mMessage.getText().length() == 0) {
|
||||
|
||||
CharSequence clipboardText = Compatibility.getClipboardText(this);
|
||||
CharSequence clipboardText = Clipboard.getClipboardText(this);
|
||||
|
||||
String data = "";
|
||||
if (clipboardText != null) {
|
||||
|
@ -23,6 +23,7 @@ import org.spongycastle.openpgp.PGPSecretKey;
|
||||
import org.spongycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.thialfihar.android.apg.Constants;
|
||||
import org.thialfihar.android.apg.Id;
|
||||
import org.thialfihar.android.apg.compatibility.Clipboard;
|
||||
import org.thialfihar.android.apg.helper.FileHelper;
|
||||
import org.thialfihar.android.apg.helper.OtherHelper;
|
||||
import org.thialfihar.android.apg.helper.PGPHelper;
|
||||
@ -36,7 +37,6 @@ import org.thialfihar.android.apg.ui.dialog.DeleteFileDialogFragment;
|
||||
import org.thialfihar.android.apg.ui.dialog.FileDialogFragment;
|
||||
import org.thialfihar.android.apg.ui.dialog.PassphraseDialogFragment;
|
||||
import org.thialfihar.android.apg.util.Choice;
|
||||
import org.thialfihar.android.apg.util.Compatibility;
|
||||
import org.thialfihar.android.apg.R;
|
||||
|
||||
import com.actionbarsherlock.app.SherlockFragmentActivity;
|
||||
@ -758,7 +758,7 @@ public class EncryptActivity extends SherlockFragmentActivity {
|
||||
case Id.target.clipboard:
|
||||
output = data.getString(ApgIntentService.RESULT_ENCRYPTED_STRING);
|
||||
Log.d(Constants.TAG, "output: " + output);
|
||||
Compatibility.copyToClipboard(EncryptActivity.this, output);
|
||||
Clipboard.copyToClipboard(EncryptActivity.this, output);
|
||||
Toast.makeText(EncryptActivity.this,
|
||||
R.string.encryptionToClipboardSuccessful, Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
|
@ -20,6 +20,7 @@ package org.thialfihar.android.apg.ui;
|
||||
import org.thialfihar.android.apg.Constants;
|
||||
import org.thialfihar.android.apg.Id;
|
||||
import org.thialfihar.android.apg.R;
|
||||
import org.thialfihar.android.apg.compatibility.DialogFragmentWorkaround;
|
||||
import org.thialfihar.android.apg.service.ApgIntentService;
|
||||
import org.thialfihar.android.apg.service.ApgIntentServiceHandler;
|
||||
import org.thialfihar.android.apg.ui.dialog.DeleteFileDialogFragment;
|
||||
@ -159,9 +160,9 @@ public class KeyListActivity extends SherlockFragmentActivity {
|
||||
// TODO: reimplement!
|
||||
// menu.add(3, Id.menu.option.search, 0, R.string.menu_search)
|
||||
// .setIcon(R.drawable.ic_menu_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
|
||||
menu.add(0, Id.menu.option.import_keys, 2, R.string.menu_importKeys).setShowAsAction(
|
||||
menu.add(0, Id.menu.option.import_keys, 5, R.string.menu_importKeys).setShowAsAction(
|
||||
MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
|
||||
menu.add(0, Id.menu.option.export_keys, 3, R.string.menu_exportKeys).setShowAsAction(
|
||||
menu.add(0, Id.menu.option.export_keys, 6, R.string.menu_exportKeys).setShowAsAction(
|
||||
MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
|
||||
|
||||
return true;
|
||||
@ -207,6 +208,7 @@ public class KeyListActivity extends SherlockFragmentActivity {
|
||||
@Override
|
||||
public void handleMessage(Message message) {
|
||||
if (message.what == FileDialogFragment.MESSAGE_OKAY) {
|
||||
Log.d(Constants.TAG, "FileDialogFragment.MESSAGE_OKAY");
|
||||
Bundle data = message.getData();
|
||||
mImportFilename = data.getString(FileDialogFragment.MESSAGE_DATA_FILENAME);
|
||||
|
||||
@ -217,14 +219,19 @@ public class KeyListActivity extends SherlockFragmentActivity {
|
||||
};
|
||||
|
||||
// Create a new Messenger for the communication back
|
||||
Messenger messenger = new Messenger(returnHandler);
|
||||
final Messenger messenger = new Messenger(returnHandler);
|
||||
|
||||
DialogFragmentWorkaround.INTERFACE.runnableRunDelayed(new Runnable() {
|
||||
public void run() {
|
||||
mFileDialog = FileDialogFragment.newInstance(messenger,
|
||||
getString(R.string.title_importKeys), getString(R.string.specifyFileToImportFrom),
|
||||
mImportFilename, null, Id.request.filename);
|
||||
getString(R.string.title_importKeys),
|
||||
getString(R.string.specifyFileToImportFrom), mImportFilename, null,
|
||||
Id.request.filename);
|
||||
|
||||
mFileDialog.show(getSupportFragmentManager(), "fileDialog");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Show dialog where to export keys
|
||||
@ -233,6 +240,24 @@ public class KeyListActivity extends SherlockFragmentActivity {
|
||||
* if -1 export all keys
|
||||
*/
|
||||
public void showExportKeysDialog(final long keyRingRowId) {
|
||||
// Message is received after file is selected
|
||||
Handler returnHandler = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(Message message) {
|
||||
if (message.what == FileDialogFragment.MESSAGE_OKAY) {
|
||||
Bundle data = message.getData();
|
||||
mExportFilename = data.getString(FileDialogFragment.MESSAGE_DATA_FILENAME);
|
||||
|
||||
exportKeys(keyRingRowId);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Create a new Messenger for the communication back
|
||||
final Messenger messenger = new Messenger(returnHandler);
|
||||
|
||||
DialogFragmentWorkaround.INTERFACE.runnableRunDelayed(new Runnable() {
|
||||
public void run() {
|
||||
String title = null;
|
||||
if (keyRingRowId != -1) {
|
||||
// single key export
|
||||
@ -248,27 +273,13 @@ public class KeyListActivity extends SherlockFragmentActivity {
|
||||
message = getString(R.string.specifyFileToExportSecretKeysTo);
|
||||
}
|
||||
|
||||
// Message is received after file is selected
|
||||
Handler returnHandler = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(Message message) {
|
||||
if (message.what == FileDialogFragment.MESSAGE_OKAY) {
|
||||
Bundle data = message.getData();
|
||||
mExportFilename = data.getString(FileDialogFragment.MESSAGE_DATA_FILENAME);
|
||||
|
||||
exportKeys(keyRingRowId);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Create a new Messenger for the communication back
|
||||
Messenger messenger = new Messenger(returnHandler);
|
||||
|
||||
mFileDialog = FileDialogFragment.newInstance(messenger, title, message, mExportFilename,
|
||||
null, Id.request.filename);
|
||||
mFileDialog = FileDialogFragment.newInstance(messenger, title, message,
|
||||
mExportFilename, null, Id.request.filename);
|
||||
|
||||
mFileDialog.show(getSupportFragmentManager(), "fileDialog");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Show dialog to delete key
|
||||
|
@ -42,14 +42,16 @@ public class KeyListPublicActivity extends KeyListActivity {
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
menu.add(1, Id.menu.option.key_server, 2, R.string.menu_keyServer)
|
||||
.setIcon(R.drawable.ic_menu_search_list)
|
||||
.setShowAsAction(
|
||||
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
|
||||
super.onCreateOptionsMenu(menu);
|
||||
|
||||
menu.add(1, Id.menu.option.scanQRCode, 1, R.string.menu_scanQRCode)
|
||||
.setIcon(R.drawable.ic_menu_scan_qrcode)
|
||||
.setShowAsAction(
|
||||
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
|
||||
menu.add(1, Id.menu.option.key_server, 2, R.string.menu_keyServer)
|
||||
.setIcon(R.drawable.ic_menu_search_list)
|
||||
.setShowAsAction(
|
||||
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ import java.util.Vector;
|
||||
|
||||
import org.thialfihar.android.apg.Id;
|
||||
import org.thialfihar.android.apg.R;
|
||||
import org.thialfihar.android.apg.compatibility.ListFragmentWorkaround;
|
||||
import org.thialfihar.android.apg.provider.ApgDatabase;
|
||||
import org.thialfihar.android.apg.provider.ApgContract.KeyRings;
|
||||
import org.thialfihar.android.apg.provider.ApgContract.Keys;
|
||||
@ -29,8 +30,6 @@ import org.thialfihar.android.apg.provider.ApgContract.UserIds;
|
||||
import org.thialfihar.android.apg.provider.ApgDatabase.Tables;
|
||||
import org.thialfihar.android.apg.ui.widget.SelectKeyCursorAdapter;
|
||||
|
||||
import com.actionbarsherlock.app.SherlockListFragment;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.database.DatabaseUtils;
|
||||
import android.net.Uri;
|
||||
@ -38,10 +37,9 @@ import android.os.Bundle;
|
||||
import android.support.v4.content.CursorLoader;
|
||||
import android.support.v4.content.Loader;
|
||||
import android.support.v4.app.LoaderManager;
|
||||
import android.view.View;
|
||||
import android.widget.ListView;
|
||||
|
||||
public class SelectPublicKeyFragment extends SherlockListFragment implements
|
||||
public class SelectPublicKeyFragment extends ListFragmentWorkaround implements
|
||||
LoaderManager.LoaderCallbacks<Cursor> {
|
||||
|
||||
private SelectPublicKeyActivity mActivity;
|
||||
@ -81,15 +79,6 @@ public class SelectPublicKeyFragment extends SherlockListFragment implements
|
||||
getLoaderManager().initLoader(0, null, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Workaround for Android 4.1. Items are not checked in layout. See
|
||||
* http://code.google.com/p/android/issues/detail?id=35885
|
||||
*/
|
||||
@Override
|
||||
public void onListItemClick(ListView l, View v, int position, long id) {
|
||||
l.setItemChecked(position, l.isItemChecked(position));
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects items based on master key ids in list view
|
||||
*
|
||||
|
@ -33,7 +33,6 @@ import android.support.v4.app.FragmentActivity;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class DeleteFileDialogFragment extends DialogFragment {
|
||||
|
||||
private static final String ARG_DELETE_FILE = "delete_file";
|
||||
|
||||
/**
|
||||
@ -66,6 +65,8 @@ public class DeleteFileDialogFragment extends DialogFragment {
|
||||
alert.setMessage(this.getString(R.string.fileDeleteConfirmation, deleteFile));
|
||||
|
||||
alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
dismiss();
|
||||
|
||||
|
@ -36,15 +36,14 @@ import android.support.v4.app.DialogFragment;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
|
||||
public class DeleteKeyDialogFragment extends DialogFragment {
|
||||
|
||||
private Messenger mMessenger;
|
||||
|
||||
private static final String ARG_MESSENGER = "messenger";
|
||||
private static final String ARG_DELETE_KEY_RING_ROW_ID = "delete_file";
|
||||
private static final String ARG_KEY_TYPE = "key_type";
|
||||
|
||||
public static final int MESSAGE_OKAY = 1;
|
||||
|
||||
private Messenger mMessenger;
|
||||
|
||||
/**
|
||||
* Creates new instance of this delete file dialog fragment
|
||||
*/
|
||||
@ -68,6 +67,7 @@ public class DeleteKeyDialogFragment extends DialogFragment {
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
final FragmentActivity activity = getActivity();
|
||||
mMessenger = getArguments().getParcelable(ARG_MESSENGER);
|
||||
|
||||
final long deleteKeyRingRowId = getArguments().getLong(ARG_DELETE_KEY_RING_ROW_ID);
|
||||
final int keyType = getArguments().getInt(ARG_KEY_TYPE);
|
||||
@ -92,6 +92,8 @@ public class DeleteKeyDialogFragment extends DialogFragment {
|
||||
: R.string.secretKeyDeletionConfirmation, userId));
|
||||
builder.setIcon(android.R.drawable.ic_dialog_alert);
|
||||
builder.setPositiveButton(R.string.btn_delete, new DialogInterface.OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
if (keyType == Id.type.public_key) {
|
||||
ProviderHelper.deletePublicKeyRing(activity, deleteKeyRingRowId);
|
||||
@ -105,6 +107,8 @@ public class DeleteKeyDialogFragment extends DialogFragment {
|
||||
}
|
||||
});
|
||||
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
dismiss();
|
||||
}
|
||||
|
@ -38,9 +38,6 @@ import android.widget.EditText;
|
||||
import android.widget.ImageButton;
|
||||
|
||||
public class FileDialogFragment extends DialogFragment {
|
||||
|
||||
private Messenger mMessenger;
|
||||
|
||||
private static final String ARG_MESSENGER = "messenger";
|
||||
private static final String ARG_TITLE = "title";
|
||||
private static final String ARG_MESSAGE = "message";
|
||||
@ -53,6 +50,8 @@ public class FileDialogFragment extends DialogFragment {
|
||||
public static final String MESSAGE_DATA_FILENAME = "filename";
|
||||
public static final String MESSAGE_DATA_CHECKED = "checked";
|
||||
|
||||
private Messenger mMessenger;
|
||||
|
||||
/**
|
||||
* Creates new instance of this file dialog fragment
|
||||
*/
|
||||
@ -107,7 +106,8 @@ public class FileDialogFragment extends DialogFragment {
|
||||
mBrowse.setOnClickListener(new View.OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
// only .asc or .gpg files
|
||||
FileHelper.openFile(activity, mFilename.getText().toString(), "text/plain", requestCode);
|
||||
FileHelper.openFile(activity, mFilename.getText().toString(), "text/plain",
|
||||
requestCode);
|
||||
}
|
||||
});
|
||||
|
||||
@ -127,6 +127,10 @@ public class FileDialogFragment extends DialogFragment {
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
Log.d(Constants.TAG, "onclock");
|
||||
|
||||
dismiss();
|
||||
|
||||
boolean checked = false;
|
||||
if (mCheckBox.isEnabled()) {
|
||||
checked = mCheckBox.isChecked();
|
||||
@ -138,12 +142,11 @@ public class FileDialogFragment extends DialogFragment {
|
||||
data.putBoolean(MESSAGE_DATA_CHECKED, checked);
|
||||
|
||||
sendMessageToHandler(MESSAGE_OKAY, data);
|
||||
|
||||
dismiss();
|
||||
}
|
||||
});
|
||||
|
||||
alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
dismiss();
|
||||
|
@ -37,15 +37,14 @@ import org.thialfihar.android.apg.ui.KeyServerQueryActivity;
|
||||
import org.thialfihar.android.apg.util.Log;
|
||||
|
||||
public class LookupUnknownKeyDialogFragment extends DialogFragment {
|
||||
|
||||
private Messenger mMessenger;
|
||||
|
||||
private static final String ARG_MESSENGER = "messenger";
|
||||
private static final String ARG_UNKNOWN_KEY_ID = "unknown_key_id";
|
||||
|
||||
public static final int MESSAGE_OKAY = 1;
|
||||
public static final int MESSAGE_CANCEL = 2;
|
||||
|
||||
private Messenger mMessenger;
|
||||
|
||||
/**
|
||||
* Creates new instance of this dialog fragment
|
||||
*
|
||||
@ -82,6 +81,8 @@ public class LookupUnknownKeyDialogFragment extends DialogFragment {
|
||||
PGPHelper.getSmallFingerPrint(unknownKeyId)));
|
||||
|
||||
alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
dismiss();
|
||||
|
||||
@ -94,6 +95,8 @@ public class LookupUnknownKeyDialogFragment extends DialogFragment {
|
||||
}
|
||||
});
|
||||
alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
dismiss();
|
||||
|
||||
|
@ -56,14 +56,12 @@ import android.widget.TextView.OnEditorActionListener;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class PassphraseDialogFragment extends DialogFragment implements OnEditorActionListener {
|
||||
|
||||
private Messenger mMessenger;
|
||||
|
||||
private static final String ARG_MESSENGER = "messenger";
|
||||
private static final String ARG_SECRET_KEY_ID = "secret_key_id";
|
||||
|
||||
public static final int MESSAGE_OKAY = 1;
|
||||
|
||||
private Messenger mMessenger;
|
||||
private EditText mPassphraseEditText;
|
||||
|
||||
/**
|
||||
@ -182,6 +180,8 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor
|
||||
mPassphraseEditText = (EditText) view.findViewById(R.id.passphrase_passphrase);
|
||||
|
||||
alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
dismiss();
|
||||
|
||||
@ -217,6 +217,8 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor
|
||||
});
|
||||
|
||||
alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
dismiss();
|
||||
}
|
||||
|
@ -26,7 +26,6 @@ import android.support.v4.app.DialogFragment;
|
||||
import android.view.KeyEvent;
|
||||
|
||||
public class ProgressDialogFragment extends DialogFragment {
|
||||
|
||||
private static final String ARG_MESSAGE_ID = "message_id";
|
||||
private static final String ARG_STYLE = "style";
|
||||
|
||||
|
@ -42,8 +42,6 @@ import android.widget.Toast;
|
||||
import android.widget.TextView.OnEditorActionListener;
|
||||
|
||||
public class SetPassphraseDialogFragment extends DialogFragment implements OnEditorActionListener {
|
||||
private Messenger mMessenger;
|
||||
|
||||
private static final String ARG_MESSENGER = "messenger";
|
||||
private static final String ARG_TITLE = "title";
|
||||
|
||||
@ -51,6 +49,7 @@ public class SetPassphraseDialogFragment extends DialogFragment implements OnEdi
|
||||
|
||||
public static final String MESSAGE_NEW_PASSPHRASE = "new_passphrase";
|
||||
|
||||
private Messenger mMessenger;
|
||||
private EditText mPassphraseEditText;
|
||||
private EditText mPassphraseAgainEditText;
|
||||
|
||||
@ -97,6 +96,8 @@ public class SetPassphraseDialogFragment extends DialogFragment implements OnEdi
|
||||
mPassphraseAgainEditText = (EditText) view.findViewById(R.id.passphrase_passphrase_again);
|
||||
|
||||
alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
dismiss();
|
||||
|
||||
@ -129,6 +130,8 @@ public class SetPassphraseDialogFragment extends DialogFragment implements OnEdi
|
||||
});
|
||||
|
||||
alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
dismiss();
|
||||
}
|
||||
|
@ -1,96 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.thialfihar.android.apg.util;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import android.content.Context;
|
||||
import org.thialfihar.android.apg.util.Log;
|
||||
|
||||
public class Compatibility {
|
||||
|
||||
private static final String clipboardLabel = "APG";
|
||||
|
||||
/**
|
||||
* Wrapper around ClipboardManager based on Android version using Reflection API
|
||||
*
|
||||
* @param context
|
||||
* @param text
|
||||
*/
|
||||
public static void copyToClipboard(Context context, String text) {
|
||||
Object clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
try {
|
||||
if ("android.text.ClipboardManager".equals(clipboard.getClass().getName())) {
|
||||
Method methodSetText = clipboard.getClass()
|
||||
.getMethod("setText", CharSequence.class);
|
||||
methodSetText.invoke(clipboard, text);
|
||||
} else if ("android.content.ClipboardManager".equals(clipboard.getClass().getName())) {
|
||||
Class<?> classClipData = Class.forName("android.content.ClipData");
|
||||
Method methodNewPlainText = classClipData.getMethod("newPlainText",
|
||||
CharSequence.class, CharSequence.class);
|
||||
Object clip = methodNewPlainText.invoke(null, clipboardLabel, text);
|
||||
methodNewPlainText = clipboard.getClass()
|
||||
.getMethod("setPrimaryClip", classClipData);
|
||||
methodNewPlainText.invoke(clipboard, clip);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e("ProjectsException", "There was and error copying the text to the clipboard: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper around ClipboardManager based on Android version using Reflection API
|
||||
*
|
||||
* @param context
|
||||
* @param text
|
||||
*/
|
||||
public static CharSequence getClipboardText(Context context) {
|
||||
Object clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
try {
|
||||
if ("android.text.ClipboardManager".equals(clipboard.getClass().getName())) {
|
||||
// CharSequence text = clipboard.getText();
|
||||
Method methodGetText = clipboard.getClass().getMethod("getText");
|
||||
Object text = methodGetText.invoke(clipboard);
|
||||
|
||||
return (CharSequence) text;
|
||||
} else if ("android.content.ClipboardManager".equals(clipboard.getClass().getName())) {
|
||||
// ClipData clipData = clipboard.getPrimaryClip();
|
||||
Method methodGetPrimaryClip = clipboard.getClass().getMethod("getPrimaryClip");
|
||||
Object clipData = methodGetPrimaryClip.invoke(clipboard);
|
||||
|
||||
// ClipData.Item clipDataItem = clipData.getItemAt(0);
|
||||
Method methodGetItemAt = clipData.getClass().getMethod("getItemAt", int.class);
|
||||
Object clipDataItem = methodGetItemAt.invoke(clipData, 0);
|
||||
|
||||
// CharSequence text = clipDataItem.coerceToText(context);
|
||||
Method methodGetString = clipDataItem.getClass().getMethod("coerceToText",
|
||||
Context.class);
|
||||
Object text = methodGetString.invoke(clipDataItem, context);
|
||||
|
||||
return (CharSequence) text;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e("ProjectsException", "There was and error getting the text from the clipboard: "
|
||||
+ e.getMessage());
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user