Commit 4d92c3b2 authored by kenan's avatar kenan

settings

parent e8f52553
......@@ -114,56 +114,6 @@ irr::io::path getExternalFilesDir(ANDROID_APP app) {
return ret;
}
float getScreenHeight(ANDROID_APP app) {
float ret;
if (!app || !app->activity || !app->activity->vm)
return ret;
JNIEnv* jni = 0;
app->activity->vm->AttachCurrentThread(&jni, NULL);
if (!jni)
return ret;
// Retrieves NativeActivity.
jobject lNativeActivity = app->activity->clazz;
jclass ClassNativeActivity = jni->GetObjectClass(lNativeActivity);
jmethodID MethodGetApp = jni->GetMethodID(ClassNativeActivity,
"getApplication", "()Landroid/app/Application;");
jobject application = jni->CallObjectMethod(lNativeActivity, MethodGetApp);
jclass classApp = jni->GetObjectClass(application);
jmethodID resdirMethod = jni->GetMethodID(classApp, "getScreenHeight",
"()F");
ret = jni->CallFloatMethod(application, resdirMethod);
jni->DeleteLocalRef(classApp);
jni->DeleteLocalRef(ClassNativeActivity);
app->activity->vm->DetachCurrentThread();
return ret;
}
float getScreenWidth(ANDROID_APP app) {
float ret;
if (!app || !app->activity || !app->activity->vm)
return ret;
JNIEnv* jni = 0;
app->activity->vm->AttachCurrentThread(&jni, NULL);
if (!jni)
return ret;
// Retrieves NativeActivity.
jobject lNativeActivity = app->activity->clazz;
jclass ClassNativeActivity = jni->GetObjectClass(lNativeActivity);
jmethodID MethodGetApp = jni->GetMethodID(ClassNativeActivity,
"getApplication", "()Landroid/app/Application;");
jobject application = jni->CallObjectMethod(lNativeActivity, MethodGetApp);
jclass classApp = jni->GetObjectClass(application);
jmethodID resdirMethod = jni->GetMethodID(classApp, "getScreenWidth",
"()F");
ret = jni->CallFloatMethod(application, resdirMethod);
jni->DeleteLocalRef(classApp);
jni->DeleteLocalRef(ClassNativeActivity);
app->activity->vm->DetachCurrentThread();
return ret;
}
irr::io::path getDBDir(ANDROID_APP app) {
irr::io::path ret;
if (!app || !app->activity || !app->activity->vm)
......
......@@ -75,21 +75,7 @@ private:
static unsigned char signed_buff[16] = { 0x30, 0x82, 0x2, 0x41, 0x30, 0x82, 0x1,
0xAA, 0xA0, 0x3, 0x2, 0x1, 0x2, 0x2, 0x4, 0x53 };
struct SDisplayMetrics {
irr::s32 widthPixels;
irr::s32 heightPixels;
irr::f32 density;
irr::s32 densityDpi;
irr::f32 scaledDensity;
irr::f32 xdpi;
irr::f32 ydpi;
};
/* jni utils*/
// Access SDisplayMetrics
extern float getScreenWidth(ANDROID_APP app);
extern float getScreenHeight(ANDROID_APP app);
// Get SDCard path.
extern irr::io::path getExternalStorageDir(ANDROID_APP app);
......
......@@ -4,7 +4,7 @@ android {
compileSdkVersion rootProject.ext.compileSdkVersion
defaultConfig {
minSdkVersion 16
minSdkVersion 19
targetSdkVersion 28
flavorDimensions "versionCode"
/* ndk {
......
......@@ -17,17 +17,36 @@ import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import cn.garymb.ygomobile.core.GameConfig;
import cn.garymb.ygomobile.core.GameSize;
import cn.garymb.ygomobile.core.IrrlichtBridge;
public abstract class GameApplication extends Application implements IrrlichtBridge.IrrlichtApplication {
private SoundPool mSoundEffectPool;
private Map<String, Integer> mSoundIdMap;
private GameConfig gameConfig = new GameConfig();
private static GameApplication sGameApplication;
private boolean isInitSoundEffectPool=false;
private static String sProcessName;
public boolean setGameConfig(GameConfig gameConfig) {
if (!this.gameConfig.equals(gameConfig)) {
this.gameConfig = gameConfig;
Log.i("kk", "setGameConfig:" + gameConfig);
if(gameConfig.isEnableSoundEffect()){
initSoundEffectPool();
setInitSoundEffectPool(true);
}
return true;
}
return false;
}
public final GameConfig getGameConfig() {
return gameConfig;
}
@Override
public void onCreate() {
super.onCreate();
......@@ -64,16 +83,6 @@ public abstract class GameApplication extends Application implements IrrlichtBri
isInitSoundEffectPool = initSoundEffectPool;
}
public int getGameWidth(){
return 1024;
}
public int getGameHeight(){
return 640;
}
public abstract boolean isKeepScale();
@SuppressWarnings("deprecation")
public void initSoundEffectPool() {
mSoundEffectPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
......@@ -92,29 +101,47 @@ public abstract class GameApplication extends Application implements IrrlichtBri
}
}
public abstract NativeInitOptions getNativeInitOptions();
public abstract GameSize getGameSize(Activity activity);
public abstract float getSmallerSize();
public NativeInitOptions getNativeInitOptions() {
return getGameConfig().getNativeInitOptions();
}
public abstract boolean isLockSreenOrientation();
@Override
public abstract float getXScale();
public abstract boolean isSensorRefresh();
@Override
public abstract float getYScale();
/**
* @deprecated
*/
public boolean canNdkCash() {
return true;
@Override
public String getCardImagePath() {
return getGameConfig().getImagePath();
}
@Override
public String getFontPath() {
return getGameConfig().getFontPath();
}
public void attachGame(Activity activity) {
public boolean isKeepScale() {
return getGameConfig().isKeepScale();
}
public boolean isLockSreenOrientation() {
return getGameConfig().isLockScreenOrientation();
}
/***
* 隐藏底部导航栏
*/
public abstract boolean isImmerSiveMode();
public boolean isImmerSiveMode() {
return getGameConfig().isImmerSiveMode();
}
public boolean isSensorRefresh() {
return getGameConfig().isSensorRefresh();
}
@Override
public void playSoundEffect(String path) {
......
package cn.garymb.ygomobile;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public final class NativeInitOptions {
public final class NativeInitOptions implements Parcelable {
private static final int BUFFER_MAX_SIZE = 8192;
......@@ -85,4 +88,64 @@ public final class NativeInitOptions {
private void putInt(ByteBuffer buffer, int value) {
buffer.putInt((Integer.reverseBytes(value)));
}
@Override
public int describeContents() {
return 0;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NativeInitOptions options = (NativeInitOptions) o;
return mOpenglVersion == options.mOpenglVersion &&
mIsSoundEffectEnabled == options.mIsSoundEffectEnabled &&
mCardQuality == options.mCardQuality &&
mIsFontAntiAliasEnabled == options.mIsFontAntiAliasEnabled &&
mIsPendulumScaleEnabled == options.mIsPendulumScaleEnabled &&
Objects.equals(mWorkPath, options.mWorkPath) &&
Objects.equals(mDbList, options.mDbList) &&
Objects.equals(mArchiveList, options.mArchiveList);
}
@Override
public int hashCode() {
return Objects.hash(mOpenglVersion, mIsSoundEffectEnabled, mWorkPath, mDbList, mArchiveList, mCardQuality, mIsFontAntiAliasEnabled, mIsPendulumScaleEnabled);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.mOpenglVersion);
dest.writeByte(this.mIsSoundEffectEnabled ? (byte) 1 : (byte) 0);
dest.writeString(this.mWorkPath);
dest.writeStringList(this.mDbList);
dest.writeStringList(this.mArchiveList);
dest.writeInt(this.mCardQuality);
dest.writeByte(this.mIsFontAntiAliasEnabled ? (byte) 1 : (byte) 0);
dest.writeByte(this.mIsPendulumScaleEnabled ? (byte) 1 : (byte) 0);
}
protected NativeInitOptions(Parcel in) {
this.mOpenglVersion = in.readInt();
this.mIsSoundEffectEnabled = in.readByte() != 0;
this.mWorkPath = in.readString();
this.mDbList = in.createStringArrayList();
this.mArchiveList = in.createStringArrayList();
this.mCardQuality = in.readInt();
this.mIsFontAntiAliasEnabled = in.readByte() != 0;
this.mIsPendulumScaleEnabled = in.readByte() != 0;
}
public static final Parcelable.Creator<NativeInitOptions> CREATOR = new Parcelable.Creator<NativeInitOptions>() {
@Override
public NativeInitOptions createFromParcel(Parcel source) {
return new NativeInitOptions(source);
}
@Override
public NativeInitOptions[] newArray(int size) {
return new NativeInitOptions[size];
}
};
}
......@@ -31,6 +31,8 @@ import java.nio.ByteBuffer;
import cn.garymb.ygodata.YGOGameOptions;
import cn.garymb.ygomobile.controller.NetworkController;
import cn.garymb.ygomobile.core.GameConfig;
import cn.garymb.ygomobile.core.GameSize;
import cn.garymb.ygomobile.core.IrrlichtBridge;
import cn.garymb.ygomobile.lib.R;
import cn.garymb.ygomobile.utils.SignUtils;
......@@ -92,8 +94,6 @@ public class YGOMobileActivity extends NativeActivity implements
private NetworkController mNetController;
private volatile boolean mOverlayShowRequest = false;
private volatile int mCompatGUIMode;
private static int sChainControlXPostion = -1;
private static int sChainControlYPostion = -1;
private GameApplication mApp;
private Handler handler = new Handler();
private volatile int mPositionX, mPositionY;
......@@ -122,15 +122,16 @@ public class YGOMobileActivity extends NativeActivity implements
@SuppressWarnings("WrongConstant")
@Override
protected void onCreate(Bundle savedInstanceState) {
if(USE_SURFACE) {
if (USE_SURFACE) {
mSurfaceView = new SurfaceView(this);
}
GameConfig config = getIntent().getParcelableExtra(GameConfig.EXTRA_CONFIG);
if(config != null) {
GameApplication.get().setGameConfig(config);
}
fullscreen();
super.onCreate(savedInstanceState);
Log.e("YGOStarter","跳转完成"+System.currentTimeMillis());
if (sChainControlXPostion < 0) {
initPostion();
}
Log.e("YGOStarter", "跳转完成" + System.currentTimeMillis());
if (app().isLockSreenOrientation()) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
......@@ -153,6 +154,7 @@ public class YGOMobileActivity extends NativeActivity implements
}
}
private GameConfig mGameConfig;
//电池管理
private PowerManager mPM;
private PowerManager.WakeLock mLock;
......@@ -160,7 +162,7 @@ public class YGOMobileActivity extends NativeActivity implements
@Override
protected void onResume() {
super.onResume();
Log.e("YGOStarter","ygo显示"+System.currentTimeMillis());
Log.e("YGOStarter", "ygo显示" + System.currentTimeMillis());
if (mLock == null) {
if (mPM == null) {
mPM = (PowerManager) getSystemService(POWER_SERVICE);
......@@ -188,20 +190,15 @@ public class YGOMobileActivity extends NativeActivity implements
}
}
private void initPostion() {
final Resources res = getResources();
sChainControlXPostion = (int) (CHAIN_CONTROL_PANEL_X_POSITION_LEFT_EDGE * app()
.getXScale());
sChainControlYPostion = (int) (app().getSmallerSize()
- CHAIN_CONTROL_PANEL_Y_REVERT_POSITION
* app().getYScale() - (res
.getDimensionPixelSize(R.dimen.chain_control_button_height) * 2 + res
.getDimensionPixelSize(R.dimen.chain_control_margin)));
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.hasExtra(GameConfig.EXTRA_CONFIG)) {
GameConfig config = getIntent().getParcelableExtra(GameConfig.EXTRA_CONFIG);
if(config != null) {
GameApplication.get().setGameConfig(config);
}
}
handleExternalCommand(intent);
}
......@@ -245,24 +242,12 @@ public class YGOMobileActivity extends NativeActivity implements
} else {
getWindow().getDecorView().setSystemUiVisibility(windowsFlags2);
}
app().attachGame(this);
GameSize size = GameApplication.get().getGameSize(this);
if (USE_SURFACE) {
changeGameSize();
} else {
int[] size = getGameSize();
changeGameSize(size);
}
}
private int[] getGameSize(){
//调整padding
float xScale = app().getXScale();
float yScale = app().getYScale();
int w = (int) (app().getGameWidth() * xScale);
int h = (int) (app().getGameHeight() * yScale);
Log.i("kk", "w1=" + app().getGameWidth() + ",h1=" + app().getGameHeight() + ",w2=" + w + ",h2=" + h + ",xScale=" + xScale + ",yScale=" + yScale);
return new int[]{w, h};
}
@Override
public int getPositionX() {
synchronized (this) {
......@@ -279,9 +264,9 @@ public class YGOMobileActivity extends NativeActivity implements
@Override
public void setContentView(View view) {
int[] size = getGameSize();
int w = size[0];
int h = size[1];
GameSize size = GameApplication.get().getGameSize(this);
int w = size.getWidth();
int h = size.getHeight();
mLayout = new FrameLayout(this);
// mLayout.setFitsSystemWindows(true);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(w, h);
......@@ -290,13 +275,12 @@ public class YGOMobileActivity extends NativeActivity implements
mLayout.addView(mSurfaceView, lp);
mLayout.addView(view, lp);
super.setContentView(mLayout);
app().attachGame(this);
getWindow().takeSurface(null);
replaced = true;
mSurfaceView.getHolder().addCallback(this);
mSurfaceView.requestFocus();
getWindow().setGravity(Gravity.CENTER);
changeGameSize();
changeGameSize(size);
} else {
mLayout.addView(view, lp);
super.setContentView(mLayout);
......@@ -305,13 +289,10 @@ public class YGOMobileActivity extends NativeActivity implements
}
}
private void changeGameSize(){
private void changeGameSize(GameSize gameSize) {
//游戏大小
int[] size = getGameSize();
int w = (int) app().getScreenHeight();
int h = (int) app().getScreenWidth();
int spX = (int) ((w - size[0]) / 2.0f);
int spY = (int) ((h - size[1]) / 2.0f);
int spX = gameSize.getTouchX();
int spY = gameSize.getTouchY();
// Log.i("ygo", "Android command 1:posX=" + spX + ",posY=" + spY);
boolean update = false;
synchronized (this) {
......@@ -504,7 +485,7 @@ public class YGOMobileActivity extends NativeActivity implements
@Override
public void surfaceCreated(SurfaceHolder holder) {
if(USE_SURFACE) {
if (USE_SURFACE) {
if (!replaced) {
return;
}
......@@ -514,7 +495,7 @@ public class YGOMobileActivity extends NativeActivity implements
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if(USE_SURFACE) {
if (USE_SURFACE) {
if (!replaced) {
return;
}
......@@ -524,7 +505,7 @@ public class YGOMobileActivity extends NativeActivity implements
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(USE_SURFACE) {
if (USE_SURFACE) {
if (!replaced) {
return;
}
......@@ -534,7 +515,7 @@ public class YGOMobileActivity extends NativeActivity implements
@Override
public void surfaceRedrawNeeded(SurfaceHolder holder) {
if(USE_SURFACE) {
if (USE_SURFACE) {
if (!replaced) {
return;
}
......
package cn.garymb.ygomobile.core;
import android.os.Parcel;
import android.os.Parcelable;
public class GameConfig {
static boolean error = false;
import cn.garymb.ygomobile.NativeInitOptions;
static {
try {
System.loadLibrary("game_version");
} catch (Throwable e) {
//ignore
error = true;
}
public class GameConfig implements Parcelable {
public static final String EXTRA_CONFIG = "ygo_config";
private NativeInitOptions nativeInitOptions;
private boolean lockScreenOrientation;
private boolean sensorRefresh;
private boolean keepScale;
/***
* 隐藏底部导航栏
*/
private boolean immerSiveMode;
private boolean enableSoundEffect;
private String fontPath;
private String resourcePath;
private String imagePath;
public NativeInitOptions getNativeInitOptions() {
return nativeInitOptions;
}
public static int getVersion(){
if(!error){
return getGameVersion();
}
public void setNativeInitOptions(NativeInitOptions nativeInitOptions) {
this.nativeInitOptions = nativeInitOptions;
}
public boolean isLockScreenOrientation() {
return lockScreenOrientation;
}
public void setLockScreenOrientation(boolean lockScreenOrientation) {
this.lockScreenOrientation = lockScreenOrientation;
}
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
public boolean isSensorRefresh() {
return sensorRefresh;
}
public void setSensorRefresh(boolean sensorRefresh) {
this.sensorRefresh = sensorRefresh;
}
public boolean isImmerSiveMode() {
return immerSiveMode;
}
public void setImmerSiveMode(boolean immerSiveMode) {
this.immerSiveMode = immerSiveMode;
}
public boolean isEnableSoundEffect() {
return enableSoundEffect;
}
public void setEnableSoundEffect(boolean enableSoundEffect) {
this.enableSoundEffect = enableSoundEffect;
}
public boolean isKeepScale() {
return keepScale;
}
public void setKeepScale(boolean keepScale) {
this.keepScale = keepScale;
}
public String getFontPath() {
return fontPath;
}
public void setFontPath(String fontPath) {
this.fontPath = fontPath;
}
public String getResourcePath() {
return resourcePath;
}
public void setResourcePath(String resourcePath) {
this.resourcePath = resourcePath;
}
public GameConfig() {
nativeInitOptions = new NativeInitOptions();
lockScreenOrientation = false;
sensorRefresh = true;
immerSiveMode = false;
enableSoundEffect = true;
}
@Override
public String toString() {
return "GameConfig{" +
"nativeInitOptions=" + nativeInitOptions +
", lockScreenOrientation=" + lockScreenOrientation +
", sensorRefresh=" + sensorRefresh +
", keepScale=" + keepScale +
", immerSiveMode=" + immerSiveMode +
", enableSoundEffect=" + enableSoundEffect +
", fontPath='" + fontPath + '\'' +
", resourcePath='" + resourcePath + '\'' +
", imagePath='" + imagePath + '\'' +
'}';
}
@Override
public int describeContents() {
return 0;
}
private static native int getGameVersion();
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(this.nativeInitOptions, flags);
dest.writeByte(this.lockScreenOrientation ? (byte) 1 : (byte) 0);
dest.writeByte(this.sensorRefresh ? (byte) 1 : (byte) 0);
dest.writeByte(this.keepScale ? (byte) 1 : (byte) 0);
dest.writeByte(this.immerSiveMode ? (byte) 1 : (byte) 0);
dest.writeByte(this.enableSoundEffect ? (byte) 1 : (byte) 0);
dest.writeString(this.fontPath);
dest.writeString(this.resourcePath);
dest.writeString(this.imagePath);
}
protected GameConfig(Parcel in) {
this.nativeInitOptions = in.readParcelable(NativeInitOptions.class.getClassLoader());
this.lockScreenOrientation = in.readByte() != 0;
this.sensorRefresh = in.readByte() != 0;
this.keepScale = in.readByte() != 0;
this.immerSiveMode = in.readByte() != 0;
this.enableSoundEffect = in.readByte() != 0;
this.fontPath = in.readString();
this.resourcePath = in.readString();
this.imagePath = in.readString();
}
public static final Creator<GameConfig> CREATOR = new Creator<GameConfig>() {
@Override
public GameConfig createFromParcel(Parcel source) {
return new GameConfig(source);
}
@Override
public GameConfig[] newArray(int size) {
return new GameConfig[size];
}
};
}
package cn.garymb.ygomobile.core;
public class GameSize {
private int width;
private int height;
private int touchX;
private int touchY;
public void update(GameSize size) {
synchronized (this) {
this.width = size.width;
this.height = size.height;
this.touchX = size.touchX;
this.touchY = size.touchY;
}
}
public void setTouch(int touchX, int touchY) {
synchronized (this) {
this.touchX = touchX;
this.touchY = touchY;
}
}
public int getWidth() {
synchronized (this) {
return width;
}
}
public int getHeight() {
synchronized (this) {
return height;
}
}
public int getTouchX() {
synchronized (this) {
return touchX;
}
}
public int getTouchY() {
synchronized (this) {
return touchY;
}
}
public GameSize() {
}
public GameSize(int width, int height, int touchX, int touchY) {
this.width = width;
this.height = height;
this.touchX = touchX;
this.touchY = touchY;
}
@Override
public String toString() {
return "GameSize{" +
"width=" + width +
", height=" + height +
", touchX=" + touchX +
", touchY=" + touchY +
'}';
}
}
......@@ -23,6 +23,8 @@ import static cn.garymb.ygomobile.utils.ByteUtils.byte2uint;
* @author mabin
*/
public final class IrrlichtBridge {
public static final float GAME_WIDTH = 1024.0f;
public static final float GAME_HEIGHT = 640.0f;
public static final String ACTION_START = "cn.garymb.ygomobile.game.start";
public static final String ACTION_STOP = "cn.garymb.ygomobile.game.stop";
public static final String EXTRA_PID = "extras.mypid";
......@@ -169,10 +171,6 @@ public final class IrrlichtBridge {
void saveIntSetting(String key,int value);
float getScreenWidth();
float getScreenHeight();
void playSoundEffect(String path);
void runWindbot(String args);
......
......@@ -4,7 +4,7 @@ android {
compileSdkVersion rootProject.ext.compileSdkVersion
defaultConfig {
minSdkVersion 16
minSdkVersion 19
//noinspection ExpiredTargetSdkVersion
targetSdkVersion 22
......
......@@ -197,11 +197,6 @@
android:name="cn.garymb.ygomobile.ui.plus.ServiceDuelAssistant"
android:priority="1000" />
<provider
android:authorities="${applicationId}.preference"
android:name="cn.garymb.ygomobile.ui.preference.YGOPreferencesProvider"
android:exported="false"/>
</application>
<uses-permission android:name="android.permission.INTERNET" />
......
package cn.garymb.ygomobile;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.util.Log;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatDelegate;
......@@ -12,9 +16,28 @@ import com.bumptech.glide.Glide;
import com.yuyh.library.imgsel.ISNav;
import com.yuyh.library.imgsel.common.ImageLoader;
import cn.garymb.ygomobile.core.GameConfig;
import cn.garymb.ygomobile.core.GameSize;
import cn.garymb.ygomobile.core.IrrlichtBridge;
import cn.garymb.ygomobile.utils.CrashHandler;
public class App extends GameApplication {
private SharedPreferences settings;
private GameSize mGameSize = new GameSize();
public SharedPreferences getSettings() {
if (settings == null) {
synchronized (this) {
if (settings == null) {
settings = getSharedPreferences("ygo_settings", Context.MODE_PRIVATE);
}
}
}
if(!isGameProcess()){
Log.e("kk", "don't running in game process");
}
return settings;
}
@Override
public void onCreate() {
......@@ -24,105 +47,110 @@ public class App extends GameApplication {
//初始化异常工具类
CrashHandler crashHandler = CrashHandler.getInstance();
crashHandler.init(getApplicationContext());
if (AppsSettings.get().isSoundEffect()) {
initSoundEffectPool();
setInitSoundEffectPool(true);
}
//初始化图片选择器
initImgsel();
// QbSdk.initX5Environment(this, null);
// QbSdk.setCurrentID("");
}
@Override
public NativeInitOptions getNativeInitOptions() {
NativeInitOptions options = AppsSettings.get().getNativeInitOptions();
return options;
}
@Override
public float getSmallerSize() {
return AppsSettings.get().getSmallerSize();
}
@Override
public void attachGame(Activity activity) {
super.attachGame(activity);
AppsSettings.get().update(activity);
public GameSize getGameSize(Activity activity) {
boolean immerSiveMode = getGameConfig().isImmerSiveMode();
boolean keepScale = getGameConfig().isKeepScale();
int maxW, maxH;
int fullW, fullH, actW, actH;
Point size = new Point();
activity.getWindowManager().getDefaultDisplay().getRealSize(size);
fullW = size.x;
fullH = size.y;
actW = activity.getWindowManager().getDefaultDisplay().getWidth();
actH = activity.getWindowManager().getDefaultDisplay().getHeight();
int w1, h1;
if (immerSiveMode) {
w1 = fullW;
h1 = fullH;
} else {
w1 = actW;
h1 = actH;
}
maxW = Math.max(w1, h1);
maxH = Math.min(w1, h1);
Log.i("kk", "maxW=" + maxW + ",maxH=" + maxH);
float sx, sy, scale;
int gw, gh;
if (keepScale) {
sx = (float) maxW / IrrlichtBridge.GAME_WIDTH;
sy = (float) maxH / IrrlichtBridge.GAME_HEIGHT;
scale = Math.min(sx, sy);
gw = (int) (IrrlichtBridge.GAME_WIDTH * scale);
gh = (int) (IrrlichtBridge.GAME_HEIGHT * scale);
} else {
gw = maxW;
gh = maxH;
}
Log.i("kk", "game=" + gw + "x" + gh);
//fix touch point
int left = (maxW - gw) / 2;
int top = (maxH - gh) / 2;
Log.i("kk", "touch fix=" + left + "x" + top);
//if(huawei and liuhai){
// left-=liuhai
// }
mGameSize = new GameSize(gw, gh, left, top);
return mGameSize;
}
@Override
public float getXScale() {
return AppsSettings.get().getXScale(getGameWidth(), getGameHeight());
if (mGameSize == null) {
//TODO error
return 1.0f;
}
return mGameSize.getWidth() / IrrlichtBridge.GAME_WIDTH;
}
@Override
public float getYScale() {
return AppsSettings.get().getYScale(getGameWidth(), getGameHeight());
}
@Override
public String getCardImagePath() {
return AppsSettings.get().getCardImagePath();
}
@Override
public String getFontPath() {
return AppsSettings.get().getFontPath();
}
@Override
public boolean isKeepScale() {
return AppsSettings.get().isKeepScale();
if (mGameSize == null) {
//TODO error
return 1.0f;
}
return mGameSize.getHeight() / IrrlichtBridge.GAME_HEIGHT;
}
@SuppressLint("ApplySharedPref")
@Override
public void saveSetting(String key, String value) {
AppsSettings.get().saveSettings(key, value);
if (Constants.CONF_LAST_DECK.equals(key)) {
LocalConfig.getInstance(this).setLastDeck(value);
} else if (Constants.CONF_LAST_CATEGORY.equals(key)) {
LocalConfig.getInstance(this).setLastCategory(value);
} else {
getSettings().edit().putString(key, value).commit();
}
}
@Override
public String getSetting(String key) {
return AppsSettings.get().getSettings(key);
if (Constants.CONF_LAST_DECK.equals(key)) {
return LocalConfig.getInstance(this).getLastDeck();
} else if (Constants.CONF_LAST_CATEGORY.equals(key)) {
return LocalConfig.getInstance(this).getLastCategory();
} else {
return getSettings().getString(key, null);
}
}
@Override
public int getIntSetting(String key, int def) {
return AppsSettings.get().getIntSettings(key, def);
return getSettings().getInt(key, def);
}
@SuppressLint("ApplySharedPref")
@Override
public void saveIntSetting(String key, int value) {
AppsSettings.get().saveIntSettings(key, value);
}
@Override
public float getScreenWidth() {
return AppsSettings.get().getScreenWidth();
}
@Override
public boolean isLockSreenOrientation() {
return AppsSettings.get().isLockSreenOrientation();
}
@Override
public boolean canNdkCash() {
return false;
}
@Override
public boolean isImmerSiveMode() {
return AppsSettings.get().isImmerSiveMode();
}
public boolean isSensorRefresh() {
return AppsSettings.get().isSensorRefresh();
}
@Override
public float getScreenHeight() {
return AppsSettings.get().getScreenHeight();
getSettings().edit().putInt(key, value).commit();
}
@Override
......
......@@ -19,9 +19,6 @@ import java.util.List;
import java.util.Locale;
import cn.garymb.ygomobile.lite.R;
import cn.garymb.ygomobile.ui.preference.PreferenceFragmentPlus;
import cn.garymb.ygomobile.ui.preference.SharedPreferencesPlus;
import cn.garymb.ygomobile.ui.preference.YGOPreferencesProvider;
import cn.garymb.ygomobile.utils.DeckUtil;
import cn.garymb.ygomobile.utils.IOUtils;
......@@ -34,10 +31,8 @@ import static cn.garymb.ygomobile.Constants.DEF_PREF_KEEP_SCALE;
import static cn.garymb.ygomobile.Constants.DEF_PREF_NOTCH_HEIGHT;
import static cn.garymb.ygomobile.Constants.DEF_PREF_ONLY_GAME;
import static cn.garymb.ygomobile.Constants.DEF_PREF_READ_EX;
import static cn.garymb.ygomobile.Constants.PREF_DEF_IMMERSIVE_MODE;
import static cn.garymb.ygomobile.Constants.PREF_DEF_SENSOR_REFRESH;
import static cn.garymb.ygomobile.Constants.PREF_FONT_SIZE;
import static cn.garymb.ygomobile.Constants.PREF_IMMERSIVE_MODE;
import static cn.garymb.ygomobile.Constants.PREF_KEEP_SCALE;
import static cn.garymb.ygomobile.Constants.PREF_LOCK_SCREEN;
import static cn.garymb.ygomobile.Constants.PREF_NOTCH_HEIGHT;
......@@ -60,12 +55,9 @@ public class AppsSettings {
private AppsSettings(Context context) {
this.context = context;
String name = context.getPackageName() + ".settings";
if (App.isGameProcess()) {
mSharedPreferences = YGOPreferencesProvider.getOrCreate(context, name);
} else {
mSharedPreferences = new SharedPreferencesPlus(context, name, Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS);
}
mSharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS);
//
LocalConfig.getInstance(context).updateFromOld(mSharedPreferences);
Log.e("YGOMobileLog", "初始化类地址: " + System.identityHashCode(this));
update(context);
}
......@@ -510,10 +502,6 @@ public class AppsSettings {
return mSharedPreferences.getBoolean(PREF_SENSOR_REFRESH, PREF_DEF_SENSOR_REFRESH);
}
public String getCurLastDeck() {
return mSharedPreferences.getString(Constants.PREF_DEF_LAST_YDK, null);
}
//获得最后卡组绝对路径
public String getLastDeckPath() {
String path;
......@@ -538,33 +526,21 @@ public class AppsSettings {
return;
}
//保存最后分类名
mSharedPreferences.edit().putString(Constants.PREF_LAST_CATEGORY, DeckUtil.getDeckTypeName(path)).apply();
LocalConfig.getInstance(context).setLastCategory(DeckUtil.getDeckTypeName(path));
//保存最后卡组名
File lastDeck = new File(path);
String lastDeckName = IOUtils.tirmName(lastDeck.getName(), YDK_FILE_EX);
mSharedPreferences.edit().putString(Constants.PREF_LAST_YDK, lastDeckName).apply();
LocalConfig.getInstance(context).setLastDeck(lastDeckName);
}
//获得最后分类名
public String getLastCategory() {
return mSharedPreferences.getString(Constants.PREF_LAST_CATEGORY, Constants.PREF_DEF_LAST_CATEGORY);
return LocalConfig.getInstance(context).getLastCategory();
}
//获得最后卡组名
public String getLastDeckName() {
return mSharedPreferences.getString(Constants.PREF_LAST_YDK, Constants.PREF_DEF_LAST_YDK);
}
public void saveIntSettings(String key, int value) {
mSharedPreferences.edit().putInt(Constants.PREF_START + key, value).apply();
}
public int getIntSettings(String key, int def) {
int v = mSharedPreferences.getInt(Constants.PREF_START + key, def);
if (v == def) {
Log.d("kk", "default " + key + "=" + getVersionString(v));
}
return v;
return LocalConfig.getInstance(context).getLastDeck();
}
/* public int resetGameVersion() {
* int version = GameConfig.getVersion();
......@@ -619,30 +595,6 @@ public class AppsSettings {
return v;
}*/
public void saveSettings(String key, String value) {
if ("lastdeck".equals(key)) {
Log.e("AppSettings", value);
mSharedPreferences.edit().putString(Constants.PREF_LAST_YDK, value).apply();
} else if ("lastcategory".equals(key)) {
Log.e("AppSettings", value);
mSharedPreferences.edit().putString(Constants.PREF_LAST_CATEGORY, value).apply();
} else {
mSharedPreferences.edit().putString(Constants.PREF_START + key, value).apply();
}
}
public String getSettings(String key) {
String val;
if ("lastdeck".equals(key)) {
val = getLastDeckName();
return val;
} else if ("lastcategory".equals(key)) {
val = getLastCategory();
return val;
}
return mSharedPreferences.getString(Constants.PREF_START + key, null);
}
public List<String> getLastRoomList() {
List<String> names = new ArrayList<>();
String json = mSharedPreferences.getString(Constants.PREF_LAST_ROOM_LIST, null);
......
......@@ -213,4 +213,7 @@ public interface Constants {
//打开ydk,是否复制到文件夹
boolean COPY_YDK_FILE = false;
String CONF_LAST_DECK = "lastdeck";
String CONF_LAST_CATEGORY = "lastcategory";
}
package cn.garymb.ygomobile;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import java.io.File;
import cn.garymb.ygomobile.utils.FileUtils;
public class LocalConfig {
@SuppressLint("StaticFieldLeak")
private static LocalConfig sLocalConfig;
public static LocalConfig getInstance(Context context) {
if (sLocalConfig == null) {
synchronized (LocalConfig.class) {
if (sLocalConfig == null) {
sLocalConfig = new LocalConfig(context);
}
}
}
return sLocalConfig;
}
private final File lastdeck;
private final File lastcategory;
private LocalConfig(Context context) {
File dir = context.getDir("ygo", Context.MODE_PRIVATE);
if (!dir.exists()) {
dir.mkdirs();
}
lastdeck = new File(dir, "lastdeck");
lastcategory = new File(dir, "lastcategory");
}
public String getLastDeck() {
if (lastdeck.exists()) {
return FileUtils.readAllString(lastdeck);
}
return null;
}
public void setLastDeck(String deck) {
FileUtils.writeAllString(lastdeck, deck);
}
public String getLastCategory() {
if (lastcategory.exists()) {
return FileUtils.readAllString(lastcategory);
}
return null;
}
public void setLastCategory(String category) {
FileUtils.writeAllString(lastcategory, category);
}
public void updateFromOld(SharedPreferences sharedPreferences){
if (sharedPreferences.contains(Constants.PREF_DEF_LAST_YDK)) {
setLastDeck(sharedPreferences.getString(Constants.PREF_DEF_LAST_YDK, null));
sharedPreferences.edit().remove(Constants.PREF_DEF_LAST_YDK).apply();
}
if (sharedPreferences.contains(Constants.PREF_LAST_CATEGORY)) {
setLastDeck(sharedPreferences.getString(Constants.PREF_LAST_CATEGORY, null));
sharedPreferences.edit().remove(Constants.PREF_LAST_CATEGORY).apply();
}
}
}
\ No newline at end of file
......@@ -25,6 +25,7 @@ import java.io.File;
import java.util.HashMap;
import cn.garymb.ygodata.YGOGameOptions;
import cn.garymb.ygomobile.core.GameConfig;
import cn.garymb.ygomobile.lite.R;
import cn.garymb.ygomobile.ui.plus.ViewTargetPlus;
import cn.garymb.ygomobile.utils.ComponentUtils;
......@@ -176,12 +177,27 @@ public class YGOStarter {
intent.putExtra(YGOGameOptions.YGO_GAME_OPTIONS_BUNDLE_TIME, System.currentTimeMillis());
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(GameConfig.EXTRA_CONFIG, genConfig());
Log.e("YGOStarter","跳转前"+System.currentTimeMillis());
activity.startActivity(intent);
Log.e("YGOStarter","跳转后"+System.currentTimeMillis());
}
}
private static GameConfig genConfig() {
GameConfig config = new GameConfig();
config.setNativeInitOptions(AppsSettings.get().getNativeInitOptions());
config.setLockScreenOrientation(AppsSettings.get().isLockSreenOrientation());
config.setSensorRefresh(AppsSettings.get().isSensorRefresh());
config.setImmerSiveMode(AppsSettings.get().isImmerSiveMode());
config.setEnableSoundEffect(AppsSettings.get().isSoundEffect());
config.setKeepScale(AppsSettings.get().isKeepScale());
config.setFontPath(AppsSettings.get().getFontPath());
config.setImagePath(AppsSettings.get().getCardImagePath());
config.setResourcePath(AppsSettings.get().getResourcePath());
return config;
}
private static HashMap<Activity, ActivityShowInfo> Infos = new HashMap<>();
private static class ActivityShowInfo {
......
package cn.garymb.ygomobile.ui.preference;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import cn.garymb.ygomobile.App;
import cn.garymb.ygomobile.GameApplication;
public class SharedPreferencesPlus implements SharedPreferences, SharedPreferences.Editor {
private String spName;
private Context context;
private ContentResolver mResolver;
private static final String TAG = "kk";
public SharedPreferencesPlus(Context context, String name, int mode) {
this.spName = name;
this.context = context;
this.mResolver = context.getContentResolver();
}
public Editor edit() {
return this;
}
protected Uri.Builder create() {
return new Uri.Builder()
.scheme("content")
.authority(YGOPreferencesProvider.AUTH)
.appendPath(spName);
}
private Uri create(String type, String key) {
return create()
.appendPath(type)
.appendPath(key).build();
}
@Override
public Editor putString(String key, String value) {
Uri uri = create(YGOPreferencesProvider.TYPE_STRING, key);
ContentValues contentValues = new ContentValues();
contentValues.put(YGOPreferencesProvider.COL_VALUE, value);
try {
mResolver.update(uri, contentValues, null, null);
} catch (Throwable e) {
Log.e(TAG, "putString:" + key + "=" + value, e);
}
return this;
}
@Override
public Editor putStringSet(String key, Set<String> values) {
return this;
}
@Override
public Editor putInt(String key, int value) {
Uri uri = create(YGOPreferencesProvider.TYPE_INT, key);
ContentValues contentValues = new ContentValues();
contentValues.put(YGOPreferencesProvider.COL_VALUE, value);
try {
mResolver.update(uri, contentValues, null, null);
} catch (Throwable e) {
Log.e(TAG, "putInt:" + key + "=" + value, e);
}
return this;
}
@Override
public Editor putLong(String key, long value) {
Uri uri = create(YGOPreferencesProvider.TYPE_LONG, key);
ContentValues contentValues = new ContentValues();
contentValues.put(YGOPreferencesProvider.COL_VALUE, value);
try {
mResolver.update(uri, contentValues, null, null);
} catch (Throwable e) {
Log.e(TAG, "putLong:" + key + "=" + value, e);
}
return this;
}
@Override
public Editor putFloat(String key, float value) {
Uri uri = create(YGOPreferencesProvider.TYPE_FLOAT, key);
ContentValues contentValues = new ContentValues();
contentValues.put(YGOPreferencesProvider.COL_VALUE, value);
try {
mResolver.update(uri, contentValues, null, null);
} catch (Throwable e) {
Log.e(TAG, "putFloat:" + key + "=" + value, e);
}
return this;
}
@Override
public Editor putBoolean(String key, boolean value) {
Uri uri = create(YGOPreferencesProvider.TYPE_BOOLEAN, key);
ContentValues contentValues = new ContentValues();
contentValues.put(YGOPreferencesProvider.COL_VALUE, value);
try {
mResolver.update(uri, contentValues, null, null);
} catch (Throwable e) {
Log.e(TAG, "putBoolean:" + key + "=" + value, e);
}
return this;
}
@Override
public Editor remove(String key) {
Uri uri = create()
.appendPath(key).build();
try {
mResolver.delete(uri, null, null);
} catch (Throwable e) {
Log.e(TAG, "remove:" + key, e);
}
return this;
}
@Override
public Editor clear() {
//TODO
return this;
}
@Override
public Map<String, ?> getAll() {
//TODO
return new HashMap<>();
}
@Override
public String getString(String key, String defValue) {
Uri uri = create(YGOPreferencesProvider.TYPE_STRING, key);
Cursor cursor = mResolver.query(uri, null, defValue, null, null);
if (cursor != null) {
try {
cursor.moveToFirst();
return cursor.getString(YGOPreferencesProvider.COL_VALUE_INDEX);
} catch (Throwable e) {
Log.e(TAG, "getString:" + key, e);
} finally {
cursor.close();
}
}
return defValue;
}
@Override
public Set<String> getStringSet(String key, Set<String> defValues) {
//TODO
return defValues;
}
@Override
public int getInt(String key, int defValue) {
Uri uri = create(YGOPreferencesProvider.TYPE_INT, key);
Cursor cursor = mResolver.query(uri, null, String.valueOf(defValue), null, null);
if (cursor != null) {
try{
cursor.moveToFirst();
return cursor.getInt(YGOPreferencesProvider.COL_VALUE_INDEX);
} catch (Throwable e) {
Log.e(TAG, "getInt:" + key, e);
} finally {
cursor.close();
}
}
return defValue;
}
@Override
public long getLong(String key, long defValue) {
Uri uri = create(YGOPreferencesProvider.TYPE_LONG, key);
Cursor cursor = mResolver.query(uri, null, String.valueOf(defValue), null, null);
if (cursor != null) {
try{
cursor.moveToFirst();
return cursor.getLong(YGOPreferencesProvider.COL_VALUE_INDEX);
} catch (Throwable e) {
Log.e(TAG, "getLong:" + key, e);
} finally {
cursor.close();
}
}
return defValue;
}
@Override
public float getFloat(String key, float defValue) {
Uri uri = create(YGOPreferencesProvider.TYPE_FLOAT, key);
Cursor cursor = mResolver.query(uri, null, String.valueOf(defValue), null, null);
if (cursor != null) {
try{
cursor.moveToFirst();
return cursor.getFloat(YGOPreferencesProvider.COL_VALUE_INDEX);
} catch (Throwable e) {
Log.e(TAG, "getFloat:" + key, e);
} finally {
cursor.close();
}
}
return defValue;
}
@Override
public boolean getBoolean(String key, boolean defValue) {
Uri uri = create(YGOPreferencesProvider.TYPE_BOOLEAN, key);
Cursor cursor = mResolver.query(uri, null, String.valueOf(defValue), null, null);
if (cursor != null) {
try{
cursor.moveToFirst();
return cursor.getInt(YGOPreferencesProvider.COL_VALUE_INDEX) > 0;
} catch (Throwable e) {
Log.e(TAG, "getBoolean:" + key, e);
} finally {
cursor.close();
}
}
return defValue;
}
@Override
public boolean contains(String key) {
//TODO
return true;
}
@Override
public boolean commit() {
//TODO
return true;
}
@Override
public void apply() {
//TODO
}
@Override
public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
//TODO
}
@Override
public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
//TODO
}
}
package cn.garymb.ygomobile.ui.preference;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.garymb.ygomobile.lite.BuildConfig;
public class YGOPreferencesProvider extends ContentProvider {
public static final String AUTH = BuildConfig.APPLICATION_ID + ".preference";
public static final String TYPE_STRING = "string";
public static final String TYPE_INT = "int";
public static final String TYPE_LONG = "long";
public static final String TYPE_FLOAT = "float";
public static final String TYPE_BOOLEAN = "boolean";
public static final String COL_NAME = "name";
public static final String COL_VALUE = "value";
public static final int COL_VALUE_INDEX = 0;
private static final Map<String, WeakReference<SharedPreferences>> sMap = new HashMap<>();
public static SharedPreferences getOrCreate(Context context, String name) {
if (name == null) {
name = context.getPackageName();
}
WeakReference<SharedPreferences> val;
synchronized (sMap) {
val = sMap.get(name);
if (val == null || val.get() == null) {
val = new WeakReference<SharedPreferences>(context.getSharedPreferences(name, Context.MODE_MULTI_PROCESS));
sMap.put(name, val);
}
}
return val.get();
}
@Override
public boolean onCreate() {
return true;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
List<String> paths = uri.getPathSegments();
String name = paths.get(0);
String type = paths.get(1);
String key = paths.get(2);
String def = selection;
SharedPreferences sharedPreferences = getOrCreate(getContext(), name);
MatrixCursor cursor = new MatrixCursor(new String[]{COL_VALUE});
try {
if (TYPE_BOOLEAN.equals(type)) {
cursor.addRow(new Object[]{(int) (sharedPreferences.getBoolean(key, "true".equalsIgnoreCase(def)) ? 1 : 0)});
} else if (TYPE_FLOAT.equals(type)) {
if (def == null) {
def = "0";
}
cursor.addRow(new Object[]{sharedPreferences.getFloat(key, Float.parseFloat(def))});
} else if (TYPE_LONG.equals(type)) {
if (def == null) {
def = "0";
}
cursor.addRow(new Object[]{sharedPreferences.getLong(key, Long.parseLong(def))});
} else if (TYPE_INT.equals(type)) {
if (def == null) {
def = "0";
}
cursor.addRow(new Object[]{sharedPreferences.getInt(key, Integer.parseInt(def))});
} else if (TYPE_STRING.equals(type)) {
cursor.addRow(new Object[]{sharedPreferences.getString(key, def)});
} else {
cursor.close();
return null;
}
} catch (Throwable e) {
Log.e("kk", "query", e);
cursor.close();
return null;
}
return cursor;
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
return null;
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
List<String> paths = uri.getPathSegments();
String name = paths.get(0);
String key = paths.get(1);
SharedPreferences sharedPreferences = getOrCreate(getContext(), name);
if (sharedPreferences.contains(key)) {
sharedPreferences.edit().remove(key).apply();
return 1;
}
return 0;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
Log.e("kk", "update " + uri);
List<String> paths = uri.getPathSegments();
String name = paths.get(0);
String type = paths.get(1);
String key = paths.get(2);
SharedPreferences sharedPreferences = getOrCreate(getContext(), name);
try {
if (TYPE_BOOLEAN.equals(type)) {
sharedPreferences.edit().putBoolean(key, values.getAsBoolean(COL_VALUE)).apply();
} else if (TYPE_FLOAT.equals(type)) {
sharedPreferences.edit().putFloat(key, values.getAsFloat(COL_VALUE)).apply();
} else if (TYPE_LONG.equals(type)) {
sharedPreferences.edit().putLong(key, values.getAsLong(COL_VALUE)).apply();
} else if (TYPE_INT.equals(type)) {
sharedPreferences.edit().putInt(key, values.getAsInteger(COL_VALUE)).apply();
} else if (TYPE_STRING.equals(type)) {
sharedPreferences.edit().putString(key, values.getAsString(COL_VALUE)).apply();
} else {
return 0;
}
}catch (Throwable e){
Log.e("kk", "update", e);
return 0;
}
return 1;
}
}
......@@ -198,14 +198,14 @@ public class SettingFragment extends PreferenceFragmentPlus {
}
}
//如果是音效开关
if (preference.getKey().equals(PREF_SOUND_EFFECT)) {
//如果打勾开启音效
if (checkBoxPreference.isChecked()) {
//如果未初始化音效
if (App.get().isInitSoundEffectPool())
App.get().initSoundEffectPool();
}
}
// if (preference.getKey().equals(PREF_SOUND_EFFECT)) {
// //如果打勾开启音效
// if (checkBoxPreference.isChecked()) {
// //如果未初始化音效
// if (App.get().isInitSoundEffectPool())
// App.get().initSoundEffectPool();
// }
// }
return true;
}
boolean rs = super.onPreferenceChange(preference, value);
......
......@@ -9,6 +9,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
......@@ -170,4 +171,45 @@ public class FileUtils {
out.write(data, 0, len);
}
}
public static String readAllString(File file) {
if (file == null || !file.exists()) {
return null;
}
FileInputStream inputStream = null;
byte[] data = new byte[1024];
try {
inputStream = new FileInputStream(file);
int len = inputStream.read(data);
return new String(data, 0, len, StandardCharsets.UTF_8).trim();
} catch (Throwable e) {
//ignore
} finally {
IOUtils.close(inputStream);
}
return null;
}
public static void writeAllString(File file, String str) {
if (file == null) {
return;
}
FileOutputStream outputStream = null;
try {
if (file.exists()) {
file.delete();
file.createNewFile();
}
outputStream = new FileOutputStream(file);
if (str != null) {
outputStream.write(str.getBytes(StandardCharsets.UTF_8));
}
outputStream.flush();
} catch (Throwable e) {
//ignore
} finally {
IOUtils.close(outputStream);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment