267 lines
10 KiB
Java
267 lines
10 KiB
Java
package com.yutou.passmanage;
|
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import android.content.DialogInterface;
|
|
import android.content.pm.ApplicationInfo;
|
|
import android.content.pm.PackageInfo;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.os.Looper;
|
|
import android.text.Editable;
|
|
import android.text.TextWatcher;
|
|
import android.view.KeyEvent;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.widget.AdapterView;
|
|
import android.widget.ArrayAdapter;
|
|
import android.widget.AutoCompleteTextView;
|
|
import android.widget.EditText;
|
|
import android.widget.ImageButton;
|
|
import android.widget.MultiAutoCompleteTextView;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
|
import com.yutou.passmanage.Adapters.PassWordListAdapter;
|
|
import com.yutou.passmanage.Datas.AppData;
|
|
import com.yutou.passmanage.Datas.ToolsPassword;
|
|
import com.yutou.passmanage.Datas.ToolsPasswordDao;
|
|
import com.yutou.passmanage.Interfaces.NetworkInterface;
|
|
import com.yutou.passmanage.Tools.NetworkTool;
|
|
import com.yutou.passmanage.Tools.RoomDatabaseManager;
|
|
import com.yutou.passmanage.Tools.Tools;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import static com.yutou.passmanage.Tools.RoomDatabaseManager.addPassword;
|
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
EditText search;
|
|
ImageButton clearSearch;
|
|
RecyclerView passList;
|
|
FloatingActionButton floatButton;
|
|
PassWordListAdapter adapter;
|
|
Handler handler = new Handler(Looper.getMainLooper());
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_main);
|
|
initView();
|
|
init();
|
|
}
|
|
|
|
void initView() {
|
|
dao = RoomDatabaseManager.build(this).passwordDao();
|
|
adapter = new PassWordListAdapter(this, new ArrayList<>());
|
|
search = findViewById(R.id.search);
|
|
clearSearch = findViewById(R.id.clearSearch);
|
|
passList = findViewById(R.id.passList);
|
|
floatButton = findViewById(R.id.floatButton);
|
|
floatButton.setOnClickListener(v -> showAddPasswordDialog());
|
|
passList.setLayoutManager(new LinearLayoutManager(this));
|
|
passList.setAdapter(adapter);
|
|
clearSearch.setOnClickListener(v -> {
|
|
search.setText("");
|
|
new Thread(this::showData).start();
|
|
});
|
|
search.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) {
|
|
new Thread(() -> {
|
|
List<ToolsPassword> list;
|
|
if (Tools.isEmpty(s.toString())) {
|
|
list = dao.getAll();
|
|
} else {
|
|
list = dao.queryPassword("%" + s.toString() + "%");
|
|
}
|
|
handler.post(() -> {
|
|
adapter.setList(list);
|
|
adapter.notifyDataSetChanged();
|
|
});
|
|
}).start();
|
|
}
|
|
|
|
@Override
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
void showAddPasswordDialog() {
|
|
View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.dialog_add_passworld, null);
|
|
EditText title, account, password;
|
|
title = view.findViewById(R.id.title);
|
|
account = view.findViewById(R.id.account);
|
|
password = view.findViewById(R.id.myPassword);
|
|
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
|
|
.setTitle("添加账号")
|
|
.setView(view)
|
|
.setPositiveButton("保存", (dialog1, which) -> {
|
|
addPassword(title.getText().toString(), account.getText().toString(), password.getText().toString(), -1, true, new NetworkInterface() {
|
|
@Override
|
|
public void httpGetData(Object data, int state) {
|
|
addDatabase(title.getText().toString(),
|
|
account.getText().toString(),
|
|
password.getText().toString(),
|
|
true);
|
|
}
|
|
|
|
@Override
|
|
public void httpError(Exception e) {
|
|
addDatabase(title.getText().toString(),
|
|
account.getText().toString(),
|
|
password.getText().toString(),
|
|
false);
|
|
}
|
|
|
|
void addDatabase(String title1, String account1, String password1, boolean upload) {
|
|
new Thread(() -> {
|
|
ToolsPassword psd = new ToolsPassword();
|
|
psd.setTitle(title1);
|
|
psd.setAccount(account1);
|
|
psd.setPassword(password1);
|
|
psd.setUpload(upload);
|
|
dao.insert(psd);
|
|
showData();
|
|
}).start();
|
|
|
|
}
|
|
});
|
|
dialog1.dismiss();
|
|
})
|
|
.setNegativeButton("取消", (dialog12, which) -> dialog12.dismiss()).create();
|
|
dialog.show();
|
|
}
|
|
|
|
|
|
void init() {
|
|
AppData.key = Tools.getConfig("key", this);
|
|
AppData.key = "6B059119-C8F6-4E19-A8F0-71801D776A1F";
|
|
if (Tools.isEmpty(AppData.key)) {
|
|
EditText key = new EditText(this);
|
|
key.setHint("激活码");
|
|
AlertDialog dialog = new AlertDialog.Builder(this)
|
|
.setTitle("输入激活码")
|
|
.setView(key)
|
|
.setPositiveButton("确定", (dialog1, which) -> {
|
|
String k = key.getText().toString().trim();
|
|
if (Tools.isEmpty(k)) {
|
|
Toast.makeText(MainActivity.this, "激活码不能为空", Toast.LENGTH_LONG).show();
|
|
return;
|
|
}
|
|
Tools.setConfig("key", k, MainActivity.this);
|
|
AppData.key = k;
|
|
dialog1.dismiss();
|
|
initData();
|
|
}).create();
|
|
dialog.show();
|
|
} else {
|
|
initData();
|
|
}
|
|
}
|
|
|
|
ToolsPasswordDao dao;
|
|
|
|
void initData() {
|
|
new Thread(() -> {
|
|
List<ToolsPassword> list = dao.getAll();
|
|
for (ToolsPassword password : list) {
|
|
System.out.println(JSONObject.toJSONString(password));
|
|
if (!password.isUpload()&&!password.isRemove()) {
|
|
addPassword(password.getTitle(), password.getAccount(), password.getPassword(), -1, true, new NetworkInterface() {
|
|
@Override
|
|
public void httpGetData(Object data, int state) {
|
|
new Thread(() -> {
|
|
password.setUpload(true);
|
|
dao.update(password);
|
|
}).start();
|
|
|
|
}
|
|
|
|
@Override
|
|
public void httpError(Exception e) {
|
|
|
|
}
|
|
});
|
|
}else if(password.isRemove()){
|
|
JSONObject json = new JSONObject();
|
|
json.put("id", password.getId());
|
|
NetworkTool.httpPost(NetworkTool.NetworkAPI.PASSWORD_REMOVE, json, new NetworkInterface() {
|
|
@Override
|
|
public void httpGetData(Object data, int state) {
|
|
remove(false);
|
|
}
|
|
|
|
@Override
|
|
public void httpError(Exception e) {
|
|
remove(true);
|
|
}
|
|
|
|
void remove(boolean isError) {
|
|
new Thread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
if (isError) {
|
|
password.setRemove(true);
|
|
dao.update(password);
|
|
} else {
|
|
dao.delete(password);
|
|
}
|
|
}
|
|
}).start();
|
|
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
NetworkTool.httpGet(NetworkTool.NetworkAPI.PASSWORD_ALL, new JSONObject(), new NetworkInterface() {
|
|
@Override
|
|
public void httpGetData(Object data, int state) {
|
|
JSONObject json = JSONObject.parseObject((String) data);
|
|
if (json.getInteger("code") == 0) {
|
|
List<ToolsPassword> list = JSONArray.parseArray(json.getJSONArray("data").toJSONString(), ToolsPassword.class);
|
|
new Thread(() -> {
|
|
for (ToolsPassword password : list) {
|
|
if (dao.isExist(password.getTitle(), password.getAccount(), password.getPassword()) == null) {
|
|
password.setUpload(true);
|
|
dao.insert(password);
|
|
}
|
|
}
|
|
showData();
|
|
}).start();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void httpError(Exception e) {
|
|
new Thread(() -> showData()).start();
|
|
}
|
|
});
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
void showData() {
|
|
List<ToolsPassword> list = dao.getAll();
|
|
handler.post(() -> {
|
|
adapter.setList(list);
|
|
adapter.notifyDataSetChanged();
|
|
});
|
|
}
|
|
} |