diff --git a/app/build.gradle b/app/build.gradle index 2ba5ac2bb..067ceace4 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,11 +1,13 @@ apply plugin: 'com.android.application' +apply plugin: 'kotlin-android-extensions' +apply plugin: 'kotlin-android' android { compileSdkVersion 29 defaultConfig { applicationId "org.dslul.openboard.inputmethod.latin" - minSdkVersion 14 + minSdkVersion 19 targetSdkVersion 29 versionCode 4 versionName "1.2" @@ -36,4 +38,9 @@ android { dependencies { implementation 'com.google.code.findbugs:jsr305:3.0.2' implementation 'androidx.legacy:legacy-support-v4:1.0.0' + implementation "androidx.core:core-ktx:1.1.0" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" +} +repositories { + mavenCentral() } \ No newline at end of file diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/annotations/ExternallyReferenced.java b/app/src/main/java/org/dslul/openboard/inputmethod/annotations/ExternallyReferenced.kt similarity index 88% rename from app/src/main/java/org/dslul/openboard/inputmethod/annotations/ExternallyReferenced.java rename to app/src/main/java/org/dslul/openboard/inputmethod/annotations/ExternallyReferenced.kt index b08ab46db..570e9dad1 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/annotations/ExternallyReferenced.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/annotations/ExternallyReferenced.kt @@ -13,12 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -package org.dslul.openboard.inputmethod.annotations; +package org.dslul.openboard.inputmethod.annotations /** * Denotes that the class, method or field should not be eliminated by ProGuard, * because it is externally referenced. (See proguard.flags) */ -public @interface ExternallyReferenced { -} +annotation class ExternallyReferenced \ No newline at end of file diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/annotations/UsedForTesting.java b/app/src/main/java/org/dslul/openboard/inputmethod/annotations/UsedForTesting.kt similarity index 89% rename from app/src/main/java/org/dslul/openboard/inputmethod/annotations/UsedForTesting.java rename to app/src/main/java/org/dslul/openboard/inputmethod/annotations/UsedForTesting.kt index f586d56e2..21e6a46de 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/annotations/UsedForTesting.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/annotations/UsedForTesting.kt @@ -13,12 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -package org.dslul.openboard.inputmethod.annotations; +package org.dslul.openboard.inputmethod.annotations /** * Denotes that the class, method or field should not be eliminated by ProGuard, * so that unit tests can access it. (See proguard.flags) */ -public @interface UsedForTesting { -} +annotation class UsedForTesting \ No newline at end of file diff --git a/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsActivity.java b/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsActivity.java deleted file mode 100644 index 1151a14b4..000000000 --- a/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsActivity.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * 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.dslul.openboard.inputmethodcommon; - -import android.graphics.drawable.Drawable; -import android.os.Bundle; -import android.preference.PreferenceActivity; - -/** - * This is a helper class for an IME's settings preference activity. It's recommended for every - * IME to have its own settings preference activity which inherits this class. - */ -public abstract class InputMethodSettingsActivity extends PreferenceActivity - implements InputMethodSettingsInterface { - private final InputMethodSettingsImpl mSettings = new InputMethodSettingsImpl(); - @SuppressWarnings("deprecation") - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setPreferenceScreen(getPreferenceManager().createPreferenceScreen(this)); - mSettings.init(this, getPreferenceScreen()); - } - - /** - * {@inheritDoc} - */ - @Override - public void setInputMethodSettingsCategoryTitle(int resId) { - mSettings.setInputMethodSettingsCategoryTitle(resId); - } - - /** - * {@inheritDoc} - */ - @Override - public void setInputMethodSettingsCategoryTitle(CharSequence title) { - mSettings.setInputMethodSettingsCategoryTitle(title); - } - - /** - * {@inheritDoc} - */ - @Override - public void setSubtypeEnablerTitle(int resId) { - mSettings.setSubtypeEnablerTitle(resId); - } - - /** - * {@inheritDoc} - */ - @Override - public void setSubtypeEnablerTitle(CharSequence title) { - mSettings.setSubtypeEnablerTitle(title); - } - - /** - * {@inheritDoc} - */ - @Override - public void setSubtypeEnablerIcon(int resId) { - mSettings.setSubtypeEnablerIcon(resId); - } - - /** - * {@inheritDoc} - */ - @Override - public void setSubtypeEnablerIcon(Drawable drawable) { - mSettings.setSubtypeEnablerIcon(drawable); - } - - /** - * {@inheritDoc} - */ - @Override - public void onResume() { - super.onResume(); - mSettings.updateSubtypeEnabler(); - } -} diff --git a/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsActivity.kt b/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsActivity.kt new file mode 100644 index 000000000..aed4817ab --- /dev/null +++ b/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsActivity.kt @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * 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.dslul.openboard.inputmethodcommon + +import android.graphics.drawable.Drawable +import android.os.Bundle +import android.preference.PreferenceActivity + +/** + * This is a helper class for an IME's settings preference activity. It's recommended for every + * IME to have its own settings preference activity which inherits this class. + */ +abstract class InputMethodSettingsActivity : PreferenceActivity(), InputMethodSettingsInterface { + private val mSettings = InputMethodSettingsImpl() + public override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + preferenceScreen = preferenceManager.createPreferenceScreen(this) + mSettings.init(this, preferenceScreen) + } + + /** + * {@inheritDoc} + */ + override fun setInputMethodSettingsCategoryTitle(resId: Int) { + mSettings.setInputMethodSettingsCategoryTitle(resId) + } + + /** + * {@inheritDoc} + */ + override fun setInputMethodSettingsCategoryTitle(title: CharSequence?) { + mSettings.setInputMethodSettingsCategoryTitle(title) + } + + /** + * {@inheritDoc} + */ + override fun setSubtypeEnablerTitle(resId: Int) { + mSettings.setSubtypeEnablerTitle(resId) + } + + /** + * {@inheritDoc} + */ + override fun setSubtypeEnablerTitle(title: CharSequence?) { + mSettings.setSubtypeEnablerTitle(title) + } + + /** + * {@inheritDoc} + */ + override fun setSubtypeEnablerIcon(resId: Int) { + mSettings.setSubtypeEnablerIcon(resId) + } + + /** + * {@inheritDoc} + */ + override fun setSubtypeEnablerIcon(drawable: Drawable?) { + mSettings.setSubtypeEnablerIcon(drawable) + } + + /** + * {@inheritDoc} + */ + public override fun onResume() { + super.onResume() + mSettings.updateSubtypeEnabler() + } +} \ No newline at end of file diff --git a/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsFragment.java b/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsFragment.java deleted file mode 100644 index 46d7abeb5..000000000 --- a/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsFragment.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * 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.dslul.openboard.inputmethodcommon; - -import android.content.Context; -import android.graphics.drawable.Drawable; -import android.os.Bundle; -import android.preference.PreferenceFragment; - -/** - * This is a helper class for an IME's settings preference fragment. It's recommended for every - * IME to have its own settings preference fragment which inherits this class. - */ -public abstract class InputMethodSettingsFragment extends PreferenceFragment - implements InputMethodSettingsInterface { - private final InputMethodSettingsImpl mSettings = new InputMethodSettingsImpl(); - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - final Context context = getActivity(); - setPreferenceScreen(getPreferenceManager().createPreferenceScreen(context)); - mSettings.init(context, getPreferenceScreen()); - } - - /** - * {@inheritDoc} - */ - @Override - public void setInputMethodSettingsCategoryTitle(int resId) { - mSettings.setInputMethodSettingsCategoryTitle(resId); - } - - /** - * {@inheritDoc} - */ - @Override - public void setInputMethodSettingsCategoryTitle(CharSequence title) { - mSettings.setInputMethodSettingsCategoryTitle(title); - } - - /** - * {@inheritDoc} - */ - @Override - public void setSubtypeEnablerTitle(int resId) { - mSettings.setSubtypeEnablerTitle(resId); - } - - /** - * {@inheritDoc} - */ - @Override - public void setSubtypeEnablerTitle(CharSequence title) { - mSettings.setSubtypeEnablerTitle(title); - } - - /** - * {@inheritDoc} - */ - @Override - public void setSubtypeEnablerIcon(int resId) { - mSettings.setSubtypeEnablerIcon(resId); - } - - /** - * {@inheritDoc} - */ - @Override - public void setSubtypeEnablerIcon(Drawable drawable) { - mSettings.setSubtypeEnablerIcon(drawable); - } - - /** - * {@inheritDoc} - */ - @Override - public void onResume() { - super.onResume(); - mSettings.updateSubtypeEnabler(); - } -} diff --git a/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsFragment.kt b/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsFragment.kt new file mode 100644 index 000000000..abc19be0a --- /dev/null +++ b/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsFragment.kt @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * 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.dslul.openboard.inputmethodcommon + +import android.content.Context +import android.graphics.drawable.Drawable +import android.os.Bundle +import android.preference.PreferenceFragment + +/** + * This is a helper class for an IME's settings preference fragment. It's recommended for every + * IME to have its own settings preference fragment which inherits this class. + */ +abstract class InputMethodSettingsFragment : PreferenceFragment(), InputMethodSettingsInterface { + private val mSettings = InputMethodSettingsImpl() + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + val context: Context = activity + preferenceScreen = preferenceManager.createPreferenceScreen(context) + mSettings.init(context, preferenceScreen) + } + + /** + * {@inheritDoc} + */ + override fun setInputMethodSettingsCategoryTitle(resId: Int) { + mSettings.setInputMethodSettingsCategoryTitle(resId) + } + + /** + * {@inheritDoc} + */ + override fun setInputMethodSettingsCategoryTitle(title: CharSequence?) { + mSettings.setInputMethodSettingsCategoryTitle(title) + } + + /** + * {@inheritDoc} + */ + override fun setSubtypeEnablerTitle(resId: Int) { + mSettings.setSubtypeEnablerTitle(resId) + } + + /** + * {@inheritDoc} + */ + override fun setSubtypeEnablerTitle(title: CharSequence?) { + mSettings.setSubtypeEnablerTitle(title) + } + + /** + * {@inheritDoc} + */ + override fun setSubtypeEnablerIcon(resId: Int) { + mSettings.setSubtypeEnablerIcon(resId) + } + + /** + * {@inheritDoc} + */ + override fun setSubtypeEnablerIcon(drawable: Drawable?) { + mSettings.setSubtypeEnablerIcon(drawable) + } + + /** + * {@inheritDoc} + */ + override fun onResume() { + super.onResume() + mSettings.updateSubtypeEnabler() + } +} \ No newline at end of file diff --git a/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsImpl.java b/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsImpl.java deleted file mode 100644 index 1756d5808..000000000 --- a/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsImpl.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * 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.dslul.openboard.inputmethodcommon; - -import android.content.Context; -import android.content.Intent; -import android.graphics.drawable.Drawable; -import android.preference.Preference; -import android.preference.Preference.OnPreferenceClickListener; -import android.preference.PreferenceScreen; -import android.provider.Settings; -import android.text.TextUtils; -import android.view.inputmethod.InputMethodInfo; -import android.view.inputmethod.InputMethodManager; -import android.view.inputmethod.InputMethodSubtype; - -import java.util.List; - -/* package private */ class InputMethodSettingsImpl implements InputMethodSettingsInterface { - private Preference mSubtypeEnablerPreference; - private int mInputMethodSettingsCategoryTitleRes; - private CharSequence mInputMethodSettingsCategoryTitle; - private int mSubtypeEnablerTitleRes; - private CharSequence mSubtypeEnablerTitle; - private int mSubtypeEnablerIconRes; - private Drawable mSubtypeEnablerIcon; - private InputMethodManager mImm; - private InputMethodInfo mImi; - - /** - * Initialize internal states of this object. - * @param context the context for this application. - * @param prefScreen a PreferenceScreen of PreferenceActivity or PreferenceFragment. - * @return true if this application is an IME and has two or more subtypes, false otherwise. - */ - public boolean init(final Context context, final PreferenceScreen prefScreen) { - mImm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); - mImi = getMyImi(context, mImm); - if (mImi == null || mImi.getSubtypeCount() <= 1) { - return false; - } - final Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS); - intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, mImi.getId()); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK - | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED - | Intent.FLAG_ACTIVITY_CLEAR_TOP); - mSubtypeEnablerPreference = new Preference(context); - mSubtypeEnablerPreference.setIntent(intent); - prefScreen.addPreference(mSubtypeEnablerPreference); - updateSubtypeEnabler(); - return true; - } - - private static InputMethodInfo getMyImi(Context context, InputMethodManager imm) { - final List imis = imm.getInputMethodList(); - for (int i = 0; i < imis.size(); ++i) { - final InputMethodInfo imi = imis.get(i); - if (imis.get(i).getPackageName().equals(context.getPackageName())) { - return imi; - } - } - return null; - } - - private static String getEnabledSubtypesLabel( - Context context, InputMethodManager imm, InputMethodInfo imi) { - if (context == null || imm == null || imi == null) return null; - final List subtypes = imm.getEnabledInputMethodSubtypeList(imi, true); - final StringBuilder sb = new StringBuilder(); - final int N = subtypes.size(); - for (int i = 0; i < N; ++i) { - final InputMethodSubtype subtype = subtypes.get(i); - if (sb.length() > 0) { - sb.append(", "); - } - sb.append(subtype.getDisplayName(context, imi.getPackageName(), - imi.getServiceInfo().applicationInfo)); - } - return sb.toString(); - } - /** - * {@inheritDoc} - */ - @Override - public void setInputMethodSettingsCategoryTitle(int resId) { - mInputMethodSettingsCategoryTitleRes = resId; - updateSubtypeEnabler(); - } - - /** - * {@inheritDoc} - */ - @Override - public void setInputMethodSettingsCategoryTitle(CharSequence title) { - mInputMethodSettingsCategoryTitleRes = 0; - mInputMethodSettingsCategoryTitle = title; - updateSubtypeEnabler(); - } - - /** - * {@inheritDoc} - */ - @Override - public void setSubtypeEnablerTitle(int resId) { - mSubtypeEnablerTitleRes = resId; - updateSubtypeEnabler(); - } - - /** - * {@inheritDoc} - */ - @Override - public void setSubtypeEnablerTitle(CharSequence title) { - mSubtypeEnablerTitleRes = 0; - mSubtypeEnablerTitle = title; - updateSubtypeEnabler(); - } - - /** - * {@inheritDoc} - */ - @Override - public void setSubtypeEnablerIcon(int resId) { - mSubtypeEnablerIconRes = resId; - updateSubtypeEnabler(); - } - - /** - * {@inheritDoc} - */ - @Override - public void setSubtypeEnablerIcon(Drawable drawable) { - mSubtypeEnablerIconRes = 0; - mSubtypeEnablerIcon = drawable; - updateSubtypeEnabler(); - } - - public void updateSubtypeEnabler() { - final Preference pref = mSubtypeEnablerPreference; - if (pref == null) { - return; - } - final Context context = pref.getContext(); - final CharSequence title; - if (mSubtypeEnablerTitleRes != 0) { - title = context.getString(mSubtypeEnablerTitleRes); - } else { - title = mSubtypeEnablerTitle; - } - pref.setTitle(title); - final Intent intent = pref.getIntent(); - if (intent != null) { - intent.putExtra(Intent.EXTRA_TITLE, title); - } - final String summary = getEnabledSubtypesLabel(context, mImm, mImi); - if (!TextUtils.isEmpty(summary)) { - pref.setSummary(summary); - } - if (mSubtypeEnablerIconRes != 0) { - pref.setIcon(mSubtypeEnablerIconRes); - } else { - pref.setIcon(mSubtypeEnablerIcon); - } - } -} diff --git a/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsImpl.kt b/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsImpl.kt new file mode 100644 index 000000000..ca8ac697f --- /dev/null +++ b/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsImpl.kt @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * 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.dslul.openboard.inputmethodcommon + +import android.content.Context +import android.content.Intent +import android.graphics.drawable.Drawable +import android.preference.Preference +import android.preference.PreferenceScreen +import android.provider.Settings +import android.text.TextUtils +import android.view.inputmethod.InputMethodInfo +import android.view.inputmethod.InputMethodManager + +/* package private */ +internal class InputMethodSettingsImpl : InputMethodSettingsInterface { + private var mSubtypeEnablerPreference: Preference? = null + private var mInputMethodSettingsCategoryTitleRes = 0 + private var mInputMethodSettingsCategoryTitle: CharSequence? = null + private var mSubtypeEnablerTitleRes = 0 + private var mSubtypeEnablerTitle: CharSequence? = null + private var mSubtypeEnablerIconRes = 0 + private var mSubtypeEnablerIcon: Drawable? = null + private var mImm: InputMethodManager? = null + private var mImi: InputMethodInfo? = null + /** + * Initialize internal states of this object. + * @param context the context for this application. + * @param prefScreen a PreferenceScreen of PreferenceActivity or PreferenceFragment. + * @return true if this application is an IME and has two or more subtypes, false otherwise. + */ + fun init(context: Context, prefScreen: PreferenceScreen): Boolean { + mImm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + mImi = getMyImi(context, mImm) + if (mImi == null || mImi!!.subtypeCount <= 1) { + return false + } + val intent = Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS) + intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, mImi!!.id) + intent.flags = (Intent.FLAG_ACTIVITY_NEW_TASK + or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED + or Intent.FLAG_ACTIVITY_CLEAR_TOP) + mSubtypeEnablerPreference = Preference(context) + mSubtypeEnablerPreference!!.intent = intent + prefScreen.addPreference(mSubtypeEnablerPreference) + updateSubtypeEnabler() + return true + } + + /** + * {@inheritDoc} + */ + override fun setInputMethodSettingsCategoryTitle(resId: Int) { + mInputMethodSettingsCategoryTitleRes = resId + updateSubtypeEnabler() + } + + /** + * {@inheritDoc} + */ + override fun setInputMethodSettingsCategoryTitle(title: CharSequence?) { + mInputMethodSettingsCategoryTitleRes = 0 + mInputMethodSettingsCategoryTitle = title + updateSubtypeEnabler() + } + + /** + * {@inheritDoc} + */ + override fun setSubtypeEnablerTitle(resId: Int) { + mSubtypeEnablerTitleRes = resId + updateSubtypeEnabler() + } + + /** + * {@inheritDoc} + */ + override fun setSubtypeEnablerTitle(title: CharSequence?) { + mSubtypeEnablerTitleRes = 0 + mSubtypeEnablerTitle = title + updateSubtypeEnabler() + } + + /** + * {@inheritDoc} + */ + override fun setSubtypeEnablerIcon(resId: Int) { + mSubtypeEnablerIconRes = resId + updateSubtypeEnabler() + } + + /** + * {@inheritDoc} + */ + override fun setSubtypeEnablerIcon(drawable: Drawable?) { + mSubtypeEnablerIconRes = 0 + mSubtypeEnablerIcon = drawable + updateSubtypeEnabler() + } + + fun updateSubtypeEnabler() { + val pref = mSubtypeEnablerPreference ?: return + val context = pref.context + val title: CharSequence? + title = if (mSubtypeEnablerTitleRes != 0) { + context.getString(mSubtypeEnablerTitleRes) + } else { + mSubtypeEnablerTitle + } + pref.title = title + val intent = pref.intent + intent?.putExtra(Intent.EXTRA_TITLE, title) + val summary = getEnabledSubtypesLabel(context, mImm, mImi) + if (!TextUtils.isEmpty(summary)) { + pref.summary = summary + } + if (mSubtypeEnablerIconRes != 0) { + pref.setIcon(mSubtypeEnablerIconRes) + } else { + pref.icon = mSubtypeEnablerIcon + } + } + + companion object { + private fun getMyImi(context: Context, imm: InputMethodManager?): InputMethodInfo? { + val imis = imm!!.inputMethodList + for (i in imis.indices) { + val imi = imis[i] + if (imis[i].packageName == context.packageName) { + return imi + } + } + return null + } + + private fun getEnabledSubtypesLabel( + context: Context?, imm: InputMethodManager?, imi: InputMethodInfo?): String? { + if (context == null || imm == null || imi == null) return null + val subtypes = imm.getEnabledInputMethodSubtypeList(imi, true) + val sb = StringBuilder() + val N = subtypes.size + for (i in 0 until N) { + val subtype = subtypes[i] + if (sb.length > 0) { + sb.append(", ") + } + sb.append(subtype.getDisplayName(context, imi.packageName, + imi.serviceInfo.applicationInfo)) + } + return sb.toString() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsInterface.java b/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsInterface.kt similarity index 78% rename from app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsInterface.java rename to app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsInterface.kt index 39f7af87a..00a6679f1 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsInterface.java +++ b/app/src/main/java/org/dslul/openboard/inputmethodcommon/InputMethodSettingsInterface.kt @@ -13,51 +13,50 @@ * See the License for the specific language governing permissions and * limitations under the License */ +package org.dslul.openboard.inputmethodcommon -package org.dslul.openboard.inputmethodcommon; - -import android.graphics.drawable.Drawable; +import android.graphics.drawable.Drawable /** * InputMethodSettingsInterface is the interface for adding IME related preferences to * PreferenceActivity or PreferenceFragment. */ -public interface InputMethodSettingsInterface { +interface InputMethodSettingsInterface { /** * Sets the title for the input method settings category with a resource ID. * @param resId The resource ID of the title. */ - public void setInputMethodSettingsCategoryTitle(int resId); + fun setInputMethodSettingsCategoryTitle(resId: Int) /** * Sets the title for the input method settings category with a CharSequence. * @param title The title for this preference. */ - public void setInputMethodSettingsCategoryTitle(CharSequence title); + fun setInputMethodSettingsCategoryTitle(title: CharSequence?) /** * Sets the title for the input method enabler preference for launching subtype enabler with a * resource ID. * @param resId The resource ID of the title. */ - public void setSubtypeEnablerTitle(int resId); + fun setSubtypeEnablerTitle(resId: Int) /** * Sets the title for the input method enabler preference for launching subtype enabler with a * CharSequence. * @param title The title for this preference. */ - public void setSubtypeEnablerTitle(CharSequence title); + fun setSubtypeEnablerTitle(title: CharSequence?) /** * Sets the icon for the preference for launching subtype enabler with a resource ID. * @param resId The resource id of an optional icon for the preference. */ - public void setSubtypeEnablerIcon(int resId); + fun setSubtypeEnablerIcon(resId: Int) /** * Sets the icon for the Preference for launching subtype enabler with a Drawable. * @param drawable The drawable of an optional icon for the preference. */ - public void setSubtypeEnablerIcon(Drawable drawable); -} + fun setSubtypeEnablerIcon(drawable: Drawable?) +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index e0c5350a6..ca4b0766c 100755 --- a/build.gradle +++ b/build.gradle @@ -1,12 +1,14 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { + ext.kotlin_version = '1.3.61' repositories { jcenter() google() } dependencies { classpath 'com.android.tools.build:gradle:3.5.3' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files