Proper error page

This commit is contained in:
str4d
2014-12-17 01:53:05 +00:00
parent a9bf22c38f
commit 2e7b56cf48
4 changed files with 171 additions and 20 deletions

View File

@@ -1,17 +1,25 @@
package i2p.bote.android;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.androidplot.pie.PieChart;
import com.androidplot.pie.Segment;
import com.androidplot.pie.SegmentFormatter;
import net.i2p.android.ui.I2PAndroidHelper;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collection;
import java.util.List;
import java.util.Set;
@@ -24,6 +32,8 @@ import i2p.bote.network.RelayPeer;
import static i2p.bote.Util._;
public class NetworkInfoFragment extends Fragment {
private Exception mConnectError;
private PieChart mKademliaPie;
private TextView mKademliaPeers;
private PieChart mRelayPie;
@@ -31,29 +41,54 @@ public class NetworkInfoFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_network_info, container, false);
Bundle savedInstanceState) {
mConnectError = I2PBote.getInstance().getConnectError();
if (mConnectError == null)
return inflater.inflate(R.layout.fragment_network_info, container, false);
else
return inflater.inflate(R.layout.fragment_network_error, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mKademliaPie = (PieChart) view.findViewById(R.id.kademlia_peers_pie);
mKademliaPeers = (TextView) view.findViewById(R.id.kademlia_peers);
mRelayPie = (PieChart) view.findViewById(R.id.relay_peers_pie);
mRelayPeers = (TextView) view.findViewById(R.id.relay_peers);
if (mConnectError == null) {
mKademliaPie = (PieChart) view.findViewById(R.id.kademlia_peers_pie);
mKademliaPeers = (TextView) view.findViewById(R.id.kademlia_peers);
mRelayPie = (PieChart) view.findViewById(R.id.relay_peers_pie);
mRelayPeers = (TextView) view.findViewById(R.id.relay_peers);
setupKademliaPeers();
setupRelayPeers();
setupKademliaPeers();
setupRelayPeers();
Collection<BannedPeer> bannedPeers = I2PBote.getInstance().getBannedPeers();
((TextView) view.findViewById(R.id.banned_peers)).setText(
"" + bannedPeers.size());
Collection<BannedPeer> bannedPeers = I2PBote.getInstance().getBannedPeers();
((TextView) view.findViewById(R.id.banned_peers)).setText(
"" + bannedPeers.size());
} else {
((TextView) view.findViewById(R.id.error)).setText(mConnectError.toString());
Exception e = I2PBote.getInstance().getConnectError();
if (e != null)
((TextView) view.findViewById(R.id.error)).setText(e.toString());
view.findViewById(R.id.copy_error).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String fullError = joinStackTrace(mConnectError);
Object clipboardService = getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) clipboardService;
clipboard.setText(fullError);
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) clipboardService;
android.content.ClipData clip = android.content.ClipData.newPlainText(
getString(R.string.bote_connection_error), fullError);
clipboard.setPrimaryClip(clip);
}
Toast.makeText(getActivity(), R.string.full_error_copied_to_clipboard, Toast.LENGTH_SHORT).show();
}
});
if ((new I2PAndroidHelper(getActivity())).isI2PAndroidInstalled())
view.findViewById(R.id.error_page_i2p_content).setVisibility(View.VISIBLE);
}
}
private void setupSegmentFormatter(SegmentFormatter sf) {
@@ -165,4 +200,44 @@ public class NetworkInfoFragment extends Fragment {
mRelayPie.getBorderPaint().setColor(Color.TRANSPARENT);
mRelayPie.getBackgroundPaint().setColor(Color.TRANSPARENT);
}
private static String joinStackTrace(Throwable e) {
StringWriter writer = null;
try {
writer = new StringWriter();
joinStackTrace(e, writer);
return writer.toString();
}
finally {
if (writer != null)
try {
writer.close();
} catch (IOException e1) {
// ignore
}
}
}
private static void joinStackTrace(Throwable e, StringWriter writer) {
PrintWriter printer = null;
try {
printer = new PrintWriter(writer);
while (e != null) {
printer.println(e);
StackTraceElement[] trace = e.getStackTrace();
for (int i = 0; i < trace.length; i++)
printer.println("\tat " + trace[i]);
e = e.getCause();
if (e != null)
printer.println("Caused by:\r\n");
}
}
finally {
if (printer != null)
printer.close();
}
}
}

View File

@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/connect_error_occurred"
android:textAppearance="@style/TextAppearance.AppCompat.Subject"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/this_is_the_error"
android:textAppearance="@style/TextAppearance.AppCompat.Primary"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/error"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="@style/TextAppearance.AppCompat.Secondary"/>
<View
style="@style/Divider.Vertical"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"/>
<ImageView
android:id="@+id/copy_error"
android:layout_width="@dimen/listitem_picture_size"
android:layout_height="match_parent"
android:minHeight="@dimen/listitem_picture_size"
android:scaleType="center"
android:src="@drawable/ic_content_copy_grey600_24dp"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/most_errors_fixed_on_restart"
android:textAppearance="@style/TextAppearance.AppCompat.Primary"/>
<TextView
android:id="@+id/error_page_i2p_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/also_check_i2p_android"
android:textAppearance="@style/TextAppearance.AppCompat.Primary"
android:visibility="gone"/>
</LinearLayout>
</ScrollView>

View File

@@ -78,11 +78,5 @@
android:text="0"
android:textAppearance="@style/TextAppearance.AppCompat.Primary"/>
<TextView
android:id="@+id/error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"/>
</LinearLayout>
</ScrollView>

View File

@@ -152,6 +152,12 @@
<string name="unreliable">Unreliable</string>
<string name="untested">Untested</string>
<string name="banned_peers">Banned Peers:</string>
<string name="connect_error_occurred">Oh dear! Bote couldn\'t connect to the network.</string>
<string name="this_is_the_error">This is the error that happened:</string>
<string name="most_errors_fixed_on_restart">You can fix most problems by restarting Bote. To do this, go back to the main Bote page, then click "Disconnect from network" in the menu, and then click "Connect to network" in the menu again.</string>
<string name="also_check_i2p_android">If that doesn\'t help, check that I2P is running. You can see I2P\'s current status in the notification drawer, or by opening the I2P app. If it isn\'t running properly, restart I2P (in the app, click "Stop", and then click "Start" when it appears). Wait for I2P to be fully running, and then star Bote.</string>
<string name="bote_connection_error">Bote connection error</string>
<string name="full_error_copied_to_clipboard">Full error copied to clipboard</string>
<string name="pref_title_general">General</string>
<string name="pref_summ_general">General settings and default identity settings</string>
<string name="pref_title_autoMail">Auto-check mail</string>