Skip to content

Commit

Permalink
initial testing. numerous quirks, i18n, and app-local storage still n…
Browse files Browse the repository at this point in the history
…eed fixing
  • Loading branch information
rksh committed Nov 2, 2017
1 parent a3ec2b6 commit 7822028
Show file tree
Hide file tree
Showing 11 changed files with 289 additions and 204 deletions.
9 changes: 9 additions & 0 deletions wiglewifiwardriving/src/main/AndroidManifest.xml
Expand Up @@ -207,6 +207,15 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<provider
android:name=".util.KmlFileProvider"
android:authorities="${applicationId}.kmlprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
<receiver android:name=".listener.TerminationReceiver" >
<intent-filter>
<action android:name="net.wigle.wigleandroid.END" />
Expand Down
Expand Up @@ -3,7 +3,6 @@
import java.util.List;

import net.wigle.wigleandroid.background.ApiListener;
import net.wigle.wigleandroid.background.LegacyObservationImporter;
import net.wigle.wigleandroid.background.ObservationImporter;
import net.wigle.wigleandroid.background.ObservationUploader;
import net.wigle.wigleandroid.background.TransferListener;
Expand Down Expand Up @@ -39,7 +38,6 @@
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

Expand Down Expand Up @@ -290,28 +288,6 @@ public void requestComplete(JSONObject object, boolean cached) {
} catch (WiGLEAuthException waex) {
//moot due to bundle handling
}
} else {
//TODO: is this dead now?
final LegacyObservationImporter task = new LegacyObservationImporter(getActivity(),
ListFragment.lameStatic.dbHelper,
new ApiListener() {
@Override
public void requestComplete(JSONObject object, boolean cached) {
if (mainActivity != null) {
try {
mainActivity.getState().dbHelper.getNetworkCountFromDB();
} catch (DBException dbe) {
MainActivity.warn("failed DB count update on import-observations", dbe);
}
mainActivity.transferComplete();
}
}
});
try {
task.startDownload(this);
} catch (WiGLEAuthException waex) {
//moot due to bundle handling
}
}
}

Expand Down
Expand Up @@ -146,6 +146,7 @@ public static class State {
public static final String USER_STATS_URL = "https://api.wigle.net/api/v2/stats/user";
public static final String OBSERVED_URL = "https://api.wigle.net/api/v2/network/mine";
public static final String FILE_POST_URL = "https://api.wigle.net/api/v2/file/upload";
public static final String KML_TRANSID_URL_STEM = "https://api.wigle.net/api/v2/file/kml/";

private static final String LOG_TAG = "wigle";
public static final String ENCODING = "ISO-8859-1";
Expand Down
Expand Up @@ -188,7 +188,7 @@ public void requestComplete(final JSONObject json, final boolean isCache) {
private void setupListView(final View view) {
final SharedPreferences prefs = getActivity().getSharedPreferences(ListFragment.SHARED_PREFS, 0);
if (listAdapter == null) {
listAdapter = new UploadsListAdapter(getActivity().getApplicationContext(), R.layout.uploadrow);
listAdapter = new UploadsListAdapter(getActivity().getApplicationContext(), R.layout.uploadrow, prefs, this);
} else if (!listAdapter.isEmpty() && !TokenAccess.hasApiToken(prefs)) {
listAdapter.clear();
}
Expand Down
Expand Up @@ -2,78 +2,163 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.content.FileProvider;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import net.wigle.wigleandroid.model.RankUser;
import net.wigle.wigleandroid.background.ApiListener;
import net.wigle.wigleandroid.background.KmlDownloader;
import net.wigle.wigleandroid.model.Upload;

import java.text.NumberFormat;
import java.util.Locale;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

/**
* the array adapter for a list of uploads.
*/
public final class UploadsListAdapter extends AbstractListAdapter<Upload> {
public UploadsListAdapter(final Context context, final int rowLayout ) {
super( context, rowLayout );
private final SharedPreferences prefs;
private final Fragment fragment;

public UploadsListAdapter(final Context context, final int rowLayout, final SharedPreferences prefs,
final Fragment fragment) {
super(context, rowLayout);
this.prefs = prefs;
this.fragment = fragment;
}

@SuppressLint("SetTextI18n")
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
View row;

if ( null == convertView ) {
row = mInflater.inflate( R.layout.uploadrow, parent, false );
}
else {
if (null == convertView) {
row = mInflater.inflate(R.layout.uploadrow, parent, false);
} else {
row = convertView;
}

Upload upload;
try {
upload = getItem(position);
}
catch ( final IndexOutOfBoundsException ex ) {
} catch (final IndexOutOfBoundsException ex) {
// yes, this happened to someone
MainActivity.info("index out of bounds: " + position + " ex: " + ex);
return row;
}

TextView tv = (TextView) row.findViewById( R.id.transid );
final String transid = upload.getTransid();
TextView tv = (TextView) row.findViewById(R.id.transid);
tv.setText(upload.getTransid());

tv = (TextView) row.findViewById( R.id.total_wifi_gps );
tv = (TextView) row.findViewById(R.id.total_wifi_gps);
tv.setText(getContext().getString(R.string.wifi_gps) + ": "
+ numberFormat.format(upload.getTotalWifiGps()));

tv = (TextView) row.findViewById( R.id.total_cell_gps );
tv = (TextView) row.findViewById(R.id.total_cell_gps);
tv.setText(getContext().getString(R.string.cell_gps) + ": "
+ numberFormat.format(upload.getTotalCellGps()));

tv = (TextView) row.findViewById( R.id.file_size );
tv = (TextView) row.findViewById(R.id.file_size);
tv.setText(getContext().getString(R.string.bytes) + ": "
+ numberFormat.format(upload.getFileSize()));


final String status = upload.getStatus();
final String userid = prefs.getString(ListFragment.PREF_AUTHNAME, "");
final Boolean isAnonymous = prefs.getBoolean(ListFragment.PREF_BE_ANONYMOUS, false);

if ((null != userid) && (!userid.isEmpty()) && (!isAnonymous) && ("Completed".equals(status))) {
ImageButton share = (ImageButton) row.findViewById(R.id.share_upload);
share.setVisibility(View.VISIBLE);
share.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MainActivity.info("Sharing transid: " + transid);
final KmlDownloader task = new KmlDownloader(fragment.getActivity(), ListFragment.lameStatic.dbHelper, transid,
new ApiListener() {
@Override
public void requestComplete(final JSONObject json, final boolean isCache) {
UploadsListAdapter.handleKmlDownload(transid, json, fragment, Intent.ACTION_SEND);
}
});
try {
task.startDownload(fragment);
} catch (WiGLEAuthException waex) {
MainActivity.warn("Authentication error on KML downloadload for transid " +
transid, waex);
}
}
});
}
String percentDonePrefix = "";
String percentDoneSuffix = "%";
if ("Queued for Processing".equals(status)) {
percentDonePrefix = "#";
percentDoneSuffix = "";
}
tv = (TextView) row.findViewById( R.id.percent_done );
tv = (TextView) row.findViewById(R.id.percent_done);
tv.setText(percentDonePrefix + upload.getPercentDone() + percentDoneSuffix);

tv = (TextView) row.findViewById( R.id.status );
tv = (TextView) row.findViewById(R.id.status);
tv.setText(upload.getStatus());

return row;
}

public static void handleKmlDownload(final String transid, final JSONObject json,
final Fragment fragment, final String actionIntent ) {
try {
if (json.getBoolean("success")) {
MainActivity.info("transid " + transid + " worked!");
String localFilePath = json.getString("file");
MainActivity.info("Local Path: "+localFilePath);
Intent intent = new Intent(actionIntent);
intent.putExtra(Intent.EXTRA_SUBJECT, "WiGLE " + transid);
try {
File file = new File(localFilePath);

// content:// url for the file.
Uri fileUri = FileProvider.getUriForFile(fragment.getContext(),
MainActivity.getMainActivity().getApplicationContext().getPackageName() +
".kmlprovider", file);

// the old, but easier to debug way of getting a file:// url for a file
//Uri fileUri = Uri.fromFile(file);

if (Intent.ACTION_SEND.equals(actionIntent)) {
//share case, populates arguments to work with email, drive
MainActivity.info("send action called for file URI: "+fileUri.toString());
intent.setType("application/vnd.google-earth.kml+xml");
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
} else {
//catch-all, handles "view" case among others.
MainActivity.info("view action called for file URI: "+fileUri.toString());
intent.setDataAndType(fileUri, "application/vnd.google-earth.kml+xml");
}
//TODO: necessary?
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
fragment.startActivity(Intent.createChooser(intent, fragment.getResources().getText(R.string.send_to)));
} catch (IllegalArgumentException e) {
MainActivity.error("Unable to open file: " + localFilePath);
e.printStackTrace();
}
} else {
MainActivity.error("Failed to download transid: " + transid);
}
} catch(
JSONException jex)

{
MainActivity.error("Exception downloading transid: " + transid);
}
}
}
@@ -0,0 +1,78 @@
package net.wigle.wigleandroid.background;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import net.wigle.wigleandroid.DatabaseHelper;
import net.wigle.wigleandroid.MainActivity;
import net.wigle.wigleandroid.WiGLEAuthException;
import net.wigle.wigleandroid.background.AbstractApiRequest;
import net.wigle.wigleandroid.background.AbstractProgressApiRequest;
import net.wigle.wigleandroid.background.ApiListener;
import net.wigle.wigleandroid.background.ObservationUploader;
import net.wigle.wigleandroid.background.Status;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* Created by arkasha on 10/27/17.
*/

public class KmlDownloader extends AbstractProgressApiRequest {
private Status status;

public KmlDownloader(final FragmentActivity context, final DatabaseHelper dbHelper /*TODO: not needed?*/,
final String transid, final ApiListener listener) {
super(context, dbHelper, "KmlDL", transid+".kml", MainActivity.KML_TRANSID_URL_STEM+transid, false,
true, true, false, AbstractApiRequest.REQUEST_GET, listener, true);
}

@Override
protected void subRun() throws IOException, InterruptedException, WiGLEAuthException {
status = Status.UNKNOWN;
final Bundle bundle = new Bundle();
sendBundledMessage( Status.DOWNLOADING.ordinal(), bundle );

String result = null;
try {
result = doDownload(this.connectionMethod);
writeSharefile(result, cacheFilename);
final JSONObject json = new JSONObject("{success: " + true + ", file:\"" +
(MainActivity.hasSD()? MainActivity.getSDPath() : context.getFilesDir().getAbsolutePath()+"/kml/" ) + cacheFilename + "\"}");
sendBundledMessage( Status.SUCCESS.ordinal(), bundle );
listener.requestComplete(json, false);
} catch (final WiGLEAuthException waex) {
// ALIBI: allow auth exception through
sendBundledMessage( Status.FAIL.ordinal(), bundle );
throw waex;
} catch (final JSONException ex) {
sendBundledMessage( Status.FAIL.ordinal(), bundle );
MainActivity.error("ex: " + ex + " result: " + result, ex);
}
}

protected void writeSharefile(final String result, final String filename) throws IOException {

if (MainActivity.hasSD()) {
if (cacheFilename != null) {
cacheResult(result);
}
} else {
//see if KML dir exists
MainActivity.info("local storage DL...");
File kmlPath = context.getDir(context.getFilesDir().getAbsolutePath()+"/kml", Context.MODE_PRIVATE);
if (kmlPath.exists() && kmlPath.isDirectory()) {
MainActivity.info("... cache directory found");
File kmlFile = new File(kmlPath, filename+".kml");
FileOutputStream out = new FileOutputStream(kmlFile);
ObservationUploader.writeFos(out, result);
}
}
}
}

0 comments on commit 7822028

Please sign in to comment.