|
| 1 | +package cn.darkal.networkdiagnosis.Activity; |
| 2 | + |
| 3 | +import android.content.DialogInterface; |
| 4 | +import android.support.design.widget.FloatingActionButton; |
| 5 | +import android.support.v7.app.ActionBar; |
| 6 | +import android.support.v7.app.AlertDialog; |
| 7 | +import android.support.v7.app.AppCompatActivity; |
| 8 | +import android.os.Bundle; |
| 9 | +import android.view.LayoutInflater; |
| 10 | +import android.view.MenuItem; |
| 11 | +import android.view.View; |
| 12 | +import android.widget.EditText; |
| 13 | +import android.widget.ListView; |
| 14 | +import android.widget.RelativeLayout; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import butterknife.BindView; |
| 19 | +import butterknife.ButterKnife; |
| 20 | +import cn.darkal.networkdiagnosis.Adapter.ContentFilterAdapter; |
| 21 | +import cn.darkal.networkdiagnosis.Bean.ResponseFilterRule; |
| 22 | +import cn.darkal.networkdiagnosis.R; |
| 23 | +import cn.darkal.networkdiagnosis.SysApplication; |
| 24 | +import cn.darkal.networkdiagnosis.Utils.DeviceUtils; |
| 25 | +import cn.darkal.networkdiagnosis.Utils.SharedPreferenceUtils; |
| 26 | + |
| 27 | +public class ChangeFilterActivity extends AppCompatActivity { |
| 28 | + @BindView(R.id.activity_change_filter) |
| 29 | + public RelativeLayout relativeLayout; |
| 30 | + |
| 31 | + @BindView(R.id.lv_filter) |
| 32 | + public ListView listView; |
| 33 | + |
| 34 | + @BindView(R.id.fab_add) |
| 35 | + public FloatingActionButton floatingActionButton; |
| 36 | + |
| 37 | + ContentFilterAdapter contentFilterAdapter; |
| 38 | + |
| 39 | + @Override |
| 40 | + protected void onCreate(Bundle savedInstanceState) { |
| 41 | + super.onCreate(savedInstanceState); |
| 42 | + setContentView(R.layout.activity_change_filter); |
| 43 | + ButterKnife.bind(this); |
| 44 | + setupActionBar(); |
| 45 | + |
| 46 | + List<ResponseFilterRule> ruleList = ((SysApplication)getApplication()).ruleList; |
| 47 | + |
| 48 | + contentFilterAdapter = new ContentFilterAdapter(this,ruleList); |
| 49 | + listView.setAdapter(contentFilterAdapter); |
| 50 | + |
| 51 | + floatingActionButton.setOnClickListener(new View.OnClickListener() { |
| 52 | + @Override |
| 53 | + public void onClick(View v) { |
| 54 | + showDialog(null); |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Set up the {@link android.app.ActionBar}, if the API is available. |
| 61 | + */ |
| 62 | + private void setupActionBar() { |
| 63 | + setTitle("返回包注入"); |
| 64 | + ActionBar actionBar = getSupportActionBar(); |
| 65 | + if (actionBar != null) { |
| 66 | + // Show the Up button in the action bar. |
| 67 | + actionBar.setDisplayHomeAsUpEnabled(true); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public void showDialog(final ResponseFilterRule responseFilterRule){ |
| 72 | + AlertDialog.Builder builder = new AlertDialog.Builder(ChangeFilterActivity.this); |
| 73 | + |
| 74 | + View textEntryView = LayoutInflater.from(ChangeFilterActivity.this).inflate(R.layout.alert_resp_filter, null); |
| 75 | + final EditText urlEditText = (EditText) textEntryView.findViewById(R.id.et_origin_url); |
| 76 | + final EditText regexEditText = (EditText) textEntryView.findViewById(R.id.et_regex); |
| 77 | + final EditText contentEditText = (EditText) textEntryView.findViewById(R.id.et_replace_result); |
| 78 | + if(responseFilterRule!=null){ |
| 79 | + urlEditText.setText(responseFilterRule.getUrl()); |
| 80 | + regexEditText.setText(responseFilterRule.getReplaceRegex()); |
| 81 | + contentEditText.setText(responseFilterRule.getReplaceContent()); |
| 82 | + builder.setTitle("修改注入项"); |
| 83 | + }else{ |
| 84 | + builder.setTitle("新增注入项"); |
| 85 | + } |
| 86 | + |
| 87 | + builder.setCancelable(true); |
| 88 | + builder.setView(textEntryView); |
| 89 | + builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { |
| 90 | + @Override |
| 91 | + public void onClick(DialogInterface dialog, int which) { |
| 92 | + if(responseFilterRule!=null){ |
| 93 | + responseFilterRule.setUrl(urlEditText.getText().toString()); |
| 94 | + responseFilterRule.setReplaceRegex(regexEditText.getText().toString()); |
| 95 | + responseFilterRule.setReplaceContent(contentEditText.getText().toString()); |
| 96 | + }else { |
| 97 | + if(urlEditText.getText().length()>0 && regexEditText.getText().length()>0 |
| 98 | + && contentEditText.getText().length()>0) { |
| 99 | + ResponseFilterRule responseFilterRule = new ResponseFilterRule(); |
| 100 | + responseFilterRule.setUrl(urlEditText.getText().toString()); |
| 101 | + responseFilterRule.setReplaceRegex(regexEditText.getText().toString()); |
| 102 | + responseFilterRule.setReplaceContent(contentEditText.getText().toString()); |
| 103 | + ((SysApplication) getApplication()).ruleList.add(responseFilterRule); |
| 104 | + } |
| 105 | + } |
| 106 | + contentFilterAdapter.notifyDataSetChanged(); |
| 107 | + } |
| 108 | + }); |
| 109 | + builder.setNegativeButton("取消",null); |
| 110 | + builder.show(); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + protected void onStop() { |
| 115 | + SharedPreferenceUtils.save(getApplicationContext(), |
| 116 | + "response_filter",((SysApplication) getApplication()).ruleList); |
| 117 | + super.onStop(); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 122 | + if (item.getItemId() == android.R.id.home) { |
| 123 | + finish(); |
| 124 | + return true; |
| 125 | + } |
| 126 | + return super.onOptionsItemSelected(item); |
| 127 | + } |
| 128 | +} |
0 commit comments