From 924d3a8b2033c99508f1422e4cea5814ab9e31da Mon Sep 17 00:00:00 2001 From: SerpentSpirale Date: Sat, 26 Feb 2022 16:07:59 +0100 Subject: [PATCH] Refactor CustomControlsActivity.java --- .../pojavlaunch/CustomControlsActivity.java | 160 ++++++++---------- 1 file changed, 68 insertions(+), 92 deletions(-) diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/CustomControlsActivity.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/CustomControlsActivity.java index 92795cd21..1dba07e59 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/CustomControlsActivity.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/CustomControlsActivity.java @@ -5,13 +5,13 @@ import android.content.*; import android.os.*; import androidx.appcompat.app.*; -import androidx.preference.*; -import android.view.*; + import android.widget.*; import androidx.drawerlayout.widget.DrawerLayout; import com.google.android.material.navigation.NavigationView; +import com.google.gson.JsonSyntaxException; import com.kdt.pickafile.*; import java.io.*; @@ -19,18 +19,13 @@ import net.kdt.pojavlaunch.prefs.*; import net.kdt.pojavlaunch.customcontrols.*; - -public class CustomControlsActivity extends BaseActivity -{ - private DrawerLayout drawerLayout; - private NavigationView navDrawer; - private ControlLayout ctrlLayout; - - private SharedPreferences mPref; +public class CustomControlsActivity extends BaseActivity { + private DrawerLayout mDrawerLayout; + private NavigationView mDrawerNavigationView; + private ControlLayout mControlLayout; public boolean isModified = false; - public boolean isFromMainActivity = false; - private static String selectedName = "new_control"; + private static String sSelectedName = "new_control"; @Override protected void onCreate(Bundle savedInstanceState) { @@ -42,47 +37,40 @@ public class CustomControlsActivity extends BaseActivity } setContentView(R.layout.control_mapping); - - mPref = PreferenceManager.getDefaultSharedPreferences(this); - ctrlLayout = (ControlLayout) findViewById(R.id.customctrl_controllayout); + mControlLayout = (ControlLayout) findViewById(R.id.customctrl_controllayout); + mDrawerLayout = (DrawerLayout) findViewById(R.id.customctrl_drawerlayout); + mDrawerNavigationView = (NavigationView) findViewById(R.id.customctrl_navigation_view); - // Menu - drawerLayout = (DrawerLayout) findViewById(R.id.customctrl_drawerlayout); - - navDrawer = (NavigationView) findViewById(R.id.customctrl_navigation_view); - navDrawer.setNavigationItemSelectedListener( - new NavigationView.OnNavigationItemSelectedListener() { - @Override - public boolean onNavigationItemSelected(MenuItem menuItem) { + mDrawerNavigationView.setNavigationItemSelectedListener( + menuItem -> { switch (menuItem.getItemId()) { case R.id.menu_ctrl_load: - load(ctrlLayout); + load(mControlLayout); break; case R.id.menu_ctrl_add: - ctrlLayout.addControlButton(new ControlData("New")); + mControlLayout.addControlButton(new ControlData("New")); break; case R.id.menu_ctrl_add_drawer: - ctrlLayout.addDrawer(new ControlDrawerData()); + mControlLayout.addDrawer(new ControlDrawerData()); break; case R.id.menu_ctrl_selectdefault: - dialogSelectDefaultCtrl(ctrlLayout); + dialogSelectDefaultCtrl(mControlLayout); break; case R.id.menu_ctrl_save: - save(false,ctrlLayout); + save(false, mControlLayout); break; } //Toast.makeText(MainActivity.this, menuItem.getTitle() + ":" + menuItem.getItemId(), Toast.LENGTH_SHORT).show(); - drawerLayout.closeDrawers(); + mDrawerLayout.closeDrawers(); return true; - } - }); + }); - ctrlLayout.setActivity(this); - ctrlLayout.setModifiable(true); + mControlLayout.setActivity(this); + mControlLayout.setModifiable(true); - loadControl(LauncherPreferences.PREF_DEFAULTCTRL_PATH,ctrlLayout); + loadControl(LauncherPreferences.PREF_DEFAULTCTRL_PATH, mControlLayout); } @Override @@ -93,18 +81,7 @@ public class CustomControlsActivity extends BaseActivity return; } - save(true,ctrlLayout); - } - - private static void setDefaultControlJson(String path,ControlLayout ctrlLayout) { - try { - // Load before save to make sure control is not error - ctrlLayout.loadLayout(Tools.GLOBAL_GSON.fromJson(Tools.read(path), CustomControls.class)); - LauncherPreferences.DEFAULT_PREF.edit().putString("defaultCtrl", path).commit(); - LauncherPreferences.PREF_DEFAULTCTRL_PATH = path; - } catch (Throwable th) { - Tools.showError(ctrlLayout.getContext(), th); - } + save(true, mControlLayout); } public static void dialogSelectDefaultCtrl(final ControlLayout layout) { @@ -127,18 +104,12 @@ public class CustomControlsActivity extends BaseActivity dialog.show(); } - private static String doSaveCtrl(String name, final ControlLayout layout) throws Exception { - String jsonPath = Tools.CTRLMAP_PATH + "/" + name + ".json"; - layout.saveLayout(jsonPath); - - return jsonPath; - } public static void save(final boolean exit, final ControlLayout layout) { final Context ctx = layout.getContext(); final EditText edit = new EditText(ctx); edit.setSingleLine(); - edit.setText(selectedName); + edit.setText(sSelectedName); AlertDialog.Builder builder = new AlertDialog.Builder(ctx); builder.setTitle(R.string.global_save); @@ -162,51 +133,38 @@ public class CustomControlsActivity extends BaseActivity }); } final AlertDialog dialog = builder.create(); - dialog.setOnShowListener(new DialogInterface.OnShowListener() { + dialog.setOnShowListener(dialogInterface -> { - @Override - public void onShow(DialogInterface dialogInterface) { - - Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE); - button.setOnClickListener(new View.OnClickListener() { - - @Override - public void onClick(View view) { - if (edit.getText().toString().isEmpty()) { - edit.setError(ctx.getResources().getString(R.string.global_error_field_empty)); - } else { - try { - String jsonPath = doSaveCtrl(edit.getText().toString(),layout); - - Toast.makeText(ctx, ctx.getString(R.string.global_save) + ": " + jsonPath, Toast.LENGTH_SHORT).show(); - - dialog.dismiss(); - if (exit) { - if(ctx instanceof MainActivity) { - ((MainActivity) ctx).leaveCustomControls(); - }else{ - ((Activity)ctx).onBackPressed(); - } - //CustomControlsActivity.super.onBackPressed(); - } - } catch (Throwable th) { - Tools.showError(ctx, th, exit); - } - } - } - }); + Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE); + button.setOnClickListener(view -> { + if (edit.getText().toString().isEmpty()) { + edit.setError(ctx.getResources().getString(R.string.global_error_field_empty)); + return; } + + try { + String jsonPath = doSaveCtrl(edit.getText().toString(),layout); + Toast.makeText(ctx, ctx.getString(R.string.global_save) + ": " + jsonPath, Toast.LENGTH_SHORT).show(); + + dialog.dismiss(); + if (!exit) return; + + if(ctx instanceof MainActivity) { + ((MainActivity) ctx).leaveCustomControls(); + }else{ + ((Activity)ctx).onBackPressed(); + } + } catch (Throwable th) { + Tools.showError(ctx, th, exit); + } + }); + }); dialog.show(); } public static void load(final ControlLayout layout) { - /*ControlJsonSelector sel = new ControlJsonSelector(layout.getContext(), R.string.global_load); - sel.setFinishCallback((f)->{ - loadControl(f.getAbsolutePath(),layout); - }); - sel.show();*/ AlertDialog.Builder builder = new AlertDialog.Builder(layout.getContext()); builder.setTitle(R.string.global_load); builder.setPositiveButton(android.R.string.cancel, null); @@ -227,12 +185,30 @@ public class CustomControlsActivity extends BaseActivity dialog.show(); } + private static void setDefaultControlJson(String path,ControlLayout ctrlLayout) { + // Load before save to make sure control is not error + try { + ctrlLayout.loadLayout(Tools.GLOBAL_GSON.fromJson(Tools.read(path), CustomControls.class)); + LauncherPreferences.DEFAULT_PREF.edit().putString("defaultCtrl", path).apply(); + LauncherPreferences.PREF_DEFAULTCTRL_PATH = path; + } catch (IOException| JsonSyntaxException exception) { + Tools.showError(ctrlLayout.getContext(), exception); + } + } + + private static String doSaveCtrl(String name, final ControlLayout layout) throws Exception { + String jsonPath = Tools.CTRLMAP_PATH + "/" + name + ".json"; + layout.saveLayout(jsonPath); + + return jsonPath; + } + private static void loadControl(String path,ControlLayout layout) { try { layout.loadLayout(path); - selectedName = new File(path).getName(); + sSelectedName = new File(path).getName(); // Remove `.json` - selectedName = selectedName.substring(0, selectedName.length() - 5); + sSelectedName = sSelectedName.substring(0, sSelectedName.length() - 5); } catch (Exception e) { Tools.showError(layout.getContext(), e); }