297 lines
12 KiB
Java
297 lines
12 KiB
Java
package com.hty.browser;
|
|
|
|
import android.app.Activity;
|
|
import android.app.AlertDialog;
|
|
import android.content.ClipData;
|
|
import android.content.ClipboardManager;
|
|
import android.content.Context;
|
|
import android.content.DialogInterface;
|
|
import android.content.Intent;
|
|
import android.os.Bundle;
|
|
import android.text.Editable;
|
|
import android.text.TextWatcher;
|
|
import android.util.Log;
|
|
import android.view.ContextMenu;
|
|
import android.view.Menu;
|
|
import android.view.MenuItem;
|
|
import android.view.View;
|
|
import android.view.WindowManager;
|
|
import android.view.inputmethod.InputMethodManager;
|
|
import android.widget.AdapterView;
|
|
import android.widget.ArrayAdapter;
|
|
import android.widget.EditText;
|
|
import android.widget.ImageButton;
|
|
import android.widget.ListView;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.lang.reflect.Field;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
public class ADBlockActivity extends Activity {
|
|
EditText editText;
|
|
ImageButton imageButton_clear;
|
|
ListView listView;
|
|
ArrayAdapter<String> adapter;
|
|
List<String> datas0, datas;
|
|
InputMethodManager IMM;
|
|
int position;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_adblock);
|
|
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
|
|
IMM = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
editText = (EditText) findViewById(R.id.editText);
|
|
editText.addTextChangedListener(new TextWatcher(){
|
|
@Override
|
|
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void afterTextChanged(Editable s) {
|
|
if (s.toString().equals("")) {
|
|
imageButton_clear.setVisibility(View.GONE);
|
|
} else {
|
|
imageButton_clear.setVisibility(View.VISIBLE);
|
|
}
|
|
search();
|
|
}
|
|
});
|
|
|
|
imageButton_clear = (ImageButton) findViewById(R.id.imageButton_clear);
|
|
imageButton_clear.setOnClickListener(new View.OnClickListener(){
|
|
@Override
|
|
public void onClick(View v) {
|
|
editText.setText("");
|
|
}
|
|
});
|
|
imageButton_clear.setVisibility(View.GONE);
|
|
|
|
datas0 = new ArrayList<>();
|
|
datas = new ArrayList<>();
|
|
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, datas);
|
|
listView = (ListView) findViewById(R.id.listView);
|
|
listView.setAdapter(adapter);
|
|
|
|
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
@Override
|
|
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
|
|
String url = ((TextView) arg1.findViewById(android.R.id.text1)).getText().toString();
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(ADBlockActivity.this);
|
|
builder.setIcon(android.R.drawable.ic_menu_info_details);
|
|
builder.setTitle("规则");
|
|
builder.setMessage(url);
|
|
builder.setPositiveButton("打开", new DialogInterface.OnClickListener() {
|
|
@Override
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
Intent intent = new Intent(ADBlockActivity.this, MainActivity.class);
|
|
intent.putExtra("url", url);
|
|
intent.putExtra("newWindow", true);
|
|
setResult(RESULT_OK, intent);
|
|
}
|
|
});
|
|
builder.setNegativeButton("取消", null);
|
|
builder.create().show();
|
|
}
|
|
});
|
|
|
|
listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
|
|
@Override
|
|
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
|
|
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
|
|
String title = ((TextView) info.targetView.findViewById(android.R.id.text1)).getText().toString();
|
|
menu.setHeaderTitle(title);
|
|
menu.setHeaderIcon(R.drawable.link);
|
|
String[] sm = { "打开", "复制", "分享", "修改", "删除" };
|
|
for (int i=0; i<sm.length; i++) {
|
|
menu.add(0, i, i, sm[i]);
|
|
}
|
|
}
|
|
});
|
|
|
|
//SL = readFile("blockrules").split(",");
|
|
datas0 = Arrays.asList(readFile("blockrules").split(","));
|
|
Log.e(Thread.currentThread().getStackTrace()[2] + "", "Read: " + datas0.toString());
|
|
search();
|
|
}
|
|
|
|
@Override
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
String[] sm = { "重载", "保存", "退出" };
|
|
for (int i=0; i<sm.length; i++) {
|
|
menu.add(0, i, i, sm[i]);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
int id = item.getItemId();
|
|
switch (id) {
|
|
case 0:
|
|
datas0 = Arrays.asList(readFile("blockrules").split(","));
|
|
search();
|
|
break;
|
|
case 1:
|
|
IMM.hideSoftInputFromWindow(editText.getWindowToken(), 0);
|
|
String s = datas.toString().replace("[", "").replace("]", "").replace(", ", ",");
|
|
Log.e(Thread.currentThread().getStackTrace()[2] + "", "Write: " + s);
|
|
writeFile("blockrules", s);
|
|
break;
|
|
case 2:
|
|
finish();
|
|
break;
|
|
case android.R.id.home:
|
|
finish();
|
|
break;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean onContextItemSelected(MenuItem item) {
|
|
AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
|
|
int index = menuInfo.position;
|
|
position = listView.getFirstVisiblePosition();
|
|
String url = ((TextView) menuInfo.targetView.findViewById(android.R.id.text1)).getText().toString();
|
|
switch (item.getItemId()) {
|
|
case 0:
|
|
Intent intent = new Intent(ADBlockActivity.this, MainActivity.class);
|
|
intent.putExtra("url", url);
|
|
intent.putExtra("newWindow", true);
|
|
setResult(RESULT_OK, intent);
|
|
finish();
|
|
break;
|
|
case 1:
|
|
ClipboardManager CM = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
|
|
CM.setPrimaryClip(ClipData.newPlainText("link", url));
|
|
Toast.makeText(getApplicationContext(), "已复制", Toast.LENGTH_SHORT).show();
|
|
break;
|
|
case 2:
|
|
intent = new Intent();
|
|
intent.setAction(Intent.ACTION_SEND);
|
|
intent.putExtra(Intent.EXTRA_TEXT, url);
|
|
intent.setType("text/plain");
|
|
startActivity(Intent.createChooser(intent, "分享"));
|
|
break;
|
|
case 3:
|
|
final EditText editText_url = new EditText(ADBlockActivity.this);
|
|
editText_url.setMaxLines(4);
|
|
editText_url.setHint("网址");
|
|
editText_url.setText(url);
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(ADBlockActivity.this);
|
|
builder.setIcon(android.R.drawable.ic_menu_edit);
|
|
builder.setTitle("修改");
|
|
builder.setView(editText_url);
|
|
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
|
@Override
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
String surl = editText_url.getText().toString();
|
|
Field field = null;
|
|
try {
|
|
//通过反射获取dialog中的私有属性mShowing
|
|
field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
|
|
field.setAccessible(true);//设置该属性可以访问
|
|
} catch (Exception e) {
|
|
}
|
|
if (surl.startsWith("http://") || surl.startsWith("https://") && !url.equals(surl)) {
|
|
datas.set(index, surl);
|
|
adapter.notifyDataSetChanged();
|
|
} else {
|
|
editText_url.setError("网址错误!");
|
|
try {
|
|
//设置dialog不可关闭
|
|
field.set(dialog, false);
|
|
dialog.dismiss();
|
|
} catch (Exception e) {
|
|
}
|
|
}
|
|
}
|
|
});
|
|
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
|
|
@Override
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
IMM.hideSoftInputFromWindow(editText_url.getWindowToken(), 0);
|
|
Field field = null;
|
|
try {
|
|
//通过反射获取dialog中的私有属性mShowing
|
|
field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
|
|
field.setAccessible(true);//设置该属性可以访问
|
|
} catch (Exception e) {
|
|
}
|
|
try {
|
|
field.set(dialog, true);
|
|
dialog.dismiss();
|
|
} catch (Exception e) {
|
|
}
|
|
}
|
|
});
|
|
builder.create().show();
|
|
break;
|
|
case 4:
|
|
position = listView.getFirstVisiblePosition();
|
|
datas.remove(url);
|
|
adapter.notifyDataSetChanged();
|
|
break;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void search() {
|
|
IMM.hideSoftInputFromWindow(editText.getWindowToken(), 0);
|
|
datas.clear();
|
|
String s = editText.getText().toString();
|
|
for (int i=0; i<datas0.size(); i++) {
|
|
if (s.equals("") || datas0.get(i).contains(s))
|
|
datas.add(datas0.get(i));
|
|
}
|
|
adapter.notifyDataSetChanged();
|
|
}
|
|
|
|
String readFile(String filename) {
|
|
String s = "";
|
|
FileInputStream FIS;
|
|
try {
|
|
FIS = openFileInput(filename);
|
|
byte[] buffer = new byte[1024];
|
|
ByteArrayOutputStream BAOS = new ByteArrayOutputStream();
|
|
int len;
|
|
while ((len = FIS.read(buffer)) != -1) {
|
|
BAOS.write(buffer, 0, len);
|
|
}
|
|
s = BAOS.toString();
|
|
FIS.close();
|
|
BAOS.close();
|
|
return s;
|
|
} catch (Exception e) {
|
|
Log.e(Thread.currentThread().getStackTrace()[2] + "", "" + e);
|
|
return s;
|
|
}
|
|
}
|
|
|
|
void writeFile(String filename, String s) {
|
|
FileOutputStream outStream = null;
|
|
try {
|
|
outStream = openFileOutput("blockrules", Context.MODE_PRIVATE);
|
|
outStream.write(s.getBytes());
|
|
outStream.close();
|
|
} catch (Exception e) {
|
|
Log.e(Thread.currentThread().getStackTrace()[2] + "", e.toString());
|
|
}
|
|
}
|
|
|
|
} |