2 * Copyright (C) 2008 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package cz.cvut.fel.dce.barcodescanner.result;
19 import android.app.Activity;
20 import android.content.Context;
21 import android.net.wifi.WifiManager;
22 import android.os.AsyncTask;
23 import android.util.Log;
24 import android.widget.Toast;
26 import cz.cvut.fel.dce.barcodescanner.CaptureActivity;
27 import cz.cvut.fel.dce.barcodescanner.R;
28 import cz.cvut.fel.dce.barcodescanner.wifi.WifiConfigManager;
29 import com.google.zxing.client.result.ParsedResult;
30 import com.google.zxing.client.result.WifiParsedResult;
33 * Handles wifi access information.
35 * @author Vikram Aggarwal
38 public final class WifiResultHandler extends ResultHandler {
40 private static final String TAG = WifiResultHandler.class.getSimpleName();
42 private final CaptureActivity parent;
44 public WifiResultHandler(CaptureActivity activity, ParsedResult result) {
45 super(activity, result);
50 public int getButtonCount() {
51 // We just need one button, and that is to configure the wireless. This could change in the future.
56 public int getButtonText(int index) {
57 return R.string.button_wifi;
61 public void handleButtonPress(int index) {
63 WifiParsedResult wifiResult = (WifiParsedResult) getResult();
64 WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
65 if (wifiManager == null) {
66 Log.w(TAG, "No WifiManager available from device");
69 final Activity activity = getActivity();
70 activity.runOnUiThread(new Runnable() {
73 Toast.makeText(activity.getApplicationContext(), R.string.wifi_changing_network, Toast.LENGTH_SHORT).show();
76 new WifiConfigManager(wifiManager).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, wifiResult);
77 parent.restartPreviewAfterDelay(0L);
81 // Display the name of the network and the network type to the user.
83 public CharSequence getDisplayContents() {
84 WifiParsedResult wifiResult = (WifiParsedResult) getResult();
85 return wifiResult.getSsid() + " (" + wifiResult.getNetworkEncryption() + ')';
89 public int getDisplayTitle() {
90 return R.string.result_wifi;