Refactor CustomControlsActivity.java

This commit is contained in:
SerpentSpirale 2022-02-26 16:07:59 +01:00 committed by ArtDev
parent cc26e7fa23
commit 924d3a8b20

View File

@ -5,13 +5,13 @@ import android.content.*;
import android.os.*; import android.os.*;
import androidx.appcompat.app.*; import androidx.appcompat.app.*;
import androidx.preference.*;
import android.view.*;
import android.widget.*; import android.widget.*;
import androidx.drawerlayout.widget.DrawerLayout; import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView; import com.google.android.material.navigation.NavigationView;
import com.google.gson.JsonSyntaxException;
import com.kdt.pickafile.*; import com.kdt.pickafile.*;
import java.io.*; import java.io.*;
@ -19,18 +19,13 @@ import net.kdt.pojavlaunch.prefs.*;
import net.kdt.pojavlaunch.customcontrols.*; import net.kdt.pojavlaunch.customcontrols.*;
public class CustomControlsActivity extends BaseActivity {
public class CustomControlsActivity extends BaseActivity private DrawerLayout mDrawerLayout;
{ private NavigationView mDrawerNavigationView;
private DrawerLayout drawerLayout; private ControlLayout mControlLayout;
private NavigationView navDrawer;
private ControlLayout ctrlLayout;
private SharedPreferences mPref;
public boolean isModified = false; public boolean isModified = false;
public boolean isFromMainActivity = false; private static String sSelectedName = "new_control";
private static String selectedName = "new_control";
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -43,46 +38,39 @@ public class CustomControlsActivity extends BaseActivity
setContentView(R.layout.control_mapping); setContentView(R.layout.control_mapping);
mPref = PreferenceManager.getDefaultSharedPreferences(this); mControlLayout = (ControlLayout) findViewById(R.id.customctrl_controllayout);
mDrawerLayout = (DrawerLayout) findViewById(R.id.customctrl_drawerlayout);
mDrawerNavigationView = (NavigationView) findViewById(R.id.customctrl_navigation_view);
ctrlLayout = (ControlLayout) findViewById(R.id.customctrl_controllayout); mDrawerNavigationView.setNavigationItemSelectedListener(
menuItem -> {
// 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) {
switch (menuItem.getItemId()) { switch (menuItem.getItemId()) {
case R.id.menu_ctrl_load: case R.id.menu_ctrl_load:
load(ctrlLayout); load(mControlLayout);
break; break;
case R.id.menu_ctrl_add: case R.id.menu_ctrl_add:
ctrlLayout.addControlButton(new ControlData("New")); mControlLayout.addControlButton(new ControlData("New"));
break; break;
case R.id.menu_ctrl_add_drawer: case R.id.menu_ctrl_add_drawer:
ctrlLayout.addDrawer(new ControlDrawerData()); mControlLayout.addDrawer(new ControlDrawerData());
break; break;
case R.id.menu_ctrl_selectdefault: case R.id.menu_ctrl_selectdefault:
dialogSelectDefaultCtrl(ctrlLayout); dialogSelectDefaultCtrl(mControlLayout);
break; break;
case R.id.menu_ctrl_save: case R.id.menu_ctrl_save:
save(false,ctrlLayout); save(false, mControlLayout);
break; break;
} }
//Toast.makeText(MainActivity.this, menuItem.getTitle() + ":" + menuItem.getItemId(), Toast.LENGTH_SHORT).show(); //Toast.makeText(MainActivity.this, menuItem.getTitle() + ":" + menuItem.getItemId(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers(); mDrawerLayout.closeDrawers();
return true; return true;
} });
});
ctrlLayout.setActivity(this); mControlLayout.setActivity(this);
ctrlLayout.setModifiable(true); mControlLayout.setModifiable(true);
loadControl(LauncherPreferences.PREF_DEFAULTCTRL_PATH,ctrlLayout); loadControl(LauncherPreferences.PREF_DEFAULTCTRL_PATH, mControlLayout);
} }
@Override @Override
@ -93,18 +81,7 @@ public class CustomControlsActivity extends BaseActivity
return; return;
} }
save(true,ctrlLayout); save(true, mControlLayout);
}
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);
}
} }
public static void dialogSelectDefaultCtrl(final ControlLayout layout) { public static void dialogSelectDefaultCtrl(final ControlLayout layout) {
@ -127,18 +104,12 @@ public class CustomControlsActivity extends BaseActivity
dialog.show(); 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) { public static void save(final boolean exit, final ControlLayout layout) {
final Context ctx = layout.getContext(); final Context ctx = layout.getContext();
final EditText edit = new EditText(ctx); final EditText edit = new EditText(ctx);
edit.setSingleLine(); edit.setSingleLine();
edit.setText(selectedName); edit.setText(sSelectedName);
AlertDialog.Builder builder = new AlertDialog.Builder(ctx); AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle(R.string.global_save); builder.setTitle(R.string.global_save);
@ -162,51 +133,38 @@ public class CustomControlsActivity extends BaseActivity
}); });
} }
final AlertDialog dialog = builder.create(); final AlertDialog dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() { dialog.setOnShowListener(dialogInterface -> {
@Override Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
public void onShow(DialogInterface dialogInterface) { button.setOnClickListener(view -> {
if (edit.getText().toString().isEmpty()) {
Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE); edit.setError(ctx.getResources().getString(R.string.global_error_field_empty));
button.setOnClickListener(new View.OnClickListener() { return;
@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);
}
}
}
});
} }
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(); dialog.show();
} }
public static void load(final ControlLayout layout) { 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()); AlertDialog.Builder builder = new AlertDialog.Builder(layout.getContext());
builder.setTitle(R.string.global_load); builder.setTitle(R.string.global_load);
builder.setPositiveButton(android.R.string.cancel, null); builder.setPositiveButton(android.R.string.cancel, null);
@ -227,12 +185,30 @@ public class CustomControlsActivity extends BaseActivity
dialog.show(); 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) { private static void loadControl(String path,ControlLayout layout) {
try { try {
layout.loadLayout(path); layout.loadLayout(path);
selectedName = new File(path).getName(); sSelectedName = new File(path).getName();
// Remove `.json` // Remove `.json`
selectedName = selectedName.substring(0, selectedName.length() - 5); sSelectedName = sSelectedName.substring(0, sSelectedName.length() - 5);
} catch (Exception e) { } catch (Exception e) {
Tools.showError(layout.getContext(), e); Tools.showError(layout.getContext(), e);
} }