Merge branch 'apply-for-android-8' into 'master'
SharedPreferences.Editor.apply() for android 8 Turns out that `SharedPreferences.Editor.apply()` was not added until `android-9`, so this is a little trick to support `android-8` still after the changes in c3b47ecd5a380678dd2df3dc2549155429d28514 See merge request !249
This commit is contained in:
		
						commit
						f033276f67
					
				| @ -172,7 +172,7 @@ public abstract class ProviderTestCase2MockContext<T extends ContentProvider> ex | ||||
| 
 | ||||
|     @TargetApi(Build.VERSION_CODES.HONEYCOMB) | ||||
|     private void shutdownProvider() { | ||||
|         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { | ||||
|         if (Build.VERSION.SDK_INT >= 11) { | ||||
|             mProvider.shutdown(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @ -4,9 +4,9 @@ import android.content.Context; | ||||
| import android.content.SharedPreferences; | ||||
| import android.content.pm.FeatureInfo; | ||||
| import android.content.pm.PackageManager; | ||||
| import android.os.Build; | ||||
| import android.preference.PreferenceManager; | ||||
| 
 | ||||
| import org.fdroid.fdroid.compat.Compatibility; | ||||
| import org.fdroid.fdroid.compat.SupportedArchitectures; | ||||
| import org.fdroid.fdroid.data.Apk; | ||||
| 
 | ||||
| @ -18,7 +18,7 @@ import java.util.Set; | ||||
| 
 | ||||
| // Call getIncompatibleReasons(apk) on an instance of this class to | ||||
| // find reasons why an apk may be incompatible with the user's device. | ||||
| public class CompatibilityChecker extends Compatibility { | ||||
| public class CompatibilityChecker { | ||||
| 
 | ||||
|     private static final String TAG = "Compatibility"; | ||||
| 
 | ||||
| @ -85,11 +85,11 @@ public class CompatibilityChecker extends Compatibility { | ||||
| 
 | ||||
|         List<String> incompatibleReasons = new ArrayList<>(); | ||||
| 
 | ||||
|         if (!hasApi(apk.minSdkVersion)) { | ||||
|         if (Build.VERSION.SDK_INT < apk.minSdkVersion) { | ||||
|             incompatibleReasons.add(context.getString( | ||||
|                     R.string.minsdk_or_later, | ||||
|                     Utils.getAndroidVersionName(apk.minSdkVersion))); | ||||
|         } else if (!upToApi(apk.maxSdkVersion)) { | ||||
|         } else if (Build.VERSION.SDK_INT > apk.maxSdkVersion) { | ||||
|             incompatibleReasons.add(context.getString( | ||||
|                     R.string.up_to_maxsdk, | ||||
|                     Utils.getAndroidVersionName(apk.maxSdkVersion))); | ||||
|  | ||||
| @ -6,6 +6,8 @@ import android.os.Build; | ||||
| import android.preference.PreferenceManager; | ||||
| import android.util.Log; | ||||
| 
 | ||||
| import org.fdroid.fdroid.compat.PreferencesCompat; | ||||
| 
 | ||||
| import java.net.InetSocketAddress; | ||||
| import java.net.Proxy; | ||||
| import java.net.SocketAddress; | ||||
| @ -36,9 +38,8 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh | ||||
|         preferences = PreferenceManager.getDefaultSharedPreferences(context); | ||||
|         preferences.registerOnSharedPreferenceChangeListener(this); | ||||
|         if (preferences.getString(PREF_LOCAL_REPO_NAME, null) == null) { | ||||
|             preferences.edit() | ||||
|                     .putString(PREF_LOCAL_REPO_NAME, getDefaultLocalRepoName()) | ||||
|                     .apply(); | ||||
|             PreferencesCompat.apply(preferences.edit() | ||||
|                     .putString(PREF_LOCAL_REPO_NAME, getDefaultLocalRepoName())); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| @ -111,7 +112,7 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh | ||||
|     } | ||||
| 
 | ||||
|     public void setPrivilegedInstallerEnabled(boolean enable) { | ||||
|         preferences.edit().putBoolean(PREF_PRIVILEGED_INSTALLER, enable).apply(); | ||||
|         PreferencesCompat.apply(preferences.edit().putBoolean(PREF_PRIVILEGED_INSTALLER, enable)); | ||||
|     } | ||||
| 
 | ||||
|     public boolean isFirstTime() { | ||||
| @ -119,7 +120,7 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh | ||||
|     } | ||||
| 
 | ||||
|     public void setFirstTime(boolean firstTime) { | ||||
|         preferences.edit().putBoolean(PREF_FIRST_TIME, firstTime).apply(); | ||||
|         PreferencesCompat.apply(preferences.edit().putBoolean(PREF_FIRST_TIME, firstTime)); | ||||
|     } | ||||
| 
 | ||||
|     public boolean isPostPrivilegedInstall() { | ||||
| @ -127,7 +128,7 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh | ||||
|     } | ||||
| 
 | ||||
|     public void setPostPrivilegedInstall(boolean postInstall) { | ||||
|         preferences.edit().putBoolean(PREF_POST_PRIVILEGED_INSTALL, postInstall).apply(); | ||||
|         PreferencesCompat.apply(preferences.edit().putBoolean(PREF_POST_PRIVILEGED_INSTALL, postInstall)); | ||||
|     } | ||||
| 
 | ||||
|     public boolean shouldCacheApks() { | ||||
| @ -147,7 +148,7 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh | ||||
|     } | ||||
| 
 | ||||
|     public void setShowNfcDuringSwap(boolean show) { | ||||
|         preferences.edit().putBoolean(PREF_SHOW_NFC_DURING_SWAP, show).apply(); | ||||
|         PreferencesCompat.apply(preferences.edit().putBoolean(PREF_SHOW_NFC_DURING_SWAP, show)); | ||||
|     } | ||||
| 
 | ||||
|     public boolean expertMode() { | ||||
|  | ||||
| @ -40,6 +40,7 @@ import android.text.TextUtils; | ||||
| import android.util.Log; | ||||
| import android.widget.Toast; | ||||
| 
 | ||||
| import org.fdroid.fdroid.compat.PreferencesCompat; | ||||
| import org.fdroid.fdroid.data.ApkProvider; | ||||
| import org.fdroid.fdroid.data.App; | ||||
| import org.fdroid.fdroid.data.AppProvider; | ||||
| @ -145,7 +146,7 @@ public class UpdateService extends IntentService implements ProgressListener { | ||||
|         // | ||||
|         //   http://stackoverflow.com/a/20032920 | ||||
|         // | ||||
|         if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) { | ||||
|         if (Build.VERSION.SDK_INT <= 10) { | ||||
|             Intent pendingIntent = new Intent(this, FDroid.class); | ||||
|             pendingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||||
|             notificationBuilder.setContentIntent(PendingIntent.getActivity(this, 0, pendingIntent, PendingIntent.FLAG_UPDATE_CURRENT)); | ||||
| @ -406,7 +407,7 @@ public class UpdateService extends IntentService implements ProgressListener { | ||||
| 
 | ||||
|             SharedPreferences.Editor e = prefs.edit(); | ||||
|             e.putLong(Preferences.PREF_UPD_LAST, System.currentTimeMillis()); | ||||
|             e.apply(); | ||||
|             PreferencesCompat.apply(e); | ||||
| 
 | ||||
|             if (errorRepos == 0) { | ||||
|                 if (changes) { | ||||
|  | ||||
| @ -1,15 +1,16 @@ | ||||
| package org.fdroid.fdroid.compat; | ||||
| 
 | ||||
| import android.annotation.TargetApi; | ||||
| import android.os.Build; | ||||
| import android.widget.ArrayAdapter; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| public class ArrayAdapterCompat extends Compatibility { | ||||
| public class ArrayAdapterCompat { | ||||
| 
 | ||||
|     @TargetApi(11) | ||||
|     public static <T> void addAll(ArrayAdapter<T> adapter, List<T> list) { | ||||
|         if (hasApi(11)) { | ||||
|         if (Build.VERSION.SDK_INT >= 11) { | ||||
|             adapter.addAll(list); | ||||
|         } else { | ||||
|             for (T category : list) { | ||||
|  | ||||
| @ -4,13 +4,14 @@ import android.annotation.TargetApi; | ||||
| import android.content.ClipData; | ||||
| import android.content.ClipboardManager; | ||||
| import android.content.Context; | ||||
| import android.os.Build; | ||||
| 
 | ||||
| public abstract class ClipboardCompat extends Compatibility { | ||||
| public abstract class ClipboardCompat { | ||||
| 
 | ||||
|     public abstract String getText(); | ||||
| 
 | ||||
|     public static ClipboardCompat create(Context context) { | ||||
|         if (hasApi(11)) { | ||||
|         if (Build.VERSION.SDK_INT >= 11) { | ||||
|             return new HoneycombClipboard(context); | ||||
|         } | ||||
|         return new OldClipboard(); | ||||
|  | ||||
| @ -1,21 +0,0 @@ | ||||
| package org.fdroid.fdroid.compat; | ||||
| 
 | ||||
| import android.os.Build; | ||||
| 
 | ||||
| public abstract class Compatibility { | ||||
| 
 | ||||
|     // like minSdkVersion | ||||
|     protected static boolean hasApi(int apiLevel) { | ||||
|         return getApi() >= apiLevel; | ||||
|     } | ||||
| 
 | ||||
|     // like maxSdkVersion | ||||
|     protected static boolean upToApi(int apiLevel) { | ||||
|         return apiLevel < 1 || getApi() <= apiLevel; | ||||
|     } | ||||
| 
 | ||||
|     static int getApi() { | ||||
|         return Build.VERSION.SDK_INT; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -17,15 +17,15 @@ import java.lang.reflect.Method; | ||||
|  * This helps prevent things like SQL injection, shell command injection | ||||
|  * and other attacks based on putting various characters into filenames. | ||||
|  */ | ||||
| public class FileCompat extends Compatibility { | ||||
| public class FileCompat { | ||||
| 
 | ||||
|     private static final String TAG = "FileCompat"; | ||||
| 
 | ||||
|     public static boolean symlink(SanitizedFile source, SanitizedFile dest) { | ||||
| 
 | ||||
|         if (hasApi(21)) { | ||||
|         if (Build.VERSION.SDK_INT >= 21) { | ||||
|             symlinkOs(source, dest); | ||||
|         } else if (hasApi(15)) { | ||||
|         } else if (Build.VERSION.SDK_INT >= 15) { | ||||
|             symlinkLibcore(source, dest); | ||||
|         } else { | ||||
|             symlinkRuntime(source, dest); | ||||
| @ -88,7 +88,7 @@ public class FileCompat extends Compatibility { | ||||
|     @TargetApi(9) | ||||
|     public static boolean setReadable(SanitizedFile file, boolean readable, boolean ownerOnly) { | ||||
| 
 | ||||
|         if (hasApi(9)) { | ||||
|         if (Build.VERSION.SDK_INT >= 9) { | ||||
|             return file.setReadable(readable, ownerOnly); | ||||
|         } | ||||
|         String mode; | ||||
| @ -126,7 +126,7 @@ public class FileCompat extends Compatibility { | ||||
|     @TargetApi(9) | ||||
|     public static boolean setExecutable(SanitizedFile file, boolean executable, boolean ownerOnly) { | ||||
| 
 | ||||
|         if (hasApi(9)) { | ||||
|         if (Build.VERSION.SDK_INT >= 9) { | ||||
|             return file.setExecutable(executable, ownerOnly); | ||||
|         } | ||||
|         String mode; | ||||
|  | ||||
| @ -36,10 +36,8 @@ import java.security.Security; | ||||
|  * Cryptography Architecture primitives. A good place to invoke them is in the | ||||
|  * application's {@code onCreate}. | ||||
|  */ | ||||
| public final class PRNGFixes extends Compatibility { | ||||
| public final class PRNGFixes { | ||||
| 
 | ||||
|     private static final int VERSION_CODE_JELLY_BEAN = 16; | ||||
|     private static final int VERSION_CODE_JELLY_BEAN_MR2 = 18; | ||||
|     private static final byte[] BUILD_FINGERPRINT_AND_DEVICE_SERIAL = | ||||
|         getBuildFingerprintAndDeviceSerial(); | ||||
| 
 | ||||
| @ -63,7 +61,7 @@ public final class PRNGFixes extends Compatibility { | ||||
|      * @throws SecurityException if the fix is needed but could not be applied. | ||||
|      */ | ||||
|     private static void applyOpenSSLFix() throws SecurityException { | ||||
|         if (getApi() < VERSION_CODE_JELLY_BEAN || getApi() > VERSION_CODE_JELLY_BEAN_MR2) { | ||||
|         if (Build.VERSION.SDK_INT < 16 || Build.VERSION.SDK_INT > 18) { | ||||
|             // No need to apply the fix | ||||
|             return; | ||||
|         } | ||||
| @ -98,7 +96,7 @@ public final class PRNGFixes extends Compatibility { | ||||
|      */ | ||||
|     private static void installLinuxPRNGSecureRandom() | ||||
|             throws SecurityException { | ||||
|         if (getApi() > VERSION_CODE_JELLY_BEAN_MR2) { | ||||
|         if (Build.VERSION.SDK_INT > 18) { | ||||
|             // No need to apply the fix | ||||
|             return; | ||||
|         } | ||||
|  | ||||
| @ -2,17 +2,18 @@ package org.fdroid.fdroid.compat; | ||||
| 
 | ||||
| import android.annotation.TargetApi; | ||||
| import android.content.pm.PackageManager; | ||||
| import android.os.Build; | ||||
| import android.util.Log; | ||||
| 
 | ||||
| import org.fdroid.fdroid.Utils; | ||||
| 
 | ||||
| public class PackageManagerCompat extends Compatibility { | ||||
| public class PackageManagerCompat { | ||||
| 
 | ||||
|     private static final String TAG = "PackageManagerCompat"; | ||||
| 
 | ||||
|     @TargetApi(11) | ||||
|     public static void setInstaller(PackageManager mPm, String packageName) { | ||||
|         if (!hasApi(11)) return; | ||||
|         if (Build.VERSION.SDK_INT < 11) return; | ||||
|         try { | ||||
|             mPm.setInstallerPackageName(packageName, "org.fdroid.fdroid"); | ||||
|             Utils.debugLog(TAG, "Installer package name for " + packageName + " set successfully"); | ||||
|  | ||||
| @ -0,0 +1,15 @@ | ||||
| package org.fdroid.fdroid.compat; | ||||
| 
 | ||||
| import android.content.SharedPreferences; | ||||
| import android.os.Build; | ||||
| 
 | ||||
| public class PreferencesCompat { | ||||
| 
 | ||||
|     public static void apply(SharedPreferences.Editor e) { | ||||
|         if (Build.VERSION.SDK_INT < 9) { | ||||
|             e.commit(); | ||||
|         } else { | ||||
|             e.apply(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -3,7 +3,7 @@ package org.fdroid.fdroid.compat; | ||||
| import android.annotation.TargetApi; | ||||
| import android.os.Build; | ||||
| 
 | ||||
| public class SupportedArchitectures extends Compatibility { | ||||
| public class SupportedArchitectures { | ||||
| 
 | ||||
|     /** | ||||
|      * The most preferred ABI is the first element in the list. | ||||
| @ -11,7 +11,7 @@ public class SupportedArchitectures extends Compatibility { | ||||
|     @TargetApi(Build.VERSION_CODES.LOLLIPOP) | ||||
|     @SuppressWarnings("deprecation") | ||||
|     public static String[] getAbis() { | ||||
|         if (hasApi(21)) { | ||||
|         if (Build.VERSION.SDK_INT >= 21) { | ||||
|             return Build.SUPPORTED_ABIS; | ||||
|         } | ||||
|         return new String[]{Build.CPU_ABI, Build.CPU_ABI2}; | ||||
|  | ||||
| @ -12,7 +12,7 @@ public class UriCompat { | ||||
|      */ | ||||
|     public static String getQueryParameter(Uri uri, String key) { | ||||
|         String value = uri.getQueryParameter(key); | ||||
|         if (value != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { | ||||
|         if (value != null && Build.VERSION.SDK_INT < 14) { | ||||
|             value = value.replaceAll("\\+", " "); | ||||
|         } | ||||
|         return value; | ||||
|  | ||||
| @ -127,7 +127,7 @@ public abstract class FDroidProvider extends ContentProvider { | ||||
|     @TargetApi(11) | ||||
|     private Set<String> getKeySet(ContentValues values) { | ||||
| 
 | ||||
|         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { | ||||
|         if (Build.VERSION.SDK_INT >= 11) { | ||||
|             return values.keySet(); | ||||
|         } | ||||
| 
 | ||||
|  | ||||
| @ -62,7 +62,7 @@ public class DefaultSdk14Installer extends Installer { | ||||
|         // following extras only work when being installed as system-app | ||||
|         // https://code.google.com/p/android/issues/detail?id=42253 | ||||
|         intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true); | ||||
|         if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) { | ||||
|         if (android.os.Build.VERSION.SDK_INT < 16) { | ||||
|             // deprecated in Android 4.1 | ||||
|             intent.putExtra(Intent.EXTRA_ALLOW_REPLACE, true); | ||||
|         } | ||||
|  | ||||
| @ -124,7 +124,7 @@ public abstract class Installer { | ||||
|         } | ||||
| 
 | ||||
|         // else -> DefaultInstaller | ||||
|         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { | ||||
|         if (android.os.Build.VERSION.SDK_INT >= 14) { | ||||
|             // Default installer on Android >= 4.0 | ||||
|             try { | ||||
|                 Utils.debugLog(TAG, "try default installer for Android >= 4"); | ||||
|  | ||||
| @ -47,10 +47,10 @@ abstract class InstallExtension { | ||||
|     } | ||||
| 
 | ||||
|     public static InstallExtension create(final Context context) { | ||||
|         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | ||||
|         if (Build.VERSION.SDK_INT >= 21) { | ||||
|             return new LollipopImpl(context); | ||||
|         } | ||||
|         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | ||||
|         if (Build.VERSION.SDK_INT >= 19) { | ||||
|             return new KitKatToLollipopImpl(context); | ||||
|         } | ||||
|         return new PreKitKatImpl(context); | ||||
|  | ||||
| @ -20,6 +20,7 @@ import org.fdroid.fdroid.AppDetails; | ||||
| import org.fdroid.fdroid.Preferences; | ||||
| import org.fdroid.fdroid.UpdateService; | ||||
| import org.fdroid.fdroid.Utils; | ||||
| import org.fdroid.fdroid.compat.PreferencesCompat; | ||||
| import org.fdroid.fdroid.data.App; | ||||
| import org.fdroid.fdroid.data.AppProvider; | ||||
| import org.fdroid.fdroid.views.AppListAdapter; | ||||
| @ -141,7 +142,7 @@ public abstract class AppListFragment extends ListFragment implements | ||||
|         boolean hasTriedEmptyUpdate = prefs.getBoolean(triedEmptyUpdate, false); | ||||
|         if (!hasTriedEmptyUpdate) { | ||||
|             Utils.debugLog(TAG, "Empty app list, and we haven't done an update yet. Forcing repo update."); | ||||
|             prefs.edit().putBoolean(triedEmptyUpdate, true).apply(); | ||||
|             PreferencesCompat.apply(prefs.edit().putBoolean(triedEmptyUpdate, true)); | ||||
|             UpdateService.updateNow(getActivity()); | ||||
|             return true; | ||||
|         } | ||||
|  | ||||
| @ -24,6 +24,7 @@ import org.fdroid.fdroid.Preferences; | ||||
| import org.fdroid.fdroid.R; | ||||
| import org.fdroid.fdroid.Utils; | ||||
| import org.fdroid.fdroid.compat.ArrayAdapterCompat; | ||||
| import org.fdroid.fdroid.compat.PreferencesCompat; | ||||
| import org.fdroid.fdroid.data.AppProvider; | ||||
| import org.fdroid.fdroid.views.AppListAdapter; | ||||
| import org.fdroid.fdroid.views.AvailableAppListAdapter; | ||||
| @ -230,7 +231,7 @@ public class AvailableAppsFragment extends AppListFragment implements | ||||
|                 Context.MODE_PRIVATE); | ||||
|         SharedPreferences.Editor e = p.edit(); | ||||
|         e.putString(CATEGORY_KEY, currentCategory); | ||||
|         e.apply(); | ||||
|         PreferencesCompat.apply(e); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|  | ||||
| @ -19,6 +19,7 @@ import org.fdroid.fdroid.FDroidApp; | ||||
| import org.fdroid.fdroid.Preferences; | ||||
| import org.fdroid.fdroid.PreferencesActivity; | ||||
| import org.fdroid.fdroid.R; | ||||
| import org.fdroid.fdroid.compat.PreferencesCompat; | ||||
| import org.fdroid.fdroid.installer.PrivilegedInstaller; | ||||
| 
 | ||||
| import info.guardianproject.netcipher.NetCipher; | ||||
| @ -204,13 +205,13 @@ public class PreferencesFragment extends PreferenceFragment | ||||
|                         // privileged permission are granted, i.e. the extension is installed correctly | ||||
|                         SharedPreferences.Editor editor = pref.getSharedPreferences().edit(); | ||||
|                         editor.putBoolean(Preferences.PREF_PRIVILEGED_INSTALLER, true); | ||||
|                         editor.apply(); | ||||
|                         PreferencesCompat.apply(editor); | ||||
|                         pref.setChecked(true); | ||||
|                     } else { | ||||
|                         // privileged permission not available | ||||
|                         SharedPreferences.Editor editor = pref.getSharedPreferences().edit(); | ||||
|                         editor.putBoolean(Preferences.PREF_PRIVILEGED_INSTALLER, false); | ||||
|                         editor.apply(); | ||||
|                         PreferencesCompat.apply(editor); | ||||
|                         pref.setChecked(false); | ||||
| 
 | ||||
|                         AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity()); | ||||
| @ -248,7 +249,7 @@ public class PreferencesFragment extends PreferenceFragment | ||||
|                 } else { | ||||
|                     SharedPreferences.Editor editor = pref.getSharedPreferences().edit(); | ||||
|                     editor.putBoolean(Preferences.PREF_PRIVILEGED_INSTALLER, false); | ||||
|                     editor.apply(); | ||||
|                     PreferencesCompat.apply(editor); | ||||
|                     pref.setChecked(false); | ||||
|                 } | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Daniel Martí
						Daniel Martí