Fixed Surfing to download all linked content like images and iframes, also added FTP download

This commit is contained in:
Ziver Koc 2014-07-31 14:41:40 +02:00
parent d9fac2807a
commit 21e55f12c4
9 changed files with 107 additions and 45 deletions

View file

@ -18,6 +18,7 @@ public abstract class UeBehaviour {
if(execListener != null) execListener.executionStarted(); if(execListener != null) execListener.executionStarted();
try { try {
setProgress(0);
execute(); execute();
} catch(Exception e){ } catch(Exception e){
if(execListener != null) execListener.exception(e); if(execListener != null) execListener.exception(e);

View file

@ -1,12 +1,21 @@
package com.ericsson.uecontrol.core.behaviour; package com.ericsson.uecontrol.core.behaviour;
import android.util.Log;
import com.ericsson.uecontrol.core.UeBehaviour; import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.gui.util.Configurator.Configurable; import com.ericsson.uecontrol.gui.util.Configurator.Configurable;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* This behaviour simulates visiting a website * This behaviour simulates visiting a website
@ -16,36 +25,88 @@ import java.net.URLConnection;
public class UeBehaviourSurfing extends UeBehaviour { public class UeBehaviourSurfing extends UeBehaviour {
@Configurable("Web Address") @Configurable("Web Address")
private String strUrl; private String rootUrl;
private long estimatedDataLength = -1;
public UeBehaviourSurfing(){ public UeBehaviourSurfing(){
this("http://google.com"); this("http://google.com");
} }
public UeBehaviourSurfing(String url){ public UeBehaviourSurfing(String url){
strUrl = url; rootUrl = url;
} }
@Override @Override
protected void execute() throws IOException { protected void execute() throws IOException {
if(!strUrl.startsWith("http://") && if(!rootUrl.startsWith("http://"))
!strUrl.startsWith("ftp://")) rootUrl = "http://"+ rootUrl;
strUrl = "http://"+strUrl;
URL url = new URL(strUrl);
List<URL> urlList = new ArrayList<URL>();
urlList.add(new URL(rootUrl));
byte[] data = new byte[100]; byte[] data = new byte[100];
URLConnection connection = url.openConnection(); IOException retException = null;
connection.setUseCaches(false); long totalRead = 0;
connection.connect();
InputStream in = connection.getInputStream();
long total = in.available(); for(int i=0; i<urlList.size(); i++) {
long progress = 0; try {
long read = 0; URL url = urlList.get(i);
Log.v(getClass().getSimpleName(), "Downloading: " + url);
while((read = in.read(data)) != -1 && !stopExecution()){ URLConnection connection = url.openConnection();
progress += read; connection.setUseCaches(false);
super.setProgress((float)progress/total); connection.connect();
super.setHandledIncomingData(read); InputStream in = connection.getInputStream();
if (estimatedDataLength < 0)
estimatedDataLength = in.available();
int read = 0;
StringBuilder content = new StringBuilder();
while ((read = in.read(data)) != -1) {
if(stopExecution())
return;
totalRead += read;
super.setProgress((float) totalRead / estimatedDataLength);
super.setHandledIncomingData(read);
content.append(new String(data, 0, read));
}
getAdditionalContent(url, content.toString(), urlList);
}catch(IOException e){
e.printStackTrace();
if(retException == null) retException = e;
}
}
estimatedDataLength = totalRead;
if(retException != null)
throw retException;
}
private static final Pattern[] CONTENT_INCLUSION_PATTERNS = new Pattern[]{
// [\w\/\.-]
// HTML
Pattern.compile("<img.* src=\"(.*?)\"", Pattern.CASE_INSENSITIVE),
Pattern.compile("<iframe.* src=\"(.*?)\"", Pattern.CASE_INSENSITIVE),
// CSS
Pattern.compile("<link.*rel=\"stylesheet\".*href=\"(.*?)\".*>", Pattern.CASE_INSENSITIVE),
Pattern.compile("@import [\"']?(.*?)[\"']?", Pattern.CASE_INSENSITIVE),
Pattern.compile("(?:import|background)[\\W:]*url\\([\"']?(?![\"']?data:)(.*?)[\"']??\\)", Pattern.CASE_INSENSITIVE),
// Javascript
Pattern.compile("<script.* src=\"(.*?)\"", Pattern.CASE_INSENSITIVE)
};
private void getAdditionalContent(URL baseUrl, String data, List<URL> urlList){
for(Pattern pattern : CONTENT_INCLUSION_PATTERNS){
Matcher m = pattern.matcher(data);
while(m.find()){
try {
String strUrl = m.group(1);
URL url = new URL(baseUrl, strUrl);
if(!urlList.contains(url))
urlList.add(url);
}catch(MalformedURLException e){
e.printStackTrace();
}
}
} }
} }
@ -55,9 +116,8 @@ public class UeBehaviourSurfing extends UeBehaviour {
return "Surfing"; return "Surfing";
} }
@Override @Override
public String toString() { public String toString() {
return "Will visit "+ strUrl; return "Will visit "+ rootUrl;
} }
} }

View file

@ -78,7 +78,7 @@ public class EditActivity extends ListActivity implements AdapterView.OnItemClic
@Override @Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
final UeBehaviour behaviour = executor.getBehaviourList().get(i); UeBehaviour behaviour = executor.getBehaviourList().get(i);
Configurator confer = new Configurator(behaviour); Configurator confer = new Configurator(behaviour);
ConfigureDialog dialog = new ConfigureDialog(); ConfigureDialog dialog = new ConfigureDialog();

View file

@ -12,6 +12,7 @@ import android.view.MenuItem;
import com.ericsson.uecontrol.R; import com.ericsson.uecontrol.R;
import com.ericsson.uecontrol.core.UeControlExecutor; import com.ericsson.uecontrol.core.UeControlExecutor;
import com.ericsson.uecontrol.core.behaviour.UeBehaviourFtp;
import com.ericsson.uecontrol.core.behaviour.UeBehaviourSleep; import com.ericsson.uecontrol.core.behaviour.UeBehaviourSleep;
import com.ericsson.uecontrol.core.behaviour.UeBehaviourSurfing; import com.ericsson.uecontrol.core.behaviour.UeBehaviourSurfing;
import com.ericsson.uecontrol.gui.fragments.BehaviourListFragment; import com.ericsson.uecontrol.gui.fragments.BehaviourListFragment;
@ -20,8 +21,7 @@ import com.ericsson.uecontrol.gui.fragments.StatusFragment;
import com.ericsson.uecontrol.gui.util.CSVWriter; import com.ericsson.uecontrol.gui.util.CSVWriter;
public class MainActivity extends FragmentActivity public class MainActivity extends FragmentActivity {
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/** /**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer. * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
@ -42,6 +42,8 @@ public class MainActivity extends FragmentActivity
// getFragmentManager().findFragmentById(R.id.navigation_drawer); // getFragmentManager().findFragmentById(R.id.navigation_drawer);
statusFragment = (StatusFragment) statusFragment = (StatusFragment)
getFragmentManager().findFragmentById(R.id.status_fragment); getFragmentManager().findFragmentById(R.id.status_fragment);
getFragmentManager().beginTransaction()
.replace(R.id.container, BehaviourListFragment.newInstance()).commit();
// Set up the drawer. // Set up the drawer.
//navigationDrawerFragment.setUp( //navigationDrawerFragment.setUp(
@ -53,16 +55,8 @@ public class MainActivity extends FragmentActivity
currentExecutor.addBehaviour(new UeBehaviourSurfing()); currentExecutor.addBehaviour(new UeBehaviourSurfing());
currentExecutor.addBehaviour(new UeBehaviourSleep(4000)); currentExecutor.addBehaviour(new UeBehaviourSleep(4000));
currentExecutor.setThroughputListener(statusFragment.getThroughputListener()); currentExecutor.setThroughputListener(statusFragment.getThroughputListener());
onNavigationDrawerItemSelected(0);
} }
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, BehaviourListFragment.newInstance()).commit();
}
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {

View file

@ -58,7 +58,7 @@ public class ConfigureDialog extends DialogFragment {
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT)); LinearLayout.LayoutParams.WRAP_CONTENT));
input.setText(confParam.getString(), TextView.BufferType.EDITABLE); input.setText(confParam.getString(), TextView.BufferType.EDITABLE);
input.setInputType(InputType.TYPE_NULL); input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
inputs.put(confParam.getName(), input); inputs.put(confParam.getName(), input);
layout.addView(input); layout.addView(input);

View file

@ -13,7 +13,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class SelectBehaviourDialog extends DialogFragment { public class SelectBehaviourDialog extends DialogFragment {
public static String[] behavioursClasses; public static String[] behaviourClasses;
public static String[] behaviourNames; public static String[] behaviourNames;
private BehaviourSelectionCallback callback; private BehaviourSelectionCallback callback;
@ -22,17 +22,18 @@ public class SelectBehaviourDialog extends DialogFragment {
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
// Generate nice behaviour names // Generate nice behaviour names
if(behavioursClasses == null) { if(behaviourClasses == null) {
behavioursClasses = getResources().getStringArray(R.array.behaviours); behaviourClasses = getResources().getStringArray(R.array.behaviours);
behaviourNames = new String[behavioursClasses.length]; behaviourNames = new String[behaviourClasses.length];
Pattern p = Pattern.compile("[\\w\\.]*\\.(?:UeBehaviour|)(\\w*)"); for (int i = 0; i < behaviourClasses.length; i++) {
for (int i = 0; i < behavioursClasses.length; i++) { try {
Matcher m = p.matcher(behavioursClasses[i]); UeBehaviour obj = (UeBehaviour) Class.forName(behaviourClasses[i]).newInstance();
if(m.find()) behaviourNames[i] = obj.getName();
behaviourNames[i] = m.group(1); }catch(Exception e){
else e.printStackTrace();
behaviourNames[i] = behavioursClasses[i]; behaviourNames[i] = behaviourClasses[i];
}
} }
} }
@ -43,7 +44,7 @@ public class SelectBehaviourDialog extends DialogFragment {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
try { try {
if (callback != null){ if (callback != null){
Class objClass = Class.forName(behavioursClasses[which]); Class objClass = Class.forName(behaviourClasses[which]);
callback.behaviourSelected(behaviourNames[which], objClass); callback.behaviourSelected(behaviourNames[which], objClass);
} }
} catch (Exception e) { } catch (Exception e) {

View file

@ -35,8 +35,10 @@ public class CSVWriter {
public CSVWriter(Context context){ public CSVWriter(Context context){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String path = prefs.getString("logging_path", "/sdcard/uecontrol/");
if(!path.endsWith("/")) path += "/";
file = new File( file = new File(
prefs.getString("logging_path", "/sdcard/uecontrol/"), path,
"log_"+fileDateFormater.format(System.currentTimeMillis())+".log"); "log_"+fileDateFormater.format(System.currentTimeMillis())+".log");
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

View file

@ -91,7 +91,10 @@ public class Configurator {
public String getNiceName(){return niceName;} public String getNiceName(){return niceName;}
public ConfigType getType(){return type;} public ConfigType getType(){return type;}
public String getString(){return value.toString();} public String getString(){
if(value == null)
return null;
return value.toString();}
public void setString(String v){ public void setString(String v){
switch(type){ switch(type){

View file

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string-array name="behaviours" > <string-array name="behaviours" >
<item>com.ericsson.uecontrol.core.behaviour.UeBehaviourFtp</item>
<item>com.ericsson.uecontrol.core.behaviour.UeBehaviourSleep</item> <item>com.ericsson.uecontrol.core.behaviour.UeBehaviourSleep</item>
<item>com.ericsson.uecontrol.core.behaviour.UeBehaviourSurfing</item> <item>com.ericsson.uecontrol.core.behaviour.UeBehaviourSurfing</item>
</string-array> </string-array>