summaryrefslogtreecommitdiff
path: root/src/com/android/certinstaller/WiFiInstaller.java
blob: 41827f6326670ff539fdbfb7245d11f22c9ee35d (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
package com.android.certinstaller;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.net.wifi.hotspot2.ConfigParser;
import android.net.wifi.hotspot2.PasspointConfiguration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.DocumentsContract;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/**
 * An Activity for provisioning a Hotspot 2.0 Release 1 configuration.
 */
public class WiFiInstaller extends Activity {

    private static final String TAG = "WifiInstaller";
    private static final String NETWORK_NAME = "network_name";
    private static final String INSTALL_STATE = "install_state";
    public static final int INSTALL_SUCCESS = 2;
    public static final int INSTALL_FAIL = 1;
    public static final int INSTALL_FAIL_NO_WIFI = 0;
    PasspointConfiguration mPasspointConfig;
    WifiManager mWifiManager;
    boolean doNotInstall;

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

        Bundle bundle = getIntent().getExtras();
        String uriString = bundle.getString(CertInstallerMain.WIFI_CONFIG_FILE);
        String mimeType = bundle.getString(CertInstallerMain.WIFI_CONFIG);
        byte[] data = bundle.getByteArray(CertInstallerMain.WIFI_CONFIG_DATA);

        Log.d(TAG, "WiFi data for " + CertInstallerMain.WIFI_CONFIG + ": " +
                mimeType + " is " + (data != null ? data.length : "-"));

        mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        mPasspointConfig = ConfigParser.parsePasspointConfig(mimeType, data);
        dropFile(Uri.parse(uriString), getApplicationContext());

        if (mPasspointConfig == null) {
            Log.w(TAG, "failed to build passpoint configuration");
            doNotInstall = true;
        } else if (mPasspointConfig.getHomeSp() == null) {
            Log.w(TAG, "Passpoint profile missing HomeSP information");
            doNotInstall = true;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        createMainDialog();
    }

    /**
     * Create a dialog that guides the user through Hotspot 2.0 Release 1 configuration file
     * installation.
     */
    private void createMainDialog() {
        Resources res = getResources();
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View layout = getLayoutInflater().inflate(R.layout.wifi_main_dialog, null);
        builder.setView(layout);

        TextView text = (TextView) layout.findViewById(R.id.wifi_info);
        if (!doNotInstall) {
            text.setText(String.format(getResources().getString(R.string.wifi_installer_detail),
                    mPasspointConfig.getHomeSp().getFriendlyName()));

            builder.setTitle(mPasspointConfig.getHomeSp().getFriendlyName());
            builder.setIcon(res.getDrawable(R.drawable.signal_wifi_4_bar_lock_black_24dp));

            builder.setPositiveButton(R.string.wifi_install_label,
                    new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(WiFiInstaller.this, getString(R.string.wifi_installing_label),
                            Toast.LENGTH_LONG).show();
                    AsyncTask.execute(new Runnable() {
                        @Override
                        public void run() {
                            boolean success = true;
                            try {
                                mWifiManager.removePasspointConfiguration(
                                        mPasspointConfig.getHomeSp().getFqdn());
                            } catch (IllegalArgumentException e) {
                                // Do nothing. This is expected if a profile with this FQDN does not
                                // exist.
                            }
                            try {
                                mWifiManager.addOrUpdatePasspointConfiguration(mPasspointConfig);
                            } catch (RuntimeException rte) {
                                Log.w(TAG, "Caught exception while installing wifi config: " +
                                           rte, rte);
                                success = false;
                            }
                            if (success) {
                                Intent intent = new Intent(getApplicationContext(),
                                        CredentialsInstallDialog.class);
                                intent.putExtra(NETWORK_NAME,
                                        mPasspointConfig.getHomeSp().getFriendlyName());
                                intent.putExtra(INSTALL_STATE, INSTALL_SUCCESS);
                                startActivity(intent);
                            } else {
                                Intent intent = new Intent(getApplicationContext(),
                                        CredentialsInstallDialog.class);
                                intent.putExtra(INSTALL_STATE, INSTALL_FAIL);
                                startActivity(intent);
                            }
                            finish();
                        }
                    });
                    dialog.dismiss();
                }
            });

            builder.setNegativeButton(R.string.wifi_cancel_label, new
                    DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    finish();
                }
            });
        } else {
            text.setText(getResources().getString(R.string.wifi_installer_download_error));
            builder.setPositiveButton(R.string.done_label, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    finish();
                }
            });
        }
        builder.create().show();
    }

    /**
     * Delete the file specified by the given URI.
     *
     * @param uri The URI of the file
     * @param context The context of the current application
     */
    private static void dropFile(Uri uri, Context context) {
      try {
        if (DocumentsContract.isDocumentUri(context, uri)) {
          DocumentsContract.deleteDocument(context.getContentResolver(), uri);
        } else {
          context.getContentResolver().delete(uri, null, null);
        }
      } catch (Exception e) {
        Log.e(TAG, "could not delete document " + uri);
      }
    }
}