aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/org/kelar/inputmethodcommon
diff options
context:
space:
mode:
authorAmin Bandali <bandali@kelar.org>2024-12-16 21:45:41 -0500
committerAmin Bandali <bandali@kelar.org>2025-01-11 14:17:35 -0500
commite9a0e66716dab4dd3184d009d8920de1961efdfa (patch)
tree02dcc096643d74645bf28459c2834c3d4a2ad7f2 /java/src/org/kelar/inputmethodcommon
parentfb3b9360d70596d7e921de8bf7d3ca99564a077e (diff)
downloadlatinime-e9a0e66716dab4dd3184d009d8920de1961efdfa.tar.gz
latinime-e9a0e66716dab4dd3184d009d8920de1961efdfa.tar.xz
latinime-e9a0e66716dab4dd3184d009d8920de1961efdfa.zip
Rename to Kelar Keyboard (org.kelar.inputmethod.latin)
Diffstat (limited to 'java/src/org/kelar/inputmethodcommon')
-rw-r--r--java/src/org/kelar/inputmethodcommon/InputMethodSettingsActivity.java94
-rw-r--r--java/src/org/kelar/inputmethodcommon/InputMethodSettingsFragment.java95
-rw-r--r--java/src/org/kelar/inputmethodcommon/InputMethodSettingsImpl.java178
-rw-r--r--java/src/org/kelar/inputmethodcommon/InputMethodSettingsInterface.java63
4 files changed, 430 insertions, 0 deletions
diff --git a/java/src/org/kelar/inputmethodcommon/InputMethodSettingsActivity.java b/java/src/org/kelar/inputmethodcommon/InputMethodSettingsActivity.java
new file mode 100644
index 000000000..1e304c3c4
--- /dev/null
+++ b/java/src/org/kelar/inputmethodcommon/InputMethodSettingsActivity.java
@@ -0,0 +1,94 @@
+/*
+ * 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.kelar.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/java/src/org/kelar/inputmethodcommon/InputMethodSettingsFragment.java b/java/src/org/kelar/inputmethodcommon/InputMethodSettingsFragment.java
new file mode 100644
index 000000000..4b1c5c7e8
--- /dev/null
+++ b/java/src/org/kelar/inputmethodcommon/InputMethodSettingsFragment.java
@@ -0,0 +1,95 @@
+/*
+ * 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.kelar.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/java/src/org/kelar/inputmethodcommon/InputMethodSettingsImpl.java b/java/src/org/kelar/inputmethodcommon/InputMethodSettingsImpl.java
new file mode 100644
index 000000000..6f1b9478f
--- /dev/null
+++ b/java/src/org/kelar/inputmethodcommon/InputMethodSettingsImpl.java
@@ -0,0 +1,178 @@
+/*
+ * 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.kelar.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;
+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<InputMethodInfo> 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<InputMethodSubtype> 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/java/src/org/kelar/inputmethodcommon/InputMethodSettingsInterface.java b/java/src/org/kelar/inputmethodcommon/InputMethodSettingsInterface.java
new file mode 100644
index 000000000..fd7a421c0
--- /dev/null
+++ b/java/src/org/kelar/inputmethodcommon/InputMethodSettingsInterface.java
@@ -0,0 +1,63 @@
+/*
+ * 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.kelar.inputmethodcommon;
+
+import android.graphics.drawable.Drawable;
+
+/**
+ * InputMethodSettingsInterface is the interface for adding IME related preferences to
+ * PreferenceActivity or PreferenceFragment.
+ */
+public 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);
+
+ /**
+ * Sets the title for the input method settings category with a CharSequence.
+ * @param title The title for this preference.
+ */
+ public void setInputMethodSettingsCategoryTitle(CharSequence title);
+
+ /**
+ * 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);
+
+ /**
+ * 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);
+
+ /**
+ * 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);
+
+ /**
+ * 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);
+}