android: refact doric dev activity,use map to manage action menu
This commit is contained in:
parent
49d3426d85
commit
7fd81ce92c
@ -36,7 +36,9 @@ import androidx.recyclerview.widget.RecyclerView;
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import pub.doric.DoricContext;
|
||||
import pub.doric.DoricContextManager;
|
||||
@ -335,26 +337,11 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat
|
||||
holder.layoutBtn.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(final View v) {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
list.add("View source");
|
||||
if (DoricDev.getInstance().isInDevMode()) {
|
||||
if (context.getDriver() instanceof DoricDebugDriver) {
|
||||
list.add("Stop debugging");
|
||||
} else {
|
||||
list.add("Start debugging");
|
||||
}
|
||||
}
|
||||
if (DoricRegistry.isEnableRenderSnapshot()) {
|
||||
list.add("Snapshot");
|
||||
}
|
||||
final String[] items = list.toArray(new String[0]);
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(holder.itemView.getContext(), R.style.Theme_Doric_Modal);
|
||||
builder.setTitle(String.format("%s %s", context.getContextId(), context.getSource()));
|
||||
builder.setIcon(new BitmapDrawable(holder.itemView.getContext().getResources(), icon_on));
|
||||
builder.setItems(items, new DialogInterface.OnClickListener() {
|
||||
final Map<String, DialogInterface.OnClickListener> actionMap = new HashMap<>();
|
||||
|
||||
actionMap.put("View source", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
if ("View source".equals(items[which])) {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(holder.itemView.getContext(), R.style.Theme_Doric_Modal_Alert);
|
||||
builder.setTitle(String.format(Locale.getDefault(),
|
||||
"View source: %s",
|
||||
@ -374,11 +361,29 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat
|
||||
});
|
||||
builder.setCancelable(false);
|
||||
builder.show();
|
||||
} else if ("Stop debugging".equals(items[which])) {
|
||||
}
|
||||
});
|
||||
if (DoricDev.getInstance().isInDevMode()) {
|
||||
if (context.getDriver() instanceof DoricDebugDriver) {
|
||||
actionMap.put("Stop debugging", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
DoricDev.getInstance().stopDebugging(true);
|
||||
} else if ("Start debugging".equals(items[which])) {
|
||||
}
|
||||
});
|
||||
} else {
|
||||
actionMap.put("Start debugging", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
DoricDev.getInstance().requestDebugging(context);
|
||||
} else if ("Snapshot".endsWith(items[which])) {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if (DoricRegistry.isEnableRenderSnapshot()) {
|
||||
actionMap.put("Snapshot", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
Activity activity = (Activity) context.getContext();
|
||||
ViewGroup view = (ViewGroup) activity.getWindow().getDecorView();
|
||||
DoricSnapshotView doricSnapshotView = new DoricSnapshotView(activity, context);
|
||||
@ -387,6 +392,20 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat
|
||||
view.addView(doricSnapshotView, layoutParams);
|
||||
((Activity) v.getContext()).finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
final String[] items = actionMap.keySet().toArray(new String[0]);
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(holder.itemView.getContext(), R.style.Theme_Doric_Modal);
|
||||
builder.setTitle(String.format("%s %s", context.getContextId(), context.getSource()));
|
||||
builder.setIcon(new BitmapDrawable(holder.itemView.getContext().getResources(), icon_on));
|
||||
builder.setItems(items, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
String title = items[which];
|
||||
DialogInterface.OnClickListener listener = actionMap.get(title);
|
||||
if (listener != null) {
|
||||
listener.onClick(dialog, which);
|
||||
}
|
||||
dialog.dismiss();
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user