Skip to content

Commit

Permalink
OpenCellID Upload & Download
Browse files Browse the repository at this point in the history
Rewrote both the upload and download methods and requested a new API key due to the
disclosure of our last key.

Contributions are required as the API key has not been white-listed and does not yet
allow for the downloading of data.
  • Loading branch information
xLaMbChOpSx committed Aug 19, 2014
1 parent 3b4e44b commit c2dc93b
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import com.SecUpwN.AIMSICD.R;
import com.SecUpwN.AIMSICD.service.AimsicdService;
import com.SecUpwN.AIMSICD.utils.Helpers;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
Expand Down Expand Up @@ -60,9 +63,16 @@ public void onResume() {
mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

mContext.registerReceiver(mMessageReceiver, new IntentFilter(AimsicdService.UPDATE_DISPLAY));
updateUI();
}

@Override
public void onPause() {
super.onPause();
mContext.unregisterReceiver(mMessageReceiver);
}

@Override
public void onDestroy() {
super.onDestroy();
Expand Down Expand Up @@ -92,6 +102,17 @@ public void onServiceDisconnected(ComponentName arg0) {
}
};

private final BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
if (bundle != null && bundle.getBoolean("update")) {
Helpers.msgShort(mContext, "Refreshing display");
updateUI();
}
}
};

private void updateUI() {
TextView content;
TableLayout tableLayout;
Expand Down Expand Up @@ -168,7 +189,7 @@ private void updateUI() {
content = (TextView) mView.findViewById(R.id.network_name);
content.setText(mAimsicdService.mDevice.getNetworkName());
content = (TextView) mView.findViewById(R.id.network_code);
content.setText(mAimsicdService.mDevice.getSmmcMcc());
content.setText(mAimsicdService.mDevice.getMncMcc());
content = (TextView) mView.findViewById(R.id.network_type);
content.setText(mAimsicdService.mDevice.getNetworkTypeName());

Expand Down
59 changes: 33 additions & 26 deletions app/src/main/java/com/SecUpwN/AIMSICD/utils/RequestTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

public class RequestTask extends AsyncTask<String, Integer, String> {

public static final char OPEN_CELL_ID_REQUEST = 1;
public static final char OPEN_CELL_ID_REQUEST_FROM_MAP = 2;
public static final char BACKUP_DATABASE = 3;
public static final char RESTORE_DATABASE = 4;
public static final char CELL_LOOKUP = 5;


private final AIMSICDDbAdapter mDbAdapter;
private final Context mContext;
Expand Down Expand Up @@ -74,21 +76,23 @@ protected String doInBackground(String... commandString) {
FileInputStream fin = new FileInputStream(file);
String csv = Helpers.convertStreamToString(fin);
mpEntity.addPart("key", new StringBody(AimsicdService.OCID_API_KEY));
mpEntity.addPart("datafile", new InputStreamBody(new ByteArrayInputStream(csv.getBytes()), "text/csv", "aimsicd-ocid-data.csv"));
mpEntity.addPart("datafile", new InputStreamBody(
new ByteArrayInputStream(csv.getBytes()), "text/csv",
"aimsicd-ocid-data.csv"));

ByteArrayOutputStream mBAOS = new ByteArrayOutputStream();
ByteArrayOutputStream bAOS = new ByteArrayOutputStream();

mpEntity.writeTo(mBAOS);
mBAOS.flush();
ByteArrayEntity bArrEntity = new ByteArrayEntity(mBAOS.toByteArray());
mBAOS.close();
mpEntity.writeTo(bAOS);
bAOS.flush();
ByteArrayEntity bArrEntity = new ByteArrayEntity(bAOS.toByteArray());
bAOS.close();
bArrEntity.setChunked(false);
bArrEntity.setContentEncoding(mpEntity.getContentEncoding());
bArrEntity.setContentType(mpEntity.getContentType());

HttpClient httpclient;
HttpPost httppost;
HttpResponse response = null;
HttpResponse response;

httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://www.opencellid.org/measure/uploadCsv");
Expand All @@ -111,6 +115,8 @@ protected String doInBackground(String... commandString) {
}

try {
int total = 0;
int progress = 0;
File dir = new File(
Environment.getExternalStorageDirectory()
+ "/AIMSICD/OpenCellID/"
Expand All @@ -121,34 +127,34 @@ protected String doInBackground(String... commandString) {
File file = new File(dir, "opencellid.csv");

URL url = new URL(commandString[0]);
URLConnection connection = url.openConnection();
connection.connect();

// download the file
InputStream input = new BufferedInputStream(url.openStream(),
8192);

// Output stream
OutputStream output = new FileOutputStream(file);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(20000);
urlConnection.setReadTimeout(20000);

byte data[] = new byte[1024];
urlConnection.setDoInput(true);
urlConnection.connect();

long total = 0;
total = urlConnection.getContentLength();
publishProgress(progress, total);

while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int) (total / 1024) * 2 );
FileOutputStream output = new FileOutputStream(file, false);
InputStream input = new BufferedInputStream(urlConnection.getInputStream());

byte[] data = new byte[1024];
while ((count = input.read(data)) > 0) {
// writing data to file
output.write(data, 0, count);
progress += count;

publishProgress(progress, total);
}

// flushing output
output.flush();

// closing streams
output.close();
input.close();

urlConnection.disconnect();

return "Successful";
} catch (MalformedURLException e) {
Expand Down Expand Up @@ -179,6 +185,7 @@ protected String doInBackground(String... commandString) {


protected void onProgressUpdate(Integer... values) {
AIMSICD.mProgressBar.setMax(values[1]);
AIMSICD.mProgressBar.setProgress(values[0]);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.SecUpwN.AIMSICD.utils;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class StackOverflowXmlParser {
// We don't use namespaces
private static final String ns = null;

public List<Cell> parse(InputStream in) throws XmlPullParserException, IOException {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
parser.nextTag();
return readCells(parser);
} finally {
in.close();
}
}

private List<Cell> readCells(XmlPullParser parser) throws XmlPullParserException, IOException {
List<Cell> cells = new ArrayList<>();

parser.require(XmlPullParser.START_TAG, ns, "rsp");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Starts by looking for the entry tag
if (name.equals("cell")) {
cells.add(readCell(parser));
} else {
skip(parser);
}
}
return cells;
}

// Parses the contents of an entry. If it encounters a title, summary, or link tag, hands them off
// to their respective "read" methods for processing. Otherwise, skips the tag.
private Cell readCell(XmlPullParser parser) throws XmlPullParserException, IOException {
Cell cell = new Cell();
parser.require(XmlPullParser.START_TAG, ns, "cell");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
switch (name) {
case "lat":
cell.setLat(readDouble(parser));
break;
case "lon":
cell.setLon(readDouble(parser));
break;
case "mcc":
cell.setMCC(readInt(parser));
break;
case "mnc":
cell.setMNC(readInt(parser));
break;
case "cellid":
cell.setCID(readInt(parser));
break;
case "lac":
cell.setLAC(readInt(parser));
break;
default:
skip(parser);
break;
}
}
return cell;
}

// For the tags title and summary, extracts their text values.
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}

// For tags containing double values.
private double readDouble(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return Double.valueOf(result);
}

// For tags containing double values.
private int readInt(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return Integer.valueOf(result);
}

private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
break;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}

}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<string name="preferences">Preferences</string>
<string name="press_once_again_to_exit">Press again to exit</string>
<string name="update_opencelldata">Update OpenCellID Data</string>
<string name="cell_lookup">Lookup Current Cell Details</string>

<string name="no_network_connection_title">No Internet Connection</string>
<string name="no_network_connection_message">Unable to download OpenCellID data without Internet
Expand Down

0 comments on commit c2dc93b

Please sign in to comment.