]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - BarCodeScanner/mobile/src/main/java/cz/cvut/fel/dce/barcodescanner/result/WifiResultHandler.java
Initial commit
[hornmich/skoda-qr-demo.git] / BarCodeScanner / mobile / src / main / java / cz / cvut / fel / dce / barcodescanner / result / WifiResultHandler.java
1 /*
2  * Copyright (C) 2008 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package cz.cvut.fel.dce.barcodescanner.result;
18
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;
25
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;
31
32 /**
33  * Handles wifi access information.
34  *
35  * @author Vikram Aggarwal
36  * @author Sean Owen
37  */
38 public final class WifiResultHandler extends ResultHandler {
39
40   private static final String TAG = WifiResultHandler.class.getSimpleName();
41
42   private final CaptureActivity parent;
43
44   public WifiResultHandler(CaptureActivity activity, ParsedResult result) {
45     super(activity, result);
46     parent = activity;
47   }
48
49   @Override
50   public int getButtonCount() {
51     // We just need one button, and that is to configure the wireless.  This could change in the future.
52     return 1;
53   }
54
55   @Override
56   public int getButtonText(int index) {
57     return R.string.button_wifi;
58   }
59
60   @Override
61   public void handleButtonPress(int index) {
62     if (index == 0) {
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");
67         return;
68       }
69       final Activity activity = getActivity();
70       activity.runOnUiThread(new Runnable() {
71         @Override
72         public void run() {
73           Toast.makeText(activity.getApplicationContext(), R.string.wifi_changing_network, Toast.LENGTH_SHORT).show();
74         }
75       });
76       new WifiConfigManager(wifiManager).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, wifiResult);
77       parent.restartPreviewAfterDelay(0L);
78     }
79   }
80
81   // Display the name of the network and the network type to the user.
82   @Override
83   public CharSequence getDisplayContents() {
84     WifiParsedResult wifiResult = (WifiParsedResult) getResult();
85     return wifiResult.getSsid() + " (" + wifiResult.getNetworkEncryption() + ')';
86   }
87
88   @Override
89   public int getDisplayTitle() {
90     return R.string.result_wifi;
91   }
92 }