summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/AbstractProviderDetailActivity.java
blob: fbb27b58f4f149626e278fe96286696b5200ff61 (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
package se.leap.bitmaskclient;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.InjectView;

import static se.leap.bitmaskclient.Constants.PROVIDER_ALLOW_ANONYMOUS;
import static se.leap.bitmaskclient.Constants.PROVIDER_KEY;
import static se.leap.bitmaskclient.Constants.REQUEST_CODE_CONFIGURE_LEAP;

public abstract class AbstractProviderDetailActivity extends ConfigWizardBaseActivity {

    final public static String TAG = "providerDetailActivity";

    @InjectView(R.id.provider_detail_description)
    AppCompatTextView description;

    @InjectView(R.id.provider_detail_options)
    ListView options;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_provider_detail);

        try {
            JSONObject providerJson = new JSONObject(preferences.getString(Provider.KEY, ""));
            setProviderHeaderText(ConfigHelper.getProviderName(preferences));
            description.setText(ConfigHelper.getDescription(preferences));

            // Show only the options allowed by the provider
            ArrayList<String> optionsList = new ArrayList<>();
            if (registrationAllowed(providerJson)) {
                optionsList.add(getString(R.string.login_to_profile));
                optionsList.add(getString(R.string.create_profile));
            }
            if (anonAllowed(providerJson)) {
                optionsList.add(getString(R.string.use_anonymously_button));
            }

            options.setAdapter(new ArrayAdapter<>(
                    this,
                    android.R.layout.simple_list_item_activated_1,
                    android.R.id.text1,
                    optionsList.toArray(new String[optionsList.size()])
            ));
            options.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String text = ((TextView) view).getText().toString();
                    Intent intent;
                    if (text.equals(getString(R.string.login_to_profile))) {
                        Log.d(TAG, "login selected");
                        intent = new Intent(getApplicationContext(), LoginActivity.class);
                    } else if (text.equals(getString(R.string.create_profile))) {
                        Log.d(TAG, "signup selected");
                        intent = new Intent(getApplicationContext(), SignupActivity.class);
                    } else {
                        Log.d(TAG, "use anonymously selected");
                        intent = new Intent();
                        intent.putExtra(Provider.KEY, provider);
                        setResult(RESULT_OK, intent);
                        finish();
                        return;
                    }
                    intent.putExtra(PROVIDER_KEY, provider);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    startActivityForResult(intent, REQUEST_CODE_CONFIGURE_LEAP);
                }
            });
        } catch (JSONException e) {
            // TODO show error and return
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        provider = intent.getParcelableExtra(PROVIDER_KEY);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_CONFIGURE_LEAP) {
            if (resultCode == RESULT_OK) {
                setResult(resultCode, data);
                finish();
            }
        }
    }

    private boolean anonAllowed(JSONObject providerJson) {
        try {
            JSONObject serviceDescription = providerJson.getJSONObject(Provider.SERVICE);
            return serviceDescription.has(PROVIDER_ALLOW_ANONYMOUS) && serviceDescription.getBoolean(PROVIDER_ALLOW_ANONYMOUS);
        } catch (JSONException e) {
            return false;
        }
    }

    private boolean registrationAllowed(JSONObject providerJson) {
        try {
            JSONObject serviceDescription = providerJson.getJSONObject(Provider.SERVICE);
            return serviceDescription.has(Provider.ALLOW_REGISTRATION) && serviceDescription.getBoolean(Provider.ALLOW_REGISTRATION);
        } catch (JSONException e) {
            return false;
        }
    }

    @Override
    public void onBackPressed() {
        SharedPreferences.Editor editor = preferences.edit();
        editor.remove(Provider.KEY).remove(PROVIDER_ALLOW_ANONYMOUS).remove(PROVIDER_KEY).apply();
        super.onBackPressed();
    }

}