Skip to content

Commit

Permalink
auto start after boot
Browse files Browse the repository at this point in the history
  • Loading branch information
yunxing committed Nov 28, 2011
1 parent c7f46f8 commit f211b96
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 18 deletions.
9 changes: 7 additions & 2 deletions client/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<activity android:name=".ui.Warning"></activity>
<activity android:name=".ui.HistoricalList"></activity>
<activity android:name=".ui.TrafficPerApplication"></activity>

<activity android:name=".ui.Thanks"></activity>
<activity android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar"
android:name=".PerfNearMe.Buttons">

Expand Down Expand Up @@ -83,7 +83,12 @@
<uses-library android:name="com.google.android.maps" />
<activity android:name=".pushNotify.PushActivity"></activity>
<service android:name=".pushNotify.PushService"></service>


<receiver android:name=".service.StartupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-library android:name="com.google.android.maps" />
Expand Down
18 changes: 18 additions & 0 deletions client/res/layout/thanks.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="500dip"

android:padding="6dip">

<LinearLayout android:layout_width="0dip"
android:layout_weight="1" android:layout_height="1000dip"
android:weightSum="1" android:orientation="vertical">

<TextView android:id="@+id/thanks" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="25px"
android:gravity="center_vertical" android:text="Thanks! \n\nWe are now collecting network data from applications on your phone.(It is anonymous with little overhead)\n
\nData collecting can be stopped at any time." />
<Button android:id="@+id/tpa_btn_stop"
android:layout_marginTop="10dip" android:text="Stop Data Collection"
android:layout_width="match_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
2 changes: 1 addition & 1 deletion client/res/layout/warning.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
android:scrollbars="vertical"
android:singleLine="false"
android:textColor="@android:color/secondary_text_dark_nodisable"
android:text="To enable statistic function, we require collecting anonymouns information from user's phone. Once we get these information, we are able to give you detailed feedback about how applications use your network traffic. Do you want to continue?"
android:text="To enable statistic function, we require collecting anonymouns information from user's phone. Once we get these information(probably in next release), we are able to give you detailed feedback about how applications use your network traffic. Do you want to continue?"
android:layout_height="98dp"/>
<RelativeLayout
android:layout_width="wrap_content"
Expand Down
20 changes: 15 additions & 5 deletions client/src/com/mobiperf/Tcpdump.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
Expand All @@ -17,6 +18,12 @@ public class Tcpdump {

private static Process process;

private static Process getRootProcess() throws IOException
{
if (process != null)
return process;
return Runtime.getRuntime().exec("su");
}
public static String currentFile(){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");//set date format
//return "/sdcard/mobiperf/client_" + df.format(new Date()) + ".pcap";
Expand All @@ -26,7 +33,7 @@ public static String currentFile(){

public static void clearOldTrace(){
try{
process = Runtime.getRuntime().exec("su");
process = getRootProcess();
DataOutputStream os = new DataOutputStream(process.getOutputStream());
//String command = "tcpdump -s 2000 -w " + file + "";
String command = "rm /data/local/client_*.pcap";
Expand All @@ -41,15 +48,19 @@ public static void clearOldTrace(){
* start new tcpdump
*/
public static void start_client(){
Log.v("Mobiperf", "start client");
//no network connection
if(InformationCenter.getNetworkTypeName().equals(""))
return;
try{
//check preferences
process = Runtime.getRuntime().exec("su");
process = getRootProcess();
DataOutputStream os = new DataOutputStream(process.getOutputStream());
//String command = "tcpdump -s 2000 -w " + file + "";

//TODO change to new TCPDUMP later
//String command = PATH + "tcpdump -s 200 -w " + currentFile();
String command = "tcpdump -s 200 -w " + currentFile();
String command = "/data/data/com.mobiperf/tcpdump";
os.writeBytes(command + "\n");
os.flush();
}catch(Exception e){
Expand All @@ -62,8 +73,7 @@ public static void start_client(){
*/
public static void terminate_client(){
try{
//check preferences
process = Runtime.getRuntime().exec("su");
process = getRootProcess();
DataOutputStream os = new DataOutputStream(process.getOutputStream());
//String command = "tcpdump -s 2000 -w " + file + "";
String command = "pkill tcpdump";
Expand Down
38 changes: 38 additions & 0 deletions client/src/com/mobiperf/service/StartupReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.mobiperf.service;

import com.mobiperf.InformationCenter;
import com.mobiperf.Utilities;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
public class StartupReceiver extends BroadcastReceiver {

static Thread serviceSwitchThread;

@Override
public void onReceive(Context context, Intent intent) {

SharedPreferences settings = context.getSharedPreferences(
"pref", 0);
//if the user doesn't allow us to upload
if(!settings.getBoolean("canUpload",
false))
return;
Log.v("Mobiperf", "receive boot intent");
InformationCenter.init(context);
InformationCenter.reset();
Thread binaryThread = Utilities.installBinaries(context);
/**** wait till all the binaries are installed ****/
try {
binaryThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent serviceIntent = new Intent(context, TcpdumpService.class);
context.startService(serviceIntent);

}
}
70 changes: 62 additions & 8 deletions client/src/com/mobiperf/service/TcpdumpService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.mobiperf.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
Expand All @@ -14,6 +17,46 @@

public class TcpdumpService extends Service {

private static boolean started = false;
public class Checker extends TimerTask {
public void run() {
Log.v("Mobiperf", "inside checker");
// Executes the command.
Process process;
boolean found = false;

try {
process = Runtime.getRuntime().exec("ps");
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line;

while ((line = reader.readLine()) != null) {
//System.out.println(line);
if(line.indexOf("tcpdump") != -1)
{
found = true;
break;
}
}
reader.close();
process.waitFor();

} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
// if tcpdump is down
if (!found)
Tcpdump.start_client();
// timer.schedule(new Worker(), new Date(System.currentTimeMillis()
// + 4 * 60 * 60 * 1000));
timer2.schedule(new Checker(), new Date(
System.currentTimeMillis() + 10 * 1000));
Log.v("Mobiperf", "outside checker");
}
}
public class Worker extends TimerTask {
public void run() {

Expand All @@ -23,35 +66,46 @@ public void run() {
//if current network is WiFi, upload trace
if(InformationCenter.getNetworkTypeName().equals("WIFI")){
//upload traces, delete old files

//scan the whole directory and upload all files, in upload delete old file
Tcpdump.upload();

}

Tcpdump.start_client();

Timer timer = new Timer(false);
//timer.schedule(new Worker(), new Date(System.currentTimeMillis() + 4 * 60 * 60 * 1000));
timer.schedule(new Worker(), new Date(System.currentTimeMillis() + 10 * 1000));
timer1.schedule(new Worker(), new Date(System.currentTimeMillis() + 30 * 60 * 1000));
Log.v("Mobiperf", "out side worker");

}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}


static private Timer timer1, timer2;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Timer timer = new Timer(false);
timer.schedule(new Worker(), new Date(System.currentTimeMillis() + 1000));
if (started)
return START_STICKY;
started = true;
Log.v("Mobiperf", "service started");
timer1 = new Timer(false);
timer1.schedule(new Worker(), new Date(System.currentTimeMillis() + 1000));

timer2 = new Timer(false);
timer2.schedule(new Checker(), new Date(System.currentTimeMillis() + 10 * 1000));
return START_STICKY;
}

@Override
public void onDestroy() {
Log.v("Mobiperf", "TCPdump service destroyed");
started = false;
super.onDestroy();
Tcpdump.terminate_client();
timer1.cancel();
timer2.cancel();
Tcpdump.terminate_client();
}


Expand Down
5 changes: 3 additions & 2 deletions client/src/com/mobiperf/ui/Home.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.mobiperf.Main;
import com.mobiperf.R;
import com.mobiperf.Utilities;
import com.mobiperf.PerfNearMe.Buttons;

public class Home extends Activity {

Expand Down Expand Up @@ -143,7 +144,7 @@ public void onClick(View v) {
// @Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),
Performance.class));
Buttons.class));
}
});
findViewById(R.id.home_btn_advance).setOnClickListener(
Expand All @@ -159,7 +160,7 @@ public void onClick(View v) {
com.mobiperf.ui.Warning.class));
else
startActivity(new Intent(getActivity(),
com.mobiperf.ui.TrafficPerApplication.class));
com.mobiperf.ui.Thanks.class));
}
});

Expand Down
56 changes: 56 additions & 0 deletions client/src/com/mobiperf/ui/Thanks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/****************************
* This file is part of the MobiPerf project (http://mobiperf.com).
* We make it open source to help the research community share our efforts.
* If you want to use all or part of this project, please give us credit and cite MobiPerf's official website (mobiperf.com).
* The package is distributed under license GPLv3.
* If you have any feedbacks or suggestions, don't hesitate to send us emails (3gtest@umich.edu).
* The server suite source code is not included in this package, if you have specific questions related with servers, please also send us emails
*
* Contact: 3gtest@umich.edu
* Development Team: Junxian Huang, Birjodh Tiwana, Zhaoguang Wang, Zhiyun Qian, Cheng Chen, Yutong Pei, Feng Qian, Qiang Xu
* Copyright: RobustNet Research Group led by Professor Z. Morley Mao, (Department of EECS, University of Michigan, Ann Arbor) and Microsoft Research
*
****************************/

package com.mobiperf.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.mobiperf.R;
import com.mobiperf.Tcpdump;

public class Thanks extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thanks);

findViewById(R.id.tpa_btn_stop).setOnClickListener(
new View.OnClickListener() {
// @Override
public void onClick(View v) {
SharedPreferences settings = getSharedPreferences("pref", 0);
SharedPreferences.Editor spe = settings.edit();
spe.putBoolean("canUpload", false);
spe.commit();
Intent intent = new Intent(getActivity(), com.mobiperf.service.TcpdumpService.class);
stopService(intent);
finish();
}
});
}
protected Activity getActivity() {
return this;
}
/******************** Menu ends here ********************/

}

0 comments on commit f211b96

Please sign in to comment.