aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/fil/libre/repwifiapp/activities/ShowStatusActivity.java
blob: 0fb89925aa7d616dc7a9c1467b26f0b90ca9bfb8 (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
//
// Copyright 2017 Filippo "Fil" Bergamo <fil.bergamo@riseup.net>
// 
// This file is part of RepWifiApp.
//
// RepWifiApp is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// RepWifiApp is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with RepWifiApp.  If not, see <http://www.gnu.org/licenses/>.
// 
// ********************************************************************

package fil.libre.repwifiapp.activities;

import fil.libre.repwifiapp.ActivityLauncher;
import fil.libre.repwifiapp.Commons;
import fil.libre.repwifiapp.R;
import fil.libre.repwifiapp.helpers.ConnectionStatus;
import fil.libre.repwifiapp.helpers.Utils;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ShowStatusActivity extends MenuEnabledActivity {

    private ConnectionStatus status;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_status);

        if (getIntent().hasExtra(ActivityLauncher.EXTRA_CONSTATUS)) {
            this.status = (ConnectionStatus) getIntent().getSerializableExtra(
                            ActivityLauncher.EXTRA_CONSTATUS);
        }

        showStatus(false);

    }

    @Override
    public void onRestart() {
        super.onRestart();
        showStatus(true);
    }

    private void setMessage(String msg) {
        TextView view = (TextView) findViewById(R.id.txt_status);
        view.setText(msg);
    }

    private void showStatus(boolean refresh) {

        if (refresh || status == null) {
            this.status = Commons.connectionEngine.getConnectionStatus();
        }

        if (status == null) {
            this.finish();

        } else if (this.status.isConnected()) {
            Utils.logDebug("StatusActivity isConnected,showing buttons");
            setMessage("Connected to " + status.SSID + "\n\n" + "IP Address: " + status.IP + "\n");
            toggleBtnDisconnect(true);

        } else {
            Utils.logDebug("StatusActivity status Else");
            setMessage("Status:\n" + status.status);
            toggleBtnDisconnect(false);
        }

        Commons.updateNotification(this);

    }

    private void toggleBtnDisconnect(boolean enable) {

        Button b = (Button) findViewById(R.id.btn_disconnect);
        Button bk = (Button) findViewById(R.id.btn_back);
        b.setEnabled(enable);
        bk.setEnabled(!enable);

        if (enable) {
            b.setVisibility(View.VISIBLE);
            bk.setVisibility(View.INVISIBLE);
        } else {
            b.setVisibility(View.INVISIBLE);
            bk.setVisibility(View.VISIBLE);
        }

    }

    public void onBtnDisconnectClick(View v) {

        boolean res = Commons.connectionEngine.disconnect();
        String msg = "";
        if (res) {
            msg = "Disconnected.";
        } else {
            msg = "FAILED to disconnect!";
        }

        Toast toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT);
        toast.show();

        showStatus(true);

    }

    public void onBtnMainClick(View v) {
        finish();
    }

    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
    }

}