Remove compat methods

This commit is contained in:
J-Jamet committed 2018-05-15 15:43:03 +02:00
1 parent 9442c7ef07
commit d3ebbba2a1
18 files changed
+35 -341

No files matched your search

@@ -42,7 +42,6 @@ import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.adapters.NodeAdapter;
import com.kunzisoft.keepass.app.App;
import com.kunzisoft.keepass.autofill.AutofillHelper;
import com.kunzisoft.keepass.compat.EditorCompat;
import com.kunzisoft.keepass.database.Database;
import com.kunzisoft.keepass.database.PwDatabase;
import com.kunzisoft.keepass.database.PwEntry;
@@ -193,7 +192,7 @@ public abstract class ListNodesActivity extends LockingActivity
editor.putBoolean(getString(R.string.sort_ascending_key), ascending);
editor.putBoolean(getString(R.string.sort_group_before_key), groupsBefore);
editor.putBoolean(getString(R.string.sort_recycle_bin_bottom_key), recycleBinBottom);
EditorCompat.apply(editor);
editor.apply();
// Tell the adapter to refresh it's list
mAdapter.notifyChangeSort(sortNodeEnum, ascending, groupsBefore);
@@ -24,8 +24,6 @@ import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import com.kunzisoft.keepass.compat.BuildCompat;
/**
* Locking Hide Activity that sets FLAG_SECURE to prevent screenshots, and from
* appearing in the recent app preview
@@ -37,10 +35,7 @@ public abstract class LockingHideActivity extends LockingActivity {
super.onCreate(savedInstanceState);
// Several gingerbread devices have problems with FLAG_SECURE
int ver = BuildCompat.getSdkVersion();
if (ver >= BuildCompat.VERSION_CODE_ICE_CREAM_SANDWICH || ver < BuildCompat.VERSION_CODE_GINGERBREAD) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
}
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
}
/* (non-Javadoc) Workaround for HTC Linkify issues
@@ -1,66 +0,0 @@
/*
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX 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.
*
* KeePass DX 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 KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.compat;
import android.content.Context;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
@SuppressWarnings({"unchecked", "rawtypes"})
public class BackupManagerCompat {
private static Class classBackupManager;
private static Constructor constructorBackupManager;
private static Method dataChanged;
private Object backupManager;
static {
try {
classBackupManager = Class.forName("android.app.backup.BackupManager");
constructorBackupManager = classBackupManager.getConstructor(Context.class);
dataChanged = classBackupManager.getMethod("dataChanged", (Class[]) null);
} catch (Exception e) {
// Do nothing, class does not exist
}
}
public BackupManagerCompat(Context ctx) {
if (constructorBackupManager != null) {
try {
backupManager = constructorBackupManager.newInstance(ctx);
} catch (Exception e) {
// Do nothing
}
}
}
public void dataChanged() {
if (backupManager != null && dataChanged != null) {
try {
dataChanged.invoke(backupManager, (Object[]) null);
} catch (Exception e) {
// Do nothing
}
}
}
}
@@ -1,53 +0,0 @@
/*
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX 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.
*
* KeePass DX 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 KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.compat;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import java.lang.reflect.Constructor;
// This compatiblity hack can go away when support for Android 1.5 api level 3 is dropped
public class BitmapDrawableCompat {
private static Constructor<BitmapDrawable> constResBitmap;
static {
try {
constResBitmap = BitmapDrawable.class.getConstructor(Resources.class, Bitmap.class);
// This constructor is support in this api version
} catch (Exception e) {
// This constructor is not supported
}
}
public static BitmapDrawable getBitmapDrawable(Resources res, Bitmap bitmap) {
if (constResBitmap != null) {
try {
return constResBitmap.newInstance(res, bitmap);
} catch (Exception e) {
// Do nothing, fall through to the safe constructor
}
}
return new BitmapDrawable(bitmap);
}
}
@@ -1,70 +0,0 @@
/*
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX 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.
*
* KeePass DX 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 KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.compat;
import android.os.Build;
import java.lang.reflect.Field;
public class BuildCompat {
private static Field manufacturer;
private static String manuText;
public static final int VERSION_CODE_GINGERBREAD = 9;
public static final int VERSION_CODE_ICE_CREAM_SANDWICH = 14;
public static final int VERSION_CODE_JELLY_BEAN = 16;
public static final int VERSION_CODE_JELLY_BEAN_MR2 = 18;
public static final int VERSION_KITKAT = 19;
public static final int VERSION_CODE_M = 23;
private static Field versionSDK;
private static int versionInt;
static {
// MANUFACTURER is only available in API version 4 and later
try {
manufacturer = Build.class.getField("MANUFACTURER");
manuText = (String) manufacturer.get(null);
} catch (Exception e) {
manuText = "";
}
// SDK is only available in API version 4 and later
try {
versionSDK = Build.VERSION.class.getField("SDK_INT");
versionInt = versionSDK.getInt(null);
} catch (Exception e) {
try {
versionInt = Integer.parseInt(Build.VERSION.SDK);
} catch (Exception nfe) {
versionInt = -1;
}
}
}
public static String getManufacturer() {
return manuText;
}
public static int getSdkVersion() {
return versionInt;
}
}
@@ -25,9 +25,6 @@ import android.net.Uri;
import java.lang.reflect.Method;
public class ClipDataCompat {
private static Class clipData;
private static Class clipDataItem;
private static Class clipDescription;
private static Method getClipDataFromIntent;
private static Method getDescription;
private static Method getItemCount;
@@ -39,14 +36,14 @@ public class ClipDataCompat {
static {
try {
clipData = Class.forName("android.content.ClipData");
Class clipData = Class.forName("android.content.ClipData");
getDescription = clipData.getMethod("getDescription", (Class[])null);
getItemCount = clipData.getMethod("getItemCount", (Class[])null);
getItemAt = clipData.getMethod("getItemAt", new Class[]{int.class});
clipDescription = Class.forName("android.content.ClipDescription");
Class clipDescription = Class.forName("android.content.ClipDescription");
getLabel = clipDescription.getMethod("getLabel", (Class[])null);
clipDataItem = Class.forName("android.content.ClipData$Item");
Class clipDataItem = Class.forName("android.content.ClipData$Item");
getUri = clipDataItem.getMethod("getUri", (Class[])null);
getClipDataFromIntent = Intent.class.getMethod("getClipData", (Class[])null);
@@ -58,7 +55,6 @@ public class ClipDataCompat {
}
public static Uri getUriFromIntent(Intent i, String key) {
boolean clipDataSucceeded = false;
if (initSucceded) {
try {
Object clip = getClipDataFromIntent.invoke(i);
@@ -1,52 +0,0 @@
/*
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX 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.
*
* KeePass DX 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 KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.compat;
import android.content.ContentResolver;
import android.net.Uri;
import java.lang.reflect.Method;
public class ContentResolverCompat {
public static boolean available;
private static Class contentResolver;
private static Method takePersistableUriPermission;
static {
try {
contentResolver = ContentResolver.class;
takePersistableUriPermission = contentResolver.getMethod("takePersistableUriPermission", new Class[]{Uri.class, int.class});
available = true;
} catch (Exception e) {
available = false;
}
}
public static void takePersistableUriPermission(ContentResolver resolver, Uri uri, int modeFlags) {
if (available) {
try {
takePersistableUriPermission.invoke(resolver, new Object[]{uri, modeFlags});
} catch (Exception e) {
// Fail silently
}
}
}
}
@@ -1,53 +0,0 @@
/*
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX 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.
*
* KeePass DX 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 KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.compat;
import android.app.Activity;
import android.content.SharedPreferences;
import java.lang.reflect.Method;
public class EditorCompat {
private static Method apply;
static {
try {
apply = Activity.class.getMethod("apply", (Class<SharedPreferences.Editor>[]) null);
} catch (Exception e) {
// Substitute commit for apply when not available (API level < 9)
try {
apply = Activity.class.getMethod("commit", (Class<SharedPreferences.Editor>[]) null);
} catch (Exception f) {
// Should be impossible, but leave apply null in this case
}
}
}
public static void apply(SharedPreferences.Editor edit) {
try {
apply.invoke(edit, (Object[]) null);
} catch (Exception e) {
// Shouldn't be possible, but call commit directly if this happens
edit.commit();
}
}
}
@@ -44,7 +44,6 @@ public final class PRNGFixes {
private static final byte[] BUILD_FINGERPRINT_AND_DEVICE_SERIAL =
getBuildFingerprintAndDeviceSerial();
private static int sdkVersion = BuildCompat.getSdkVersion();
/** Hidden constructor to prevent instantiation. */
private PRNGFixes() {}
@@ -67,11 +66,11 @@ public final class PRNGFixes {
private static boolean supportedOnThisDevice() {
// Blacklist on samsung devices
if (StrUtil.indexOfIgnoreCase(BuildCompat.getManufacturer(), "samsung", Locale.ENGLISH) >= 0) {
if (StrUtil.indexOfIgnoreCase(Build.MANUFACTURER, "samsung", Locale.ENGLISH) >= 0) {
return false;
}
if (sdkVersion > BuildCompat.VERSION_CODE_JELLY_BEAN_MR2) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
return false;
}
@@ -126,8 +125,8 @@ public final class PRNGFixes {
* @throws SecurityException if the fix is needed but could not be applied.
*/
private static void applyOpenSSLFix() throws SecurityException {
if ((sdkVersion < BuildCompat.VERSION_CODE_JELLY_BEAN)
|| (sdkVersion > BuildCompat.VERSION_CODE_JELLY_BEAN_MR2)) {
if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
|| (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2)) {
// No need to apply the fix
return;
}
@@ -162,7 +161,7 @@ public final class PRNGFixes {
*/
private static void installLinuxPRNGSecureRandom()
throws SecurityException {
if (sdkVersion > BuildCompat.VERSION_CODE_JELLY_BEAN_MR2) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
// No need to apply the fix
return;
}
@@ -22,6 +22,7 @@ package com.kunzisoft.keepass.compat;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import com.kunzisoft.keepass.R;
@@ -45,7 +46,7 @@ public class StorageAF {
}
}
public static boolean supportsStorageFramework() { return BuildCompat.getSdkVersion() >= BuildCompat.VERSION_KITKAT; }
public static boolean supportsStorageFramework() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; }
public static boolean useStorageFramework(Context ctx) {
if (!supportsStorageFramework()) { return false; }
@@ -20,9 +20,9 @@
package com.kunzisoft.keepass.dialogs;
import android.content.Context;
import android.os.Build;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.compat.BuildCompat;
public class ReadOnlyDialog extends WarningDialog {
@@ -31,7 +31,7 @@ public class ReadOnlyDialog extends WarningDialog {
warning = context.getString(R.string.read_only_warning);
if (BuildCompat.getSdkVersion() >= BuildCompat.VERSION_KITKAT) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
warning = warning.concat("\n\n").concat(context.getString(R.string.read_only_kitkat_warning));
}
}
@@ -26,13 +26,14 @@ import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.kunzisoft.keepass.compat.EditorCompat;
import android.util.Log;
import java.io.File;
import java.io.FileFilter;
public class FileDbHelper {
private static final String TAG = FileDbHelper.class.getName();
public static final String LAST_FILENAME = "lastFile";
public static final String LAST_KEYFILE = "lastKey";
@@ -102,9 +103,9 @@ public class FileDbHelper {
SharedPreferences.Editor editor = prefs.edit();
editor.remove(LAST_FILENAME);
editor.remove(LAST_KEYFILE);
EditorCompat.apply(editor);
editor.apply();
} catch (Exception e) {
assert(true);
Log.e(TAG, "Unable to delete database preference", e);
}
}
}
@@ -252,7 +253,7 @@ public class FileDbHelper {
* Deletes a database including its journal file and other auxiliary files
* that may have been created by the database engine.
*
* @param file The database file path.
* @param ctx Context to get database path
* @return True if the database was successfully deleted.
*/
public static boolean deleteDatabase(Context ctx) {
@@ -29,7 +29,6 @@ import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import com.kunzisoft.keepass.compat.ContentResolverCompat;
import com.kunzisoft.keepass.compat.StorageAF;
import com.kunzisoft.keepass.utils.Interaction;
import com.kunzisoft.keepass.utils.UriUtil;
@@ -200,9 +199,11 @@ public class KeyFileHelper {
if (StorageAF.useStorageFramework(activity)) {
try {
// try to persist read and write permissions
ContentResolver resolver = activity.getContentResolver();
ContentResolverCompat.takePersistableUriPermission(resolver, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
ContentResolverCompat.takePersistableUriPermission(resolver, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
ContentResolver resolver = activity.getContentResolver();
resolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
resolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
} catch (Exception e) {
// nop
}
@@ -27,7 +27,6 @@ import android.net.Uri;
import android.preference.PreferenceManager;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.compat.EditorCompat;
import com.kunzisoft.keepass.utils.UriUtil;
import java.io.File;
@@ -190,7 +189,7 @@ public class RecentFileHistory {
for (int i = 0; i < size; i++) {
edit.putString(keyprefix + "_" + i, list.get(i));
}
EditorCompat.apply(edit);
edit.apply();
}
public void deleteFile(Uri uri) {
@@ -25,6 +25,7 @@ import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
@@ -33,7 +34,6 @@ import android.util.Log;
import android.widget.ImageView;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.compat.BitmapDrawableCompat;
import com.kunzisoft.keepass.database.PwIcon;
import com.kunzisoft.keepass.database.PwIconCustom;
import com.kunzisoft.keepass.database.PwIconStandard;
@@ -305,7 +305,7 @@ public class IconDrawableFactory {
bitmap = resize(bitmap);
draw = BitmapDrawableCompat.getBitmapDrawable(context.getResources(), bitmap);
draw = new BitmapDrawable(context.getResources(), bitmap);
customIconMap.put(icon.uuid, draw);
}
@@ -22,6 +22,7 @@ package com.kunzisoft.keepass.password;
import android.Manifest;
import android.app.Activity;
import android.app.assist.AssistStructure;
import android.app.backup.BackupManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
@@ -58,9 +59,7 @@ import com.kunzisoft.keepass.activities.GroupActivity;
import com.kunzisoft.keepass.activities.LockingActivity;
import com.kunzisoft.keepass.app.App;
import com.kunzisoft.keepass.autofill.AutofillHelper;
import com.kunzisoft.keepass.compat.BackupManagerCompat;
import com.kunzisoft.keepass.compat.ClipDataCompat;
import com.kunzisoft.keepass.compat.EditorCompat;
import com.kunzisoft.keepass.database.Database;
import com.kunzisoft.keepass.database.action.LoadDBRunnable;
import com.kunzisoft.keepass.database.action.OnFinishRunnable;
@@ -790,9 +789,9 @@ public class PasswordActivity extends StylishActivity
SharedPreferences.Editor editor = prefs.edit();
editor.putString(KEY_DEFAULT_FILENAME, newDefaultFileName);
EditorCompat.apply(editor);
editor.apply();
BackupManagerCompat backupManager = new BackupManagerCompat(PasswordActivity.this);
BackupManager backupManager = new BackupManager(PasswordActivity.this);
backupManager.dataChanged();
}
}
@@ -20,6 +20,7 @@
package com.kunzisoft.keepass.settings;
import android.app.Activity;
import android.app.backup.BackupManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
@@ -28,14 +29,13 @@ import android.view.MenuItem;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.activities.LockingActivity;
import com.kunzisoft.keepass.compat.BackupManagerCompat;
public class SettingsActivity extends LockingActivity implements MainPreferenceFragment.Callback {
private static final String TAG_NESTED = "TAG_NESTED";
private BackupManagerCompat backupManager;
private BackupManager backupManager;
private Toolbar toolbar;
@@ -78,7 +78,7 @@ public class SettingsActivity extends LockingActivity implements MainPreferenceF
.commit();
}
backupManager = new BackupManagerCompat(this);
backupManager = new BackupManager(this);
}
@Override
@@ -26,7 +26,6 @@ import android.preference.PreferenceManager;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.activities.LockingActivity;
import com.kunzisoft.keepass.app.App;
import com.kunzisoft.keepass.compat.EditorCompat;
public class TimeoutHelper {
@@ -41,8 +40,7 @@ public class TimeoutHelper {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(act);
SharedPreferences.Editor edit = prefs.edit();
edit.putLong(act.getString(R.string.timeout_key), time);
EditorCompat.apply(edit);
edit.apply();
if ( App.getDB().getLoaded() ) {
Timeout.start(act);