2828import android .net .wifi .ScanResult ;
2929import android .net .wifi .SupplicantState ;
3030import android .net .wifi .WifiConfiguration ;
31- import android .net .wifi .WifiInfo ;
3231import android .net .wifi .WifiManager ;
3332import android .os .Bundle ;
3433import android .support .v4 .view .MenuItemCompat ;
5453import org .csploit .android .gui .dialogs .InputDialog .InputDialogListener ;
5554import org .csploit .android .gui .dialogs .WifiCrackDialog ;
5655import org .csploit .android .gui .dialogs .WifiCrackDialog .WifiCrackDialogListener ;
57- import org .csploit .android .net .Network ;
5856import org .csploit .android .wifi .Keygen ;
5957import org .csploit .android .wifi .NetworkManager ;
6058import org .csploit .android .wifi .WirelessMatcher ;
@@ -87,15 +85,40 @@ public class WifiScannerActivity extends ListActivity
8785 private String mCurrentKey = null ;
8886 private int mCurrentNetworkId = -1 ;
8987 private ClipboardManager mClipboard = null ;
88+ private WifiConfiguration mPreviousConfig = null ;
9089
91- public void onSuccessfulConnection () {
90+ private void onEnd () {
9291 List <WifiConfiguration > configurations = mWifiManager .getConfiguredNetworks ();
93- if (configurations != null ){
92+ boolean restore = false ;
93+
94+ if (configurations != null ) {
9495 for (WifiConfiguration config : configurations ){
9596 mWifiManager .enableNetwork (config .networkId , false );
97+ restore = restore || (mPreviousConfig != null && mPreviousConfig .SSID .equals (config .SSID ) &&
98+ ( mCurrentKey == null || !mCurrentKey .equals (mPreviousConfig .preSharedKey ) ) );
9699 }
97100 }
98101
102+ if (restore && !mConnected ) {
103+ restorePreviousConfig ();
104+ }
105+ }
106+
107+ private void restorePreviousConfig () {
108+ WifiConfiguration config = NetworkManager .getWifiConfiguration (mWifiManager , mPreviousConfig );
109+
110+ if (config != null ) {
111+ mWifiManager .removeNetwork (config .networkId );
112+ }
113+
114+ if (mWifiManager .addNetwork (mPreviousConfig ) != -1 ) {
115+ mWifiManager .saveConfiguration ();
116+ }
117+
118+ mPreviousConfig = null ;
119+ }
120+
121+ public void onSuccessfulConnection (){
99122 if (mCurrentKey != null ){
100123 mStatusText .setText (Html .fromHtml (getString (R .string .connected_to ) + mCurrentAp .SSID + getString (R .string .connected_to2 ) + mCurrentKey + getString (R .string .connected_to3 )));
101124 Toast .makeText (this , getString (R .string .wifi_key_copied ), Toast .LENGTH_SHORT ).show ();
@@ -105,23 +128,29 @@ public void onSuccessfulConnection(){
105128
106129 mConnectionReceiver .unregister ();
107130 mConnected = true ;
131+
132+ onEnd ();
108133 }
109134
110135 public void onFailedConnection (){
111136 mWifiManager .removeNetwork (mCurrentNetworkId );
112- if (mKeyList .size () == 0 ){
113- mStatusText .setText (Html .fromHtml (getString (R .string .connection_to ) + mCurrentAp .SSID + getString (R .string .connection_to2 )));
114137
115- List <WifiConfiguration > configurations = mWifiManager .getConfiguredNetworks ();
116- if (configurations != null ){
117- for (WifiConfiguration config : configurations ){
118- mWifiManager .enableNetwork (config .networkId , false );
119- }
138+ if (!mKeyList .isEmpty ()) {
139+ nextConnectionAttempt ();
140+ return ;
141+ }
142+
143+ mStatusText .setText (Html .fromHtml (getString (R .string .connection_to ) + mCurrentAp .SSID + getString (R .string .connection_to2 )));
144+
145+ List <WifiConfiguration > configurations = mWifiManager .getConfiguredNetworks ();
146+ if (configurations != null ){
147+ for (WifiConfiguration config : configurations ){
148+ mWifiManager .enableNetwork (config .networkId , false );
120149 }
150+ }
121151
122- mConnectionReceiver .unregister ();
123- } else
124- nextConnectionAttempt ();
152+ mConnectionReceiver .unregister ();
153+ onEnd ();
125154 }
126155
127156 @ Override
@@ -134,7 +163,6 @@ public void onCreate(Bundle savedInstanceState){
134163 else
135164 setTheme (R .style .AppTheme );
136165 setContentView (R .layout .wifi_scanner );
137- //getActionBar().setDisplayHomeAsUpEnabled(true);
138166
139167 mWifiManager = (WifiManager ) this .getSystemService (Context .WIFI_SERVICE );
140168 mClipboard = (ClipboardManager ) this .getSystemService (Context .CLIPBOARD_SERVICE );
@@ -143,7 +171,7 @@ public void onCreate(Bundle savedInstanceState){
143171 mConnectionReceiver = new ConnectionReceiver ();
144172 mStatusText = (TextView ) findViewById (R .id .scanStatusText );
145173 mAdapter = new ScanAdapter ();
146- mKeyList = new ArrayList <String >();
174+ mKeyList = new ArrayList <>();
147175
148176 getListView ().setAdapter (mAdapter );
149177
@@ -155,11 +183,6 @@ public void onCreate(Bundle savedInstanceState){
155183 mStatusText .setText (getString (R .string .wifi_activated ));
156184 }
157185
158- if (Network .isWifiConnected (this )){
159- WifiInfo info = mWifiManager .getConnectionInfo ();
160- NetworkManager .cleanPreviousConfiguration (mWifiManager , info );
161- }
162-
163186 mScanReceiver .register (this );
164187
165188 if (mMenu != null ) {
@@ -314,6 +337,12 @@ protected void onListItemClick(ListView l, View v, int position, long id){
314337 if (result != null ){
315338 final Keygen keygen = mWifiMatcher .getKeygen (result );
316339
340+ mPreviousConfig = NetworkManager .getWifiConfiguration (mWifiManager , result );
341+
342+ if (mPreviousConfig != null ) {
343+ mWifiManager .removeNetwork (mPreviousConfig .networkId );
344+ }
345+
317346 if (keygen != null && (result .capabilities .contains ("WEP" ) || result .capabilities .contains ("WPA" ))){
318347 mKeyList .clear ();
319348 new WifiCrackDialog
0 commit comments