Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Upload complete broadcast #686

Merged
merged 6 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -846,8 +846,8 @@ private void setRunDistUI(final View view, final SharedPreferences prefs) {
public void makeUploadDialog(final MainActivity main) {
final SharedPreferences prefs = main.getSharedPreferences( PreferenceKeys.SHARED_PREFS, 0 );
final boolean beAnonymous = prefs.getBoolean(PreferenceKeys.PREF_BE_ANONYMOUS, false);
final String username = beAnonymous? "anonymous":
prefs.getString( PreferenceKeys.PREF_USERNAME, "anonymous" );
final String username = beAnonymous? ANONYMOUS:
prefs.getString( PreferenceKeys.PREF_USERNAME, ANONYMOUS );

final String text = getString(R.string.list_upload) + "\n" + getString(R.string.username) + ": " + username;
final FragmentActivity a = getActivity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public static class State {
int uiMode;
AtomicBoolean uiRestart;
AtomicBoolean ttsNag = new AtomicBoolean(true);
WiGLEApiManager apiManager;
public WiGLEApiManager apiManager;
Map<Integer, String> btVendors = Collections.emptyMap();
Map<Integer, String> btMfgrIds = Collections.emptyMap();
}
Expand Down Expand Up @@ -2326,7 +2326,10 @@ public void finish() {
stopService(serviceIntent);
try {
// have to use the app context to bind to the service, cuz we're in tabs
getApplicationContext().unbindService(state.serviceConnection);
final Context c = getApplicationContext();
if (null != c) {
c.unbindService(state.serviceConnection);
}
} catch (final IllegalArgumentException ex) {
Logging.info("serviceConnection not registered: " + ex, ex);
}
Expand Down Expand Up @@ -2388,14 +2391,6 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
return super.onKeyDown(keyCode, event);
}

public void doUpload() {
selectFragment(R.id.nav_list);
ListFragment listFragment = getListFragmentIfCurrent();
if (null != listFragment) {
listFragment.makeUploadDialog(this);
}
}

/**
* pure-background upload method for intent-based uploads
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public final class WigleService extends Service {
private final IBinder wigleServiceBinder = new WigleServiceBinder(this);
private final NumberFormat countFormat = NumberFormat.getIntegerInstance();

public static final String UPLOAD_COMPLETE_INTENT = "net.wigle.wigleandroid.UPLOAD_COMPLETE";
public static final String UPLOAD_FAILED_INTENT = "net.wigle.wigleandroid.UPLOAD_FAILED";
public static final String UPLOAD_INTENT = "net.wigle.wigleandroid.UPLOAD";
public static final String PAUSE_INTENT = "net.wigle.wigleandroid.PAUSE";
public static final String SCAN_INTENT = "net.wigle.wigleandroid.SCAN";

private class GuardThread extends Thread {
GuardThread() {
}
Expand Down Expand Up @@ -237,7 +243,7 @@ public void setupNotification() {
}

final Intent pauseSharedIntent = new Intent();
pauseSharedIntent.setAction("net.wigle.wigleandroid.PAUSE");
pauseSharedIntent.setAction(PAUSE_INTENT);
pauseSharedIntent.setClass(getApplicationContext(), net.wigle.wigleandroid.listener.ScanControlReceiver.class);

final MainActivity ma = MainActivity.getMainActivity();
Expand All @@ -246,12 +252,12 @@ public void setupNotification() {
if (null != ma) {
final PendingIntent pauseIntent = PendingIntent.getBroadcast(MainActivity.getMainActivity(), 0, pauseSharedIntent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_CANCEL_CURRENT);
final Intent scanSharedIntent = new Intent();
scanSharedIntent.setAction("net.wigle.wigleandroid.SCAN");
scanSharedIntent.setAction(SCAN_INTENT);
scanSharedIntent.setClass(getApplicationContext(), net.wigle.wigleandroid.listener.ScanControlReceiver.class);
final PendingIntent scanIntent = PendingIntent.getBroadcast(MainActivity.getMainActivity(), 0, scanSharedIntent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_CANCEL_CURRENT);

final Intent uploadSharedIntent = new Intent();
uploadSharedIntent.setAction("net.wigle.wigleandroid.UPLOAD");
uploadSharedIntent.setAction(UPLOAD_INTENT);
uploadSharedIntent.setClass(getApplicationContext(), net.wigle.wigleandroid.listener.UploadReceiver.class);
final PendingIntent uploadIntent = PendingIntent.getBroadcast(MainActivity.getMainActivity(), 0, uploadSharedIntent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_CANCEL_CURRENT);
if (SDK_INT >= 31) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ public final void run() {
} catch ( InterruptedException ex ) {
Logging.info( name + " interrupted: " + ex );
} catch ( final WiGLEAuthException waex) {
//DEBUG: MainActivity.error("auth error", waex);
//DEBUG: Logging.error("auth error", waex);
Bundle errorBundle = new Bundle();
errorBundle.putCharSequence("AUTH_ERROR", waex.getMessage());
sendBundledMessage(BackgroundGuiHandler.AUTHENTICATION_ERROR, errorBundle);
sendBundledMessage(Status.BAD_LOGIN.ordinal(), errorBundle);
} catch ( final IOException ioex) {
Logging.error("connection error", ioex);
Bundle errorBundle = new Bundle();
errorBundle.putString(BackgroundGuiHandler.ERROR, "IOException");
errorBundle.putCharSequence("CONN_ERROR", ioex.getMessage());
sendBundledMessage(BackgroundGuiHandler.CONNECTION_ERROR, errorBundle);
sendBundledMessage(Status.CONNECTION_ERROR.ordinal(), errorBundle);
} catch ( final Exception ex ) {
dbHelper.deathDialog(name, ex);
} finally {
Expand Down Expand Up @@ -187,13 +187,16 @@ public final void setContext( final FragmentActivity context ) {
handler.setContext(context);
}

/**
* make sure that we have an auth name and API token
* @return true if both are present, otherwise false.
*/
protected final boolean validAuth() {
final SharedPreferences prefs = context.getSharedPreferences( PreferenceKeys.SHARED_PREFS, 0);
if ( (!prefs.getString(PreferenceKeys.PREF_AUTHNAME,"").isEmpty()) && (TokenAccess.hasApiToken(prefs))) {
return true;
}
return false;

}


Expand All @@ -216,16 +219,6 @@ protected final String getPassword() {
return password;
}

protected final String getToken() {
final SharedPreferences prefs = context.getSharedPreferences( PreferenceKeys.SHARED_PREFS, 0);
String token = TokenAccess.getApiToken(prefs);

if ( prefs.getBoolean( PreferenceKeys.PREF_BE_ANONYMOUS, false) ) {
token = "";
}
return token;
}

/**
* @return null if ok, else an error status
*/
Expand All @@ -235,11 +228,10 @@ protected final Status validateUserPass(final String username, final String pass
Logging.error( "username not defined" );
status = Status.BAD_USERNAME;
}
else if ( "".equals( password ) && ! ListFragment.ANONYMOUS.equals( username.toLowerCase(Locale.US) ) ) {
if ( "".equals( password ) && ! ListFragment.ANONYMOUS.equals( username.toLowerCase(Locale.US) ) ) {
Logging.error( "password not defined and username isn't 'anonymous'" );
status = Status.BAD_PASSWORD;
}

return status;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@

public class BackgroundGuiHandler extends Handler {
public static final int WRITING_PERCENT_START = 100000;
public static final int AUTHENTICATION_ERROR = 1;
public static final int CONNECTION_ERROR = -1;
public static final String ERROR = "error";
public static final String FILENAME = "filename";
public static final String FILEPATH = "filepath";
public static final String TRANSIDS = "transIds";

private FragmentActivity context;
private final Object lock;
Expand All @@ -63,13 +62,13 @@ public void setContext(final FragmentActivity context) {
@Override
public void handleMessage( final Message msg ) {
synchronized ( lock ) {
if (msg.what == AUTHENTICATION_ERROR) {
if (msg.what == Status.BAD_LOGIN.ordinal()) {
WiGLEToast.showOverActivity(this.context, R.string.error_general, context.getString(R.string.status_login_fail));
if (pp != null) {
pp.hide();
}
}
if (msg.what == CONNECTION_ERROR) {
if (msg.what == Status.CONNECTION_ERROR.ordinal()) {
WiGLEToast.showOverActivity(this.context, R.string.error_general, context.getString(R.string.no_wigle_conn));
if (pp != null) {
pp.hide();
Expand Down Expand Up @@ -141,7 +140,7 @@ public void handleMessage( final Message msg ) {
WiGLEToast.showOverFragment(context, status.getTitle(),
composeDisplayMessage(context, msg.peekData().getString(ERROR),
msg.peekData().getString(FILEPATH), msg.peekData().getString(FILENAME),
status.getMessage()));
status.getMessage(), msg.peekData().getString(TRANSIDS)));
} else if (Status.WRITE_SUCCESS.equals(status)) {
final String fileName = msg.peekData().getString(FILENAME);
if (null != fileName) {
Expand Down Expand Up @@ -213,14 +212,33 @@ public void handleMessage( final Message msg ) {
}
} else {
//Other file types get a default dialog
Logging.error("file ending success error");
showError(fm, msg, status);
}
} else {
//Null filename - weird case
Logging.error("null filename error");
showError(fm, msg, status);
}
} else {
showError(fm, msg, status);
boolean settingsForward = false;
if (msg.what == Status.BAD_USERNAME.ordinal()) {
WiGLEToast.showOverActivity(this.context, R.string.error_general, context.getString(R.string.status_no_user));
settingsForward = true;
} else if (msg.what == Status.BAD_PASSWORD.ordinal()) {
WiGLEToast.showOverActivity(this.context, R.string.error_general, context.getString(R.string.status_no_pass));
settingsForward = true;
} else {
showError(fm, msg, status);
}
//ALIBI: on first-launch, this is a little weird, since user just confirmed "anonymous" - but we don't set the pref for them when they click "ok" - we forward them
if (settingsForward) {
try {
MainActivity.getMainActivity().selectFragment(R.id.nav_settings);
} catch (Exception ex) {
Logging.info("failed to start settings fragment: " + ex, ex);
}
}
}
}
}
Expand All @@ -236,8 +254,20 @@ private void showError(final FragmentManager fm, final Message msg, final Status
Logging.warn("illegal state in background gui handler: ", ex);
}
}

/**
* Compose a file-specific string (for use in a dialog) including optional error, file name/path, and transId list (for uploads)
* TODO: this does a poor job of i18n, the "File location:" and "Transaction IDs:" messages should be delegated to local String files
* @param context the context for string i18n
* @param error the error string
* @param filepath the path of the file
* @param filename the name of the file
* @param messageId the R.string.id of the message to prepend to error
* @param transIds the list of transIds for transaction Ids
* @return a composited string
*/
public static String composeDisplayMessage(Context context, String error, String filepath,
String filename, final int messageId) {
String filename, final int messageId, final String transIds) {
if ( filename != null ) {
// just don't show the gz
int index = filename.indexOf( ".gz" );
Expand All @@ -253,11 +283,15 @@ public static String composeDisplayMessage(Context context, String error, String
filename = "";
} else {
final String showPath = filepath == null ? "" : filepath;
filename = "\n\nFile location:\n" + showPath + filename;
filename = "File location:\n" + showPath + filename;
}
String transIdString = "";
if (transIds != null && !transIds.isEmpty()) {
transIdString = String.format("\n\nTransaction ID(s):\n"+transIds);
}
error = error == null ? "" : " Error: " + error;
error = (error == null || error.isEmpty()) ? "" : error+"\n\n";

return context.getString(messageId) + error + filename;
return error + filename + transIdString;
}

public static class BackgroundAlertDialog extends DialogFragment {
Expand Down Expand Up @@ -287,7 +321,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {

builder.setMessage( composeDisplayMessage(activity, (null != bundle) ? bundle.getString( ERROR ):null,
(null != bundle) ? bundle.getString( FILEPATH ):null, (null != bundle) ? bundle.getString( FILENAME ):null,
message ));
message, (null != bundle)?bundle.getString( TRANSIDS) : null));

AlertDialog ad = builder.create();
ad.setButton( DialogInterface.BUTTON_POSITIVE, "OK", (dialog, which) -> {
Expand Down
Loading
Loading