From b1a3fdb99f5c577227fe80298f01bfbeb01322ad Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Wed, 19 Dec 2018 14:12:42 -0800 Subject: [PATCH] Create /data/system/inputmethod/ directory lazily With this CL, the directory /data/system/inputmethod/ is also created only when necessary. Also the /data/system/inputmethod/ will be deleted automatically when it is empty. There should be no developer-facing behavior change. Bug: 121223050 Test: Manually verified as follows 1. Build and flash aosp_taimen-userdebug into taimen 2. Wait until the device fully boots up 3. adb reboot # to avoid Bug 121259290 4. adb root 5. adb shell cat /data/system/inputmethod/subtypes.xml -> make sure the content looks as follows: 6. Open AOSP Keyboard settings 7. Go to "Appearance & Layouts" -> "Custom input styles" 8. Remove all layouts 9. adb shell cat /data/system/inputmethod/subtypes.xml -> make sure the file no longer exists 10. adb shell ls /data/system/inputmethod -> make sure the directory no longer exists 11. Open AOSP Keyboard settings 12. Go to "Appearance & Layouts" -> "Custom input styles" 13. Add English (US) - Dvorak 14. adb shell ls /data/system/inputmethod -> make sure the directory exists and only the system user can access it Change-Id: I2307b5e4edf7b90d2fc03138f233d6051f80cf90 --- .../inputmethod/InputMethodManagerService.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java index 597d81ffab5c6..78dd2200f9a1c 100644 --- a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java +++ b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java @@ -73,6 +73,7 @@ import android.os.Binder; import android.os.Bundle; import android.os.Debug; import android.os.Environment; +import android.os.FileUtils; import android.os.Handler; import android.os.IBinder; import android.os.IInterface; @@ -4299,9 +4300,6 @@ public class InputMethodManagerService extends IInputMethodManager.Stub ? new File(Environment.getDataDirectory(), SYSTEM_PATH) : Environment.getUserSystemDirectory(userId); final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH); - if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) { - Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath()); - } final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME); mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile, "input-subtypes"); readAdditionalInputMethodSubtypes(mAdditionalSubtypesMap, @@ -4349,6 +4347,18 @@ public class InputMethodManagerService extends IInputMethodManager.Stub if (subtypesFile.exists()) { subtypesFile.delete(); } + final File parentDir = subtypesFile.getBaseFile().getParentFile(); + if (parentDir != null && FileUtils.listFilesOrEmpty(parentDir).length == 0) { + if (!parentDir.delete()) { + Slog.e(TAG, "Failed to delete the empty parent directory " + parentDir); + } + } + return; + } + + final File parentDir = subtypesFile.getBaseFile().getParentFile(); + if (!parentDir.exists() && !parentDir.mkdirs()) { + Slog.e(TAG, "Failed to create a parent directory " + parentDir); return; }