Hidden Service dialog

This commit is contained in:
arrase 2016-11-17 02:57:37 +01:00
parent 135e6c4ae0
commit 3f7435c90e
6 changed files with 177 additions and 4 deletions

View File

@ -5,7 +5,6 @@ import android.database.ContentObserver;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
@ -14,6 +13,7 @@ import android.view.View;
import org.torproject.android.R;
import org.torproject.android.ui.hs.adapters.HSAdapter;
import org.torproject.android.ui.hs.dialogs.HSDataDialog;
import org.torproject.android.ui.hs.providers.HSContentProvider;
public class HiddenServicesActivity extends AppCompatActivity {
@ -37,8 +37,8 @@ public class HiddenServicesActivity extends AppCompatActivity {
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
HSDataDialog dialog = new HSDataDialog();
dialog.show(getSupportFragmentManager(), "HSDataDialog");
}
});

View File

@ -0,0 +1,84 @@
package org.torproject.android.ui.hs.dialogs;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.torproject.android.R;
import org.torproject.android.ui.hs.providers.HSContentProvider;
public class HSDataDialog extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the layout
final View dialog_view = getActivity().getLayoutInflater().inflate(R.layout.dialog_hs_data, null);
// Use the Builder class for convenient dialog construction
final AlertDialog serverDataDialog = new AlertDialog.Builder(getActivity())
.setView(dialog_view)
.setTitle(R.string.hs_dialog_title)
.create();
// Buttons action
Button save = (Button) dialog_view.findViewById(R.id.HSDialogSave);
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String serverName = ((EditText) dialog_view.findViewById(R.id.hsName)).getText().toString();
Integer serverPort = Integer.parseInt(
((EditText) dialog_view.findViewById(R.id.serverPort)).getText().toString()
);
if (checkInput(serverPort)) {
saveData(serverName, serverPort);
serverDataDialog.dismiss();
}
}
});
Button cancel = (Button) dialog_view.findViewById(R.id.HSDialogCancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
serverDataDialog.cancel();
}
});
return serverDataDialog;
}
private boolean checkInput(Integer port) {
boolean is_ok = true;
Integer error_msg = 0;
if (port <= 1 || port > 65535) {
error_msg = R.string.invalid_port;
is_ok = false;
}
if (!is_ok) {
Toast.makeText(getContext(), error_msg, Toast.LENGTH_SHORT).show();
}
return is_ok;
}
private void saveData(String name, Integer port) {
ContentValues fields = new ContentValues();
fields.put("name", name);
fields.put("port", port);
ContentResolver cr = getContext().getContentResolver();
cr.insert(HSContentProvider.CONTENT_URI, fields);
}
}

View File

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/HSNameLabel"
android:text="@string/name"
android:textAppearance="@style/TextAppearance.AppCompat.Widget.PopupMenu.Small"
android:paddingLeft="5dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="@+id/hsName" />
<TextView
android:text="@string/port"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/HSPortLabel"
android:textAppearance="@style/TextAppearance.AppCompat.Widget.PopupMenu.Small"
android:paddingLeft="5dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:id="@+id/serverPort"
android:inputType="number" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="@string/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/HSDialogCancel"
android:layout_weight="1"
style="@style/Widget.AppCompat.Button.Borderless.Colored" />
<Button
android:text="@string/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/HSDialogSave"
android:layout_weight="1"
style="@style/Widget.AppCompat.Button.Borderless.Colored" />
</LinearLayout>
</LinearLayout>

View File

@ -331,4 +331,10 @@
<string name="vpn_default_world">World (Location)</string>
<string name="title_activity_hidden_services">Hidden Services</string>
<string name="save">Save</string>
<string name="cancel">Cancel</string>
<string name="hs_dialog_title">Hidden Service</string>
<string name="port">Port</string>
<string name="name">Name</string>
<string name="invalid_port">Invalid Port</string>
</resources>

View File

@ -0,0 +1,20 @@
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

View File

@ -9,6 +9,6 @@
#proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txt
# Project target.
target=android-23
target=android-15
android.library.reference.1=external/jsocks/jsockslib
android.library.reference.2=external/appcompat