Skip to content

Commit

Permalink
core: Preferences: adjust some necessary places
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Aug 4, 2021
1 parent 8a88038 commit df69aef
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 44 deletions.
18 changes: 0 additions & 18 deletions app/src/main/java/com/osfans/trime/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package com.osfans.trime;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
Expand All @@ -35,7 +34,6 @@
import android.os.SystemClock;
import android.util.TypedValue;

import com.osfans.trime.enums.InlineModeType;
import com.osfans.trime.enums.WindowsPositionType;
import com.osfans.trime.ime.core.Preferences;
import com.osfans.trime.util.AppVersionUtils;
Expand All @@ -62,7 +60,6 @@ public class Config {
private String schema_id;

private static Config self = null;
private SharedPreferences mPref;

private Map<String, String> fallbackColors;
private Map presetColorSchemes, presetKeyboards;
Expand All @@ -73,7 +70,6 @@ public class Config {

public Config(Context context) {
self = this;
mPref = Function.getPref(context);
themeName = getPrefs().getLooks().getSelectedTheme();
prepareRime(context);
deployTheme(context);
Expand Down Expand Up @@ -686,20 +682,6 @@ public Drawable getDrawable(String key) {
return drawableObject(o);
}

public InlineModeType getInlinePreedit() {
switch (getPrefs().getKeyboard().getInlinePreedit()) {
case "preview":
case "preedit":
case "true":
return InlineModeType.INLINE_PREVIEW;
case "composition":
return InlineModeType.INLINE_COMPOSITION;
case "input":
return InlineModeType.INLINE_INPUT;
}
return InlineModeType.INLINE_NONE;
}

public WindowsPositionType getWinPos() {
return WindowsPositionType.fromString(getString("layout/position"));
}
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/com/osfans/trime/Effect.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package com.osfans.trime;

import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Build;
import android.os.VibrationEffect;
Expand Down Expand Up @@ -57,7 +56,6 @@ public Effect(Context context) {
}

public void reset() {
SharedPreferences pref = Function.getPref(context);
duration = getPrefs().getKeyboard().getVibrationDuration();
durationLong = duration * 1L;
amplitude = getPrefs().getKeyboard().getVibrationAmplitude();
Expand Down
35 changes: 27 additions & 8 deletions app/src/main/java/com/osfans/trime/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

import android.annotation.TargetApi;
import android.app.SearchManager;
import android.content.ComponentName;
import android.content.ClipboardManager;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
Expand All @@ -31,12 +31,14 @@
import android.icu.util.Calendar;
import android.icu.util.ULocale;
import android.net.Uri;
import android.os.Build.*;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.preference.PreferenceManager;
import android.util.Log;
import android.util.SparseArray;
import android.view.KeyEvent;

import com.osfans.trime.ime.core.Preferences;
import com.osfans.trime.settings.PrefMainActivity;

import java.text.FieldPosition;
Expand Down Expand Up @@ -227,11 +229,9 @@ public static void sync(Context context) {
}

public static void syncBackground(Context ctx){
boolean success = Rime.syncUserData(ctx);
getPref(ctx).edit() //记录同步时间和状态
.putLong("last_sync_time",new Date().getTime())
.putBoolean("last_sync_status",success)
.apply();
final Preferences prefs = Preferences.Companion.defaultInstance();
prefs.getConf().setLastSyncTime(new Date().getTime());
prefs.getConf().setLastSyncStatus(Rime.syncUserData(ctx));
}

public static String getVersion(Context context) {
Expand All @@ -242,6 +242,16 @@ public static String getVersion(Context context) {
}
}

/**
* Check if the app is installed.
*
* @param context the parent context
* @param app the app package name
* @return if the app is installed
*
* @deprecated use {@link com.blankj.utilcode.util.AppUtils#isAppInstalled(String)} instead.
*/
@Deprecated
public static boolean isAppAvailable(Context context, String app) {
final PackageManager packageManager = context.getPackageManager();
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
Expand All @@ -256,6 +266,15 @@ public static boolean isAppAvailable(Context context, String app) {
return false;
}

/**
* Get the default shared preferences.
* @param context the parent context
* @return the default shared preferences
*
* @deprecated Use {@link com.osfans.trime.ime.core.Preferences} to organize
* shared preferences.
*/
@Deprecated
public static SharedPreferences getPref(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.osfans.trime.enums

package com.osfans.trime.enums;
/** 嵌入模式枚举 */
enum class InlineModeType {
INLINE_NONE, INLINE_PREVIEW, INLINE_COMPOSITION, INLINE_INPUT;

/** 嵌入模式枚举 */
public enum InlineModeType {
INLINE_NONE,
INLINE_PREVIEW,
INLINE_COMPOSITION,
INLINE_INPUT
}
companion object {
fun fromString(string: String): InlineModeType {
return when (string) {
"preview", "preedit", "true" -> INLINE_PREVIEW
"composition" -> INLINE_COMPOSITION
"input" -> INLINE_INPUT
else -> INLINE_NONE
}
}
}
}
7 changes: 4 additions & 3 deletions app/src/main/java/com/osfans/trime/ime/core/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.SharedPreferences
import androidx.preference.PreferenceManager
import com.blankj.utilcode.util.PathUtils
import com.osfans.trime.R
import com.osfans.trime.enums.InlineModeType
import java.lang.ref.WeakReference

/**
Expand Down Expand Up @@ -158,9 +159,9 @@ class Preferences(
const val LONG_PRESS_TIMEOUT = "keyboard__key_long_press_timeout"
const val REPEAT_INTERVAL = "keyboard__key_repeat_interval"
}
var inlinePreedit: String = ""
get() = prefs.getPref(INLINE_PREEDIT_MODE, "preview")
private set
var inlinePreedit: InlineModeType
get() = InlineModeType.fromString(prefs.getPref(INLINE_PREEDIT_MODE, "preview"))
set(v) = prefs.setPref(INLINE_PREEDIT_MODE, v)
var softCursorEnabled: Boolean = false
get() = prefs.getPref(SOFT_CURSOR_ENABLED, true)
private set
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/osfans/trime/ime/core/Trime.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public void run() {
}

public void loadConfig() {
inlinePreedit = mConfig.getInlinePreedit();
inlinePreedit = getPrefs().getKeyboard().getInlinePreedit();
winPos = mConfig.getWinPos();
movable = mConfig.getString("layout/movable");
candSpacing = mConfig.getPixel("layout/spacing");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class OtherFragment: PreferenceFragmentCompat(),
}
override fun onPreferenceTreeClick(preference: Preference?): Boolean {
return when (preference?.key) {
"conf__clipboard_manager" -> {
"other__clipboard_manager" -> {
PreferenceManager.getDefaultSharedPreferences(context).getString("pref_clipboard_manager", "")
?.let {
ClipBoardManagerDialog(requireContext(),
Expand All @@ -59,20 +59,20 @@ class OtherFragment: PreferenceFragmentCompat(),
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
val trime = Trime.getService()
when (key) {
"conf__show_status_bar_icon" -> {
"other__show_status_bar_icon" -> {
if (sharedPreferences?.getBoolean(key, false) == true) {
trime?.showStatusIcon(R.drawable.ic_status)
} else { trime.hideStatusIcon() }
}

"conf__clipboard_compare" -> {
"other__clipboard_compare" -> {
Config.get(context).setClipBoardCompare(
sharedPreferences?.getString(key, "")

)
}

"conf__clipboard_output" -> {
"other__clipboard_output" -> {
Config.get(context).setClipBoardOutput(
sharedPreferences?.getString(key, "")
)
Expand Down

0 comments on commit df69aef

Please sign in to comment.