summaryrefslogtreecommitdiff
path: root/app/java/src/MainActivity.java
blob: 6d232b94825e9e058dc1d1de317b640f66a3dd87 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package org.pihole.dnsproxy;

import android.app.Activity;
import android.app.AlertDialog;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.VpnService;
import android.net.wifi.WifiManager;

import android.os.Bundle;

import android.preference.PreferenceManager;

import android.util.Log;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.TextureView;
import android.view.View;

import android.widget.Button;
import android.widget.Toast;
import android.provider.Settings;

public class MainActivity extends Activity {

    private Button buttonStart;
    private Intent dnsProxyService;

    private BroadcastReceiver receiveDNSProxyService = new BroadcastReceiver() {

        /**
         * Set button_start state on receive from DNSProxyService
         */
        @Override
        public void onReceive(Context context, Intent intent) {
            setStateButtonStart();
        }
    };

    private BroadcastReceiver receiveNetworkChange = new BroadcastReceiver() {

        /**
         * Set button_start state on receive of network change
         */
        @Override
        public void onReceive(Context context, Intent intent) {
            setStateButtonStart();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        this.buttonStart = (Button) findViewById(R.id.button_start);
        this.dnsProxyService = new Intent(this, DNSProxyService.class);
    }

    @Override
    public void onResume() {
        super.onResume();

        this.setStateButtonStart();
        this.toggleWifiListenerService();
        registerReceiver(this.receiveDNSProxyService, new IntentFilter(DNSProxyService.NOTIFICATION));
        registerReceiver(this.receiveNetworkChange, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));
    }

    @Override
    public void onPause() {
        super.onPause();

        unregisterReceiver(this.receiveDNSProxyService);
        unregisterReceiver(this.receiveNetworkChange);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.settings, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();

        if (itemId == R.id.menu__settings) {
            Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
            startActivity(intent);

            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * is called by "startActivityForResult" internally
     */
    @Override
    public void onActivityResult(int request, int result, Intent data) {
        if (result == RESULT_OK) {
          this.startDNSProxyService();
        }
    }

    /**
     * button_start is clicked
     */
    public void onClickButtonStart(View view) {
        if (!DNSProxyService.isRunning()) { //start
            Intent intent = VpnService.prepare(this);

            if (intent != null) {
                startActivityForResult(intent, 0);
            } else {
                this.onActivityResult(0, RESULT_OK, null);
            }
        }
        else { // stop
            this.stopDNSProxyService();
        }
    }

    /**
     * Set button_start state
     */
    private void setStateButtonStart() {
        // change text
        if (!DNSProxyService.isRunning()) {
            this.buttonStart.setText(getString(R.string.button_start__start));
        } else {
            this.buttonStart.setText(getString(R.string.button_start__stop));
        }

        // toggle enabled
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (networkInfo.isConnected() || DNSProxyService.isRunning()) {
            this.buttonStart.setEnabled(true);
        } else if (!networkInfo.isConnected() && !DNSProxyService.isRunning()) {
            this.buttonStart.setEnabled(false);
        }
    }

    /**
     * Start DNSProxyService
     */
    private void startDNSProxyService() {
        startService(this.dnsProxyService.setAction(DNSProxyService.ACTION_START));

        Toast
            .makeText(
                this,
                getString(R.string.service_starting),
                Toast.LENGTH_SHORT
            )
            .show();
    }

    /**
     * Stop DNSProxyService
     */
    private void stopDNSProxyService() {
        startService(this.dnsProxyService.setAction(DNSProxyService.ACTION_STOP));

        Toast
            .makeText(
                this,
                getString(R.string.service_stopping),
                Toast.LENGTH_SHORT
            )
            .show();
    }

    private void toggleWifiListenerService() {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        if (sharedPreferences.getBoolean("use_wifi_listener", false)) {
            startService(new Intent(this, WifiListenerService.class));
        } else {
            stopService(new Intent(this, WifiListenerService.class));
        }
    }
}