Commits

Michael Knight committed 260f3d7

Initial import.

Comments (0)

Files changed (18)

+*.pydevproject
+.project
+.metadata
+bin/**
+tmp/**
+tmp/**/*
+*.tmp
+*.bak
+*.swp
+*~.nib
+local.properties
+.classpath
+.settings/
+.loadpath
+
+# External tool builders
+.externalToolBuilders/
+
+# Locally stored "Eclipse launch configurations"
+*.launch
+
+# CDT-specific
+.cproject
+
+# PDT-specific
+.buildpath
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.miknight.bameter"
+    android:versionCode="1"
+    android:versionName="0.1" >
+
+    <uses-sdk android:minSdkVersion="15" />
+
+    <application
+        android:icon="@drawable/icon"
+        android:label="@string/app_name" >
+        <activity
+            android:name=".BameterActivity"
+            android:label="@string/app_name" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+
+</manifest>

gen/com/miknight/bameter/BuildConfig.java

+/** Automatically generated file. DO NOT MODIFY */
+package com.miknight.bameter;
+
+public final class BuildConfig {
+    public final static boolean DEBUG = true;
+}

gen/com/miknight/bameter/R.java

+/* AUTO-GENERATED FILE.  DO NOT MODIFY.
+ *
+ * This class was automatically generated by the
+ * aapt tool from the resource data it found.  It
+ * should not be modified by hand.
+ */
+
+package com.miknight.bameter;
+
+public final class R {
+    public static final class attr {
+    }
+    public static final class drawable {
+        public static final int ic_launcher=0x7f020000;
+        public static final int icon=0x7f020001;
+        public static final int notify_icon=0x7f020002;
+    }
+    public static final class id {
+        public static final int button1=0x7f050002;
+        public static final int textView1=0x7f050000;
+        public static final int textView2=0x7f050001;
+    }
+    public static final class layout {
+        public static final int main=0x7f030000;
+    }
+    public static final class string {
+        public static final int app_name=0x7f040000;
+        public static final int exit_button=0x7f040004;
+        public static final int hello=0x7f040001;
+        public static final int instructions=0x7f040003;
+        public static final int placeholder=0x7f040002;
+    }
+}
+-optimizationpasses 5
+-dontusemixedcaseclassnames
+-dontskipnonpubliclibraryclasses
+-dontpreverify
+-verbose
+-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
+
+-keep public class * extends android.app.Activity
+-keep public class * extends android.app.Application
+-keep public class * extends android.app.Service
+-keep public class * extends android.content.BroadcastReceiver
+-keep public class * extends android.content.ContentProvider
+-keep public class * extends android.app.backup.BackupAgentHelper
+-keep public class * extends android.preference.Preference
+-keep public class com.android.vending.licensing.ILicensingService
+
+-keepclasseswithmembernames class * {
+    native <methods>;
+}
+
+-keepclasseswithmembers class * {
+    public <init>(android.content.Context, android.util.AttributeSet);
+}
+
+-keepclasseswithmembers class * {
+    public <init>(android.content.Context, android.util.AttributeSet, int);
+}
+
+-keepclassmembers class * extends android.app.Activity {
+   public void *(android.view.View);
+}
+
+-keepclassmembers enum * {
+    public static **[] values();
+    public static ** valueOf(java.lang.String);
+}
+
+-keep class * implements android.os.Parcelable {
+  public static final android.os.Parcelable$Creator *;
+}
+# This file is automatically generated by Android Tools.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+#
+# This file must be checked in Version Control Systems.
+#
+# To customize properties used by the Ant build system use,
+# "ant.properties", and override values to adapt the script to your
+# project structure.
+
+# Project target.
+target=android-15

res/drawable-hdpi/ic_launcher.png

Added
New image

res/drawable-hdpi/icon.png

Added
New image

res/drawable-hdpi/notify_icon.png

Added
New image

res/drawable-ldpi/ic_launcher.png

Added
New image

res/drawable-ldpi/icon.png

Added
New image

res/drawable-ldpi/notify_icon.png

Added
New image

res/drawable-mdpi/ic_launcher.png

Added
New image

res/drawable-mdpi/icon.png

Added
New image

res/drawable-mdpi/notify_icon.png

Added
New image
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="wrap_content"
+    android:layout_height="fill_parent"
+    android:orientation="vertical" >
+
+    <TextView
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginBottom="10dp"
+        android:layout_marginLeft="10dp"
+        android:layout_marginRight="10dp"
+        android:layout_marginTop="10dp"
+        android:text="@string/hello" />
+
+    <TextView
+        android:id="@+id/textView1"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginLeft="10dp"
+        android:layout_marginRight="10dp"
+        android:text="@string/placeholder"
+        android:textAppearance="?android:attr/textAppearanceLarge" />
+
+    <TextView
+        android:id="@+id/textView2"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginLeft="10dp"
+        android:layout_marginRight="10dp"
+        android:layout_marginTop="30dp"
+        android:text="@string/instructions" />
+
+    <Button
+        android:id="@+id/button1"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center_horizontal"
+        android:layout_marginLeft="30dp"
+        android:layout_marginRight="30dp"
+        android:layout_marginTop="50dp"
+        android:onClick="exitApplication"
+        android:text="@string/exit_button" />
+
+</LinearLayout>

res/values/strings.xml

+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+    <string name="app_name">BaMeter</string>
+    <string name="hello">Bandwidth meter:</string>
+    <string name="placeholder">Error: Unable to detect bandwidth.</string>
+    <string name="instructions">Stats will continue to be reported as an ongoing notification while you use your device. Exit the application to clear the notification. (Open this app to bring it back.)</string>
+    <string name="exit_button">Exit</string>
+
+</resources>

src/com/miknight/bameter/BameterActivity.java

+package com.miknight.bameter;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+import android.app.Activity;
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.Intent;
+import android.content.res.Resources;
+import android.os.Bundle;
+import android.os.Handler;
+import android.view.View;
+import android.widget.TextView;
+
+public class BameterActivity extends Activity {
+	
+	private TextView ptext;
+	private Handler handler;
+	private long downloaded, uploaded;
+	
+    /** Called when the activity is first created. */
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.main);
+        handler = new Handler();
+        startRepeatingTask();
+    }
+    
+    private void generateNotification(String txt) {
+    	Resources res = getResources();
+        int icon = R.drawable.notify_icon;
+        String appName = res.getString(R.string.app_name);
+        
+        Context context = getApplicationContext();
+        String ns = Context.NOTIFICATION_SERVICE;
+        
+        Intent notificationIntent = new Intent(context, BameterActivity.class);
+        PendingIntent contentIntent = PendingIntent
+        		.getActivity(context, 
+        		1, notificationIntent,
+                PendingIntent.FLAG_CANCEL_CURRENT);
+        
+        NotificationManager nm = (NotificationManager) getSystemService(ns);
+        Notification notification = new Notification.Builder(context)
+        .setContentTitle(appName)
+        .setContentText(txt)
+        .setContentIntent(contentIntent)
+        .setOngoing(true)
+        .setSmallIcon(icon)
+        .getNotification();
+        
+        nm.notify(1, notification);
+    }
+    
+    Runnable pingRunner = new Runnable() {
+    	public void run() {
+    		pingNetworkStats("eth0");
+    		handler.postDelayed(pingRunner, 1000);
+    	}
+    };
+    
+    void startRepeatingTask() {
+    	pingRunner.run(); 
+    }
+
+    void stopRepeatingTask() {
+    	handler.removeCallbacks(pingRunner);
+    }
+    
+    private void pingNetworkStats(String device) {
+    	Process p;
+    	String downloadOutput = "";
+    	String uploadOutput = "";
+    	String debug = "";
+    	try {
+    		p = Runtime.getRuntime().exec("cat /proc/net/dev");
+    		BufferedReader istream = new BufferedReader(new InputStreamReader(p.getInputStream()));
+    		String line;
+    		while ((line = istream.readLine()) != null) {
+    			debug += line + "\n";
+    			if (line.matches(".*" + device + ":.*") || line.matches(".*rmnet0.*")) {
+    				downloadOutput = line.split("\\s+")[2];
+    				uploadOutput = line.split("\\s+")[10];
+    			}
+    		}
+    	} catch (IOException e) {
+    		// TODO Auto-generated catch block
+    		e.printStackTrace();
+    		return;
+    	}
+    	String output = "";
+    	if (downloadOutput.isEmpty() || uploadOutput.isEmpty()) {
+    		output = debug;
+    	} else {
+    		long newDownloaded = Long.parseLong(downloadOutput);
+    		long d_diff = (newDownloaded - downloaded);
+    		long newUploaded = Long.parseLong(uploadOutput);
+    		long u_diff = (newUploaded - uploaded);
+    		output = d_diff/1024 + " KB/s down,\n"
+    				+ u_diff/1024 + " KB/s up.\n";
+    		downloaded = newDownloaded;
+    		uploaded = newUploaded;
+    	}
+    	setMeterText(output);
+        generateNotification(output);
+    }
+    
+    private void setMeterText(String s) {
+        ptext = (TextView) findViewById(R.id.textView1);
+        ptext.setText(s);
+    }
+    
+    public void exitApplication(View view) {
+    	NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+    	nm.cancelAll();
+        System.exit(0);
+    }
+}