Compare commits

...

5 Commits

Author SHA1 Message Date
Jules Aguillon
fc68f2e07d Relase 1.19.1 2022-11-11 15:51:51 +01:00
Edgars
8350efaa83 Add missing Latvian translations 2022-11-11 15:43:34 +01:00
Jules Aguillon
ef34303c7e Avoid crashing in direct-boot mode
The settings activity can't open in direct-boot mode. The emoji pane
opens without the "last used" data.
2022-11-11 15:39:28 +01:00
Jules Aguillon
f1ce6abe5a Direct-boot aware preferences
Store preferences in device protected storage, which is available before
the device is unlocked.
The keyboard was crashing when trying to access the encrypted
preferences.

The emoji pane uses a separate preferences file, the old data is lost.

The SettingsActivity can't easily use the new preferences storage.
Instead, it continues to use the "default" preferences store, which is
copied back to the protected storage when needed.
2022-11-11 14:27:02 +01:00
Jules Aguillon
29fbb27a8a Increase the targetSdkVersion to 31 2022-11-11 10:17:47 +01:00
8 changed files with 179 additions and 52 deletions

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="juloo.keyboard2" android:versionCode="27" android:versionName="1.19.0" android:hardwareAccelerated="false"> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="juloo.keyboard2" android:versionCode="28" android:versionName="1.19.1" android:hardwareAccelerated="false">
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="30"/> <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="31"/>
<application android:label="@string/app_name" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:hardwareAccelerated="false"> <application android:label="@string/app_name" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:hardwareAccelerated="false">
<service android:name="juloo.keyboard2.Keyboard2" android:label="@string/app_name" android:permission="android.permission.BIND_INPUT_METHOD" android:exported="true" android:directBootAware="true"> <service android:name="juloo.keyboard2.Keyboard2" android:label="@string/app_name" android:permission="android.permission.BIND_INPUT_METHOD" android:exported="true" android:directBootAware="true">
<intent-filter> <intent-filter>
@@ -8,7 +8,7 @@
</intent-filter> </intent-filter>
<meta-data android:name="android.view.im" android:resource="@xml/method"/> <meta-data android:name="android.view.im" android:resource="@xml/method"/>
</service> </service>
<activity android:name="juloo.keyboard2.SettingsActivity" android:icon="@drawable/ic_launcher" android:label="@string/settings_activity_label" android:theme="@style/android:Theme.DeviceDefault"> <activity android:name="juloo.keyboard2.SettingsActivity" android:icon="@drawable/ic_launcher" android:label="@string/settings_activity_label" android:theme="@style/android:Theme.DeviceDefault" android:exported="true" android:directBootAware="true">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN"/> <action android:name="android.intent.action.MAIN"/>
</intent-filter> </intent-filter>

View File

@@ -0,0 +1,6 @@
Updated translations: Latvian
Fix crash when typing device password
Increase target SDK version to 31
Thanks to the contributors: @eandersons

View File

@@ -10,6 +10,10 @@
<string name="pref_accents_e_all_installed">Rādīt uzsvara zīmes visām uzstādītajām valodām</string> <string name="pref_accents_e_all_installed">Rādīt uzsvara zīmes visām uzstādītajām valodām</string>
<string name="pref_accents_e_selected">Rādīt uzsvara zīmes tikai atlasītajām valodām</string> <string name="pref_accents_e_selected">Rādīt uzsvara zīmes tikai atlasītajām valodām</string>
<string name="pref_accents_e_none">Paslēpt uzsvara zīmes</string> <string name="pref_accents_e_none">Paslēpt uzsvara zīmes</string>
<string name="pref_show_numpad_title">Rādīt ciparnīcu</string>
<string name="pref_show_numpad_never">Nekad</string>
<string name="pref_show_numpad_landscape">Tikai guleniskajā skatā</string>
<string name="pref_show_numpad_always">Vienmēr</string>
<string name="pref_autocapitalisation_title">Automātiski lielie burti</string> <string name="pref_autocapitalisation_title">Automātiski lielie burti</string>
<string name="pref_autocapitalisation_summary">Piespiest Shift teikuma sākumā</string> <string name="pref_autocapitalisation_summary">Piespiest Shift teikuma sākumā</string>
<string name="pref_extra_keys_title">Pievienot tastatūrai taustiņus</string> <string name="pref_extra_keys_title">Pievienot tastatūrai taustiņus</string>
@@ -38,6 +42,7 @@
<string name="pref_theme_e_dark">Tumšs</string> <string name="pref_theme_e_dark">Tumšs</string>
<string name="pref_theme_e_light">Gaišs</string> <string name="pref_theme_e_light">Gaišs</string>
<string name="pref_theme_e_black">Melns</string> <string name="pref_theme_e_black">Melns</string>
<string name="pref_theme_e_white">Balts</string>
<string name="pref_swipe_dist_e_very_short">Ļoti tuvs</string> <string name="pref_swipe_dist_e_very_short">Ļoti tuvs</string>
<string name="pref_swipe_dist_e_short">Tuvs</string> <string name="pref_swipe_dist_e_short">Tuvs</string>
<string name="pref_swipe_dist_e_default">Vidējs</string> <string name="pref_swipe_dist_e_default">Vidējs</string>

View File

@@ -5,7 +5,6 @@ import android.content.res.Resources;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Build; import android.os.Build;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.KeyEvent; import android.view.KeyEvent;
@@ -15,6 +14,8 @@ import java.util.HashSet;
final class Config final class Config
{ {
private final SharedPreferences _prefs;
// From resources // From resources
public final float marginTop; public final float marginTop;
public final float keyPadding; public final float keyPadding;
@@ -53,9 +54,9 @@ final class Config
public final IKeyEventHandler handler; public final IKeyEventHandler handler;
private Config(Context context, IKeyEventHandler h) private Config(SharedPreferences prefs, Resources res, IKeyEventHandler h)
{ {
Resources res = context.getResources(); _prefs = prefs;
// static values // static values
marginTop = res.getDimension(R.dimen.margin_top); marginTop = res.getDimension(R.dimen.margin_top);
keyPadding = res.getDimension(R.dimen.key_padding); keyPadding = res.getDimension(R.dimen.key_padding);
@@ -76,7 +77,7 @@ final class Config
characterSize = 1.f; characterSize = 1.f;
accents = 1; accents = 1;
// from prefs // from prefs
refresh(context); refresh(res);
// initialized later // initialized later
shouldOfferSwitchingToNextInputMethod = false; shouldOfferSwitchingToNextInputMethod = false;
shouldOfferSwitchingToProgramming = false; shouldOfferSwitchingToProgramming = false;
@@ -90,10 +91,8 @@ final class Config
/* /*
** Reload prefs ** Reload prefs
*/ */
public void refresh(Context context) public void refresh(Resources res)
{ {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics(); DisplayMetrics dm = res.getDisplayMetrics();
// The height of the keyboard is relative to the height of the screen. // The height of the keyboard is relative to the height of the screen.
// This is the height of the keyboard if it have 4 rows. // This is the height of the keyboard if it have 4 rows.
@@ -101,52 +100,52 @@ final class Config
// Scale some dimensions depending on orientation // Scale some dimensions depending on orientation
float horizontalIntervalScale = 1.f; float horizontalIntervalScale = 1.f;
float characterSizeScale = 1.f; float characterSizeScale = 1.f;
String show_numpad_s = prefs.getString("show_numpad", "never"); String show_numpad_s = _prefs.getString("show_numpad", "never");
show_numpad = "always".equals(show_numpad_s); show_numpad = "always".equals(show_numpad_s);
if (res.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) // Landscape mode if (res.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) // Landscape mode
{ {
if ("landscape".equals(show_numpad_s)) if ("landscape".equals(show_numpad_s))
show_numpad = true; show_numpad = true;
keyboardHeightPercent = prefs.getInt("keyboard_height_landscape", 50); keyboardHeightPercent = _prefs.getInt("keyboard_height_landscape", 50);
horizontalIntervalScale = 2.f; horizontalIntervalScale = 2.f;
characterSizeScale = 1.25f; characterSizeScale = 1.25f;
} }
else else
{ {
keyboardHeightPercent = prefs.getInt("keyboard_height", 35); keyboardHeightPercent = _prefs.getInt("keyboard_height", 35);
} }
String layout_s = prefs.getString("layout", "system"); String layout_s = _prefs.getString("layout", "system");
layout = layout_s.equals("system") ? -1 : layoutId_of_string(layout_s); layout = layout_s.equals("system") ? -1 : layoutId_of_string(layout_s);
String prog_layout_s = prefs.getString("programming_layout", "none"); String prog_layout_s = _prefs.getString("programming_layout", "none");
programming_layout = prog_layout_s.equals("none") ? -1 : layoutId_of_string(prog_layout_s); programming_layout = prog_layout_s.equals("none") ? -1 : layoutId_of_string(prog_layout_s);
// The swipe distance is defined relatively to the "exact physical pixels // The swipe distance is defined relatively to the "exact physical pixels
// per inch of the screen", which isn't affected by the scaling settings. // per inch of the screen", which isn't affected by the scaling settings.
// Take the mean of both dimensions as an approximation of the diagonal. // Take the mean of both dimensions as an approximation of the diagonal.
float physical_scaling = (dm.widthPixels + dm.heightPixels) / (dm.xdpi + dm.ydpi); float physical_scaling = (dm.widthPixels + dm.heightPixels) / (dm.xdpi + dm.ydpi);
swipe_dist_px = Float.valueOf(prefs.getString("swipe_dist", "15")) * physical_scaling;; swipe_dist_px = Float.valueOf(_prefs.getString("swipe_dist", "15")) * physical_scaling;;
vibrateEnabled = prefs.getBoolean("vibrate_enabled", vibrateEnabled); vibrateEnabled = _prefs.getBoolean("vibrate_enabled", vibrateEnabled);
longPressTimeout = prefs.getInt("longpress_timeout", (int)longPressTimeout); longPressTimeout = _prefs.getInt("longpress_timeout", (int)longPressTimeout);
longPressInterval = prefs.getInt("longpress_interval", (int)longPressInterval); longPressInterval = _prefs.getInt("longpress_interval", (int)longPressInterval);
marginBottom = getDipPref(dm, prefs, "margin_bottom", marginBottom); marginBottom = getDipPref(dm, _prefs, "margin_bottom", marginBottom);
keyVerticalInterval = getDipPref(dm, prefs, "key_vertical_space", keyVerticalInterval); keyVerticalInterval = getDipPref(dm, _prefs, "key_vertical_space", keyVerticalInterval);
keyHorizontalInterval = keyHorizontalInterval =
getDipPref(dm, prefs, "key_horizontal_space", keyHorizontalInterval) getDipPref(dm, _prefs, "key_horizontal_space", keyHorizontalInterval)
* horizontalIntervalScale; * horizontalIntervalScale;
// Do not substract keyVerticalInterval from keyHeight because this is done // Do not substract keyVerticalInterval from keyHeight because this is done
// during rendered. // during rendered.
keyHeight = dm.heightPixels * keyboardHeightPercent / 100 / 4; keyHeight = dm.heightPixels * keyboardHeightPercent / 100 / 4;
horizontalMargin = horizontalMargin =
getDipPref(dm, prefs, "horizontal_margin", horizontalMargin) getDipPref(dm, _prefs, "horizontal_margin", horizontalMargin)
+ res.getDimension(R.dimen.extra_horizontal_margin); + res.getDimension(R.dimen.extra_horizontal_margin);
preciseRepeat = prefs.getBoolean("precise_repeat", preciseRepeat); preciseRepeat = _prefs.getBoolean("precise_repeat", preciseRepeat);
double_tap_lock_shift = prefs.getBoolean("lock_double_tap", false); double_tap_lock_shift = _prefs.getBoolean("lock_double_tap", false);
characterSize = characterSize =
prefs.getFloat("character_size", characterSize) _prefs.getFloat("character_size", characterSize)
* characterSizeScale; * characterSizeScale;
accents = Integer.valueOf(prefs.getString("accents", "1")); accents = Integer.valueOf(_prefs.getString("accents", "1"));
theme = getThemeId(res, prefs.getString("theme", "")); theme = getThemeId(res, _prefs.getString("theme", ""));
autocapitalisation = prefs.getBoolean("autocapitalisation", true); autocapitalisation = _prefs.getBoolean("autocapitalisation", true);
extra_keys_param = ExtraKeyCheckBoxPreference.get_extra_keys(prefs); extra_keys_param = ExtraKeyCheckBoxPreference.get_extra_keys(_prefs);
} }
/** Update the layout according to the configuration. /** Update the layout according to the configuration.
@@ -277,9 +276,10 @@ final class Config
private static Config _globalConfig = null; private static Config _globalConfig = null;
public static void initGlobalConfig(Context context, IKeyEventHandler handler) public static void initGlobalConfig(SharedPreferences prefs, Resources res,
IKeyEventHandler handler)
{ {
_globalConfig = new Config(context, handler); _globalConfig = new Config(prefs, res, handler);
} }
public static Config globalConfig() public static Config globalConfig()

View File

@@ -0,0 +1,86 @@
package juloo.keyboard2;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build.VERSION;
import android.preference.PreferenceManager;
import java.util.Map;
import java.util.Set;
public final class DirectBootAwarePreferences
{
/* On API >= 24, preferences are read from the device protected storage. This
* storage is less protected than the default, no personnal or sensitive
* information is stored there (only the keyboard settings). This storage is
* accessible during boot and allow the keyboard to read its settings and
* allow typing the storage password. */
public static SharedPreferences get_shared_preferences(Context context)
{
if (VERSION.SDK_INT < 24)
return PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences prefs = get_protected_prefs(context);
check_need_migration(context, prefs);
return prefs;
}
/* Copy shared preferences to device protected storage. Not using
* [Context.moveSharedPreferencesFrom] because the settings activity still
* use [PreferenceActivity], which can't work on a non-default shared
* preference file. */
public static void copy_preferences_to_protected_storage(Context context,
SharedPreferences src)
{
if (VERSION.SDK_INT >= 24)
copy_shared_preferences(src, get_protected_prefs(context));
}
static SharedPreferences get_protected_prefs(Context context)
{
String pref_name =
PreferenceManager.getDefaultSharedPreferencesName(context);
return context.createDeviceProtectedStorageContext()
.getSharedPreferences(pref_name, Context.MODE_PRIVATE);
}
static void check_need_migration(Context app_context,
SharedPreferences protected_prefs)
{
if (!protected_prefs.getBoolean("need_migration", true))
return;
SharedPreferences prefs;
try
{
prefs = PreferenceManager.getDefaultSharedPreferences(app_context);
}
catch (Exception e)
{
// Device is locked, migrate later.
return;
}
prefs.edit().putBoolean("need_migration", false).commit();
copy_shared_preferences(prefs, protected_prefs);
}
static void copy_shared_preferences(SharedPreferences src, SharedPreferences dst)
{
SharedPreferences.Editor e = dst.edit();
Map<String, ?> entries = src.getAll();
for (String k : entries.keySet())
{
Object v = entries.get(k);
if (v instanceof Boolean)
e.putBoolean(k, (Boolean)v);
else if (v instanceof Float)
e.putFloat(k, (Float)v);
else if (v instanceof Integer)
e.putInt(k, (Integer)v);
else if (v instanceof Long)
e.putLong(k, (Long)v);
else if (v instanceof String)
e.putString(k, (String)v);
else if (v instanceof Set)
e.putStringSet(k, (Set<String>)v);
}
e.commit();
}
}

View File

@@ -3,7 +3,6 @@ package juloo.keyboard2;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.preference.PreferenceManager;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.Gravity; import android.view.Gravity;
import android.view.View; import android.view.View;
@@ -84,9 +83,10 @@ public class EmojiGridView extends GridView
private void saveLastUsed() private void saveLastUsed()
{ {
SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(getContext()).edit(); SharedPreferences.Editor edit;
try { edit = emojiSharedPreferences().edit(); }
catch (Exception _e) { return; }
HashSet<String> set = new HashSet<String>(); HashSet<String> set = new HashSet<String>();
for (Emoji emoji : _lastUsed.keySet()) for (Emoji emoji : _lastUsed.keySet())
set.add(String.valueOf(_lastUsed.get(emoji)) + "-" + emoji.name()); set.add(String.valueOf(_lastUsed.get(emoji)) + "-" + emoji.name());
edit.putStringSet(LAST_USE_PREF, set); edit.putStringSet(LAST_USE_PREF, set);
@@ -95,16 +95,18 @@ public class EmojiGridView extends GridView
private void loadLastUsed() private void loadLastUsed()
{ {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
Set<String> lastUseSet = prefs.getStringSet(LAST_USE_PREF, null);
_lastUsed = new HashMap<Emoji, Integer>(); _lastUsed = new HashMap<Emoji, Integer>();
SharedPreferences prefs;
// Storage might not be available (eg. the device is locked), avoid
// crashing.
try { prefs = emojiSharedPreferences(); }
catch (Exception _e) { return; }
Set<String> lastUseSet = prefs.getStringSet(LAST_USE_PREF, null);
if (lastUseSet != null) if (lastUseSet != null)
for (String emojiData : lastUseSet) for (String emojiData : lastUseSet)
{ {
String[] data = emojiData.split("-", 2); String[] data = emojiData.split("-", 2);
Emoji emoji; Emoji emoji;
if (data.length != 2) if (data.length != 2)
continue ; continue ;
emoji = Emoji.getEmojiByName(data[1]); emoji = Emoji.getEmojiByName(data[1]);
@@ -114,6 +116,11 @@ public class EmojiGridView extends GridView
} }
} }
SharedPreferences emojiSharedPreferences()
{
return getContext().getSharedPreferences("emoji_last_use", Context.MODE_PRIVATE);
}
private static class EmojiView extends TextView private static class EmojiView extends TextView
{ {
public EmojiView(Context context) public EmojiView(Context context)

View File

@@ -6,23 +6,22 @@ import android.content.SharedPreferences;
import android.inputmethodservice.InputMethodService; import android.inputmethodservice.InputMethodService;
import android.os.Build.VERSION; import android.os.Build.VERSION;
import android.os.IBinder; import android.os.IBinder;
import android.preference.PreferenceManager;
import android.text.InputType; import android.text.InputType;
import android.util.Log;
import android.util.LogPrinter;
import android.view.ContextThemeWrapper; import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.inputmethod.EditorInfo; import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype; import android.view.inputmethod.InputMethodSubtype;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.util.Log;
import android.util.LogPrinter;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
public class Keyboard2 extends InputMethodService public class Keyboard2 extends InputMethodService
@@ -49,11 +48,10 @@ public class Keyboard2 extends InputMethodService
public void onCreate() public void onCreate()
{ {
super.onCreate(); super.onCreate();
PreferenceManager.setDefaultValues(this, R.xml.settings, false); SharedPreferences prefs = DirectBootAwarePreferences.get_shared_preferences(this);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this); prefs.registerOnSharedPreferenceChangeListener(this);
Config.initGlobalConfig(this, new KeyEventHandler(this.new Receiver())); Config.initGlobalConfig(prefs, getResources(), new KeyEventHandler(this.new Receiver()));
_config = Config.globalConfig(); _config = Config.globalConfig();
_config.refresh(this);
_keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard); _keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard);
_keyboardView.reset(); _keyboardView.reset();
_debug_logs = getResources().getBoolean(R.bool.debug_logs); _debug_logs = getResources().getBoolean(R.bool.debug_logs);
@@ -205,7 +203,7 @@ public class Keyboard2 extends InputMethodService
private void refreshConfig() private void refreshConfig()
{ {
int prev_theme = _config.theme; int prev_theme = _config.theme;
_config.refresh(this); _config.refresh(getResources());
refreshSubtypeImm(); refreshSubtypeImm();
// Refreshing the theme config requires re-creating the views // Refreshing the theme config requires re-creating the views
if (prev_theme != _config.theme) if (prev_theme != _config.theme)

View File

@@ -1,9 +1,11 @@
package juloo.keyboard2; package juloo.keyboard2;
import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
public class SettingsActivity extends PreferenceActivity public class SettingsActivity extends PreferenceActivity
{ {
@@ -12,7 +14,13 @@ public class SettingsActivity extends PreferenceActivity
{ {
detectSystemTheme(); detectSystemTheme();
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
SharedPreferences prefs;
// The preferences can't be read when in direct-boot mode. Avoid crashing
// and don't allow changing the settings.
try { prefs = PreferenceManager.getDefaultSharedPreferences(this); }
catch (Exception _e) { fallbackEncrypted(); return; }
addPreferencesFromResource(R.xml.settings); addPreferencesFromResource(R.xml.settings);
prefs.registerOnSharedPreferenceChangeListener(this.new OnPreferencesChange());
} }
/** The default theme is [Theme.DeviceDefault], which is dark. Detect if the /** The default theme is [Theme.DeviceDefault], which is dark. Detect if the
@@ -26,4 +34,21 @@ public class SettingsActivity extends PreferenceActivity
setTheme(android.R.style.Theme_DeviceDefault_Light); setTheme(android.R.style.Theme_DeviceDefault_Light);
} }
} }
void fallbackEncrypted()
{
// Can't communicate with the user here.
finish();
}
/** See DirectBootAwarePreferences. */
class OnPreferencesChange implements SharedPreferences.OnSharedPreferenceChangeListener
{
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String _key)
{
DirectBootAwarePreferences
.copy_preferences_to_protected_storage(SettingsActivity.this, prefs);
}
}
} }