From b072705f962f3d223fb7f8984e69a5289f7f1b3c Mon Sep 17 00:00:00 2001 From: Bjørn Mork Date: Tue, 4 Jun 2019 13:14:54 +0200 Subject: fixing crash on null, which was the the activity was destroyed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Bjørn Mork --- Defogger/AndroidManifest.xml | 35 +++++++++-------- Defogger/res/values/strings.xml | 3 ++ .../android/defogger/ConfigureNetworkActivity.java | 32 +++++++++------ .../no/mork/android/defogger/IpCamActivity.java | 45 +++++++--------------- .../src/no/mork/android/defogger/MainActivity.java | 25 ++++++++---- Defogger/src/no/mork/android/defogger/Util.java | 10 +++-- 6 files changed, 79 insertions(+), 71 deletions(-) diff --git a/Defogger/AndroidManifest.xml b/Defogger/AndroidManifest.xml index d977486..c5a53bd 100644 --- a/Defogger/AndroidManifest.xml +++ b/Defogger/AndroidManifest.xml @@ -17,22 +17,23 @@ - - - - - - - - - + + + + + + + + + diff --git a/Defogger/res/values/strings.xml b/Defogger/res/values/strings.xml index 88502ab..27ee25e 100644 --- a/Defogger/res/values/strings.xml +++ b/Defogger/res/values/strings.xml @@ -17,6 +17,9 @@ Enable unsigned firmware (permanent) Run command + Read D-Link Camera QR Code + + Bluetooth Client diff --git a/Defogger/src/no/mork/android/defogger/ConfigureNetworkActivity.java b/Defogger/src/no/mork/android/defogger/ConfigureNetworkActivity.java index 7613fa9..38a8fec 100644 --- a/Defogger/src/no/mork/android/defogger/ConfigureNetworkActivity.java +++ b/Defogger/src/no/mork/android/defogger/ConfigureNetworkActivity.java @@ -34,6 +34,7 @@ public class ConfigureNetworkActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { + Log.d(msg, "onCreate()"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_configurenetwork); @@ -74,18 +75,15 @@ public class ConfigureNetworkActivity extends Activity { ListView listView = (ListView) findViewById(R.id.networks); listView.setAdapter(networklist); - intent.putExtra("netconf", ""); - setResult(RESULT_CANCELED, intent); } public void returnConfigResult(String config) { - Log.d(msg, "returnConfigResult()"); + Log.d(msg, "returnConfigResult(): " + config); Intent intent = new Intent(); if (config != null) { intent.putExtra("netconf", config); setResult(RESULT_OK, intent); } else { - intent.putExtra("netconf", ""); setResult(RESULT_CANCELED, intent); } finish(); @@ -98,21 +96,31 @@ public class ConfigureNetworkActivity extends Activity { if (selected == null) returnConfigResult(null); - Map kv = Util.splitKV(selected, "."); + Log.d(msg, "selected is " + selected); + + Map kv = Util.splitKV(selected, ","); EditText edit = (EditText) findViewById(R.id.password); String password = edit.getText().toString(); - String ret = null; - String ssid = kv.get("I"); // FIXME: allow entering SSID in edit field + + edit = (EditText) findViewById(R.id.ssid); + String ssid = kv.containsKey("I") ? kv.get("I") : edit.getText().toString(); + + /* returning empty ssid is not allowed */ + if (ssid == null || ssid.length() == 0) + returnConfigResult(null); + if (password == null) password = ""; - /* assume open network if password is empty? */ - if (password.length() == 0 && (!kv.get("S").equals("0") || !kv.get("E").equals("0"))) + /* set defaults, assuming open network if password is empty and ssid was not found */ + int S = kv.containsKey("S") ? Integer.parseInt(kv.get("S")) : password.length() > 0 ? 4 : 0; + int E = kv.containsKey("E") ? Integer.parseInt(kv.get("E")) : password.length() > 0 ? 2 : 0; + + /* password is required unless open network */ + if (password.length() == 0 && (S != 0 || E !=0)) returnConfigResult(null); else - ret = "M=0;I=" + ssid + ";S=" + kv.get("S") + ";E=" + kv.get("E") + ";K=" + password; - - returnConfigResult(ret); + returnConfigResult("M=0;I=" + ssid + ";S=" + S + ";E=" + E + ";K=" + password); } private class NetAdapter extends ArrayAdapter { diff --git a/Defogger/src/no/mork/android/defogger/IpCamActivity.java b/Defogger/src/no/mork/android/defogger/IpCamActivity.java index 3b0c774..1e2f251 100644 --- a/Defogger/src/no/mork/android/defogger/IpCamActivity.java +++ b/Defogger/src/no/mork/android/defogger/IpCamActivity.java @@ -69,6 +69,7 @@ public class IpCamActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { + Log.d(msg, "onCreate()"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_ipcam); @@ -76,9 +77,11 @@ public class IpCamActivity extends Activity { Intent intent = getIntent(); pincode = intent.getStringExtra("pincode"); Bundle b = intent.getExtras(); - if (b != null) + if (b == null) // when can this happen? Answer: If we crashed and are restarted by the system.... + Log.e(msg, "Caller failed to provide us with a Bluetooth device - all actions will fail (pincode is " + pincode + ")"); + else device = b.getParcelable("btdevice"); - + EditText cmd = (EditText) findViewById(R.id.command); cmd.setOnEditorActionListener(new OnEditorActionListener() { @Override @@ -100,25 +103,6 @@ public class IpCamActivity extends Activity { connectDevice(device); } - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - Log.d(msg, "onSaveInstanceState()"); - outState.putParcelable("btdevice", device); - outState.putString("pincode", pincode); - } - - @Override - public void onRestoreInstanceState(Bundle savedInstanceState) { - // Always call the superclass so it can restore the view hierarchy - super.onRestoreInstanceState(savedInstanceState); - Log.d(msg, "onRestoreInstanceState()"); - - // Restore state members from saved instance - device = savedInstanceState.getParcelable("btdevice"); - pincode = savedInstanceState.getString("pincode"); - } - @Override protected void onActivityResult(int req, int res, Intent intent) { super.onActivityResult(req, res, intent); @@ -214,6 +198,9 @@ public class IpCamActivity extends Activity { break; case 0xa103: // wifilink wifilink = kv.get("S").equals("1"); + // refresh IP config if link changed to up + if (wifilink) + readChar(0xa104); break; case 0xa104: // ipconfig displayIpConfig(kv); @@ -333,7 +320,7 @@ public class IpCamActivity extends Activity { } private void connectDevice(BluetoothDevice device) { - if (device == null) + if (device == null || connected) return; Log.d(msg, "connectDevice() " + device.getAddress()); @@ -366,15 +353,14 @@ public class IpCamActivity extends Activity { if (mGatt != null) mGatt.close(); - Log.d(msg, "disconnectDevice() " + device.getAddress() + " with reason: " + reason); - finish(); + Log.d(msg, "disconnectDevice() with reason: " + reason); + finish(); } private void getCurrentConfig() { View v = new View(this); getWifiLink(v); getWifiConfig(v); - getIpConfig(v); getSysInfo(v); doWifiScan(v); } @@ -484,10 +470,6 @@ public class IpCamActivity extends Activity { readChar(0xa103); } - public void getIpConfig(View view) { - readChar(0xa104); - } - public void getSysInfo(View view) { readChar(0xa200); } @@ -533,9 +515,10 @@ public class IpCamActivity extends Activity { return; } + Log.d(msg, "Would configure: " + netconf); /* OK, go */ - writeChar(0xa101, netconf); // configure wifi - writeChar(0xa102, "C=1"); // connect + // writeChar(0xa101, netconf); // configure wifi + // writeChar(0xa102, "C=1"); // connect /* refresh current settings for display */ getCurrentConfig(); diff --git a/Defogger/src/no/mork/android/defogger/MainActivity.java b/Defogger/src/no/mork/android/defogger/MainActivity.java index d90413a..0aa6379 100644 --- a/Defogger/src/no/mork/android/defogger/MainActivity.java +++ b/Defogger/src/no/mork/android/defogger/MainActivity.java @@ -31,6 +31,8 @@ public class MainActivity extends Activity { private static final int REQUEST_GET_DEVICE = 2; private BluetoothAdapter bluetoothAdapter; + private BluetoothDevice device; + private String pincode; @Override protected void onCreate(Bundle savedInstanceState) { @@ -49,6 +51,9 @@ public class MainActivity extends Activity { protected void onActivityResult(int requestCode, int resultCode, Intent dataIntent) { super.onActivityResult(requestCode, resultCode, dataIntent); + Log.d(msg, "onActivityResult() requestCode=" + requestCode); + Log.d(msg, "Intent is " + dataIntent); + switch (requestCode) { case IntentIntegrator.REQUEST_CODE: IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, dataIntent); @@ -68,19 +73,22 @@ public class MainActivity extends Activity { break; } - BluetoothDevice dev = dataIntent.getExtras().getParcelable("btdevice"); - if (dev == null) { + device = dataIntent.getExtras().getParcelable("btdevice"); + if (device == null) { + pincode = null; setStatus("No camera selected"); break; } String pincode = dataIntent.getStringExtra("pincode"); if (pincode == null || pincode.length() < 6) { + pincode = null; + device = null; setStatus("Bogus pincode"); break; } - startIpCamActivity(dev, pincode); + startIpCamActivity(); break; default: Log.d(msg, "unknown request???"); @@ -108,8 +116,9 @@ public class MainActivity extends Activity { } } - public void startIpCamActivity(BluetoothDevice device, String pincode) { + public void startIpCamActivity() { Intent intent = new Intent(this, IpCamActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.putExtra("pincode", pincode); intent.putExtra("btdevice", device); startActivity(intent); @@ -122,7 +131,8 @@ public class MainActivity extends Activity { public void startQRReaderActivity(View view) { IntentIntegrator integrator = new IntentIntegrator(this); - integrator.initiateScan(); + integrator.setTitleByID(R.values.qrtitle); + integrator.initiateScan(IntentIntegrator.QR_CODE_TYPES); } private void handleQRScanResult(IntentResult res) { @@ -151,8 +161,9 @@ public class MainActivity extends Activity { Log.d(msg, "Got invalid MAC address from QR scan:" + mac.toString()); return; } - BluetoothDevice dev = bluetoothAdapter.getRemoteDevice(mac.toString()); - startIpCamActivity(dev, data[5]); + device = bluetoothAdapter.getRemoteDevice(mac.toString()); + pincode = data[5]; + startIpCamActivity(); } diff --git a/Defogger/src/no/mork/android/defogger/Util.java b/Defogger/src/no/mork/android/defogger/Util.java index 1492843..6bbfd99 100644 --- a/Defogger/src/no/mork/android/defogger/Util.java +++ b/Defogger/src/no/mork/android/defogger/Util.java @@ -21,11 +21,13 @@ public class Util { public static Map splitKV(String kv, String splitter) { Map ret = new HashMap(); + + if (kv != null) + for (String s : kv.split(splitter)) { + String[] foo = s.split("="); + ret.put(foo[0], foo[1]); + } - for (String s : kv.split(splitter)) { - String[] foo = s.split("="); - ret.put(foo[0], foo[1]); - } return ret; } -- cgit v1.2.3