diff options
Diffstat (limited to 'app/src/main/java')
12 files changed, 663 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.java b/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.java new file mode 100644 index 0000000..0e6cb95 --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.java | |||
@@ -0,0 +1,71 @@ | |||
1 | package org.pacien.tincapp.activities; | ||
2 | |||
3 | import android.content.ClipData; | ||
4 | import android.content.ClipboardManager; | ||
5 | import android.content.Context; | ||
6 | import android.content.Intent; | ||
7 | import android.net.Uri; | ||
8 | import android.os.Bundle; | ||
9 | import android.support.annotation.StringRes; | ||
10 | import android.support.design.widget.Snackbar; | ||
11 | import android.support.v7.app.AlertDialog; | ||
12 | import android.support.v7.app.AppCompatActivity; | ||
13 | import android.support.v7.widget.Toolbar; | ||
14 | import android.view.Menu; | ||
15 | import android.view.MenuItem; | ||
16 | import android.view.ViewGroup; | ||
17 | |||
18 | import org.pacien.tincapp.BuildConfig; | ||
19 | import org.pacien.tincapp.R; | ||
20 | import org.pacien.tincapp.context.AppInfo; | ||
21 | |||
22 | /** | ||
23 | * @author pacien | ||
24 | */ | ||
25 | public abstract class BaseActivity extends AppCompatActivity { | ||
26 | |||
27 | @Override | ||
28 | protected void onCreate(Bundle savedInstanceState) { | ||
29 | super.onCreate(savedInstanceState); | ||
30 | setContentView(R.layout.base); | ||
31 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | ||
32 | setSupportActionBar(toolbar); | ||
33 | } | ||
34 | |||
35 | @Override | ||
36 | public boolean onCreateOptionsMenu(Menu m) { | ||
37 | getMenuInflater().inflate(R.menu.menu_base, m); | ||
38 | return true; | ||
39 | } | ||
40 | |||
41 | public void aboutDialog(MenuItem i) { | ||
42 | new AlertDialog.Builder(this) | ||
43 | .setTitle(BuildConfig.APPLICATION_ID) | ||
44 | .setMessage(getResources().getString(R.string.app_short_desc) + "\n\n" + | ||
45 | getResources().getString(R.string.app_copyright) + " " + | ||
46 | getResources().getString(R.string.app_license) + "\n\n" + | ||
47 | AppInfo.all(getResources())) | ||
48 | .setNeutralButton(R.string.action_open_project_website, (dialog, which) -> openWebsite(R.string.app_website_url)) | ||
49 | .setPositiveButton(R.string.action_close, (dialog, which) -> { /* nop */ }) | ||
50 | .show(); | ||
51 | } | ||
52 | |||
53 | protected ViewGroup getContentView() { | ||
54 | return (ViewGroup) findViewById(R.id.main_content); | ||
55 | } | ||
56 | |||
57 | protected void openWebsite(@StringRes int url) { | ||
58 | startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getResources().getString(url)))); | ||
59 | } | ||
60 | |||
61 | protected void notify(@StringRes int msg) { | ||
62 | Snackbar.make(findViewById(R.id.activity_base), msg, Snackbar.LENGTH_LONG).show(); | ||
63 | } | ||
64 | |||
65 | protected void copyIntoClipboard(String label, String str) { | ||
66 | ClipboardManager c = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); | ||
67 | c.setPrimaryClip(ClipData.newPlainText(label, str)); | ||
68 | notify(R.string.message_text_copied); | ||
69 | } | ||
70 | |||
71 | } | ||
diff --git a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.java b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.java new file mode 100644 index 0000000..e469fa0 --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.java | |||
@@ -0,0 +1,82 @@ | |||
1 | package org.pacien.tincapp.activities; | ||
2 | |||
3 | import android.annotation.SuppressLint; | ||
4 | import android.content.Intent; | ||
5 | import android.os.Bundle; | ||
6 | import android.support.v7.app.AlertDialog; | ||
7 | import android.view.View; | ||
8 | import android.view.ViewGroup; | ||
9 | import android.widget.EditText; | ||
10 | import android.widget.FrameLayout; | ||
11 | |||
12 | import org.pacien.tincapp.R; | ||
13 | import org.pacien.tincapp.commands.PermissionFixer; | ||
14 | import org.pacien.tincapp.context.AppPaths; | ||
15 | import org.pacien.tincapp.service.TincVpnService; | ||
16 | |||
17 | /** | ||
18 | * @author pacien | ||
19 | */ | ||
20 | public class StartActivity extends BaseActivity { | ||
21 | |||
22 | @Override | ||
23 | protected void onCreate(Bundle savedInstanceState) { | ||
24 | super.onCreate(savedInstanceState); | ||
25 | getLayoutInflater().inflate(R.layout.page_start, getContentView()); | ||
26 | } | ||
27 | |||
28 | @Override | ||
29 | protected void onActivityResult(int request, int result, Intent data) { | ||
30 | notify(result == RESULT_OK ? R.string.message_vpn_permissions_granted : R.string.message_vpn_permissions_denied); | ||
31 | } | ||
32 | |||
33 | public void requestVpnPermission(View v) { | ||
34 | Intent askPermIntent = TincVpnService.prepare(this); | ||
35 | |||
36 | if (askPermIntent != null) | ||
37 | startActivityForResult(askPermIntent, 0); | ||
38 | else | ||
39 | onActivityResult(0, RESULT_OK, null); | ||
40 | } | ||
41 | |||
42 | public void startVpnDialog(View v) { | ||
43 | final EditText i = new EditText(this); | ||
44 | i.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); | ||
45 | i.setHint(R.string.field_net_name); | ||
46 | |||
47 | @SuppressLint("InflateParams") | ||
48 | ViewGroup vg = (ViewGroup) getLayoutInflater().inflate(R.layout.dialog_frame, null); | ||
49 | vg.addView(i); | ||
50 | |||
51 | new AlertDialog.Builder(this) | ||
52 | .setTitle(R.string.title_connect_to_network) | ||
53 | .setView(vg) | ||
54 | .setPositiveButton(R.string.action_connect, (dialog, which) -> startVpn(i.getText().toString())) | ||
55 | .setNegativeButton(R.string.action_close, (dialog, which) -> { /* nop */ }) | ||
56 | .show(); | ||
57 | } | ||
58 | |||
59 | public void confDirDialog(View v) { | ||
60 | String confDir = AppPaths.confDir(this).getPath(); | ||
61 | |||
62 | new AlertDialog.Builder(this) | ||
63 | .setTitle(R.string.title_tinc_config_dir) | ||
64 | .setMessage(confDir) | ||
65 | .setNeutralButton(R.string.action_fix_perms, (dialog, which) -> fixPerms()) | ||
66 | .setNegativeButton(R.string.action_copy, | ||
67 | (dialog, which) -> copyIntoClipboard(getResources().getString(R.string.title_tinc_config_dir), confDir)) | ||
68 | .setPositiveButton(R.string.action_close, (dialog, which) -> { /* nop */ }) | ||
69 | .show(); | ||
70 | } | ||
71 | |||
72 | private void startVpn(String netName) { | ||
73 | startService(new Intent(this, TincVpnService.class) | ||
74 | .putExtra(TincVpnService.INTENT_EXTRA_NET_NAME, netName)); | ||
75 | } | ||
76 | |||
77 | private void fixPerms() { | ||
78 | boolean ok = PermissionFixer.makePrivateDirsPublic(getApplicationContext()); | ||
79 | notify(ok ? R.string.message_perms_fixed : R.string.message_perms_fix_failure); | ||
80 | } | ||
81 | |||
82 | } | ||
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Command.java b/app/src/main/java/org/pacien/tincapp/commands/Command.java new file mode 100644 index 0000000..28ff226 --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/commands/Command.java | |||
@@ -0,0 +1,67 @@ | |||
1 | package org.pacien.tincapp.commands; | ||
2 | |||
3 | import com.annimon.stream.Collectors; | ||
4 | import com.annimon.stream.Optional; | ||
5 | import com.annimon.stream.Stream; | ||
6 | |||
7 | import java.util.Arrays; | ||
8 | import java.util.Collections; | ||
9 | import java.util.LinkedList; | ||
10 | import java.util.List; | ||
11 | |||
12 | /** | ||
13 | * @author pacien | ||
14 | */ | ||
15 | class Command { | ||
16 | |||
17 | static private class Option { | ||
18 | final String key; | ||
19 | final Optional<java.lang.String> val; | ||
20 | |||
21 | Option(String key, String val) { | ||
22 | this.key = key; | ||
23 | this.val = Optional.ofNullable(val); | ||
24 | } | ||
25 | |||
26 | @Override | ||
27 | public String toString() { | ||
28 | return val.isPresent() ? "--" + key + "=" + val.get() : "--" + key; | ||
29 | } | ||
30 | } | ||
31 | |||
32 |