Skip to content

Commit

Permalink
Implement network id & moon world id copy
Browse files Browse the repository at this point in the history
  • Loading branch information
kaaass committed Apr 7, 2021
1 parent 7d567ec commit 5453ce9
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 32 deletions.
22 changes: 17 additions & 5 deletions app/src/main/java/net/kaaass/zerotierfix/ui/MoonOrbitFragment.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.kaaass.zerotierfix.ui;

import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
Expand All @@ -10,6 +12,7 @@
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
Expand Down Expand Up @@ -268,13 +271,22 @@ public ViewHolder(View view) {
public boolean onLongClick(View view) {
Log.d(TAG, "Long click " + this);
PopupMenu popupMenu = new PopupMenu(MoonOrbitFragment.this.getActivity(), view);
popupMenu.getMenuInflater().inflate(R.menu.context_menu_network_item, popupMenu.getMenu());
popupMenu.getMenuInflater().inflate(R.menu.popup_menu_moon_orbit, popupMenu.getMenu());
popupMenu.show();
popupMenu.setOnMenuItemClickListener(menuItem -> {
Log.d(TAG, "Click popup delete " + this);
// 触发事件
MoonOrbitFragment.this.eventBus.post(new RemoveMoonOrbitEvent(mItem.getMoonWorldId(), mItem.getMoonSeed()));
return true;
if (menuItem.getItemId() == R.id.menu_item_delete_moon_orbit) {
// 触发事件
MoonOrbitFragment.this.eventBus.post(new RemoveMoonOrbitEvent(mItem.getMoonWorldId(), mItem.getMoonSeed()));
return true;
} else if (menuItem.getItemId() == R.id.menu_item_copy_moon_world_id) {
// 复制 Moon 地址
ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(getString(R.string.network_id), Long.toHexString(this.mItem.getMoonWorldId()));
clipboard.setPrimaryClip(clip);
Toast.makeText(getContext(), R.string.text_copied, Toast.LENGTH_SHORT).show();
return true;
}
return false;
});
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.kaaass.zerotierfix.ui;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -77,6 +79,7 @@ public class NetworkListFragment extends Fragment {
public static final int START_VPN = 2;
public static final String TAG = "NetworkListFragment";
private final EventBus eventBus;
private final List<Network> mNetworks = new ArrayList<>();
boolean mIsBound = false;
private JoinAfterAuth joinAfterAuth;
private RecyclerViewAdapter recyclerViewAdapter;
Expand All @@ -94,7 +97,6 @@ public void onServiceDisconnected(ComponentName componentName) {
NetworkListFragment.this.setIsBound(false);
}
};
private final List<Network> mNetworks = new ArrayList<>();
private VirtualNetworkConfig[] mVNC;
private TextView nodeIdView;
private TextView nodeStatusView;
Expand Down Expand Up @@ -489,7 +491,7 @@ private void setOfflineState() {
*/
private void updateNetworkList() {
List<Network> networkList = getNetworkList();
if (!networkList.isEmpty()) {
if (networkList != null) {
// 设置连接状态
for (Network oldNetwork : this.mNetworks) {
for (Network network : networkList) {
Expand Down Expand Up @@ -669,37 +671,44 @@ public boolean onClick(View view) {
public boolean onLongClick(View view) {
Log.d(NetworkListFragment.TAG, "ConvertView OnLongClickListener");
PopupMenu popupMenu = new PopupMenu(NetworkListFragment.this.getActivity(), view);
popupMenu.getMenuInflater().inflate(R.menu.context_menu_network_item, popupMenu.getMenu());
popupMenu.getMenuInflater().inflate(R.menu.popup_menu_network_item, popupMenu.getMenu());
popupMenu.show();
popupMenu.setOnMenuItemClickListener(menuItem -> {
if (menuItem.getItemId() != R.id.menu_item_delete_network) {
return false;
}
// 删除对应网络
DaoSession daoSession = ((AnalyticsApplication) NetworkListFragment.this.getActivity().getApplication()).getDaoSession();
AssignedAddressDao assignedAddressDao = daoSession.getAssignedAddressDao();
NetworkConfigDao networkConfigDao = daoSession.getNetworkConfigDao();
NetworkDao networkDao = daoSession.getNetworkDao();
if (this.mItem != null) {
if (this.mItem.getConnected()) {
NetworkListFragment.this.stopService();
}
NetworkConfig networkConfig = this.mItem.getNetworkConfig();
if (networkConfig != null) {
List<AssignedAddress> assignedAddresses = networkConfig.getAssignedAddresses();
if (!assignedAddresses.isEmpty()) {
for (AssignedAddress assignedAddress : assignedAddresses) {
assignedAddressDao.delete(assignedAddress);
if (menuItem.getItemId() == R.id.menu_item_delete_network) {
// 删除对应网络
DaoSession daoSession = ((AnalyticsApplication) NetworkListFragment.this.getActivity().getApplication()).getDaoSession();
AssignedAddressDao assignedAddressDao = daoSession.getAssignedAddressDao();
NetworkConfigDao networkConfigDao = daoSession.getNetworkConfigDao();
NetworkDao networkDao = daoSession.getNetworkDao();
if (this.mItem != null) {
if (this.mItem.getConnected()) {
NetworkListFragment.this.stopService();
}
NetworkConfig networkConfig = this.mItem.getNetworkConfig();
if (networkConfig != null) {
List<AssignedAddress> assignedAddresses = networkConfig.getAssignedAddresses();
if (!assignedAddresses.isEmpty()) {
for (AssignedAddress assignedAddress : assignedAddresses) {
assignedAddressDao.delete(assignedAddress);
}
}
networkConfigDao.delete(networkConfig);
}
networkConfigDao.delete(networkConfig);
networkDao.delete(this.mItem);
}
networkDao.delete(this.mItem);
daoSession.clear();
// 更新数据
NetworkListFragment.this.updateNetworkListAndNotify();
return true;
} else if (menuItem.getItemId() == R.id.menu_item_copy_network_id) {
// 复制网络 ID
ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(getString(R.string.network_id), this.mItem.getNetworkIdStr());
clipboard.setPrimaryClip(clip);
Toast.makeText(getContext(), R.string.text_copied, Toast.LENGTH_SHORT).show();
return true;
}
daoSession.clear();
// 更新数据
NetworkListFragment.this.updateNetworkListAndNotify();
return true;
return false;
});
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_item_copy_moon_world_id" android:title="@string/copy_moon_world_id_to_clipboard"/>
<item android:id="@+id/menu_item_delete_moon_orbit" android:title="@string/delete_moon_orbit"/>
</menu>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_item_copy_network_id" android:title="@string/copy_network_id_to_clipboard"/>
<item android:id="@+id/menu_item_delete_network" android:title="@string/delete_network"/>
</menu>
3 changes: 3 additions & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,7 @@
<string name="fail_retrieve_peer_list">无法获得结点信息,请检查网络连接状况</string>
<string name="peer_relay">中继</string>
<string name="click_to_add_network">点击 + 按钮添加网络</string>
<string name="copy_network_id_to_clipboard">复制网络 ID 到剪切板</string>
<string name="text_copied">文本已复制</string>
<string name="copy_moon_world_id_to_clipboard">复制 Moon 地址到剪切板</string>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,7 @@
<string name="fail_retrieve_peer_list">Failed to retrieve peer info. Please check your network connection.</string>
<string name="peer_relay">Relay</string>
<string name="click_to_add_network">Click + to add network</string>
<string name="copy_network_id_to_clipboard">Copy Network ID to Clipboard</string>
<string name="text_copied">Text copied</string>
<string name="copy_moon_world_id_to_clipboard">Copy Moon World ID to clipboard</string>
</resources>

0 comments on commit 5453ce9

Please sign in to comment.