Skip to content

Commit

Permalink
Attempt at fixing strange bugs related to null Keygens.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruiaraujo committed Jun 1, 2015
1 parent e6c0938 commit ca562ef
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,22 @@
import android.net.wifi.WifiManager;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;

public class WiFiNetwork implements Comparable<WiFiNetwork>, Parcelable {

final private ScanResult scanResult;
private ScanResult scanResult;
final private String ssidName;
final private String macAddress;
final private int level;
final private String encryption;
final private ArrayList<Keygen> keygens;
private ArrayList<Keygen> keygens;

public WiFiNetwork(ScanResult scanResult, ZipInputStream magicInfo) {
this.ssidName = scanResult.SSID;
this.macAddress = scanResult.BSSID.toUpperCase(Locale.getDefault());
this.level = WifiManager.calculateSignalLevel(scanResult.level, 4);
this.encryption = scanResult.capabilities;
this.scanResult = scanResult;
this.keygens = WirelessMatcher.getKeygen(ssidName, macAddress,
magicInfo);
this(scanResult.SSID,scanResult.BSSID,
WifiManager.calculateSignalLevel(scanResult.level, 4),
scanResult.capabilities, magicInfo);
this.scanResult = scanResult;
}

public WiFiNetwork(final String ssid, final String mac, int level,
Expand Down Expand Up @@ -70,7 +68,7 @@ public String getMacAddress() {
return macAddress;
}

public int compareTo(WiFiNetwork another) {
public int compareTo(@NonNull WiFiNetwork another) {
if (getSupportState() == another.getSupportState()) {
if (another.level == this.level)
return ssidName.compareTo(another.ssidName);
Expand Down Expand Up @@ -110,7 +108,7 @@ protected WiFiNetwork(Parcel in) {
else
encryption = OPEN;
level = in.readInt();
keygens = new ArrayList<Keygen>();
keygens = new ArrayList<>();
in.readList(keygens, Keygen.class.getClassLoader());
if (in.readInt() == 1)
scanResult = in.readParcelable(ScanResult.class.getClassLoader());
Expand Down Expand Up @@ -141,9 +139,13 @@ public ScanResult getScanResult() {
return scanResult;
}

public ArrayList<Keygen> getKeygens() {
return keygens;
}
public void setKeygens(ZipInputStream magicInfo) {
this.keygens = WirelessMatcher.getKeygen(ssidName, macAddress,
magicInfo);
}
public ArrayList<Keygen> getKeygens() {
return keygens;
}

// Constants used for different security types
public static final String PSK = "PSK";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.ZipInputStream;

import org.acra.ACRA;
import org.exobel.routerkeygen.AdsUtils;
Expand Down Expand Up @@ -86,17 +88,16 @@ public NetworkFragment() {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(NETWORK_ID)) {
wifiNetwork = (WiFiNetwork) getArguments()
.getParcelable(NETWORK_ID);
wifiNetwork = getArguments().getParcelable(NETWORK_ID);
restoreMissingKeygens();
thread = new KeygenThread(wifiNetwork);
}
if (savedInstanceState != null) {
String[] passwords = savedInstanceState
.getStringArray(PASSWORD_LIST);
if (passwords != null) {
passwordList = new ArrayList<String>();
for (String p : passwords)
passwordList.add(p);
passwordList = new ArrayList<>();
passwordList.addAll(Arrays.asList(passwords));
}
}
setHasOptionsMenu(true);
Expand Down Expand Up @@ -164,9 +165,10 @@ private boolean isAutoConnectServiceRunning() {
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (passwordList != null)
if (passwordList != null) {
outState.putStringArray(PASSWORD_LIST,
passwordList.toArray(new String[0]));
passwordList.toArray(new String[passwordList.size()]));
}
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
Expand All @@ -187,7 +189,36 @@ public void onActivityCreated(Bundle savedInstanceState) {
@Override
public void onDetach() {
super.onDetach();
thread.cancel();
if (thread != null){
//This thread can be null if there was a previosly calculated
//password list
thread.cancel();
}
}

/**
* Some devices seem to have bugs with the parcelable implementation
* So we try to restore missing objects here.
*/
private void restoreMissingKeygens(){
boolean foundMissingKeygen = false;
for (Keygen keygen : wifiNetwork.getKeygens()) {
if (keygen == null) {
foundMissingKeygen = true;
break;
}
}
if (foundMissingKeygen){
//If any is missing, simply replace them all.
ZipInputStream zipInputStream = new ZipInputStream(
getActivity().getResources().openRawResource(R.raw.magic_info));
wifiNetwork.setKeygens(zipInputStream);
try {
zipInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

@Override
Expand Down Expand Up @@ -288,7 +319,7 @@ public void onItemClick(AdapterView<?> parent, View view,
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
});
list.setAdapter(new ArrayAdapter<String>(getActivity(),
list.setAdapter(new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1, passwordList));
root.showNext();
}
Expand Down Expand Up @@ -360,7 +391,7 @@ public void cancel() {

@Override
protected List<String> doInBackground(Void... params) {
final List<String> result = new ArrayList<String>();
final List<String> result = new ArrayList<>();
for (Keygen keygen : wifiNetwork.getKeygens()) {
if (keygen instanceof ThomsonKeygen) {
getPrefs();
Expand Down

0 comments on commit ca562ef

Please sign in to comment.