489 lines
24 KiB
Java
489 lines
24 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.ContentValues;
|
||
import android.content.Context;
|
||
import android.content.DialogInterface;
|
||
import android.content.Intent;
|
||
import android.database.Cursor;
|
||
import android.database.sqlite.SQLiteDatabase;
|
||
import android.graphics.Color;
|
||
import android.net.Uri;
|
||
import android.os.Bundle;
|
||
import android.text.Editable;
|
||
import android.text.TextWatcher;
|
||
import android.util.Log;
|
||
import android.view.ContextMenu;
|
||
import android.view.ContextMenu.ContextMenuInfo;
|
||
import android.view.Menu;
|
||
import android.view.MenuItem;
|
||
import android.view.MotionEvent;
|
||
import android.view.View;
|
||
import android.view.View.OnCreateContextMenuListener;
|
||
import android.view.WindowManager;
|
||
import android.view.inputmethod.InputMethodManager;
|
||
import android.widget.AdapterView;
|
||
import android.widget.AdapterView.AdapterContextMenuInfo;
|
||
import android.widget.AdapterView.OnItemClickListener;
|
||
import android.widget.ArrayAdapter;
|
||
import android.widget.EditText;
|
||
import android.widget.ImageButton;
|
||
import android.widget.ImageView;
|
||
import android.widget.LinearLayout;
|
||
import android.widget.ListView;
|
||
import android.widget.RadioButton;
|
||
import android.widget.RadioGroup;
|
||
import android.widget.SimpleCursorAdapter;
|
||
import android.widget.Spinner;
|
||
import android.widget.TextView;
|
||
import android.widget.Toast;
|
||
|
||
import org.w3c.dom.Text;
|
||
|
||
import java.io.BufferedWriter;
|
||
import java.io.File;
|
||
import java.io.FileWriter;
|
||
import java.lang.reflect.Field;
|
||
import java.util.ArrayList;
|
||
|
||
public class FavoriteActivity extends Activity {
|
||
EditText editText;
|
||
ImageButton imageButton_clear;
|
||
InputMethodManager IMM;
|
||
ListView listView;
|
||
SimpleCursorAdapter adapter;
|
||
Spinner spinner;
|
||
int position = 0;
|
||
|
||
@Override
|
||
protected void onCreate(Bundle savedInstanceState) {
|
||
super.onCreate(savedInstanceState);
|
||
setContentView(R.layout.activity_favorite);
|
||
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
|
||
IMM = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
||
imageButton_clear = (ImageButton) findViewById(R.id.imageButton_clear);
|
||
imageButton_clear.setOnClickListener(new View.OnClickListener(){
|
||
@Override
|
||
public void onClick(View v) {
|
||
editText.setText("");
|
||
IMM.hideSoftInputFromWindow(editText.getWindowToken(), 0);
|
||
}
|
||
});
|
||
imageButton_clear.setVisibility(View.GONE);
|
||
editText = (EditText) findViewById(R.id.editText);
|
||
editText.addTextChangedListener(new EditChangedListener());
|
||
spinner = (Spinner) findViewById(R.id.spinner);
|
||
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
|
||
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
|
||
Log.e(Thread.currentThread().getStackTrace()[2] + "", "spinner: " + arg2);
|
||
IMM.hideSoftInputFromWindow(editText.getWindowToken(), 0);
|
||
String s = arg0.getItemAtPosition(arg2).toString();
|
||
search(1, s);
|
||
}
|
||
public void onNothingSelected(AdapterView<?> arg0) {
|
||
}
|
||
});
|
||
genCategory();
|
||
|
||
listView = (ListView) findViewById(R.id.listView1);
|
||
|
||
listView.setOnItemClickListener(new OnItemClickListener() {
|
||
@Override
|
||
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
|
||
String url = ((TextView) arg1.findViewById(R.id.website)).getText().toString();
|
||
Intent intent = new Intent(FavoriteActivity.this, MainActivity.class);
|
||
intent.putExtra("url", url);
|
||
setResult(RESULT_OK, intent);
|
||
IMM.hideSoftInputFromWindow(editText.getWindowToken(), 0);
|
||
finish();
|
||
}
|
||
});
|
||
|
||
listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
|
||
@Override
|
||
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
|
||
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
|
||
String title = ((TextView) info.targetView.findViewById(R.id.title)).getText().toString();
|
||
menu.setHeaderIcon(R.drawable.link);
|
||
menu.setHeaderTitle(title);
|
||
String[] sm = { "在新窗口中打开", "复制链接", "分享", "修改", "删除" };
|
||
for (int i=0; i<sm.length; i++) {
|
||
menu.add(0, i, i, sm[i]);
|
||
}
|
||
}
|
||
});
|
||
|
||
listView.setOnTouchListener(new View.OnTouchListener() {
|
||
@Override
|
||
public boolean onTouch(View v, MotionEvent event) {
|
||
IMM.hideSoftInputFromWindow(editText.getWindowToken(), 0);
|
||
return false;
|
||
}
|
||
});
|
||
}
|
||
|
||
@Override
|
||
public boolean onCreateOptionsMenu(Menu menu) {
|
||
String[] sm = { "新建分类", "分类管理", "分享数据库", "导出HTML", "导出CSV" };
|
||
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:
|
||
AlertDialog.Builder builder = new AlertDialog.Builder(FavoriteActivity.this);
|
||
builder.setIcon(android.R.drawable.ic_input_add);
|
||
builder.setTitle("新建分类");
|
||
final EditText editText1 = new EditText(this);
|
||
builder.setView(editText1);
|
||
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
||
@Override
|
||
public void onClick(DialogInterface dialog, int which) {
|
||
String s = editText1.getText().toString();
|
||
DBHelper helper = new DBHelper(getApplicationContext());
|
||
Cursor cursor = helper.category(s);
|
||
if (cursor.getCount() == 0) {
|
||
ContentValues values = new ContentValues();
|
||
values.put("category", s);
|
||
helper.insert("category", values);
|
||
IMM.hideSoftInputFromWindow(editText1.getWindowToken(), 0);
|
||
genCategory();
|
||
} else {
|
||
Toast.makeText(getApplicationContext(), "分类已存在", Toast.LENGTH_SHORT).show();
|
||
}
|
||
}
|
||
});
|
||
builder.setNegativeButton("取消", null);
|
||
builder.create().show();
|
||
break;
|
||
case 1:
|
||
builder = new AlertDialog.Builder(FavoriteActivity.this);
|
||
builder.setIcon(android.R.drawable.ic_menu_edit);
|
||
final String title = spinner.getSelectedItem().toString();
|
||
if (title.equals(""))
|
||
break;
|
||
builder.setTitle(title);
|
||
final EditText editText_rename = new EditText(FavoriteActivity.this);
|
||
editText_rename.setText(title);
|
||
editText_rename.setSelection(title.length());
|
||
builder.setView(editText_rename);
|
||
builder.setPositiveButton("重命名", new DialogInterface.OnClickListener() {
|
||
@Override
|
||
public void onClick(DialogInterface dialog, int which) {
|
||
DBHelper helper = new DBHelper(getApplicationContext());
|
||
SQLiteDatabase db = helper.getWritableDatabase();
|
||
ContentValues values = new ContentValues();
|
||
String category = editText_rename.getText().toString();
|
||
values.put("category", category);
|
||
db.update("category", values, "category = '" + title + "'", null);
|
||
db.update(helper.TableName, values, "category = '" + title + "'", null);
|
||
genCategory();
|
||
IMM.hideSoftInputFromWindow(editText_rename.getWindowToken(), 0);
|
||
}
|
||
});
|
||
builder.setNeutralButton("删除", new DialogInterface.OnClickListener() {
|
||
@Override
|
||
public void onClick(DialogInterface dialog, int which) {
|
||
DBHelper helper = new DBHelper(getApplicationContext());
|
||
SQLiteDatabase db = helper.getWritableDatabase();
|
||
db.delete("category", "category=?", new String[] { title });
|
||
ContentValues values = new ContentValues();
|
||
values.put("category", "");
|
||
db.update(helper.TableName, values, "category = '" + title + "'", null);
|
||
genCategory();
|
||
}
|
||
});
|
||
builder.setNegativeButton("取消", null);
|
||
builder.create().show();
|
||
break;
|
||
case 2:
|
||
File file = new File(DBHelper.DATABASE_NAME);
|
||
if (file.exists()) {
|
||
Intent intent = new Intent();
|
||
intent.setAction(Intent.ACTION_SEND);
|
||
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
|
||
intent.setType("*/*");
|
||
startActivity(Intent.createChooser(intent, "分享 " + DBHelper.filename));
|
||
} else {
|
||
Toast.makeText(getApplicationContext(), "数据库文件不存在", Toast.LENGTH_SHORT).show();
|
||
}
|
||
break;
|
||
case 3:
|
||
DBHelper helper = new DBHelper(getApplicationContext());
|
||
Cursor cursor = helper.query("");
|
||
String s = "<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\"/>\n<title>收藏夹</title>\n<style>\na { text-decoration: none; }\ntable { table-layout: fixed; width: 100%; border-collapse: collapse; }\nth, td { border: 1px solid black; padding: 5px; overflow: hidden; text-overflow: ellipsis; }\ntd:nth-child(3) { white-space: nowrap; }\n</style>\n</head>\n<body>\n<h2 align=center>收藏夹" + cursor.getCount() + "</h2>\n<table>\n<tr><th>标题</th><th>网址</th><th>分类</th></tr>\n";
|
||
if (cursor != null) {
|
||
while (cursor.moveToNext()) {
|
||
String title1 = cursor.getString(cursor.getColumnIndex("title"));
|
||
String website = cursor.getString(cursor.getColumnIndex("website"));
|
||
String category = cursor.getString(cursor.getColumnIndex("category"));
|
||
s = s + "<tr><td>" + title1 + "</td><td><a href=\"" + website + "\" target=\"_blank\">" + website + "</a></td><td>" + category + "</td></tr>\n";
|
||
}
|
||
}
|
||
s += "</table>\n</body>\n</html>";
|
||
writeFile("webfav.htm", s);
|
||
break;
|
||
case 4:
|
||
s = "";
|
||
helper = new DBHelper(getApplicationContext());
|
||
cursor = helper.query("");
|
||
if (cursor != null) {
|
||
while (cursor.moveToNext()) {
|
||
String title1 = cursor.getString(cursor.getColumnIndex("title"));
|
||
String website = cursor.getString(cursor.getColumnIndex("website"));
|
||
String category = cursor.getString(cursor.getColumnIndex("category"));
|
||
s = s + title1.replace(",", ",") + "," + website + "," + category + "\n";
|
||
}
|
||
}
|
||
writeFile("webfav.csv", s);
|
||
break;
|
||
case android.R.id.home:
|
||
IMM.hideSoftInputFromWindow(editText.getWindowToken(), 0);
|
||
finish();
|
||
break;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
@Override
|
||
public boolean onContextItemSelected(MenuItem item) {
|
||
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
|
||
position = listView.getFirstVisiblePosition();
|
||
final String sid = ((TextView) menuInfo.targetView.findViewById(R.id.id)).getText().toString();
|
||
final String title = ((TextView) menuInfo.targetView.findViewById(R.id.title)).getText().toString();
|
||
final String url = ((TextView) menuInfo.targetView.findViewById(R.id.website)).getText().toString();
|
||
final String category = ((TextView) menuInfo.targetView.findViewById(R.id.category)).getText().toString();
|
||
switch (item.getItemId()) {
|
||
case 0:
|
||
Intent intent = new Intent(FavoriteActivity.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, title + "\n" + url);
|
||
intent.setType("text/plain");
|
||
startActivity(Intent.createChooser(intent, "分享"));
|
||
break;
|
||
case 3:
|
||
LinearLayout layout = new LinearLayout(FavoriteActivity.this);
|
||
layout.setOrientation(LinearLayout.VERTICAL);
|
||
final EditText editText_title = new EditText(FavoriteActivity.this);
|
||
editText_title.setMaxLines(3);
|
||
editText_title.setHint("标题");
|
||
editText_title.setText(title);
|
||
layout.addView(editText_title);
|
||
final EditText editText_url = new EditText(FavoriteActivity.this);
|
||
editText_url.setMaxLines(5);
|
||
editText_title.setHint("网址");
|
||
editText_url.setText(url);
|
||
layout.addView(editText_url);
|
||
final Spinner spinner = new Spinner(this);
|
||
spinner.setBackgroundColor(Color.GRAY);
|
||
ArrayList<String> list = new ArrayList<>();
|
||
DBHelper helper = new DBHelper(this);
|
||
Cursor cursor = helper.category();
|
||
if (cursor != null) {
|
||
while (cursor.moveToNext()) {
|
||
String s = cursor.getString(cursor.getColumnIndex("category"));
|
||
list.add(s);
|
||
}
|
||
}
|
||
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, list);
|
||
spinner.setAdapter(adapter);
|
||
spinner.setSelection(list.indexOf(category));
|
||
layout.addView(spinner);
|
||
AlertDialog.Builder builder = new AlertDialog.Builder(FavoriteActivity.this);
|
||
builder.setIcon(android.R.drawable.btn_star_big_on);
|
||
builder.setTitle("修改收藏");
|
||
builder.setView(layout);
|
||
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
||
@Override
|
||
public void onClick(DialogInterface dialog, int which) {
|
||
String stitle = editText_title.getText().toString();
|
||
String surl = editText_url.getText().toString();
|
||
Field field = null;
|
||
try {
|
||
//通过反射获取dialog中的私有属性mShowing
|
||
field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
|
||
field.setAccessible(true);//设置该属性可以访问
|
||
} catch (Exception ex) {
|
||
}
|
||
if (!stitle.equals("") && (surl.startsWith("http") || !surl.startsWith("file:///"))) {
|
||
DBHelper dbHelper = new DBHelper(getApplicationContext());
|
||
SQLiteDatabase db = dbHelper.getWritableDatabase();
|
||
ContentValues values = new ContentValues();
|
||
values.put("website", surl);
|
||
values.put("title", stitle);
|
||
values.put("category", spinner.getSelectedItem().toString());
|
||
int i = db.update(DBHelper.TableName, values, "_id = " + sid, null);
|
||
if (i != -1) {
|
||
IMM.hideSoftInputFromWindow(editText_title.getWindowToken(), 0);
|
||
String s = spinner.getSelectedItem().toString();
|
||
if (s.equals(""))
|
||
search(0, editText.getText().toString());
|
||
else
|
||
search(1, s);
|
||
try { //关闭
|
||
field.set(dialog, true);
|
||
dialog.dismiss();
|
||
} catch (Exception ex) {
|
||
}
|
||
} else
|
||
Toast.makeText(getApplicationContext(), "修改失败", Toast.LENGTH_SHORT).show();
|
||
} else {
|
||
if (stitle.equals("")){
|
||
editText_title.setError("标题不能为空!");
|
||
}
|
||
if (!surl.startsWith("http://") || !surl.startsWith("https://") || !surl.startsWith("file://")){
|
||
editText_url.setError("网址错误!");
|
||
}
|
||
try {
|
||
//设置dialog不可关闭
|
||
field.set(dialog, false);
|
||
dialog.dismiss();
|
||
} catch (Exception ex) {
|
||
}
|
||
}
|
||
}
|
||
});
|
||
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
|
||
@Override
|
||
public void onClick(DialogInterface dialog, int which) {
|
||
IMM.hideSoftInputFromWindow(editText_title.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();
|
||
int id = Integer.parseInt(((TextView) menuInfo.targetView.findViewById(R.id.id)).getText().toString());
|
||
helper = new DBHelper(getApplicationContext());
|
||
int i = helper.del(id);
|
||
if (i != -1)
|
||
search(0, editText.getText().toString());
|
||
else
|
||
Toast.makeText(getApplicationContext(), "删除失败", Toast.LENGTH_SHORT).show();
|
||
break;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
void search(int type, String s) {
|
||
DBHelper helper = new DBHelper(this);
|
||
Cursor cursor1 = null;
|
||
if (type == 0) {
|
||
cursor1 = helper.query(s);
|
||
} else if (type == 1) {
|
||
cursor1 = helper.queryCategory(s);
|
||
}
|
||
int count = cursor1.getCount();
|
||
setTitle("收藏夹 - " + s + " " + count);
|
||
String[] from = { "_id", "title", "website", "category", "website" };
|
||
int[] to = { R.id.id, R.id.title, R.id.website, R.id.category, R.id.imageView_favicon };
|
||
adapter = new SimpleCursorAdapter(this, R.layout.favorite_row, cursor1, from, to, 0);
|
||
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
|
||
public boolean setViewValue(View view, Cursor cursor, int columnIndex){
|
||
//Log.e(Thread.currentThread().getStackTrace()[2] + "", view.toString() + columnIndex);
|
||
if (view.getId() == R.id.imageView_favicon) {
|
||
String website = cursor.getString(columnIndex);
|
||
if (website.startsWith("https://")) {
|
||
((ImageView) view).setImageResource(android.R.drawable.ic_secure);
|
||
} else if (website.startsWith("http://")) {
|
||
((ImageView) view).setImageResource(android.R.drawable.ic_partial_secure);
|
||
} else if (website.startsWith("file://")) {
|
||
((ImageView) view).setImageResource(android.R.drawable.stat_notify_sdcard);
|
||
} else {
|
||
((ImageView) view).setImageResource(R.drawable.network);
|
||
}
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
});
|
||
listView.setAdapter(adapter);
|
||
Log.e(Thread.currentThread().getStackTrace()[2] + "", position + "");
|
||
listView.setSelection(position);
|
||
}
|
||
|
||
void genCategory() {
|
||
IMM.hideSoftInputFromWindow(editText.getWindowToken(), 0);
|
||
ArrayList<String> list = new ArrayList<>();
|
||
DBHelper helper = new DBHelper(this);
|
||
Cursor cursor = helper.category();
|
||
if (cursor != null) {
|
||
while (cursor.moveToNext()) {
|
||
String s = cursor.getString(cursor.getColumnIndex("category"));
|
||
list.add(s);
|
||
}
|
||
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, list);
|
||
spinner.setAdapter(adapter);
|
||
}
|
||
}
|
||
|
||
class EditChangedListener implements 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(0, s.toString());
|
||
}
|
||
}
|
||
|
||
void writeFile(String filename, String s) {
|
||
File file = new File(MainActivity.dir, filename);
|
||
try {
|
||
BufferedWriter BW = new BufferedWriter(new FileWriter(file, false)); //false覆盖
|
||
if (filename.endsWith(".csv")) {
|
||
BW.write('\ufeff'); // Excel通过文件的BOM头来判断文件编码
|
||
BW.flush();
|
||
}
|
||
BW.write(s);
|
||
BW.flush();
|
||
Toast.makeText(getApplicationContext(), "写文件 " + MainActivity.dir + File.separator + filename + " 成功", Toast.LENGTH_SHORT).show();
|
||
} catch (Exception e) {
|
||
Log.e(Thread.currentThread().getStackTrace()[2] + "", e.toString());
|
||
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
|
||
}
|
||
}
|
||
|
||
} |