Skip to content

Commit

Permalink
Replaced handler with runOnUiThread, misc improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Iscle <[email protected]>
  • Loading branch information
iscle committed May 30, 2021
1 parent ef91066 commit 2216a4b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
33 changes: 11 additions & 22 deletions app/src/main/java/com/aof/mcinabox/activity/OldMainActivity.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.aof.mcinabox.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;

import com.aof.mcinabox.R;
import com.aof.mcinabox.databinding.ActivityOldMainBinding;
import com.aof.mcinabox.gamecontroller.definitions.manifest.AppManifest;
import com.aof.mcinabox.launcher.lang.LangManager;
import com.aof.mcinabox.launcher.setting.SettingManager;
Expand All @@ -28,6 +25,7 @@
import java.util.TimerTask;

public class OldMainActivity extends BaseActivity {
private static final String TAG = "MainActivity";

public static final int LAUNCHER_IMPT_RTPACK = 127;
public static WeakReference<OldMainActivity> CURRENT_ACTIVITY;
Expand All @@ -36,16 +34,17 @@ public class OldMainActivity extends BaseActivity {
public TipperManager mTipperManager;
public SettingManager mSettingManager;
public ThemeManager mThemeManager;
private ActivityOldMainBinding binding;
private static final int REFRESH_DELAY = 0; //ms
private static final int REFRESH_PERIOD = 500; //ms
private static final String TAG = "MainActivity";
public static SettingJson Setting;
private boolean enableSettingChecker = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_old_main);
binding = ActivityOldMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
//静态对象
CURRENT_ACTIVITY = new WeakReference<>(this);
//使用语言管理器切换语言
Expand Down Expand Up @@ -75,7 +74,7 @@ protected void onCreate(Bundle savedInstanceState) {
getWindow().setNavigationBarColor(Color.WHITE);
}

findViewById(R.id.toolbar_button_new_ui).setOnClickListener(v -> {
binding.layoutToolbarMain.toolbarButtonNewUi.setOnClickListener(v -> {
Intent i = new Intent(OldMainActivity.this, MainActivity.class);
startActivity(i);
});
Expand Down Expand Up @@ -156,18 +155,6 @@ public void refreshLauncher() {
mUiManager.refreshUis();
}

@SuppressLint("HandlerLeak")
public Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 1) {
Log.e("mcinabox", "Updata Setting.");
refreshLauncher();
updateSettingFromUis();
}
super.handleMessage(msg);
}
};

/**
* 【给Minecraft目录设置无媒体标签】
**/
Expand Down Expand Up @@ -219,9 +206,11 @@ private TimerTask createRefreshTimerTask() {
return new TimerTask() {
@Override
public void run() {
Message msg = new Message();
msg.what = 1;
handler.sendMessage(msg);
runOnUiThread(() -> {
Log.d(TAG, "run: Updating settings.");
refreshLauncher();
updateSettingFromUis();
});
}
};
}
Expand Down
14 changes: 10 additions & 4 deletions app/src/main/java/com/aof/mcinabox/activity/SplashActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.aof.mcinabox.activity;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
Expand All @@ -9,6 +10,8 @@
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.aof.mcinabox.R;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;

public class SplashActivity extends BaseActivity {
Expand All @@ -32,8 +35,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

private void startApp() {
Intent i = new Intent(this, OldMainActivity.class);
startActivity(i);
startActivity(new Intent(this, OldMainActivity.class));
finishAffinity();
}

Expand All @@ -51,8 +53,12 @@ private boolean checkPermissions() {
}

private void handleInitFailed() {
Toast.makeText(this, "Oops! Something went wrong while starting the app.", Toast.LENGTH_SHORT).show();
finishAffinity();
new AlertDialog.Builder(this)
.setTitle(R.string.error)
.setMessage(R.string.error_starting_the_app)
.setCancelable(false)
.setPositiveButton("Exit", (dialog, which) -> finishAffinity())
.show();
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,7 @@
<string name="title_enter" translatable="false">Enter</string>
<string name="title_t_enter" translatable="false">T + Enter</string>
<string name="title_done" translatable="false">Done</string>
<string name="error">Error</string>
<string name="error_starting_the_app">Something went wrong while starting the app. Try again later.</string>

</resources>

0 comments on commit 2216a4b

Please sign in to comment.