Skip to content

Commit

Permalink
General cleanup
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 2216a4b commit b8bb6bc
Show file tree
Hide file tree
Showing 92 changed files with 1,497 additions and 1,161 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".activity.LoginActivity"
android:screenOrientation="userLandscape" />
<activity
android:name=".activity.MainActivity"
android:screenOrientation="userLandscape" />
Expand All @@ -28,6 +31,7 @@
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/aof/mcinabox/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ private ArgumentsSubstitutor createArgumentsSubstitutor(Profile profile, Account
String path = new File(new File(assetsDirectory, "objects"), hash.substring(0, 2) + "/" + hash).getAbsolutePath();
map.put("asset=" + entry.getKey(), path);
}
} catch (IOException ignored) {}
} catch (IOException ignored) {
}
map.put("launcher_name", "MCinaBox");
map.put("launcher_version", BuildConfig.VERSION_NAME);
map.put("natives_directory", nativeDirectory.getAbsolutePath());
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/aof/mcinabox/activity/LoginActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.aof.mcinabox.activity;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.aof.mcinabox.databinding.ActivityLoginBinding;

public class LoginActivity extends AppCompatActivity {

private ActivityLoginBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityLoginBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class OldMainActivity extends BaseActivity {
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 int REFRESH_PERIOD = 1000; //ms
public static SettingJson Setting;
private boolean enableSettingChecker = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ private boolean checkPermissions() {
&& ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PERMISSION_GRANTED) {
return true;
} else {
ActivityCompat.requestPermissions(this, new String[] {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE},
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_STORAGE_PERMISSION);
return false;
}
Expand Down
34 changes: 34 additions & 0 deletions app/src/main/java/com/aof/mcinabox/adapter/SettingsAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.aof.mcinabox.adapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.aof.mcinabox.fragment.AccountSettingsFragment;
import com.aof.mcinabox.fragment.HomeFragment;

public class SettingsAdapter extends FragmentStateAdapter {

public SettingsAdapter(@NonNull Fragment fragment) {
super(fragment);
}

@NonNull
@Override
public Fragment createFragment(int position) {
switch (position) {
case 0:
return new AccountSettingsFragment();
case 1:
return new HomeFragment();
case 2:
return new AccountSettingsFragment();
}
return null;
}

@Override
public int getItemCount() {
return 3;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ private FileChooserDialog(Context context, String title, String startPath, Strin
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context)
.setView(binding.getRoot())
.setNegativeButton("Cancel", null);
final String alertDialogTitle;
if (title != null) {
alertDialogTitle = title;
} else {
if (title == null) {
title = "Select a file";
if (extension != null) {
title += " (" + extension + ")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class MarginItemDecoration extends RecyclerView.ItemDecoration {
private static final int px = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 8, Resources.getSystem().getDisplayMetrics());

@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
int position = parent.getChildLayoutPosition(view);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.aof.mcinabox.fragment;

import android.os.Bundle;

import com.aof.mcinabox.R;

public class AccountSettingsFragment extends BasePreferenceFragment {
private static final String TAG = "AccountSettingsFragment";

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.account_preferences, rootKey);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public BaseFragment(int contentLayoutId) {
}

public MCinaBox getMCinaBox() {
return (MCinaBox) getActivity().getApplication();
return (MCinaBox) requireActivity().getApplication();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.aof.mcinabox.fragment;

import androidx.preference.PreferenceFragmentCompat;

import com.aof.mcinabox.MCinaBox;

public abstract class BasePreferenceFragment extends PreferenceFragmentCompat {

public BasePreferenceFragment() {
super();
}

public MCinaBox getMCinaBox() {
return (MCinaBox) getActivity().getApplication();
}
}
44 changes: 28 additions & 16 deletions app/src/main/java/com/aof/mcinabox/fragment/HomeFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import java.util.List;

public class HomeFragment extends BaseFragment implements VersionsManager.OnVersionsChangedListener, AccountsManager.OnAccountsChangedListener {
public class HomeFragment extends BaseFragment {

private FragmentHomeBinding binding;
private VersionAdapter versionAdapter;
Expand All @@ -36,36 +36,48 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

binding.settingsButton.setOnClickListener(v -> {
Navigation.findNavController(v).navigate(HomeFragmentDirections.actionHomeFragmentToSettingsFragment());
});
binding.settingsButton.setOnClickListener(this::handleSettings);
binding.loginButton.setOnClickListener(this::handleLogin);

final VersionsManager versionsManager = getMCinaBox().getVersionsManager();
versionAdapter = new VersionAdapter(getMCinaBox(), versionsManager.getProfiles());
versionsManager.addOnVersionsChangedListener(this);
versionsManager.addOnVersionsChangedListener(versionsChangedListener);

final AccountsManager accountsManager = getMCinaBox().getAccountsManager();
accountAdapter = new AccountAdapter(getMCinaBox(), accountsManager.getAccounts());
accountsManager.addOnAccountsChangedListener(this);
accountsManager.addOnAccountsChangedListener(accountsChangedListener);

binding.bottomBar.versionSpinner.setAdapter(versionAdapter);
binding.bottomBar.accountSpinner.setAdapter(accountAdapter);
}

@Override
public void onDestroyView() {
getMCinaBox().getVersionsManager().removeOnVersionsChangedListener(this);
getMCinaBox().getAccountsManager().removeOnAccountsChangedListener(this);
super.onDestroyView();
private void handleSettings(View v) {
Navigation.findNavController(v)
.navigate(HomeFragmentDirections.actionHomeFragmentToSettingsFragment());
}

@Override
public void onVersionsChanged(List<Profile> profiles) {
versionAdapter.notifyDataSetChanged();
private void handleLogin(View v) {

}

@Override
public void onAccountsChanged(List<Account> accounts) {
accountAdapter.notifyDataSetChanged();
public void onDestroyView() {
getMCinaBox().getVersionsManager().removeOnVersionsChangedListener(versionsChangedListener);
getMCinaBox().getAccountsManager().removeOnAccountsChangedListener(accountsChangedListener);
super.onDestroyView();
}

private final VersionsManager.OnVersionsChangedListener versionsChangedListener = new VersionsManager.OnVersionsChangedListener() {
@Override
public void onVersionsChanged(List<Profile> profiles) {
versionAdapter.notifyDataSetChanged();
}
};

private final AccountsManager.OnAccountsChangedListener accountsChangedListener = new AccountsManager.OnAccountsChangedListener() {
@Override
public void onAccountsChanged(List<Account> accounts) {
accountAdapter.notifyDataSetChanged();
}
};
}
17 changes: 17 additions & 0 deletions app/src/main/java/com/aof/mcinabox/fragment/SettingsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.aof.mcinabox.adapter.SettingsAdapter;
import com.aof.mcinabox.databinding.FragmentSettingsBinding;
import com.google.android.material.tabs.TabLayoutMediator;

public class SettingsFragment extends BaseFragment {

Expand All @@ -25,5 +27,20 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

binding.viewPager.setAdapter(new SettingsAdapter(this));

new TabLayoutMediator(binding.tabLayout, binding.viewPager, (tab, position) -> {
switch (position) {
case 0:
tab.setText("Tab 1");
break;
case 1:
tab.setText("Tab 2");
break;
case 2:
tab.setText("Tab 3");
break;
}
}).attach();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class CustomizeKeyboardEditorActivity extends AppCompatActivity implement
private int screenWidth;
private int screenHeight;

private int pointer[] = new int[]{0, 0};
private final int[] pointer = new int[]{0, 0};
private Controller mController;
private boolean isGrabbed;
private TimerTask systemUiTimerTask;
Expand Down Expand Up @@ -135,7 +135,7 @@ public void init() {
//禁用自定义键盘
this.custmoizeKeyboard.setEnabled(false);
//先保存键盘文件
((CustomizeKeyboard)this.custmoizeKeyboard).mManager.autoSaveKeyboard();
((CustomizeKeyboard) this.custmoizeKeyboard).mManager.autoSaveKeyboard();
//卸载自定义键盘
this.removeInput(custmoizeKeyboard);
//重写自定义键盘,并创建新的自定义键盘
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public CkbManager(@NonNull Context context, @NonNull CallCustomizeKeyboard call,
init();
}

public int[] getDisplaySize(){
public int[] getDisplaySize() {
return new int[]{displayWidth, displayHeight};
}

Expand Down Expand Up @@ -193,7 +193,7 @@ public boolean exportKeyboard(String fileName) {
return outputFile(kr, fileName);
}

public static boolean outputFile(KeyboardRecorder kr, String fileName){
public static boolean outputFile(KeyboardRecorder kr, String fileName) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
StringBuilder jsonString = new StringBuilder(gson.toJson(kr));
jsonString.insert(0, "/*\n *This file is craeted by MCinaBox\n *Please DON'T edit the file if you don't know how it works.\n*/\n");
Expand All @@ -217,7 +217,7 @@ public void autoLoadKeyboard() {
loadKeyboard(LAST_KEYBOARD_LAYOUT_NAME + ".json");
}

public boolean loadKeyboard(File file){
public boolean loadKeyboard(File file) {
if (!file.exists()) {
return false;
}
Expand All @@ -230,13 +230,13 @@ public boolean loadKeyboard(File file){
} catch (Exception e) {
e.printStackTrace();
//当失败时尝试通过加载旧版的按键
DialogUtils.createBothChoicesDialog(mContext, mContext.getString(R.string.title_note), mContext.getString(R.string.tips_try_to_convert_keyboard_layout), mContext.getString(R.string.title_ok), mContext.getString(R.string.title_cancel), new DialogSupports(){
DialogUtils.createBothChoicesDialog(mContext, mContext.getString(R.string.title_note), mContext.getString(R.string.tips_try_to_convert_keyboard_layout), mContext.getString(R.string.title_ok), mContext.getString(R.string.title_cancel), new DialogSupports() {
@Override
public void runWhenPositive() {
super.runWhenPositive();
if(new GameButtonConverter(mContext).output(file)){
if (new GameButtonConverter(mContext).output(file)) {
DialogUtils.createSingleChoiceDialog(mContext, mContext.getString(R.string.title_note), String.format(mContext.getString(R.string.tips_successed_to_convert_keyboard_file), file.getName() + "-new.json"), mContext.getString(R.string.title_ok), null);
}else{
} else {
DialogUtils.createSingleChoiceDialog(mContext, mContext.getString(R.string.title_note), mContext.getString(R.string.tips_failed_to_convert_keyboard_file), mContext.getString(R.string.title_ok), null);
}
}
Expand All @@ -252,17 +252,17 @@ public boolean loadKeyboard(String fileName) {
return loadKeyboard(file);
}

public boolean loadKeyboard(KeyboardRecorder kr){
public boolean loadKeyboard(KeyboardRecorder kr) {
GameButtonRecorder[] gbr;
if (kr != null) {
gbr = kr.getRecorderDatas();
} else {
return false;
}

switch ( kr.getVersionCode() ){
switch (kr.getVersionCode()) {
case KeyboardRecorder.VERSION_UNKNOWN:
for(GameButtonRecorder tgbr : gbr){
for (GameButtonRecorder tgbr : gbr) {
tgbr.keyPos[0] = DisplayUtils.getDpFromPx(mContext, tgbr.keyPos[0]);
tgbr.keyPos[1] = DisplayUtils.getDpFromPx(mContext, tgbr.keyPos[1]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.content.Context;
import android.content.DialogInterface;
import android.os.FileObserver;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
Expand Down Expand Up @@ -92,11 +91,7 @@ private void initUI() {
*/

//当进入游戏的时候自动设定客制化键盘模式为生效,如果是编辑界面,则不自动设置
if (mManager.getController() != null) {
radioGame.setChecked(true);
}else {
radioGame.setChecked(false);
}
radioGame.setChecked(mManager.getController() != null);

}

Expand Down Expand Up @@ -217,8 +212,8 @@ public void runWhenPositive() {
});
}

if(v == buttonDefault){
DialogUtils.createBothChoicesDialog(mContext, mContext.getString(R.string.title_warn),"您确定要使用默认键盘布局吗?", mContext.getString(R.string.title_ok), mContext.getString(R.string.title_cancel), new DialogSupports(){
if (v == buttonDefault) {
DialogUtils.createBothChoicesDialog(mContext, mContext.getString(R.string.title_warn), "您确定要使用默认键盘布局吗?", mContext.getString(R.string.title_ok), mContext.getString(R.string.title_cancel), new DialogSupports() {
@Override
public void runWhenPositive() {
super.runWhenPositive();
Expand Down
Loading

0 comments on commit b8bb6bc

Please sign in to comment.