public void onServiceConnected(ComponentName className, IBinder service) {
          ConnectionManagerService.LocalBinder binder =
              (ConnectionManagerService.LocalBinder) service;
          connMgrService = binder.getService();
          Log.d(TAG, "ConnectionManagerService connected");
          // attach to remote intent service to allow it call back
          connMgrService.setConnector(ConnectorActivity.this);
          connMgrService.setSimpleConnectionInfo(devName, useSSL);

          // in case we miss it at onResume()
          // getPeerDeviceNetInfo();
        }
 public void onSetConnectionInfo() {
   if (Closed) return;
   Log.d(TAG, "SET_CONNECTION_INFO");
   if (peerNetData != null && chosenNType != NetInfo.NoNet) {
     NetInfo cn = connNets[chosenNType];
     if (isLeader) {
       DeviceInfo devv = new DeviceInfo(null, cn.addr, null);
       if (connMgrService != null) connMgrService.startPeerSearch(devv, -1);
     } else {
       DeviceInfo devv = new DeviceInfo(null, peerNetData.addr, null);
       if (connMgrService != null) connMgrService.startPeerSearch(devv, -1);
     }
   }
 }
  public void onGetActiveNetwork(NetInfo net) {
    if (Closed) return;
    Log.d(TAG, "GET_ACTIVE_NETWORK");
    Log.d(TAG, "chosen Ntype : " + chosenNType);

    if (net != null) {
      if (actNetType != NetInfo.NoNet
          && connNets[actNetType] != null
          && net.name != null
          && net.name.equals(connNets[actNetType].name)) {
        return;
      }
      actNetType = net.type;
      if (isLeader && chosenNType == actNetType) {
        netActivatedAtLeader(net);
      }
      if (connMgrService != null) {
        connMgrService.getDeviceInfo();
      }
      /*
       * Log.d(TAG, "chosen Ntype : " +
       * chosenNType+", net.type="+net.type); if (chosenNType == net.type)
       * { setUseSSL(qrData.useSSL); }
       */
    }
  }
  void resetChosenNType() {
    //
    if (isLeader && connMgrService != null) {
      connMgrService.stopPeerSearch();
      connMgrService.onConnectorDestroy();
    }
    //
    if (isLeader) {
      showGroup(2);
    }
    // chosenNType = NetInfo.NoNet;

    wifiBtn.setChecked(false);
    wifiDirectBtn.setChecked(false);
    wifiHotspotBtn.setChecked(false);
    wifiInfo.setText(wifiInfoText);
    wifiDirectInfo.setText(wifiDirectInfoText);
    wifiHotspotInfo.setText(wifiHotspotInfoText);
  }
  void setUseSSL(boolean useSSL) {
    Log.d(TAG, "setUseSSL: " + useSSL);

    // update shared preference
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean(ConnectionManager.PREF_KEY_USE_SSL, useSSL);
    editor.commit();
    // notif router service
    if (connMgrService != null) {
      connMgrService.setSimpleConnectionInfo(null, useSSL);
    }
  }
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
   switch (keyCode) {
     case KeyEvent.KEYCODE_BACK:
       Closed = true;
       if (connMgrService != null) {
         connMgrService.onConnectorDestroy();
       }
       finish();
       return true;
   }
   return super.onKeyDown(keyCode, event);
 }
 public void onNetworkConnected(NetInfo net) {
   if (Closed) return;
   Log.d(TAG, "NETWORK_CONNECTED");
   connNets[net.type] = net;
   NetInfo[] nets = new NetInfo[] {net};
   if (isLeader) {
     netConnectedAtLeader(nets);
   }
   Log.d(TAG, "activateNet type=" + net.type);
   if (net.type == chosenNType) {
     if (connMgrService != null) connMgrService.activateNetwork(net);
   }
 }
 void doWifiHotspot() {
   chosenNType = NetInfo.WiFiHotspot;
   if (connNets[NetInfo.WiFiHotspot] != null) {
     if (actNetType != NetInfo.WiFiHotspot) {
       connMgrService.activateNetwork(connNets[NetInfo.WiFiHotspot]);
     } else {
       NetInfo net = connNets[NetInfo.WiFiHotspot];
       netActivatedAtLeader(net);
       setUseSSL(peerNetData.useSSL);
     }
   } else {
     configWifi();
   }
 }
 void doWifiDirect() {
   chosenNType = NetInfo.WiFiDirect;
   if (connNets[NetInfo.WiFiDirect] != null) {
     if (actNetType != NetInfo.WiFiDirect) {
       connMgrService.activateNetwork(connNets[NetInfo.WiFiDirect]);
     } else {
       NetInfo net = connNets[NetInfo.WiFiDirect];
       netActivatedAtLeader(net);
       setUseSSL(peerNetData.useSSL);
     }
   } else {
     Log.d(TAG, "start Wifi Direct network");
     grpMgr.createNetwork();
   }
 }
 void doWifi() {
   Log.d(TAG, "doWifi");
   chosenNType = NetInfo.WiFi;
   if (connNets[NetInfo.WiFi] != null) {
     if (actNetType != NetInfo.WiFi) {
       connMgrService.activateNetwork(connNets[NetInfo.WiFi]);
     } else {
       NetInfo net = connNets[NetInfo.WiFi];
       netActivatedAtLeader(net);
       setUseSSL(peerNetData.useSSL);
     }
   } else {
     configWifi();
   }
 }
 public void onSearchStart(DeviceInfo leader) {
   if (Closed) return;
   Log.d(TAG, "SEARCH_START1");
   if (!isLeader && peerNetData != null && chosenNType != NetInfo.NoNet) {
     Log.d(TAG, "group member search started, exit connector");
     Closed = true;
     if (connMgrService != null) {
       connMgrService.onConnectorDestroy();
     }
     finish();
     if (nfcIntent != null) { // member connects thru NFC, bring up
       // CnnMgr
       Intent intent = new Intent(Router.ACTION_CONNECTION_MANAGEMENT);
       startActivity(intent);
     }
   }
 }
 public void onGetNetworks(NetInfo[] nets) {
   if (Closed) return;
   Log.d(TAG, "GET_NETWORKS = " + nets);
   if (nets != null && nets.length > 0) {
     for (NetInfo net : nets) {
       connNets[net.type] = net;
     }
     // dont check & limit to leader, since user may have not
     // chosen yet.
     // simply update GUI for all, it will not show foe member
     // if (isLeader) {
     netConnectedAtLeader(nets);
     // }
     if (connMgrService != null) {
       connMgrService.getActiveNetwork();
     }
   }
 }
  @Override
  protected void onDestroy() {
    if (connMgrService != null) {
      Log.d(TAG, "CtorActivity destroyed");
      connMgrService.onConnectorDestroy();
      unbindService(mConnection);
      connMgrService = null;
    }

    if (grpMgr != null) {
      grpMgr.onDestroy();
    }

    // dont destroy connMgrService here
    // since connMgr will start right away
    super.onDestroy();
    Log.d(TAG, "onDestroyed");
  }
  void setupWifiConn(PeerNetInfo qrData) {
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    /*
     * WifiInfo winfo = wifiManager.getConnectionInfo(); boolean match =
     * qrData.ssid.equals(winfo.getSSID()); //sometimes the above winfo is
     * encoded strangely, further verify if (!match) {
     * List<WifiConfiguration> existingConfigs = wifiManager
     * .getConfiguredNetworks(); for (WifiConfiguration ec :
     * existingConfigs) { if (ec.SSID.equals(qrData.ssid)) { match = true;
     * break; } } } if (winfo != null && match) {
     */
    // NetInfo connWifi = connNets[NetInfo.WiFi];
    // if(connWifi!=null&&connWifi.name!=null&&connWifi.name.equals(qrData.ssid))
    // {

    boolean match = false;
    NetInfo netWifi = connNets[NetInfo.WiFi];
    if (netWifi != null && netWifi.name != null) {
      String name1 = netWifi.name.trim();
      String name2 = qrData.ssid.trim();
      Log.d(TAG, "ssids = " + name1 + ", " + name2);
      name1 = trimQuote(name1);
      name2 = trimQuote(name2);
      Log.d(TAG, "ssids2 = " + name1 + ", " + name2);
      if (name1.equals(name2)) {
        match = true;
      }
    }
    if (match) {
      // already connect to the right wifi net
      Log.d(TAG, "already connect to right wifi");
      if (actNetType != NetInfo.WiFi) {
        connMgrService.activateNetwork(connNets[NetInfo.WiFi]);
      } else {
        setUseSSL(qrData.useSSL);
      }
    } else {
      Log.d(TAG, "start connect to wifi");
      new WifiConnector(this, wifiManager).execute(qrData);
    }
  }
 void resumeLeader() {
   if (isLeader) {
     if (connMgrService != null) {
       connMgrService.setConnector(this);
     }
     // clear gui
     wifiBtn.setChecked(false);
     wifiDirectBtn.setChecked(false);
     wifiHotspotBtn.setChecked(false);
     wifiInfo.setText(wifiInfoText);
     wifiDirectInfo.setText(wifiDirectInfoText);
     wifiHotspotInfo.setText(wifiHotspotInfoText);
     // query peers
     doLeader();
     //
     /*
      * switch (chosenNType) { case NetInfo.WiFi: if (wifiBtn != null) {
      * wifiBtn.setChecked(true); } break; case NetInfo.WiFiDirect: if
      * (wifiDirectBtn != null) { wifiDirectBtn.setChecked(true); }
      * break; case NetInfo.WiFiHotspot: if (wifiHotspotBtn != null) {
      * wifiHotspotBtn.setChecked(true); } break; }
      */
   }
 }
 // start call-chain:
 // getNetworks()->getActiveNetwork()->getPeerDevices()/getDeviceInfo()
 void getPeerDeviceNetInfo() {
   if (connMgrService != null) {
     connMgrService.getNetworks();
   }
 }
  public Dialog onCreateDialog(int id) {
    switch (id) {
      case WIFI_DIRECT_WARNING_DIALOG:
        return new AlertDialog.Builder(this)
            .setTitle(R.string.wifidir_warning_title)
            .setIcon(R.drawable.router_icon)
            .setMessage(R.string.wifidir_warning)
            .setPositiveButton(
                R.string.ok,
                new Dialog.OnClickListener() {

                  public void onClick(DialogInterface dialogInterface, int i) {

                    // dialogInterface.dismiss();
                    removeDialog(WIFI_DIRECT_WARNING_DIALOG);

                    // ask user to turn on wifi direct
                    Log.d(TAG, "ask user to turn on wifi direct");
                    try {
                      if (Utils.ANDROID_VERSION >= 16) {
                        Intent in = new Intent(Settings.ACTION_WIFI_SETTINGS);
                        startActivity(in);
                      } else {
                        Intent in = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
                        startActivity(in);
                      }
                    } catch (ActivityNotFoundException anf) {
                      Log.d(
                          TAG,
                          "no activity for : Settings.ACTION_WIRELESS_SETTINGS" + anf.getMessage());
                      Intent in = new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK);
                      startActivity(in);
                    }
                  }
                })
            .setNegativeButton(
                R.string.cancel,
                new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton) {
                    removeDialog(WIFI_DIRECT_WARNING_DIALOG);
                  }
                })
            .create();

      case WIFI_CONNECTOR_FAIL_DIALOG:
        if (peerNetData == null) {
          Closed = true;
          if (connMgrService != null) {
            connMgrService.onConnectorDestroy();
          }
          finish();
          return null;
        }
        CharSequence msg1 = getResources().getText(R.string.wifi_conn_fail_msg1);
        CharSequence msg2 = getResources().getText(R.string.wifi_conn_fail_msg2);
        return new AlertDialog.Builder(this)
            .setTitle(R.string.wifi_conn_fail_title)
            .setIcon(R.drawable.router_icon)
            .setMessage(msg1 + " " + peerNetData.ssid + msg2)
            .setNegativeButton(
                R.string.cancel,
                new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton) {
                    removeDialog(WIFI_CONNECTOR_FAIL_DIALOG);
                    Closed = true;
                    if (connMgrService != null) {
                      connMgrService.onConnectorDestroy();
                    }
                    finish();
                  }
                })
            .create();
      default:
        break;
    }
    return null;
  }
 public void onGetDeviceInfo(DeviceInfo dev) {
   connMgrService.setDeviceInfo(dev);
 }