aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/activities/StartActivity.java
blob: e469fa0687e14c8611e8c25751b0b4c58417402b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package org.pacien.tincapp.activities;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;

import org.pacien.tincapp.R;
import org.pacien.tincapp.commands.PermissionFixer;
import org.pacien.tincapp.context.AppPaths;
import org.pacien.tincapp.service.TincVpnService;

/**
 * @author pacien
 */
public class StartActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getLayoutInflater().inflate(R.layout.page_start, getContentView());
	}

	@Override
	protected void onActivityResult(int request, int result, Intent data) {
		notify(result == RESULT_OK ? R.string.message_vpn_permissions_granted : R.string.message_vpn_permissions_denied);
	}

	public void requestVpnPermission(View v) {
		Intent askPermIntent = TincVpnService.prepare(this);

		if (askPermIntent != null)
			startActivityForResult(askPermIntent, 0);
		else
			onActivityResult(0, RESULT_OK, null);
	}

	public void startVpnDialog(View v) {
		final EditText i = new EditText(this);
		i.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
		i.setHint(R.string.field_net_name);

		@SuppressLint("InflateParams")
		ViewGroup vg = (ViewGroup) getLayoutInflater().inflate(R.layout.dialog_frame, null);
		vg.addView(i);

		new AlertDialog.Builder(this)
				.setTitle(R.string.title_connect_to_network)
				.setView(vg)
				.setPositiveButton(R.string.action_connect, (dialog, which) -> startVpn(i.getText().toString()))
				.setNegativeButton(R.string.action_close, (dialog, which) -> { /* nop */ })
				.show();
	}

	public void confDirDialog(View v) {
		String confDir = AppPaths.confDir(this).getPath();

		new AlertDialog.Builder(this)
				.setTitle(R.string.title_tinc_config_dir)
				.setMessage(confDir)
				.setNeutralButton(R.string.action_fix_perms, (dialog, which) -> fixPerms())
				.setNegativeButton(R.string.action_copy,
						(dialog, which) -> copyIntoClipboard(getResources().getString(R.string.title_tinc_config_dir), confDir))
				.setPositiveButton(R.string.action_close, (dialog, which) -> { /* nop */ })
				.show();
	}

	private void startVpn(String netName) {
		startService(new Intent(this, TincVpnService.class)
				.putExtra(TincVpnService.INTENT_EXTRA_NET_NAME, netName));
	}

	private void fixPerms() {
		boolean ok = PermissionFixer.makePrivateDirsPublic(getApplicationContext());
		notify(ok ? R.string.message_perms_fixed : R.string.message_perms_fix_failure);
	}

}