Commit b2ae1e07 authored by kirk's avatar kirk

refactor multi-threading implementation to use a single Handler in place of...

refactor multi-threading implementation to use a single Handler in place of multiple AsyncTask instances to address backwards compatibility issues in older versions of Android
parent 6bd668ba
......@@ -15,10 +15,6 @@
*/
package us.rader.wyfy;
import us.rader.wyfy.db.WiFiSettingsContract;
import us.rader.wyfy.model.WifiSettings;
import us.rader.wyfy.model.WifiSettings.ConnectionOutcome;
import us.rader.wyfy.nfc.NdefReaderActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
......@@ -42,6 +38,11 @@ import android.view.MenuItem;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import us.rader.wyfy.db.WiFiSettingsContract;
import us.rader.wyfy.model.WifiSettings;
import us.rader.wyfy.model.WifiSettings.ConnectionOutcome;
import us.rader.wyfy.nfc.NdefReaderActivity;
/**
* Launcher {@link Activity} for <code>WyFy</code> app
*
......@@ -389,7 +390,7 @@ public final class MainActivity extends FragmentActivity implements
super.onCreate(savedInstanceState);
wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
setContentView(R.layout.main);
setFragments(savedInstanceState);
setFragments();
if (savedInstanceState == null) {
......@@ -524,7 +525,11 @@ public final class MainActivity extends FragmentActivity implements
if (wifiSettings.parse(uri)) {
wifiSettingsFragment.onModelChanged(true);
if (wifiSettingsFragment != null) {
wifiSettingsFragment.onModelChanged(true);
}
} else {
......@@ -749,20 +754,11 @@ public final class MainActivity extends FragmentActivity implements
/**
* Initialize the UI {@link Fragment} instances according to the current
* screen layout
*
* @param savedInstanceState
* saved state or <code>null</code>
*/
private void setFragments(Bundle savedInstanceState) {
private void setFragments() {
if (findViewById(R.id.single_fragment) != null) {
if (savedInstanceState != null) {
return;
}
setSinglePane();
} else if (findViewById(R.id.two_fragments_vertical) != null) {
......
......@@ -15,11 +15,9 @@
*/
package us.rader.wyfy;
import us.rader.wyfy.db.WifiSettingsDatabaseHelper;
import us.rader.wyfy.model.WifiSettings;
import us.rader.wyfy.model.WifiSettings.Security;
import android.app.Activity;
import android.os.AsyncTask;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
......@@ -33,6 +31,11 @@ import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import us.rader.wyfy.db.QueryHandler;
import us.rader.wyfy.db.WifiSettingsDatabaseHelper;
import us.rader.wyfy.model.WifiSettings;
import us.rader.wyfy.model.WifiSettings.Security;
/**
* {@link Fragment} for the WIFI wifiSettings UI
*
......@@ -96,55 +99,59 @@ public final class WifiSettingsFragment extends Fragment {
}
/**
* Invoke {@link WifiSettingsDatabaseHelper#lookupPassword()} in a worker
* thread, update the UI in the main thread
* Invoke
* {@link WifiSettingsDatabaseHelper#lookupPassword(android.database.sqlite.SQLiteDatabase, String)}
* in a worker thread, update the UI in the main thread
*/
private class LookupPasswordTask extends AsyncTask<Void, Void, String> {
private final class LookupPasswordListener implements
QueryHandler.QueryListener {
/**
* Invoke {@link WifiSettingsDatabaseHelper#lookupPassword()} in a
* worker thread
* Update the UI to reflect the result of a database query
*
* @param params
* ignored
* @param ssid
* the SSID
*
* @return value returned by
* {@link WifiSettingsDatabaseHelper#lookupPassword()}
* @param password
* the password
*
* @see android.os.AsyncTask#doInBackground(Void...)
* @see us.rader.wyfy.db.QueryHandler.QueryListener#onPasswordResult(java.lang.String,
* java.lang.String)
*/
@Override
protected String doInBackground(Void... params) {
public void onPasswordResult(final String ssid, final String password) {
if (dbHelper == null) {
getActivity().runOnUiThread(new Runnable() {
return null;
@Override
public void run() {
}
if (password != null) {
wifiSettings.setPassword(password);
}
onModelChanged(false);
return dbHelper.lookupPassword();
}
});
}
/**
* Update the UI
* Nothing to do for this class
*
* @param result
* value returned by
* {@link WifiSettingsDatabaseHelper#lookupPassword()}
* @param cursor
* ignored
*
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
* @see us.rader.wyfy.db.QueryHandler.QueryListener#onQueryPerformed(android.database.Cursor)
*/
@Override
protected void onPostExecute(String result) {
if (result != null) {
public void onQueryPerformed(Cursor cursor) {
wifiSettings.setPassword(result);
}
onModelChanged(false);
// nothing to do in this class
}
......@@ -248,38 +255,6 @@ public final class WifiSettingsFragment extends Fragment {
}
/**
* Invoke {@link WifiSettingsDatabaseHelper#storeWifiSettings()} in a worker
* thread
*/
private class StoreWifiSettingsTask extends AsyncTask<Void, Void, Void> {
/**
* Invoke {@link WifiSettingsDatabaseHelper#storeWifiSettings()} in a
* worker thread
*
* @param params
* ignored
*
* @return <code>null</code>
*
* @see android.os.AsyncTask#doInBackground(Void...)
*/
@Override
protected Void doInBackground(Void... params) {
if (dbHelper != null) {
dbHelper.storeWifiSettings();
}
return null;
}
}
/**
* {@link TextWatcher} used to track changes to text controls in this
* fragment's layout
......@@ -381,11 +356,6 @@ public final class WifiSettingsFragment extends Fragment {
}
/**
* {@link WifiSettingsDatabaseHelper}
*/
private WifiSettingsDatabaseHelper dbHelper;
/**
* {@link CheckBox} for a wifi access point with a SSID that isn't broadcast
*/
......@@ -397,11 +367,6 @@ public final class WifiSettingsFragment extends Fragment {
*/
private OnWifiSettingsChangedListener listener;
/**
* {@link LookupPasswordTask}
*/
private LookupPasswordTask lookupPasswordTask;
/**
* {@link EditText} for a wifi access point password string or WEP key
*/
......@@ -417,15 +382,6 @@ public final class WifiSettingsFragment extends Fragment {
*/
private EditText ssidText;
/**
* Initialize {@link #lookupPasswordTask}
*/
public WifiSettingsFragment() {
lookupPasswordTask = new LookupPasswordTask();
}
/**
* Set the {@link #listener}
*
......@@ -439,7 +395,6 @@ public final class WifiSettingsFragment extends Fragment {
super.onAttach(activity);
this.listener = (OnWifiSettingsChangedListener) activity;
dbHelper = new WifiSettingsDatabaseHelper(activity);
}
......@@ -515,7 +470,6 @@ public final class WifiSettingsFragment extends Fragment {
super.onDetach();
this.listener = null;
dbHelper = null;
}
......@@ -528,7 +482,11 @@ public final class WifiSettingsFragment extends Fragment {
*/
public void onInitializedFromActiveConnection() {
lookupPasswordTask.execute();
QueryHandler handler = QueryHandler.getInstance(getActivity());
WifiSettingsDatabaseHelper helper = handler.getHelper();
SQLiteDatabase db = helper.getWritableDatabase();
handler.lookupPassword(db, new LookupPasswordListener(),
wifiSettings.getSsid());
}
......@@ -539,8 +497,6 @@ public final class WifiSettingsFragment extends Fragment {
* @param updateDatabase
* also store the new settings in the database if and only if
* <code>updateDatabase</code> is <code>true</code>
*
* @see StoreWifiSettingsTask
*/
public void onModelChanged(boolean updateDatabase) {
......@@ -569,8 +525,7 @@ public final class WifiSettingsFragment extends Fragment {
if (updateDatabase) {
new StoreWifiSettingsTask().execute();
storeWifiSettings();
}
}
......@@ -622,7 +577,7 @@ public final class WifiSettingsFragment extends Fragment {
if (updateDatabase) {
new StoreWifiSettingsTask().execute();
storeWifiSettings();
}
......@@ -639,4 +594,18 @@ public final class WifiSettingsFragment extends Fragment {
}
}
/**
* Invoke
* {@link WifiSettingsDatabaseHelper#storeWifiSettings(SQLiteDatabase)}
* asynchronousy
*/
private void storeWifiSettings() {
QueryHandler handler = QueryHandler.getInstance(getActivity());
WifiSettingsDatabaseHelper helper = handler.getHelper();
SQLiteDatabase db = helper.getWritableDatabase();
handler.storeWifiSettings(db);
}
}
......@@ -15,8 +15,6 @@
*/
package us.rader.wyfy.db;
import us.rader.wyfy.db.WiFiSettingsContract.WifiSettingsEntry;
import us.rader.wyfy.model.WifiSettings;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
......@@ -24,6 +22,9 @@ import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import us.rader.wyfy.db.WiFiSettingsContract.WifiSettingsEntry;
import us.rader.wyfy.model.WifiSettings;
/**
* {@link SQLiteOpenHelper} for the WyFy datbase
*
......@@ -34,6 +35,12 @@ import android.provider.BaseColumns;
*/
public final class WifiSettingsDatabaseHelper extends SQLiteOpenHelper {
/**
* Database selection string to match by SSID
*/
public static final String SELECT_BY_SSID = WiFiSettingsContract.WifiSettingsEntry.COLUMN_NAME_SSID
+ " LIKE ?"; //$NON-NLS-1$
/**
* Database name
*/
......@@ -84,6 +91,9 @@ public final class WifiSettingsDatabaseHelper extends SQLiteOpenHelper {
/**
* Delete any row(s) matching the given criteria
*
* @param db
* {@link SQLiteDatabase}
*
* @param selection
* the selection string
*
......@@ -91,59 +101,46 @@ public final class WifiSettingsDatabaseHelper extends SQLiteOpenHelper {
* string with which to replace '?' variables in
* <code>selection</code>
*/
public void delete(String selection, String... selectionArgs) {
SQLiteDatabase db = getWritableDatabase();
try {
db.delete(WiFiSettingsContract.WifiSettingsEntry.TABLE_NAME,
selection, selectionArgs);
} finally {
public void delete(SQLiteDatabase db, String selection,
String... selectionArgs) {
db.close();
db.delete(WiFiSettingsContract.WifiSettingsEntry.TABLE_NAME, selection,
selectionArgs);
}
}
/**
* Return the password stored in the database for the current value of the
* {@link WifiSettings} singleton's SSID
* Return the password stored in the database for the given SSID
*
* @param db
* {@link SQLiteDatabase}
*
* @param ssid
* SSID
*
* @return password or <code>null</code> to indicate that no entry has yet
* been created for the current SSID
* been created for the given SSID
*/
public String lookupPassword() {
public String lookupPassword(SQLiteDatabase db, String ssid) {
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = query(db, SELECT_BY_SSID, ssid);
try {
Cursor cursor = query(db, null);
try {
if (!cursor.moveToNext()) {
return null;
}
if (!cursor.moveToNext()) {
int index = cursor
.getColumnIndex(WiFiSettingsContract.WifiSettingsEntry.COLUMN_NAME_PASSWORD);
String password = cursor.getString(index);
return password;
} finally {
cursor.close();
return null;
}
int index = cursor
.getColumnIndex(WiFiSettingsContract.WifiSettingsEntry.COLUMN_NAME_PASSWORD);
String password = cursor.getString(index);
return password;
} finally {
db.close();
cursor.close();
}
}
......@@ -225,7 +222,7 @@ public final class WifiSettingsDatabaseHelper extends SQLiteOpenHelper {
*
* This will return an empty {@link Cursor} if no matching row has yet been
* added to the database. If this ever returns a {@link Cursor} with more
* than one entry, then some data corruption has occured due to a bug
* than one entry, then some data corruption has occurred due to a bug
* somewhere in the app
*
* @param db
......@@ -257,10 +254,11 @@ public final class WifiSettingsDatabaseHelper extends SQLiteOpenHelper {
/**
* Update the existing row or insert a new row for the current state of the
* {@link WifiSettings} singleton
*
* @param db
* {@link SQLiteDatabase}
*/
public void storeWifiSettings() {
SQLiteDatabase db = getWritableDatabase();
public void storeWifiSettings(SQLiteDatabase db) {
try {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment